Friday, 22 May 2020

Factorial program in c


Factorial program in c:
The factorial of a non negative number value is the product of all the integers from 1 to that particular number.
                2! = 2*1 =2
                3! = 3*2*1 =6
                4! = 4*3*2*1 = 24
                5! = 5*4*3*2*1 = 120
                6! = 6*5*4*3*2*1 = 720
                7! = 7*6*5*4*3*2*1 = 5040
                8! = 8*7*6*5*4*3*2*1 = 40320
               
                above are some factorials.
                Factorials are calculated for non negative numbers.
 
There are several ways or methods to find the factorial of given number like 
  • using loops 
  • factorial using functions 
  • factorial using recursion.
First read a number and then whether that given number is greater than or equal to zero or not. If greater then find the factorial of given number else print the message as not defined as number is negative. Zero factorial is 1. One factorial is also 1 (i.e.; 0! =1 and 1! = 1).
Below is the program to find the factorial program in c .
 
Factorial program using for loop:

#include <stdio.h>
void main(){
   int number, i, factorial =1;
   printf("provide the number to find factorial. ");
   scanf("%d",&number);
  
   // check number is greater than zero or not.
   if( number>=0){
       for(i=1; i<=number; i++){
                                                factorial= factorial * i;
                   }
                   printf("factorial of given number is %d", factorial);
   }
   else{
                printf("factorial not defined for negative values....");
                printf("please enter positive numbers ");
                }
}

output1:
                provide the number to find factorial
                5
                factorial of given number is 120
output2:
                provide the number to find factorial
                6
                factorial of given number is 720
output3:
                provide the number to find factorial
                7
                factorial of given number is 5040
output4:
                provide the number to find factorial
                0
                factorial of given number is 1
output5:
                provide the number to find factorial
                1
                factorial of given number is 1
output6:
                provide the number to find factorial
                -5
                factorial not defined for negative values....
                please enter positive numbers
output7:
                provide the number to find factorial
                9
                factorial of given number is ********* (unknown value)


In above program we can find the factorial of a given number upto 7 but if we try to find the factorial of 8 we are getting some unknown value because it is crossing the range of int datatype.
Hence instead of using int we can use long int.


#include<stdio.h>
void main(){
   long int number, i, factorial=1;
   printf("provide the number to find factorial. ");
   scanf("%ld", &number);
  
   // check number is greater than zero or not.
   if(number>=0){
       for(i=1; i<=number; i++){
                                                factorial =factorial*i;
                   }
                   printf(" factorial of given number is %ld", factorial);
   }
   else{
                printf(" factorial not defined for negative values....");
                printf(" please enter positive numbers ");
                }
}





factorial program using while loop:


#include<stdio.h>
void main(){
   int number, i, factorial=1;
   printf("provide the number to find factorial. ");
   scanf("%d",&number);
  
   // check number is greater than zero or not.
   if(number>=0){
                                i=1;
       while( i<=number ){
                                                factorial= factorial * i;
                                                i++;
                   }
                   printf(" factorial of given number is %d", factorial);
   }
   else{
                printf(" factorial not defined for negative values....");
                printf(" please enter positive numbers ");
                }
}



we can also write the factorial of number using recursion.

No comments:

Post a Comment