0
1.5kviews
Write a program to implement calculator with following operations:-

(i) Add two numbers

(ii) Subtract two numbers

(iii) Division two numbers

(iv) Multiply two numbers.

1 Answer
0
10views

Algorithm:

  • Start.
  • Declare option, n1, n2, sub, mul, div and add.
  • Promote the user to enter two numbers.
  • Accept the number as a and b.
  • Print the menu and promote the user for option.
  • Accept the option as option.
  • Switch (option)

    • If option == 1 do the following.

      • Add = n1 + n2;
      • Display add.
    • If option == 2 do the following.

      • Sub = n1 - n2;
      • Display sub.
    • If option == 3 do the following.

      • Mul = n1 * n2;
      • Display mul.
    • If option == 4 do the following.

      • Div = n1 / n2;
      • Display div.
  • Stop.

Program:

#include<stdio.h>
int main(){
int n1,n2,option,add,sub,mul,div,v;
    do{
        printf("Enter 1 for Addition\n");
        printf("Enter 2 for Subtraction\n");
        printf("Enter 3 for Multiplication\n");
        printf("Enter 4 for Division\n");       
        printf("Enter your option:");
        scanf("%d", &option);       
        printf("Enter the First Number:");
        scanf("%d", &n1);
        printf("Enter the Second Number:");
        scanf("%d", & n2);      
        switch(option){
            Case 1:
                    add = n1 + n2;
                    printf("Addition=%d", add);
                    break;
            Case 2:
                    sub = n1 - n2;
                    printf("Subtraction=%d", sub);
                    break;
            Case 3:
                    mul = n1 * n2;
                    printf("Multiplication=%d", mul);
                    break;      
            Case 4:
                    div = n1 / n2;
                    printf("Division=%d", div);
                    break;
            Default:
                    printf("Invalid option");           
        }
        printf("\n Do you want to continue?");
        printf("\n Enter 1 for continue 0 to exit:");
        scanf("%d", &v);
    }
    while (v==1);
}

Output:

Enter 1 for continue 0 to exit: 0

Enter 1 for Addition

Enter 2 for Subtraction

Enter 3 for Multiplication

Enter 4 for Division

Enter your option: 1

Enter the First Number: 10

Enter the Second Number: 20

Addition: 30

Please log in to add an answer.