Recursion factorial program confusion in C - c

int factorial(int n);
int main()
{
int n;
printf("Enter an positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, factorial(n));
return 0;
}
int factorial(int n)
{
if(n!=1)
return n*factorial(n-1);
}
Please explain how this program is working. I applied a for loop after if statement in factorial function but how this is working.

int factorial(int n){
if(n==1)
return 1;
return n*(factorial(n-1));
}
This is correct :) I think that your code shouldn't compile because the function factorial(int n) has no defined return statement!

Related

can not run the simple function program

#include <stdio.h>
#include <conio.h>
int sum();
//find the sum of two numbers entered by the user.
int main(){
int n, m;
printf("Enter two numbers: \n");
scanf("%d", &m);
scanf("%d", &n);
int result = sum(m, n);
printf(result);
getch();
return 0;
}
int sum(m, n){
int c;
c = m+n;
return c;
}
i was just writing a simple program with function but i don't know why it is not running it tells me to debug can someone tell me what is the problem with it
Change int sum() ; to int sum(int, int) ;
Change printf(result) to printf("%d", result) ;
Change int sum(m, n) to int sum(int m, int n) ;(https://i.stack.imgur.com/Z07cx.jpg)
Another way of writing above program is
(https://i.stack.imgur.com/VOXRY.jpg)
#include <stdio.h>
#include <conio.h>
int sum(int n, int m);
//find the sum of two numbers entered by the user.
int main(){
int n, m;
printf("Enter two numbers: \n");
scanf("%d", &m);
scanf("%d", &n);
int result = sum(m, n);
printf("%d",result);
getch();
return 0;
}
int sum(int n, int m){
int c;
c = m+n;
return c;
}

Why does my code work without "void" in my main function?

I have to do a short assignment for my introductory C class, where I have to ask for a number "N" from the user and calculate it's factorial. The requirements were for me to create a function with the prototype long int factorial(int N). I managed to do it, but I'm confused as to why my code is working with a specific change I made. Here is my code:
#include <stdio.h>
long int factorial(int);
int main(void)
{
int N;
printf("Enter a number: ");
scanf("%d", &N);
printf("The factorial of %d is: %ld", N, factorial(N));
return 0;
}
long int factorial(int N)
{
long int result=1 ;
int i;
for(i=1; i<=N; i++)
result = result * i;
return result;
}
My code at this point didn't work, and would just return the result of N+1 (if I input 5 for example, it would output 6). I was tweaking random things at this point to see what was the problem, and the removal of "void" in my main function fixed it. The problem is, I don't understand why.
#include <stdio.h>
long int factorial(int);
int main()
{
int N;
printf("Enter a number: ");
scanf("%d", &N);
printf("The factorial of %d is: %ld", N, factorial(N));
return 0;
}
long int factorial(int N)
{
long int result=1 ;
int i;
for(i=1; i<=N; i++)
result = result * i;
return result;
}
Could anyone explain why the removal of void in my code fixed this?

Prime factors in c by using only recursion

I wrote this program in C for prime factors but its crashing after printing first prime factor, So i need help
#include<stdio.h>
void prime(int n,int i)
{
i=2;
if(n%i==0)
{
printf("%d ",i);
n=n/i;
prime(n,i);
}
else
{
i++;
prime(n,i);
}
}
void main()
{
int n;
scanf("%d",&n);
prime(n,2);
}
#include<stdio.h>
void prime(int n,int i)
{
if(n==0)
;
if(n==1)
;
else if(n%i==0){
printf("%d ", i);
n=n/i;
prime(n,i);
}
else{
i++;
prime(n,i);
}
}
int main()
{
int n;
scanf("%d",&n);
prime(n,2);
return 0;
}
If you add the cases for n==0 and n==1 (and remove the i=2;), you'll be all set.
edit - removed voided main as suggested by Jonathan Leffler
It's doing an infinite loop as you are resetting i every time when doing
i = 2;
But even without that, you'll probably overflow i and you don't have a recursion terminal case.
you will have to rethink your logic for this to work.

Sum of Factorial Fraction

I'm working on a factorial sum which goes like: 1/1!+1/2!+1/3!... until the desired count. Here is my code so far:
#include <stdio.h>
int factorial(int n)
{
if (n==0)
return 1;
else
return 1/(n * factorial(n-1));
}
int main ()
{
int i, n;
float sum=0;
printf("Enter desired factorial fraction: ");
scanf("%d", &n);
for (i=1; i<=n; i++) sum = sum + factorial(i);
printf("The value is %f\n", sum);
return 0;
}
I have a small idea of what I'm doing, I am really new at this. My thought process was to set up the number crunching function (My jargon is probably off) and then initiate the main function. I thought I had the right set up, but after a couple hours with this I feel just lost. Any help and guidance will be much appreciated.
Update
Here is the updated code:
#include <stdio.h>
float factorial(int n)
{
if (n==1)
return 1;
else
return ((1.0/n) * factorial(n-1.0));
}
int main ()
{
float i, n;
float sum=0;
printf("Enter desired factorial fraction: ");
scanf("%f", &n);
for (i=1; i<=n; i++) sum = sum + factorial(i);
printf("The value is %f\n", sum);
return 0;
}
So thank you guys, now the only thing left is for me to figure out why my input of "0" does not produce the result "1" since 0!=1. Should I move that if statement inside the "int main()"?
You should use float as the return value of factorial() (you can only get 0 or 1 if you use int) and its logic is not correct.
You need to change it to
float factorial(int n)
{
if (n==0) // or n==1
return 1;
else
return (1.0/n) * factorial(n-1);
}
Full code example can be seen here: http://ideone.com/o2XGhE
your factorial() must be like this
int factorial(double n)
{
if (n==0)
return 1;
else
return ((1/n) * factorial(n-1));
}
The definition of factorial() should be
int factorial(int n)
{
if
(n==0) return 1;
else
return n * factorial(n-1);
}
And in your main() function, you should do something like:
for (i=1; i<=n; i++) sum = sum + 1.0 / factorial(i);
Have a separate function the calculate factorial(n)
than add the rest of the code, so the only recursive thing will be calculation of n!
you can use this one :
int factorial(float n)
{
if (n==1)
return 1;
else
return (1/n) * factorial(n-1));
}
An elegant solution is to use conditional ternary operator:
float factorial(float n) {
return n ? ((1/n) * factorial(n-1)) : 1;
}

Factorial program c using recursive function in C with while loop

Factorial program using recursion in c with while loop.In this program once the execution reaches the function return statement it will not go back to the function call. Instead,it executes the function repeatedly. can anybody please tell me what's wrong in this program.
#include<stdio.h>
int fact(int n)
{
int x=1;
while(n>1)
{
x=n*fact(n-1);
}
return(x);
}
void main()
{
int n,fact1;
scanf("%d",&n);
fact1=fact(n);
printf("%d",fact1);
}
The reason that your program gets into an infinite loop is that the loop
while (n > 1)
x = n * fact(n-1);
never decrements n. Since n never decreases, the program will never leave the loop. Peter is correct in the comments: change the while to an if, and you will have a factorial function that handles all positive parameters correctly. However, even after changing while to if, your fact won't have the property that fact(0) == 1, as is required for a correct factorial function.
This
while(n>1)
is causing the looping. You don't change n inside the loop, so the loop is infinite.
Change while to if.
This is the method of factorial:
public int fact(int n)
{
if (n < 1)
{
return 1;
}
else
{
return n * fact(n - 1);
}
}
#include <stdio.h>
#include <stdlib.h>
/** main returns int, use it! */
int main(int argc, char **argv)
{
if (argc <= 2) {
if (argv) argc = atoi(argv[1] );
else return argc;
}
argc *= main (argc-1, NULL);
if (argv) {
printf("=%d\n", argc);
return 0;
}
return argc;
}
/*
Write a C++ Program to input a positive number,
Calculate and display factorial of this number
by recursion.
*/
#include<iostream.h>
#include<conio.h>
long factorial(int n);
void main()
{
clrscr();
int number, counter;
label1:
cout<<"\n Enter the Number = ";
cin>>number;
if ( number < 0)
{
cout<<"\n Enter a non negative number, please!";
goto label1;
}
cout<<"\n\n ----------- Results ------------";
cout<<"\n\n The Factorial of the number "<<number<<"\n is "<<factorial(number);
getch();
}
long factorial(int n)
{
if ( n == 0 )
return 1;
else
return n * factorial(n-1);
}
You can use the simple approach using recursion
#include <stdio.h>
int fact(int n)
{
if(n==1)
return 1;
else
return n * fact(n-1);
}
int main()
{
int f;
f = fact(5);
printf("Factorial = %d",f);
return 0;
}
Read it more C program to find factorial using recursion
/*several versions of a factorial program.*/
#include<stdio.h>
int main()
{
int n;
long factorial;
printf("Compute the factorial of what number? ");
scanf("%d", &n);
factorial = 1L;
while(n > 0)
factorial *= n--;
printf("The factorial is %ld\n", factorial);
return 0;
}
#include<stdio.h>
/*the same, but counting up to n instead of down to 0*/
int main()
{
register int count;
int n;
long factorial;
printf("Compute the factorial of what number? ");
scanf("%d", &n);
factorial = 1L;
count = 1;
while(count <= n)
factorial *= count++;
printf("%d! = %ld\n", n, factorial);
return 0;
}
#include<stdio.h>
/*an equivalent loop using 'for' instead of 'while'*/
int main()
{
register int count;
int n;
long factorial;
printf("Compute the factorial of what number? ");
scanf("%d", &n);
for(factorial = 1L, count = 1; count <= n; count++)
factorial *= count;
printf("%d! = %ld\n", n, factorial);
return 0;
}
/*WAP to find factorial using recursion*/
#include<stdio.h>
#include<stdlib.h>
int fact1=1;
int fact(int no)
{
fact1=fact1*no;
no--;
if(no!=1)
{
fact(no);
}
return fact1;
}
int main()
{
int no,ans;``
system("clear");
printf("Enter a no. : ");
scanf("%d",&no);
ans=fact(no);
printf("Fact : %d",ans);
return 0;
}
Factorial program using recursion in C with while loop.
int fact(int n)
{
int x=1;
while(n>=1)
{
return(n*fact(n-1));
}
return(1);
}
You can use this approach.
int factorial(int a)
{
while(a>1)
{
return a*factorial(a-1);
}
return 1;
}

Resources