Wednesday, December 2, 2009

C Program to calculate factorialmsum of digits nd check whether the year is leap or not using functions,switch case, do-while

#include
#include
void yr();
void fac();
void sum();
void main()
{
int a;
clrscr();
do
{
printf("Menu");
printf("\n1:To know the year is leap or not");
printf("\n2:For factorial");
printf("\n3:For sum of digit");
printf("\n4:For Exit");
printf("\nEnter your choice");
scanf("%d",&a);
switch(a)
{
case 1:yr();
break;
case 2:fac();
break;
case 3:sum();
break;
case 4:exit(0);
default:printf("Invalid case");
}
}while(a!=4);
getch();
}
void yr()
{
int year;
printf("Enter any year");
scanf("%d",&year);
if((year%4)==0)
{
printf("\nYear is leap");
}
else
{
printf("Year is not leap");
}
getch();
}
void fac()
{
int n,m=1;
printf("\nEnter any no.");
scanf("%d",&n);
while(n>=1)
{
m=m*n;
n=n-1;
}
printf("%d",m);
getch();
}
void sum()
{
int i,n,s=0;
printf("\nEnter value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
s=s+i;
}
printf("Sum=%d",s);
getch();
}

No comments: