So, my C doesn't want to open my functions. I don't know where to look further as I don't have any idea what I did wrong in this one. It works fine with another exercise.
Here's what I've written:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int hello();
int sum(int);
int k, i, getal1, getal2;
int goobye(void);
int main(void)
{
int hello();
for (i = 0; i < 5; i++)
{
printf("Give 2 numbers <100 : ");
scanf("%d%*c%d%*c", &getal1, &getal2);
int sum(int k);
}
int goodbye(void);
}
int hello()
{
printf("Welcome, this program will ask you to solve 1 sum");
return 0;
}
int sum(int k)
{
int som, som2;
som = getal1 + getal2;
printf("What is the sum of %d %d? %d", getal1, getal2);
scanf("%d", &som2);
if (som == som2)
{
printf("According to you, the sum of %d and %d equals %d. That is correct", getal1, getal2, som2);
}
else
{
printf("According to you, the sum of %d and %d equals %d. That is not correct", getal1, getal2, som2);
}
return 0;
}
int goobye(void)
{
printf("Thanks for your cooperation.");
return 0;
}
Thanks in advance!
in main function you should call a function without type
int main(void)
{
hello();
for (i = 0; i < 5; i++)
{
printf("Give 2 numbers <100 : ");
scanf("%d%*c%d%*c", &getal1, &getal2);
sum(k);
}
goodbye();
}
Edit the main function like this
Calls are not perform because you don't call the functions, you just redeclare them. This is probably a copy/paste error, but in the end, the calls don't happen.
I modified you code hunk and added comments
int main(void)
{
int hello(); //here is a forward declaration of hello()
hello(); // calls hello
for (i = 0; i < 5; i++)
{
printf("Give 2 numbers <100 : ");
scanf("%d%*c%d%*c", &getal1, &getal2);
int sum(int k); // forward declaration of sum()
sum(k); //call sum on k
}
int goodbye(void); // forward declaration of goodbye
goodbye(); //call goodbye
//careful your original code has a typo; you wrote goobye()
}
Firstly you need to read about the syntax while calling functions.
Also why you assign a certain return type to a certain function i.e. the difference between int hello(); and void hello();.
Also you have placed return 0 statement at multiple places which would end your program before you actually want it to!
#include <stdio.h>
void hello();
int sum(int);
int k, i, getal1, getal2;
void goobye(void);
int main(void)
{
hello(); //no return type mention when calling a function
for (i = 0; i < 5; i++)
{
printf("Give 2 numbers <100 : ");
scanf("%d%*c%d%*c", &getal1, &getal2);
sum(getal1, getal2);//you wish to calculate the sum of these two numbers i suppose
}
goodbye();
return 0;
}
void hello()
{
printf("Welcome, this program will ask you to solve 1 sum");
// return 0 here will end your program
//return 0;
}
int sum(int getal1, int getal2)
{
int som, som2;
som = getal1 + getal2;
printf("What is the sum of %d %d? %d", getal1, getal2);
scanf("%d", &som2);
if (som == som2)
{
printf("According to you, the sum of %d and %d equals %d. That is correct", getal1, getal2, som2);
}
else
{
printf("According to you, the sum of %d and %d equals %d. That is not correct", getal1, getal2, som2);
}//again the same issue with return 0 as above
//return 0;
}
void goobye(void)
{
printf("Thanks for your cooperation.");//again the same issue with return 0 as above
//return 0;
}
Related
Trying to make a code that gets the factorial of the inputted number.
int factorial(int number, int i)
{
int endval;
for(i = number - 1; i>0; i--){
endval = number * i;
}
if (endval == 0){
printf("1");
}
return endval;
}
int main()
{
int endvalue, numA, numB;
char userchoice[1];
printf("Enter a choice to make (f for factorial): \n");
scanf("%s", userchoice);
if(strcmp(userchoice, "f")== 0){
printf("Enter a value to get it's factorial: ");
scanf("%d", &numA);
endvalue = factorial(numA, numB);
printf("%d", endvalue);
return 0;}
getch();
return 0;
}
For some reason the whole for loop doesn't do anything in the function when I set the answer (number*i)= endval. It just prints out the same number I inputted and gives me an absurd answer for 0!.
int factorial(int number, int i)
{
int endval;
for(i = number - 1; i>0; i--){
endval = number * i;
}
if (endval == 0){
printf("1");
}
return endval;
}
However the code works perfectly fine when I remove endval variable entirely (with the exception that it gets 0! = 10)
int factorial(int number, int i)
{
for(i = number - 1; i>0; i--){
number = number * i;
}
if (number == 0) {printf("1");}
return number;
}
Is there anything I missed in the code that's causing these errors?
A definiton of factorial is:
factorial(0) = 1
factorial(n) = n * factorial(n-1)
Note: Factorial is legal only for number >= 0
In C, this definition is:
int factorial(int number)
{
if (number < 0)
return -1;
if (number == 0)
return (1);
/*else*/
return (number * factorial(number-1));
}
#include <stdio.h>
#include <string.h>
int factorial(int number)
{
int endval=1;
for(int i = number ; i>0; i--){
endval *= i;
}
return endval;
}
int main()
{
int endvalue=0;
int numA=0;
char userchoice[1];
printf("Enter a choice to make (f for factorial): ");
int ret=scanf("%s", userchoice);
if (!ret){
printf("Error in scanf: %d", ret);
}
if(strcmp(userchoice, "f")== 0){
printf("Enter a value to get it's factorial: ");
scanf("%d", &numA);
endvalue = factorial(numA);
printf("%d", endvalue);
return 0;
}
getchar();
return 0;
}
Code with some changes will work
factorial() function can get only one argument.
As a good habit all variables must be initialized.
Add include statement to source and be explicit not rely on compiler.
As we use strcmp() we must include string.h
use standard getchar() instead of getch()
Also can check return value of library function scanf() to ensure reading is correct or not.
You can use warnings from compiler to get most of above notes. In gcc: gcc -Wall code.c
Use a debugger to run program line by line and monitor variables value in each steps or use as many printf() to see what happens in function call.
There are possibly few things to correct. See please attached code.
int factorial(int number)
{
if (number == 0){ return 1; }
int endval=1, i;
for(i = 1; i<=number; i++) { endval *= i; }
return endval;
}
int main() {
int endvalue, numA;
char userchoice[1];
printf("Enter a choice to make (f for factorial): \n");
scanf("%s", userchoice);
if(strcmp(userchoice, "f")== 0) {
printf("Enter a value to get it's factorial: ");
scanf("%d", &numA);
endvalue = factorial(numA);
printf("%d", endvalue);
return 0;
}
getch();
return 0;
}
Whenever i put the correct int number instead of printing Correct its Print Invalid.
int main(void)
{
int number = 042646;
int pass;
printf("Enter the PIN.\n");
scanf("%d", &pass);/*enter code here*/
if (pass == number)
{
printf("Correct\n");
}
else
{
printf("Invalid\n");
}
}
In "C" a number preceded by 0 is interpreted as an octal number. Here is a simple code which will help you to see the issue:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int number = 42646;
int number_octal = 042646;
int pass = 0;
printf("Enter the PIN.\n");
scanf("%d", &pass);/*enter code here*/
/* Debug */
printf("Pass: %d\n", pass);
printf("Number: %d\n", number);
printf("number_octal: %d\n", number_octal);
if (pass == number)
{
printf("Correct\n");
}
else
{
printf("Invalid\n");
}
return 0;
}
I am learning about functions and how to call upon them and use them in class. I don't quite understand where I've gone wrong here. I know that there are some mistakes around the int main part. I have asked my teacher and he is reluctant on giving me an example that would solve my problems or help me out. I think my main problem is at factorial_result = factorial();
#include <stdio.h>
void mystamp(void)
{
printf("My name is John Appleseed\n");
printf("My lab time is 12:30 on Sunday\n");
return;
}
int getnum(void)
{
int local_var;
printf("Please enter an integer: ");
scanf("%d%*c", local_var);
return(local_var);
}
int factorial(void)
{
int x,f=1,local_var;
for(x=1; x <= local_var; x++)
f = f * x;
return(f);
}
int main(void)
{
int result;
int factorial_result;
mystamp();
result = getnum();
factorial_result = factorial();
printf("You typed %d\n", result);
printf("The factorial is %d\n", factorial_result);
return;
}
Declare local_var as a global variable and do:
local_var = getnum();
OR
Change main() to:
int main(void)
{
int result;
int factorial_result;
mystamp();
result = getnum();
factorial_result = factorial(result);
printf("You typed %d\n", result);
printf("The factorial is %d\n", factorial_result);
return;
}
And factorial() to:
int factorial(int n)
{
int x,f=1,local_var=n;
for(x=1; x <= local_var; x++)
f = f * x;
return(f);
}
Your factorial should be calculated based on the input( i.e in your case int result ).
So, your method factorial() should looks as follows :
int factorial( int number )
{
int factorial_value = 1;
while( number > 0 )
{
factorial_value *= number;
number--;
}
return factorial_value;
}
Then, the correct factorial would be returned and printed accordingly ! Regarding the scope of the variables that you have used, see the comments under your question.
#include <stdio.h>
int factorial(int);
int main()
{
int num;
int result;
printf("Enter a number to find it's Factorial: ");
scanf("%d", &num);
if (num < 0)
{
printf("Factorial of negative number not possible\n");
}
else
{
result = factorial(num);
printf("The Factorial of %d is %d.\n", num, result);
}
return 0;
}
int factorial(int num)
{
if (num == 0 || num == 1)
{
return 1;
}
else
{
return(num * factorial(num - 1));
}
}
This is a simple factorial program using recursion calling function !
include
int main()
{
int c, n, fact = 1;
printf("Enter a number to calculate its factorial\n"); scanf("%d", &n);
for (c = 1; c <= n; c++) fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
My function should get a non-negative integer, and return how many digits there are in the number, for Example for the number 563 the function return 3.
And for 0 will return 1.
*I'm new in c so it still very confusing for me.
Thanks.
Here is my code:
#include <stdio.h>
int numOfDigits(int n); //Declartion
void main()
{
int num1, counter = 0, newNum;
printf("Enter A Number: ");
scanf("%d", &num1);
}
int numOfDigits(int n1)
{
int counter = 0;
if (n1 == 0)
return 1;
while (n1 > 0) {
counter++;
n1 /= 10;
}
return counter;
}
What should I write to make the program work?
Just call the digit counting function that you made and assign its return value to a variable, and then print the value of that variable using printf() function; or alternatively, call your digit counting function inside printf() as an argument:
#include <stdio.h>
int numOfDigits(int n);
int main(void)
{
int num1;
int counter = 0;
printf("Enter A Number: ");
scanf("%d", &num1);
counter = numOfDigits(num1);
printf("number of digits: %d\n", counter);
//printf("number of digits: %d\n", numOfDigits(num1)); alternate method
}
int numOfDigits(int n1)
{
int counter = 0;
if (n1 == 0)
return 1;
while (n1 > 0) {
counter++;
n1 /= 10;
}
return counter;
}
A couple of notes:
Since you mentioned that you need to get a non-negative number, consider using unsigned int type, and change the format specifier inside scanf() and printf() call to "%u".
Function main should be declared as int main(void) or int main(int argc, char **argv)
I have written a program to check for Palindrome number.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
main()
{
int n,i;
printf("Please enter a number: ");
scanf("%d", &n);
/* Function Prototypes */
int reverse(int *p);
i=reverse(&n);
printf("Number returned %d",i);
if (i == n)
{
printf("The number is a palindrome");
}
else
{
printf("The number is NOT a palindrome");
}
}
int reverse( int *p)
{
int rev=0;
while(*p !=0)
{
rev=rev*10;
rev=rev+ *p%10;
*p=*p/10;
}
return (rev);
}
But it's always showing "Number is not a palindrome " irrespective of number is not a palindrome or not.
The reverse function leaves its argument pointing to zero. The argument doesn't need to be a pointer, and passing n by value instead solves the problem.
Here's fixed code, somewhat reformatted and with error-checking added.
#include <stdio.h>
int reverse(int p) {
int rev = 0;
while (p != 0) {
rev = rev * 10;
rev = rev + p%10;
p = p/10;
}
return rev;
}
int main(void) {
int n, i;
printf("Please enter a number: ");
if (scanf("%d", &n) != 1) {
printf("failed to read number.\n");
return 1;
}
i = reverse(n);
if (i == n) {
printf("%d is a palindrome: reversing it gives %d\n", n, i);
} else {
printf("%d isn't a palindrome: reversing it gives %d\n", n, i);
}
return 0;
}
It's an important skill to be able to debug programs. Here's a good link for some beginner techniques: http://ericlippert.com/2014/03/05/how-to-debug-small-programs/