skip to main | skip to sidebar

Tuesday, August 23, 2011

Simple Arithmetic Operation using Conditional Structure if & else in C Language


This source code perform simple arithmetic operation using Conditional structure (if & else) and Arithmetic operators ( +, -, *, /, % ).












#include <stdio.h>
#include <conio.h>

void main() {
clrscr();
int operand1, operand2;
char oprtor;
printf("Enter the first number: ");
scanf("%d",&operand1);
printf("\nEnter the second number: ");
scanf("%d",&operand2);
printf("\nEnter operation: ");
scanf("%s",&oprtor);
if (oprtor == '+') { //Addition
printf("%d %c %d = %d",operand1, oprtor, operand2, operand1 + operand2);
}
else if (oprtor == '-') {// Subtraction
printf("%d %c %d = %d",operand1, oprtor, operand2, operand1 - operand2);
}

else if (oprtor == '*') { //Multiplication
printf("%d %c %d = %d",operand1, oprtor, operand2, operand1 * operand2);
}
else if (oprtor == '/') { //Division
printf("%d %c %d = %d",operand1, oprtor, operand2, operand1 / operand2);
}
else if (oprtor == '%') { // Modulo, display the Reamaider
printf("%d %c %d = remainder %d",operand1, oprtor, operand2, (operand1) % operand2);
}
getch();

}

0 comments:

Post a Comment