Monday, 18 June 2012

Programming Exercise 2: Calculator Program

Write a calculator program that takes two (integer) numbers as input and allows the user to select which operation they would like to perform on the two number, either:



  • Addition 
  • Subtraction 
  • Multiplication 
  • Division 
and display the result: Terminate the program when enter 'n'

The Answer is

‎#include <stdio.h>


int main()


{


float num1, num2;


char operation;


while (1) {


printf("Please enter 2 integer:");
scanf("%f%f", &num1, &num2);
printf("Enter an option(A for addition, B for subtraction, C for multiplication,\n D for division, n for exit):");
scanf("%s", &operation);


if (operation == 'A')


printf("%f\n", num1+num2);


if (operation == 'B')


printf("%f\n", num1-num2);


if (operation == 'C')


printf("%f\n", num1*num2);


if (operation == 'D')


printf("%f\n", num1/num2);


if (operation == 'n')


break;


}


}

0 comments:

Post a Comment