Simple Menu Driven Calculator Program Using Switch Statement
Simple Menu Driven Calculator Program Using Switch Statement
Algorithm:
Step 1: Start the program
Step 2: Read the two variable num1 and num2
Step 3: Display menu
Step 4: Enter the option code
Step 5: Evaluate option code with case statements
Step 5.1: case 1 ans1=num1+num2, print ans1. goto step 7
Step 5.2: case 2 ans1=num1-num2, print ans1. goto step 7
Step 5.3: case 3 ans1=num1*num2, print ans1. goto step 7
Step 5.4: case 4 ans2=(float)num1/num2, print ans2. goto step 7
Step 6: Entered case option is invalid code the print “Wrong Choice”
Step 7: Stop the program
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,ans1,choice;
float ans2;
clrscr();
printf(" \n enter two numbers");
scanf("%d%d",&num1,&num2);
printf(" \nenter your choice \n 1.Addition \n 2.Subtraction \n 3.Multiplication \n
4.Division \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
ans1=num1+num2;
printf("Addition =%d",ans1);
break;
case 2:
ans1=num1-num2;
printf("Subtraction =%d",ans1);
break;
case 3:
ans1=num1*num2;
printf("Multiplication =%d",ans1);
break;
case 4:
ans2=(float)num1/num2;
printf("Division =%f",ans2);
break;
default:
printf("wrong choice");
break;
}
getch();
}
Result:
Thus the program to create simple menu driven calculator using switch has been
successfully executed.
5. Prime or Not
Aim:
To create a C program for checking and print of given number is prime or not.
Algorithm:
Step 1: Start the program
Step 2: Read the value of n
Step 3: Assign i=2
Step 4: Repeat the following steps until i<=n/2
Step 4.1: If n % i = 0 Then Print “Not Prime” and Exit.
Step 4.2: i = i+1
Step 5: if i=n then Print “Prime Number”
Step 6: Stop the program
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i, flag=0;
clrscr();
printf("\nEnter a positive integer: ");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("\n%d is a prime number",n);
else
printf("\n%d is not a prime number",n);
}
OUTPUT
Enter a positive integer: 5
5 is a prime number
Result:
Thus the program to find whether the given number is prime or not has been executed
successfully.
6. Fibonacci Number
Aim:
To create a C program to generate the Fibonacci series for the given number.
Algorithm:
Step 1: Start the program
Step 2: Read the value of n
Step 3: Assign f1 = 0, f2=1 and f=0
Step 4: Print f1,f2
Step 5: f=f1+f2
Step 6: Repeat the following steps until f<n
Step 6.1 f = f1+f2
Step 6.2 f1 = f2
Step 6.3 f2 = f
Step 6.4 Print f
Step 7: Stop the program
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int f,f1=0,f2=1,i;
clrscr();
printf("Enter the limit:");
scanf("%d",&n);
printf(“\n%d\n%d”,f1,f2);
for(i=3;i<=n;i++)
{
f=f1+f2;
printf("\n%d",f);
f1=f2;
f2=f;
}
getch();
}
OUTPUT
Enter the limit: 5
0
1
1
2
3
Result:
Thus the C program to generate the Fibonacci series for the given number has been
executed successfully.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
unsigned long int a, num, sum=0,rnum=0,rem;
clrscr();
printf("\n Enter the No:");
scanf("%ld",&num);
a=num;
while(num!=0)
{
rem=num%10;
sum=sum+rem;
rnum=rnum*10+rem;
num=num/10;
}
printf("\n The Sum of Digits %ldis=%ld\n",a,sum);
printf("\n The Reverse %ld is=%ld\n",a,rnum);
if(a==rnum)
printf("\n The Given number is a Palindrome");
else
printf("\n The Given number is not a Palindrome");
getch();
}
Output:-
Enter the No:12345
The Sum of Digits 12345is=15
The Reverse 12345 is=54321
The Given number is not a Palindrome
Result:-
Thus the C program to find the sum & reverse of digits and check whether the
number is Palindrome or Not is Verified successfully.