Menu driven program for basic arithmetic operations

Program Description

This program demonstrates simple arithmetic operations on given numbers using menu system.The main objective of this program is to demonstrate the program flow control by user selected options from the given menu. The program uses switch structure to implement this feature

Program
/********************************************
*	code.cheraus.com 
*	
*  Menu driven program for basic arithmetic operations
*	 
*
********************************************/

#include<stdio.h>

int main()
{
   float num1,num2,ans;
   int opt;
   
   //taking user input
   do
   {
   printf("\nEnter the First Number : ");
   scanf("%f",&num1);
   printf("\nEnter the Second Number : ");
   scanf("%f",&num2);
   
   //Displaying menu
   printf("\n-----Main Menu----\n1.Addition");
   printf("\n2.Subtraction\n3.Multiply\n4.Divide\n5.Exit");
   printf("\nEnter your choice : ");
   scanf("%d",&opt);
   switch(opt)
   {
     case 1:
              ans = num1+num2;
              printf("\nThe addition of 2 numbers is : %f",ans);
              break;
     case 2:
             ans = num1-num2;
             printf("\nThe differnce of 2 numbers is : %f",ans);
             break;
     case 3:
             ans = num1*num2;
             printf("\nThe product of 2 numbers is : %f",ans);
             break;
     case 4:
              ans = num1/num2;
              printf("\nThe division of 2 numbers is : %f",ans);
              break;
    case 5:
             break;
    default:  //error message for wrong choice
              printf("\nYou Entered Wrong Choice\n");
              break;
   }
    }while(opt!=5);
  return 0;
}

Output
Enter the First Number : 23
Enter the Second Number : 123
-----Main Menu----
1.Addition
2.Subtraction
3.Multiply
4.Divide
5.Exit
Enter your choice : 1
The addition of 2 numbers is : 146.000000
Enter the First Number : 123
Enter the Second Number : 234

-----Main Menu----
1.Addition
2.Subtraction
3.Multiply
4.Divide
5.Exit
Enter your choice : 2
The differnce of 2 numbers is : -111.000000
Enter the First Number : 12
Enter the Second Number : 13

-----Main Menu----
1.Addition
2.Subtraction
3.Multiply
4.Divide
5.Exit
Enter your choice : 3
The product of 2 numbers is : 156.000000
Enter the First Number : 123
Enter the Second Number : 23

-----Main Menu----
1.Addition
2.Subtraction
3.Multiply
4.Divide
5.Exit
Enter your choice : 4
The division of 2 numbers is : 5.347826

Comments
comments powered by Disqus