Q1: Write a C program to print Factorial of a number as an output?
Using Goto:
#include<stdio.h>
void main()
{
int n;
int s=1;
clrscr();
scanf("%d",&n);
printf("\nthe number is=%d",n);
again :
if(n>1)
{
s=s*n;
n=n-1;
goto again;
}
printf("\nfactorial of no.- %d=\t%d",n,s);
getch();
}
Using For loop:
#include<conio.h>
#include<stdio.h>
void main()
{
//factorial number
int n,i;
int f=1;
clrscr();
scanf("%d",&n);
printf("\n\nthe number is=%d",n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("\n\nfactorial of no. %d = %d",n,f);
getch();
}
***********************************************************************************
Sample Output:
Comments
Post a Comment
Thanks for the comment and don't forget to subscribe.