20_10

Write a C program for calculator program with Basic operations (+, -, *, /,%) of two numbers using switch statement.

Source code:

 /*Write a C program for calculator program with Basic operations (+, -, *, /,%) of two numbers using switch statement. */

#include <stdio.h>

int main()

{

int num1,num2;

float result =0;

char ch;    /*to store operator choice */

printf("Enter the 1st number: ");

scanf("%d",&num1); 

printf("Enter the 2nd number: ");

scanf("%d",&num2); 

printf("Enter the operation(+,-,*,/) : ");

scanf(" %c",&ch); /* Operation to perform +,-,*,/  */

switch(ch)    

    {

        case '+':

            result=num1+num2;

            break;

             

        case '-':

            result=num1-num2;

            break;

         

        case '*':

            result=num1*num2;

            break;

             

        case '/':

            result=(float)num1/(float)num2;

            break;

             

        case '%':

            result=num1%num2;

            break;

        default:

            printf("Invalid operation.\n");

    }

printf("Result: %d %c %d = %f\n",num1,ch,num2,result);

return 0;

}

Output:
C program for calculator program with Basic operations of two numbers using switch statement


No comments:

Post a Comment

Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...