 Factorial nos less than N | Sololearn: Learn to code for FREE!

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

+4

Missing semicolons in last two lines Put semicolons on line 21 & 22

+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; }

+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.

+2

John Wells thank you sir

+1

Line 15 change: while(f<N) to: while(n<N)

+1

f needs to be assigned something. otherwise, it could be 123456789.

+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

+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. ☺️

+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.

0