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);
}
c recursion factorial
add a comment |
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);
}
c recursion factorial
5
Does changing the while to an if solve the problem?
– Peter de Rivaz
Feb 11 '12 at 19:25
1
Your program has an infinite loop whenn > 1
, see Peter's comment
– Seth Carnegie
Feb 11 '12 at 19:27
BTW, if this is a homework problem, please tag it with homework.
– Caleb
Feb 11 '12 at 19:30
2
Just thought you might appreciate some input. Your approach to the problem (implementing factorial recursively?) is wrong. There are two ways to implement factorial, and it seems like you're combining them -- rethink how it works.
– Pochi
Feb 11 '12 at 19:58
add a comment |
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);
}
c recursion factorial
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);
}
c recursion factorial
c recursion factorial
edited Feb 11 '12 at 19:27
Seth Carnegie
60.4k14149215
60.4k14149215
asked Feb 11 '12 at 19:23
MeiMei
50234
50234
5
Does changing the while to an if solve the problem?
– Peter de Rivaz
Feb 11 '12 at 19:25
1
Your program has an infinite loop whenn > 1
, see Peter's comment
– Seth Carnegie
Feb 11 '12 at 19:27
BTW, if this is a homework problem, please tag it with homework.
– Caleb
Feb 11 '12 at 19:30
2
Just thought you might appreciate some input. Your approach to the problem (implementing factorial recursively?) is wrong. There are two ways to implement factorial, and it seems like you're combining them -- rethink how it works.
– Pochi
Feb 11 '12 at 19:58
add a comment |
5
Does changing the while to an if solve the problem?
– Peter de Rivaz
Feb 11 '12 at 19:25
1
Your program has an infinite loop whenn > 1
, see Peter's comment
– Seth Carnegie
Feb 11 '12 at 19:27
BTW, if this is a homework problem, please tag it with homework.
– Caleb
Feb 11 '12 at 19:30
2
Just thought you might appreciate some input. Your approach to the problem (implementing factorial recursively?) is wrong. There are two ways to implement factorial, and it seems like you're combining them -- rethink how it works.
– Pochi
Feb 11 '12 at 19:58
5
5
Does changing the while to an if solve the problem?
– Peter de Rivaz
Feb 11 '12 at 19:25
Does changing the while to an if solve the problem?
– Peter de Rivaz
Feb 11 '12 at 19:25
1
1
Your program has an infinite loop when
n > 1
, see Peter's comment– Seth Carnegie
Feb 11 '12 at 19:27
Your program has an infinite loop when
n > 1
, see Peter's comment– Seth Carnegie
Feb 11 '12 at 19:27
BTW, if this is a homework problem, please tag it with homework.
– Caleb
Feb 11 '12 at 19:30
BTW, if this is a homework problem, please tag it with homework.
– Caleb
Feb 11 '12 at 19:30
2
2
Just thought you might appreciate some input. Your approach to the problem (implementing factorial recursively?) is wrong. There are two ways to implement factorial, and it seems like you're combining them -- rethink how it works.
– Pochi
Feb 11 '12 at 19:58
Just thought you might appreciate some input. Your approach to the problem (implementing factorial recursively?) is wrong. There are two ways to implement factorial, and it seems like you're combining them -- rethink how it works.
– Pochi
Feb 11 '12 at 19:58
add a comment |
9 Answers
9
active
oldest
votes
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.
add a comment |
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
.
add a comment |
This is the method of factorial:
public int fact(int n)
{
if (n < 1)
{
return 1;
}
else
{
return n * fact(n - 1);
}
}
add a comment |
#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("=%dn", argc);
return 0;
}
return argc;
}
add a comment |
/*
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<<"nn ----------- Results ------------";
cout<<"nn 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);
}
OP asked for C, not c++
– Mawg
May 8 '15 at 12:34
add a comment |
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
add a comment |
/*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 %ldn", 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! = %ldn", 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! = %ldn", n, factorial);
return 0;
}
add a comment |
/*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;
}
add a comment |
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);
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9243312%2ffactorial-program-c-using-recursive-function-in-c-with-while-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Feb 11 '12 at 19:29
Adam MihalcinAdam Mihalcin
11.9k32442
11.9k32442
add a comment |
add a comment |
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
.
add a comment |
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
.
add a comment |
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
while(n>1)
is causing the looping. You don't change n
inside the loop, so the loop is infinite.
Change while
to if
.
answered Feb 11 '12 at 19:29
jpalecekjpalecek
40.6k581127
40.6k581127
add a comment |
add a comment |
This is the method of factorial:
public int fact(int n)
{
if (n < 1)
{
return 1;
}
else
{
return n * fact(n - 1);
}
}
add a comment |
This is the method of factorial:
public int fact(int n)
{
if (n < 1)
{
return 1;
}
else
{
return n * fact(n - 1);
}
}
add a comment |
This is the method of factorial:
public int fact(int n)
{
if (n < 1)
{
return 1;
}
else
{
return n * fact(n - 1);
}
}
This is the method of factorial:
public int fact(int n)
{
if (n < 1)
{
return 1;
}
else
{
return n * fact(n - 1);
}
}
edited Sep 6 '13 at 9:30
Sankumarsingh
7,58274063
7,58274063
answered Sep 6 '13 at 8:57
HieuHieu
311
311
add a comment |
add a comment |
#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("=%dn", argc);
return 0;
}
return argc;
}
add a comment |
#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("=%dn", argc);
return 0;
}
return argc;
}
add a comment |
#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("=%dn", argc);
return 0;
}
return argc;
}
#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("=%dn", argc);
return 0;
}
return argc;
}
answered Jul 11 '12 at 9:31
wildplasserwildplasser
30.9k53968
30.9k53968
add a comment |
add a comment |
/*
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<<"nn ----------- Results ------------";
cout<<"nn 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);
}
OP asked for C, not c++
– Mawg
May 8 '15 at 12:34
add a comment |
/*
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<<"nn ----------- Results ------------";
cout<<"nn 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);
}
OP asked for C, not c++
– Mawg
May 8 '15 at 12:34
add a comment |
/*
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<<"nn ----------- Results ------------";
cout<<"nn 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);
}
/*
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<<"nn ----------- Results ------------";
cout<<"nn 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);
}
answered Dec 11 '13 at 10:02
Perfect Computer NotesPerfect Computer Notes
313
313
OP asked for C, not c++
– Mawg
May 8 '15 at 12:34
add a comment |
OP asked for C, not c++
– Mawg
May 8 '15 at 12:34
OP asked for C, not c++
– Mawg
May 8 '15 at 12:34
OP asked for C, not c++
– Mawg
May 8 '15 at 12:34
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Aug 28 '17 at 13:55
answered Apr 28 '15 at 3:12
Pankaj PrakashPankaj Prakash
773919
773919
add a comment |
add a comment |
/*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 %ldn", 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! = %ldn", 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! = %ldn", n, factorial);
return 0;
}
add a comment |
/*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 %ldn", 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! = %ldn", 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! = %ldn", n, factorial);
return 0;
}
add a comment |
/*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 %ldn", 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! = %ldn", 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! = %ldn", n, factorial);
return 0;
}
/*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 %ldn", 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! = %ldn", 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! = %ldn", n, factorial);
return 0;
}
answered Feb 11 '12 at 19:42
cMinorcMinor
12.9k63198335
12.9k63198335
add a comment |
add a comment |
/*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;
}
add a comment |
/*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;
}
add a comment |
/*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;
}
/*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;
}
answered Apr 3 '16 at 5:38
Aman RaoAman Rao
1
1
add a comment |
add a comment |
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);
}
add a comment |
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);
}
add a comment |
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);
}
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);
}
answered Nov 13 '18 at 12:48
Eksplorator DanychEksplorator Danych
53
53
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9243312%2ffactorial-program-c-using-recursive-function-in-c-with-while-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
Does changing the while to an if solve the problem?
– Peter de Rivaz
Feb 11 '12 at 19:25
1
Your program has an infinite loop when
n > 1
, see Peter's comment– Seth Carnegie
Feb 11 '12 at 19:27
BTW, if this is a homework problem, please tag it with homework.
– Caleb
Feb 11 '12 at 19:30
2
Just thought you might appreciate some input. Your approach to the problem (implementing factorial recursively?) is wrong. There are two ways to implement factorial, and it seems like you're combining them -- rethink how it works.
– Pochi
Feb 11 '12 at 19:58