Skip to main content

To Find Factorial Of A Number Using C Program

 

Program 1: Factorial program in c using for loop

  1. #include<stdio.h>
  2. int main(){
  3. int i,f=1,num;
  4.  
  5. printf("Enter a number: ");
  6. scanf("%d",&num);
  7.  
  8. for(i=1;i<=num;i++)
  9. f=f*i;
  10.  
  11. printf("Factorial of %d is: %d",num,f);
  12. return 0;
  13. }

Result

Enter a number: 8
Factorial of 8 is: 40320



Program 2: Factorial program in c using pointers

  1. #include<stdio.h>
  2.  
  3. void findFactorial(int,int *);
  4. int main(){
  5. int i,factorial,num;
  6.  
  7. printf("Enter a number: ");
  8. scanf("%d",&num);
  9.  
  10. findFactorial(num,&factorial);
  11. printf("Factorial of %d is: %d",num,*factorial);
  12.  
  13. return 0;
  14. }
  15.  
  16. void findFactorial(int num,int *factorial){
  17. int i;
  18.  
  19. *factorial =1;
  20.  
  21. for(i=1;i<=num;i++)
  22. *factorial=*factorial*i;
  23. }

Result

Enter a number: 8
Factorial of 8 is: 40320



Program 3: Factorial program in c using function

  1. #include<stdio.h>
  2.  
  3. int findFactorial(int);
  4. int main(){
  5. int i,factorial,num;
  6.  
  7. printf("Enter a number: ");
  8. scanf("%d",&num);
  9.  
  10. factorial = findFactorial(num);
  11. printf("Factorial of %d is: %d",num,factorial);
  12.  
  13. return 0;
  14. }
  15.  
  16. int findFactorial(int num){
  17. int i,f=1;
  18.  
  19. for(i=1;i<=num;i++)
  20. f=f*i;
  21.  
  22. return f;
  23. }

Result

Enter a number: 8
Factorial of 8 is: 40320



Program 4: Factorial series in c

  1. #include<stdio.h>
  2. int main(){
  3. long f=1;
  4. int i,num,min,max;
  5.  
  6. printf("Enter the minimum range: ");
  7. scanf("%d",&min);
  8.  
  9. printf("Enter the maximum range: ");
  10. scanf("%d",&max);
  11.  
  12. printf("Factorial series in given range: ");
  13. for(num=min;num<=max;num++){
  14. f=1;
  15.  
  16. for(i=1;i<=num;i++)
  17. f=f*i;
  18.  
  19. printf("%ld ",f);
  20. }
  21.  
  22. return 0;
  23. }

Result

Enter the minimum range: 1
Enter the maximum range: 10
Factorial series in given range: 1 2 6 24 120 720 5040 40320 362880 



Program 5: Factorial program in c without using recursion

  1. #include<stdio.h>
  2. int main(){
  3. int i=1,f=1,num;
  4.  
  5. printf("Enter a number: ");
  6. scanf("%d",&num);
  7.  
  8. while(i<=num){
  9. f=f*i;
  10. i++;
  11. }
  12.  
  13. printf("Factorial of %d is: %d",num,f);
  14. return 0;
  15. }

Result

Enter a number: 5
Factorial of 5 is: 120

Comments

Popular posts from this blog

C program to find prime factors of a number

  Write a C program to input a number from user and find Prime factors of the given number using loop. C program to list all prime factors of a given number. Logic to find prime factors of a number in C programming. Example Input Input any number: 10 Output Prime factors of 10: 2, 5 Required knowledge Basic C programming ,  If statement ,  For loop ,  Nested loop What is Prime factor? Factors of a number  that are  prime numbers  are called as Prime factors of that number. For example: 2 and 5 are the prime factors of 10. Logic to check prime factors of a number Step by step descriptive logic to find prime factors. Input a number from user. Store it in some variable say  num . Run a loop from  2  to  num/2 , increment 1 in each iteration. The loop structure should look like  for(i=2; i<=num/2; i++) . You may think why loop from 2 to  num/2 ? Because prime number starts from 2 and any factor of a number  n  is ...

Questions of C Program

1. Write a C program to find the area of a circle. 2. Write a C program to find the area of a triangle. 3. Write a C program to swap two numbers without temp variable (or a third variable) 4. Write a C program to print ASCII value of the character 5. Write a C program to read a character in uppercase and then print in lowercase. 6. Write a C program to swap two numbers with temp variable 7. Write a C program to calculate the average of two number 8. Write a C program to calculate the simple interest. 9. Write a C program to find the quotient and remainder of two numbers 10.Write a C program that displays the size of every datatype. 1. Write a c program to add two integers and print the result ,inputs must be taken using scanf function. 2. Write a c program to add two float numbers without third variable and print the result ,inputs must be taken using scanf function. 3. Write a c program, to subtract two integers and print the result, inputs must be taken using scanf function. 4. Write...