C Program for Basic Calculator

July 27, 2020

Here is the C Program for the basic calculator. This is a simple calculator to do the basic arithmetics operations like addition, subtraction, multiplication, and division.

C Program for Basic Calculator


#include<stdio.h> 
int main()
{
    char yes;
    int num1, num2, result, choice;

    yes = 'y';
    while(yes == 'y' || yes == 'Y')
    {
        printf("Enter First Integer Number: ");
        scanf("%d", &num1);

        printf("Enter Second Integer Number: ");
        scanf("%d", &num2);

        printf("\n1 for Addition\n2 for Subtraction\n3 for Multiplication\n4 for Division\n\nSelect Your Option:");
        scanf("%d", &choice);

        printf("\n");
        switch(choice) 
        {
                case(1):
                    result = num1 + num2;
                    printf("%d + %d = %d\n", num1, num2, result);
                    break;
                case(2):
                    result = num1 - num2;
                    printf("%d - %d = %d\n", num1, num2, result);
                    break;
                case(3):
                    result = num1 * num2;
                    printf("%d * %d = %d\n", num1, num2, result);
                    break;
                case(4):
                    result = num1 / (float)num2;
                    printf("%d / %d = %d\n", num1, num2, result);
                    break;
                default:
                    printf("Please choose correct choice.!!\n");
        }

        printf("\nRepeat Again? (Y/N): ");
        scanf(" %c", &yes);
    }

    return 0;
}

Output

Enter First Integer Number: 45
Enter Second Integer Number: 30
1 for Addition
2 for Subtraction
3 for Multiplication
4 for Division

Select Your Option:1

45 + 30 = 75

Repeat Again? (Y/N): y
Enter First Integer Number: 65
Enter Second Integer Number: 30

1 for Addition
2 for Subtraction
3 for Multiplication
4 for Division

Select Your Option:2

65 - 30 = 35

Repeat Again? (Y/N): n


...Program finished with exit code 0
Press ENTER to exit console.         

Tagged in:
About Author
Adeeb C (227 Articles)

Adeeb C is a Web Designer, Web Application Developer, Android App Developer, WordPress Developer, Professional Blogger. He has 5 years of experience in WordPress, Web Developing, Professional Blogging, and 2 years of experience in Android App Developing.