0

Factorial nos less than N

Write a C program to list all the factorial numbers less than or equal to an input number n. A number N is called a factorial number if it is the factorial of a positive integer. For example, the first few factorial numbers are 1, 2, 6, 24, 120, ... *Note* - We do not list the factorial of 0. Input ----- A positive integer, say n. Output ------ All factorial numbers less than or equal to n. please see the code i have uploaded below. and help me please. thanks a lot. https://code.sololearn.com/c4PrwXX2Dehe/?ref=app John Wells

19th Sep 2018, 3:39 PM
Nilutpol Kashyap
Nilutpol Kashyap - avatar
12 Antworten
+ 4
Missing semicolons in last two lines Put semicolons on line 21 & 22
19th Sep 2018, 3:59 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 3
use do while loop, condition will be same #include<stdio.h> int fact(int n) { int ans = 1; for(int i = 0; i < n; i++) { ans = ans * (i + 1); } return ans; } int main() { int N, n = 1, f; scanf("%d",&N); do { f = fact(n); printf("%d\t",f); n = n + 1; }while(f < n); return 0; }
19th Sep 2018, 4:17 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 2
Or initialize f, if you want n=5 to only 1, 2, & 6. If you want to skip 6, add if (f<N) around printf.
19th Sep 2018, 4:11 PM
John Wells
John Wells - avatar
+ 2
John Wells thank you sir
19th Sep 2018, 5:08 PM
Nilutpol Kashyap
Nilutpol Kashyap - avatar
+ 1
Line 15 change: while(f<N) to: while(n<N)
19th Sep 2018, 4:04 PM
John Wells
John Wells - avatar
+ 1
f needs to be assigned something. otherwise, it could be 123456789.
19th Sep 2018, 4:18 PM
John Wells
John Wells - avatar
+ 1
John Wells thank you sir. I initialized f= 0 and added an if. (f>N) statement before break. it's working great. thanks again
19th Sep 2018, 4:22 PM
Nilutpol Kashyap
Nilutpol Kashyap - avatar
+ 1
nAutAxH AhmAd thank you. but using do while it would simply print one extra factorial no. I have sorted out my mistake. thank you very much for your time, help and patience. ☺️
19th Sep 2018, 4:23 PM
Nilutpol Kashyap
Nilutpol Kashyap - avatar
+ 1
Uninitialized variables have a value of whatever is left in memory, until you assign something to them. It had to be higher than 5.
19th Sep 2018, 4:29 PM
John Wells
John Wells - avatar
0
nAutAxH AhmAd I have corrected it but still it is showing one factorial more. please help me
19th Sep 2018, 4:04 PM
Nilutpol Kashyap
Nilutpol Kashyap - avatar
0
John Wells its not working. I have included an if statement inside main. it's working for inputs like 25 & 30 etc but it is not working for values like 4, 5, 6. can u pls look into it.
19th Sep 2018, 4:16 PM
Nilutpol Kashyap
Nilutpol Kashyap - avatar
0
John Wells sir can you please explain in more details. I am not able to understand.
19th Sep 2018, 4:24 PM
Nilutpol Kashyap
Nilutpol Kashyap - avatar