Header Ads Widget

C Program to Perform All Arithmetic Operations on Two Numbers

Write a C program to accept two numbers from user and perform all arithmetic operations. Basic arithmetic operations are addition, subtraction, multiplication and division.  So, let’s start to develop our program.

Output will be look like this:

Enter two numbers: 10 5

Addition: 10 + 5 = 15

Subtraction: 10 – 5 = 5

Multiplication: 10 * 5 = 50

Division: 10/5 = 2


Program :

/* Author: CodeTryCatch
*C program to perform all arithmetic operations on two numbers
*/
#include<stdio.h>
void main()
{
    int num1,num2;
    printf("Enter two numbers :");
    scanf("%d%d",&num1,&num2);
    printf("\nOutput :\n\n");
    printf("Addition: %d + %d = %d\n\n",num1,num2,num1+num2);
    printf("Subtraction: %d - %d = %d\n\n",num1,num2,num1-num2);
    printf("Multiplication: %d * %d = %d\n\n",num1,num2,num1*num2);
    printf("Division: %d / %d = %.2f\n",num1,num2,(float)num1/num2); 
}


Step by step descriptive logic of a program.

Step 1 : First we declare two variables num1 and num2 of integer type.

Step 2 : For other arithmetic calculation we are not taking any another variable, because here we are performing operation printf() function instead of storing in any variable.

Step 3 : By developing such program, we reduce memory consumption as well as number of code lines which helps to compile and run our code faster.

Step 4 : In division calculation we used %.2f format specifier, to get fractional output as well. E.g. if user entered num1=10, num2= 7 then we want output as 1.43. Otherwise it will show 10/7 = 1. 

(Note: %f =float data type & %d =integer are format specifier.) 


Output :




Happy coding...
Thanks for visiting….😊

Post a Comment

0 Comments