0% found this document useful (0 votes)
179 views

Simple Menu Driven Calculator Program Using Switch Statement

The document describes 7 C programs: 1. A menu-driven calculator using switch statements 2. A program to check if a number is prime 3. A program to generate the Fibonacci series up to a given number 4. A program to calculate the sum and reverse of digits of a number and check if it is a palindrome 5. The algorithms and programs are presented along with sample outputs 6. The document concludes each program was successfully executed.

Uploaded by

Vasantha Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
179 views

Simple Menu Driven Calculator Program Using Switch Statement

The document describes 7 C programs: 1. A menu-driven calculator using switch statements 2. A program to check if a number is prime 3. A program to generate the Fibonacci series up to a given number 4. A program to calculate the sum and reverse of digits of a number and check if it is a palindrome 5. The algorithms and programs are presented along with sample outputs 6. The document concludes each program was successfully executed.

Uploaded by

Vasantha Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

4.

Simple Menu Driven Calculator Program Using Switch Statement


Aim:
To write a C Program to write a 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();
}

/*OUTPUT FOR ADDITION:


enter two number
55
66
enter your choice
1.Addition
2.Subtraction
3.Multiplication
4.Division
1
Addition=121*/

/*OUTPUT FOR SUBTRACTION:


enter two number
67
66
enter your choice
1.Addition
2.Subtraction
3.Multiplication
4.Division
2
Subtraction=1*/

/*OUTPUT FOR MULTIPLICATION:


enter two number
4
2
enter your choice
1.Addition
2.Subtraction
3.Multiplication
4.Division
3
Multiplication=8 */

/*OUTPUT FOR DIVISION:


enter two number
55
5
enter your choice
1.Addition
2.Subtraction
3.Multiplication
4.Division
4
Division =11*/

/*OUTPUT FOR WRONG CHOICE:


enter two number
55
66
enter your choice
1.Addition
2.Subtraction
3.Multiplication
4.Division
5
wrong choice

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.

7. SUM OF DIGITS, REVERSE, PALINDROME


Aim:-
To write a C program to find the sum & reverse of digits and Check is Palindrome or
not
Algorithm:-
STEP 1: Start the program.
STEP 2: Enter the number.
STEP 3: Set a loop upto the number is not equal to zero .
remnum%10
sumsum+rem
rnum rnum *10 +rem
num num/10
STEP 4: After the end of the loop print the sum and reverse no of the digit.
STEP 5: Find whether the reverse no is equal then is palindrome or not
STEP 6: Stop the Program.

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.

You might also like