C-programming - Arithmetic operations using function and switch case, C program to perform arithmetic operations using function and switch case

Arithmetic operations using function and switch case

This program uses function to perform the selected mathematical operation. Here switch case is used to select the options.

 

#include<stdio.h>
#include<conio.h>
int res;
int sum(int x,int y);
int sub(int x,int y);
int mul(int x,int y);
int div(int x,int y);
int main()
{
    int choice,a,b;
    printf("select anyone of the following operation\n");
    printf("1.addition\n2.subtraction\n3.multiplication\n4.division\n");
    scanf("%d",&choice);
    printf("enter the two values");
    scanf("%d%d",&a,&b);
    if(choice==1)
    printf("sum is %d\n",sum(a,b));
    else if(choice==2)
    printf("difference is %d",sub(a,b));
    else if(choice==3)
    printf("product is %d",mul(a,b));
    else if(choice==4)
    printf("quotient is %d",div(a,b));
    else
    printf("not a valid choice");
    getch();
}
int sum(int x,int y)
{
    res=x+y;
    return res;
}
int sub(int x,int y)
{
    res=x-y;
    return res;
}    
int mul(int x,int y)
{
    res=x*y;
    return res;
}
int div(int x,int y)
{
    res=x/y;
    return res;
}
    


output


select anyone of the following operation
1.addition
2.subtraction
3.multiplication
4.division
1
enter two values
2
4
sum is 6

The topic on C-programming - Arithmetic operations using function and switch case is posted by - Math

Hope you have enjoyed, C-programming - Arithmetic operations using function and switch caseThanks for your time

Tech Bluff