C Program to Implement Simple Calculator using Do While Loop

Write C Program to Implement Simple Calculator using Do While Loop

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a, b, i;

    printf("Enter Number 1 :--> ");
    scanf("%d", &a);

    printf("\nEnter Number 2 :--> ");
    scanf("%d", &b);

    do
    {
        printf("\nEnter 1 for '+'  ");
        printf("\nEnter 2 for '-'  ");
        printf("\nEnter 3 for '*'  ");
        printf("\nEnter 4 for '/'  \n");
        printf("Enter 0 for Exit ");
        printf("\nEnter your choice :--> ");

        scanf("%d", &i);

        switch(i)
        {
            case 0: break;

            case 1:
                printf("\na + b  = %d\n", a + b);
                break;

            case 2:
                printf("\na - b = %d\n", a - b);
                break;

            case 3:
                printf("\na * b = %d\n", a * b);
                break;

            case 4:
                printf("\na / b = %f\n", a / b);
                break;

            default:
                printf("\nEnter valid option\n");
        }
    } while(i != 0);
 }

Output:

Enter Number 1 :--> 8

Enter Number 2 :--> 3

Enter 1 for '+'
Enter 2 for '-'
Enter 3 for '*'
Enter 4 for '/'
Enter 0 for Exit
Enter your choice :--> 1

a + b  = 11

Enter 1 for '+'
Enter 2 for '-'
Enter 3 for '*'
Enter 4 for '/'
Enter 0 for Exit
Enter your choice :--> 2

a - b = 5

Enter 1 for '+'
Enter 2 for '-'
Enter 3 for '*'
Enter 4 for '/'
Enter 0 for Exit
Enter your choice :--> 3

a * b = 24

Enter 1 for '+'
Enter 2 for '-'
Enter 3 for '*'
Enter 4 for '/'
Enter 0 for Exit
Enter your choice :--> 4

a / b = 0.000000

Enter 1 for '+'
Enter 2 for '-'
Enter 3 for '*'
Enter 4 for '/'
Enter 0 for Exit
Enter your choice :--> 0

≪ Previous | Next ≫