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.)
0 Comments
If you have any doubts, please mention in comment or if you want to discuss privately then please use this email : codetrycatch@gmail.com.
We will definitely try to contact you. Thanks.