Introduction: In previous article i explained Else if ladder or multiple else if to check the day corresponding to number in C Language and Else if ladder or multiple else if to check the month of entered number in C language. In this
article i am going to explain how to perform addition(+),
subtraction(-), multiplication(*), division(/) and modulus (%) operations
on two numbers corresponding to selected operator from + , - , * , / ,%
using ladder else if or we can say multiple else if statement in C
Language.Modulus operator(%) gives the remainder after integer division
e.g divide 10 by 4 and you will get 2 as remainder Let's understand by
creating a program.
#include<conio.h>
#include<conio.h>
#include<stdio.h>
main()
{
char op;
printf("Enter first number : ");
scanf("%d",&a);
printf("Enter second number : ");
scanf("%d",&b);
printf("Enter any operator
from + , - , * , / ,% ");
scanf(" %c",&op);
if(op=='+')
{
c=a+b;
printf("Sum of %d and %d =
%d ",a,b,c);
}
else if(op=='-')
{
c=a-b;
printf("Subtraction
of %d and %d = %d ",a,b,c);
}
else if(op=='*')
{
c=a*b;
printf("Multiplication
of %d and %d = %d",a,b,c);
}
else if(op=='/')
{
c=a/b;
printf("%d divided by
%d = %d ",a,b,c);
}
else if(op=='%')
{
c=a%b;
printf("Remainder
of %d divided by %d = %d ",a,b,c);
}
else
{
printf("INVALID OPERATOR!! Please enter correct one");
}
getch();
}
Run the program using Ctrl+F9
Output:
First Run
Enter first number : 4
Enter first number : 4
Enter second number : 2
Enter any operator
from + , - , * , / ,% : *
Multiplication of 4 and 2 = 8
Second Run
Enter first number :
10
Enter second number : 40
Enter operator
from + , - , * , / ,% : +
Addition of 10 and 40 = 50
Third Run
Enter first number :
100
Enter second number : 20
Enter operator
from + , - , * , / ,% : -
Subtraction of 100
and 20 = 80
Fourth Run
Enter first number :
40
Enter second number : 5
Enter operator
from + , - , * , / ,% : /
40 divided by 5= 8
Fifth Run
Enter first number :
20
Enter second number :4
Enter operator
from + , - , * , / ,% : %
Remainder of 20 divided by 4 = 0
If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..