Menu driven C program and algorithm to perform palindrome, armstrong, factorial, fibonacci.

Algorithm

1.start
2.do
3.display 1. fibonacci series 2. palindrome 3. armstrong 4. factorial 
4.read choice
5.if choice=1
   5.1 read limit
   5.2 c=1,a=0,b=0
   5.3 while c<=limit
   5.4 display c
   5.5 a=c+b
   5.6 b=c
   5.7 c=a go to step 5.3
6. else if choice=2
   6.1 read number
   6.2 set rev=0,rem=0 and dup=num
   6.3 repeat steps 6.4 to 6.6 until num>0
   6.4 rem=num%10
   6.5 rev=(rev*10)+rem
   6.6 num=num/10
   6.7 after num>0 is false check if rev=dup
   6.8 display number is palindrome
   6.9 else display number isn't palindrome
7. else if choice =3
   7.1 read number
   7.2 set dup=num
   7.3 repeat steps 7.4 to 7.6 until num>0
   7.4 rem=num%10
   7.5 sum=sum+(rem*rem*rem)
   7.6 num=num/10
   7.7 after condition is false check if sum==dup
   7.8 display number is armstrong
   7.9 else number isn't armstrong
8. else if choice =4
   8.1 read number
   8.2 set fact=1,i=2
   8.3 if i<=num
   8.4 fact=fact*i
   8.5 i++ go to step 8.3
   8.6 after condition is false
   8.7 display fact
   8.8 else
9. default display invalid choice
10. stop

Program
# include<stdio.h>
#include<conio.h> //only windows users
void main()
{
clrscr();               //only for windows users
int ch,a,b,c,rem,rev,i,num,dup,sum,fact,lim;
a=0;
b=0;
c=1;
do
{
printf("\n 1. fibonacci");
printf("\n 2. palindrome");
printf("\n 3. armstrong");
printf("\n 4. factorial");
printf("\n  Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("\n enter the limit");
scanf("%d",&lim);
while(c<=lim)
{
printf("\n%d\t",c);
a=c+b;
b=c;
c=a;
}
break;
}
case 2:
{
printf("\n enter a number");
scanf("%d",&num);
rev=0;rem=0;
dup=num;
while(num>0)
{
rem=num%10;
rev=(rev*10)+rem;
num=num/10;
}
if(rev==dup)
printf("\n number is palindrome");
else
printf("\n number is not palindrome");
break;
}
case 3:
{
printf("\n enter the number");
scanf("%d",&num);
rem=0;sum=0;
dup=num;
while(num>0)
{
rem=num%10;
sum=sum+(rem*rem*rem);
num=num/10;
}
if(sum==dup)
printf("\n the number is armstrong");
else
print("\n the number is not armstrong");
break;
}
case 4:
{
printf("\n enter a number");
scanf("%d",&num);
fact=1;
for(i=2;i<=num;i++)
{
fact=fact*i;
}
printf("\n factorial of %d=%d\n",num,fact);
break;
}
default: printf("\n INVALID ENTRY");
}
}while(ch!=5);
getch();             //only for windows users
}


No comments:

Post a Comment