Tuesday, 25 September 2012

Enhanced Calculator

Below is a MULTIPLICATION calculator, but it will ONLY let you enter a number between 1 and 100...

#include <stdio.h>
int getUserInput();
int main()
{
 int works = 0,
     first = 0,
     second = 0,
     result = 0;
 // printf("first is %d and second is %d", first, second);
 while (works == 0)
 {
      int input = getUserInput();
      if (input != 0)
      {
        if (first == 0)
        {
          first = input;
          // printf("first is now %d", first);
        } else
        {
          second = input;
          works = 1;
        }
      }
 }
  // printf("first is %d and second is %d\n", first, second);
  result = first * second;
  printf("%d", result);
  return 0;
}
int getUserInput()
{
 int x;
 printf("Enter a number between 1 and 100: ");
 scanf("%d", &x);
 if (x >= 1 && x <= 100)
 {
  return x;
 }
 else
 {
  return 0;
 }
}

Output:
Enter a number between 1 and 100: 6
Enter a number between 1 and 100: 7
42
 
Output - 2:
Enter a number between 1 and 100: 312321
Enter a number between 1 and 100: 312
Enter a number between 1 and 100: 6
Enter a number between 1 and 100: 34
204
 - Note that the program multiplied only 6 and 34 together since the first two numbers were too big.

No comments:

Post a Comment