Rita found that the project includes most of the calculator built-in functions like addition, subtraction, multiplication, division, modulo, power, and average. So she consolidated everything as Calculator module and hands over this module to Priya.

Rita instructed Priya to implement this module as a menu driven program. She listed out the order of menu.
Help Priya to write a program to implement a menu driven calculator.

Function Specification:
int addition(int a, int b)
int subtraction(int a, int b)
int multiplication(int a, int b)
float division(int a, int b)
int modulo(int a, int b)
float average(int a, int b)
int power(int a, int b)

The choice of operations are as follows:
1 – addition
2 – subtraction
3 – multiplication
4 – division
5 – modulus
6 – average
7 - power

Input Format:
Input consists of 3 integers. The first 2 integers correspond to the operands and the third integer corresponds to the choice of operation.
Output Format:
The output consists of a single integer or floating point number based on the choice of operation. All floating point numbers are displayed with respect to 2 decimal places.
Refer sample input and output for formatting details.

Sample Input 1:
23
22
1
Sample Output1:
45

Sample Input 2:
23
2
3
Sample Output2:
46


Program Code:
#include<stdio.h>
int addition (int a,int b)
{
    return(a+b);
}
int subtraction (int a,int b) 
{
    return(a-b);
}
int multiplication (int a,int b) 
{
    return(a*b);
}
float division (int a,int b) 
{
    float k;
    k= (float)a/b;
    return(k);
}
int modulo(int a,int b) 
{
    return(a%b);
}
int power(int a,int b) 
{
    int i,power=1;
    for(i=1; i<=b; i++)
    {
        power = power * a;
    }
    return power; 
}
float average(int a,int b) 
{
    float k=a+b;
    return (k/2);
}

int main()
{
int a,b,ch;
scanf("%d%d%d",&a,&b,&ch);
switch(ch)
{
    case 1:
    printf("%d",addition(a,b));
    break;
    
    case 2:
    printf("%d",subtraction(a,b));
    break;
    
    case 3:
    printf("%d",multiplication(a,b));
    break;
    
    case 4:
    printf("%.2f",division(a,b));
    break;
    
    case 5:
    printf("%d",modulo(a,b));
    break;
    
    case 6:
    printf("%.2f",average(a,b));
    break;

    case 7:
    printf("%d",power(a,b));
    break;  
}
return 0;
}
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: