Thursday, May 2, 2019

Write a Calculator program in C.

Arithmetic operations using (switch...........case)

program:

#include<stdio.h> 
void main()
{
    int a,b,res,choice;
    printf("enter two numbers \n"); scanf("%d%d",&a,&b);
    printf(" 1.add \n 2.subract \n 3.multiply \n 4.divide \n"); printf("enter your choice \n");
    scanf("%d",&choice); 
    switch(choice)
    {
        case 1:
        res=a+b;
        printf("the sum of %d and %d is %d \n",a,b,res); break;
        case 2:
        res=a-b;
        printf("the difference of %d and %d is %d \n",a,b,res); break;
        case 3:
        res=a*b;
        printf("the product of %d and %d is %d \n",a,b,res); break;
        case 4:
        res=a/b;
        printf("the division of %d by %d is %d \n",a,b,res); break;
        default:
        printf("you have entered wrong choice\n please... try again\n"); break;
    }
}

output:-


No comments:

Post a Comment