Brijmohan lal Sahu - Facebook
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Showing posts with label else. Show all posts
Showing posts with label else. Show all posts

Friday, December 5, 2014

Program to print factorial of any input number using while loop in C.



factorial


Code Box

-----------------------------------------------

/*Program to print factorial of any input number*/

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
float fact;
/* If we take fact as integer, it can not be able
to store factorial of value greater than 9 */
fact=1.0;
clrscr();

printf("\nEnter any number:");
scanf("%d",&num);

clrscr();  //for clear screen

printf("\n-------------------------------");
printf("\nNumber entered is: %d",num);
printf("\n-------------------------------");
printf("\nFactorial of %d is:",num);
printf("\n-----------------------------\n");

while(num>=1)
{
if(num==0)
{
printf("\n1"); //factorial of 0 is 1
break;
}
fact=fact*num;
num--;
}

printf("\n%.0f",fact);

printf("\n------------------------------");

getch();
}


Program to print factorial of any input number using for loop in C.



factorial


Code Box
----------------------------------------------------------

/*Program to print factorial of any input number*/

#include<stdio.h>
#include<conio.h>
void main()
{
int num,i;

float fact;

/* If we take fact as integer, it can not be able
to store factorial of value greater than 9 */

fact=1.0;

clrscr();

printf("\nEnter any number:");
scanf("%d",&num);

clrscr();  //for clear screen

printf("\n-------------------------------");
printf("\nNumber entered is: %d",num);
printf("\n-------------------------------");
printf("\nFactorial of %d is:",num);
printf("\n-----------------------------\n");


if(num==0)
{
printf("\n1"); //factorial of 0 is 1
}
else
{
for(i=1;i<=num;i++)
{
fact=fact*num;
num--;
}
}
printf("\n%.0f",fact);

printf("\n------------------------------");

getch();
}