- 3
C
Write a program to calculate the factorial of an integer number
11 Answers
+ 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);
+ 3
Done. What's next?
+ 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);
+ 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;
+ 1
Thank you šš½
+ 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
+ 1
https://code.sololearn.com/cXNy62wCMMnG/?ref=app
Follow the code
+ 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;
}
}
0
Am not really familiar with the app. Am still learning. Plus I am still struggling with c programming
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;
}
- 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;
}