just a newbie in C here. I'm trying to print out the factorial given the input using recursion & pointers, but when my input is 5, the output was 2293620. Can somebody help me with this? I'm not sure where did that number come from, because factorial of 5 should give me 120. Thanks for your help!
#include<stdio.h>
int countlength(int *num) {
int x = 1;
if (*num == 1) {
return 1;
} else {
return *num * countlength(num-1);
}
}
int main() {
int n, l;
printf("Enter number: ");
scanf("%d", &n);
l = countlength(&n);
printf("The factorial of %d is %d\n", n, l);
return 0;
}
Remove references from your code.
#include<stdio.h>
int countlength(int num) {
int x = 1;
if (num == 1) {
return 1;
} else {
return num * countlength(num-1);
}
}
int main() {
int n, l;
printf("Enter number: ");
scanf("%d", &n);
l = countlength(n);
printf("The factorial of %d is %d\n", n, l);
return 0;
}
In this case, I think you don't need to use pass by reference. You should use pass by value. Then your code will work perfectlyly
Change this line:
return num * countlength(num - 1);
to this:
int val = *num - 1;
return *num * countlength(&val);
So you correctly assign the value and pass it as a pointer.
int countlength(int *num) {
int x = *num - 1;
if (*num == 1) {
return 1;
} else {
return *num * countlength(&x);
}
}
int main() {
int n, l;
printf("Enter number: ");
scanf("%d", &n);
l = countlength(&n);
printf("The factorial of %d is %d\n", n, l);
return 0;
}```
**Try this**
int countlength(int *num) {
if (*num == 1) {
return 1;
} else {
return *num * countlength(&(*num - 1));
}
}
int main() {
int n, l;
printf("Enter number: ");
scanf("%d", &n);
l = countlength(&n);
printf("The factorial of %d is %d\n", n, l);
return 0;
}
try this
Related
#include <stdio.h>
int bolucu(int n){
int temp;
temp=n;
int basamak=0;
while(temp != 0){
temp/=10;
++basamak;
}
int digits = 0;
int m = n;
while (m) {
digits++;
m /= 10;
}
digits /= 2;
int tmp = 0, lower_half = 0;
while (digits--) {
tmp *= 10;
tmp += n % 10;
n /= 10;
}
while (tmp) {
lower_half *= 10;
lower_half += tmp % 10;
tmp /= 10;
}
if (basamak % 2==1){
n/=10;
}
int a;
int b;
a = n;
b=lower_half;
printf("%d %d\n",a,b);
int loopTemp;
for(int i=0;i<10;i++){
a=3*a+2;
b=2*b+3;
if(a>b){
temp=a;
a=b;
b=temp;
}
if(a==b){
printf("Congratulations you caught one!!!\n");
return 1;
break;
}
}
if(a!=b){
printf("10 tries were not enough!\n");
return 2;
}
}
int main()
{
int number;
printf("\nEnter a number with at least two digits: ");
scanf("%d",&number);
bolucu(number);
while(bolucu(number) != 1){
printf("\nEnter a new number: ");
scanf("%d",&number);
printf("%d",bolucu(number));
}
return 0;
}
e.g:
This is terminal screen.
As you can see there is a second one. First one is true but i don't want second one.
How can i get rid of the second calling?
(Also sorry for bad code writing, i'm new)
What im missing here?
And i cant use any library other than stdio.(Like math.h)
The reason for this is because you call the bolucu() function both inside the while loop and in it's condition check. To fix this, call the function and hold it's result in a variable once, and then use that single result in both the check and your print statement. Your main function can be rewritten like so:
int main()
{
int number;
printf("\nEnter a number with at least two digits: ");
scanf("%d", &number);
int result = bolucu(number);
while (result != 1)
{
printf("\nEnter a new number: ");
scanf("%d", &number);
result = bolucu(number);
printf("%d", result);
}
return 0;
}
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;
}
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;
}
I want to make a backtracking program to calculate the sum of every prime number smaller then n. Can you help me doing that ? I was working on a code but I do not know why it is not working ! Thx in advance !
I think I`m doing something wrong !
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int v[20],n;
void afisare(int k)
{
int i;
for(i=0;i<=k;i++)
{
printf("%d",v[i]);
}
}
int valid(int k)
{
int i,prim=0;
for(i=1;i<k;i++)
{
if(v[k]%i==0)
{
prim++;
}
}
if(prim==2)
{
return 1;
}
else
{
return 0;
}
}
void backtr(int k)
{
int val;
for(val=1;val<=n;val++)
{
v[k]=val;
if(valid(k))
{
if(k<n-1)
{
afisare(k);
}
else
{
backtr(k+1);
}
}
}
}
int main()
{
int n;
printf("n=");
scanf("%d",&n);
backtr(1);
return 0;
}
A bit late but I hope to help someone else
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int verify(int s) {
if (s == 0)
return 0;
else if (s == 1)
return 1;
else
int z = 0;
int i = 1;
while (i <= s) {
if (s % i == 0)
z++;
i++;
}
if (z == 2)
return s;
else
return 0;
}
int backback(int n, int s, int *sum) {
if (s == n) {
return;
}
int z = verify(s);
*sum = *sum + z;
backback(n, s + 1, sum);
return *sum;
}
int back(int n) {
if (n <= 0)
return 0;
else if (n == 1)
return 1;
else
int sum = 0;
int x = backback(n, 0, &sum);
return x;
}
int main(void) {
int n;
printf("Insert an integer number: \t");
scanf("%d", &n);
int x = back(n);
if (x == 0)
printf("\nInvalid number");
else if (x == 1)
printf("\nThe sum of prime numbers of the number %d is: \t%d", n, x);
else
printf("\nThe sum of prime numbers of the number %d is: \t%d", n, x);
return 0;
}
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/