C program to find Factorial

 TASK-

Write a complete and an efficient C program that takes an integer number from the user and calculates its factorial.

NOTE: There are two possibilities:

  1.  The Entered number is 0

In this possibility the factorial will be 1.

     2. The Entered number is non zero.

for example: Entered number is 4 factorial would be equal to 24.

 


 CODE:

#include<stdio.h>
int main(){


int fact=1,num,i;   //fact is taken for factorial

        printf("Enter the number ");
scanf("%d",&num);

         if (num==0)  //First possibility 
{
printf("The factorial of %d is 1",num);
}
else    //Second possibility
{
for (i=1;i<=num;i++)  

/* logic of factorial = loop gets start from 1 and goes till num,if we break num (e.g num=3) 1*2*3 would be factorial same thing happening here fact = 1 and it gets multiplied by i, i=1 then i=2..... and upto num*/
{
fact=fact*i;
}
printf("The factorial of %d is %d",num,fact);
}
getchar();
return 0;
}

NEED WORK PRODUCE WORK

My name is Abdul Rehman I am from Pakistan. I am doing BS in Computer and information sciences. Currently, I am creating these blogs to help students of computer sciences all over the world..

Post a Comment (0)
Previous Post Next Post