0% found this document useful (0 votes)
23 views2 pages

Switchstmt 1

The program takes in two numbers and an operator from the user and performs the calculation based on the operator entered. It uses a switch statement to check the operator and perform the appropriate calculation, printing the result. If an invalid operator is entered, it prints an error message.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

Switchstmt 1

The program takes in two numbers and an operator from the user and performs the calculation based on the operator entered. It uses a switch statement to check the operator and perform the appropriate calculation, printing the result. If an invalid operator is entered, it prints an error message.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>

int main() {

char operator;

int n1, n2;

printf("Enter an operator (+, -, *, /): ");

scanf("%c", &operator);

printf("Enter two operands: ");

scanf("%d %d",&n1, &n2);

switch(operator)

case '+':

printf("%d + %d = %d",n1, n2, n1+n2);

break;

case '-':

printf("%d - %d = %d",n1, n2, n1-n2);

break;

case '*':

printf("%d * %d= %d",n1, n2, n1*n2);

break;

case '/':

printf("%d / %d = %d",n1, n2, n1/n2);

break;

// operator doesn't match any case constant +, -, *, /

default:

printf("Error! operator is not correct");


}

return 0;

You might also like