- 3

C

Write a program to calculate the factorial of an integer number

29th Jan 2022, 8:14 PM
Morelle Sam
Morelle Sam - avatar
11 Réponses
+ 3
Morelle if you multiply large int by large int you will need a larger variable // Set it to one for identity property of math // Factoral = N(N-1) until N = 1 // ie 4! = 4x3x2x1 = 24 long factoral = 1; int number_to_factoral; puts("Enter an integer"); // Don't need printf if you are not using formatting. scanf(" %d", &number_to_factoral); //This is only going to work for whole numbers for(int i = number_to_factoral; i > 0 ; i--){ factoral *= i; } printf("%d! is %ld", number_to_factoral, factoral);
29th Jan 2022, 9:01 PM
William Owens
William Owens - avatar
+ 3
Done. What's next?
29th Jan 2022, 8:35 PM
Brian
Brian - avatar
+ 2
Morelle Sam there are many syntax errors in the program and a math/logic error in the calculation. Uncapitalize the first letters in the lines. (I assume that came from typing on your phone.) Add missing semicolons. Use printf, not print. Correct the spelling of "integer". Use format specifier %d, not %dd in scanf and printf. Use ; in the for loop, not comma. Fact is not defined. Insert int fact=1; before the loop. Fix the calculation to be fact = i*fact;. Replace the final print with printf("the factorial of %d = %d", n, fact);
29th Jan 2022, 8:55 PM
Brian
Brian - avatar
+ 1
int i = 1 Int n Print (“enter an interger”); Scanf(“%dd”, &n); for(i=1, i<=n, i++) Fact = n*(n-1); Printf(“the factorial %dd= n, fact) return 0;
29th Jan 2022, 8:35 PM
Morelle Sam
Morelle Sam - avatar
+ 1
Thank you 🙏🏽
29th Jan 2022, 8:57 PM
Morelle Sam
Morelle Sam - avatar
+ 1
G'day Morelle Sam did you know you can link your code to your post? (Has to be a "code bit" can't be an attempt at code coach, most people copy from code coach into a code bit). Also, have you found the search bar of SoloLearn Q &A? C language is very powerful but also is very unforgiving for incorrect syntax. Brian was right in every way. https://www.sololearn.com/discuss/333866/?ref=app
29th Jan 2022, 9:40 PM
HungryTradie
HungryTradie - avatar
+ 1
https://code.sololearn.com/cXNy62wCMMnG/?ref=app Follow the code
30th Jan 2022, 3:06 PM
Partha
Partha - avatar
+ 1
#include<stdio.h> int factorial(int number) { If( number==1 || or number==0) { return 1; } /* this is done because factorial of 0 and 1 is always 1*/ else { return (number*factorial(number-1) ) ; // this is the formula of factorial of a number } } int main () { int num; printf("Enter the number \n") ; scanf("%d" , &num) ; printf("The factorial of%d is%d" \n" , num, factorial (num)) ; return 0; } }
31st Jan 2022, 2:43 PM
Simran Singh
Simran Singh - avatar
0
Am not really familiar with the app. Am still learning. Plus I am still struggling with c programming
29th Jan 2022, 10:06 PM
Morelle Sam
Morelle Sam - avatar
0
In C, #include <stdio.h> int main() { int n,i,j=1; scanf("%d",&n); for(i=n;i>1;i--) { j*=i; } printf("%d",j); return 0; }
8th Jun 2022, 3:25 AM
Ranit Ghosh
Ranit Ghosh - avatar
- 1
#include <stdio.h> int main() { int n, i; unsigned long long fact = 1; printf("Enter an integer: "); scanf("%d", &n); // shows error if the user enters a negative integer if (n < 0) printf("Error! Factorial of a negative number doesn't exist."); else { for (i = 1; i <= n; ++i) { fact *= i; } printf("Factorial of %d = %llu", n, fact); } return 0; }
31st Jan 2022, 1:52 PM
Rajulapudi Aswini
Rajulapudi Aswini - avatar