Project 18: Broken Keypad. The keypad on your oven is used to enter the desired baking temperature and is arranged like the digits on a phone
LANGUAGE: Java
CHALLENGE:
Project 18: Broken Keypad. The keypad on your oven is used to enter the desired baking temperature and is arranged like the digits on a phone:
123
456
789
0
Unfortunately the circuitry is damaged and the digits in the leftmost
column no longer function. In other words, the digits 1, 4, and 7 do not work. If a recipe calls for a temperature that can’t be entered, then you would like to substitute a temperature that can be entered. Write a program that inputs a desired temperature. The temperature must be between 0 and 999 degrees. If the desired temperature does not contain 1, 4, or 7, then output the desired temperature. Otherwise, compute the next largest and the next smallest temperature that does not contain 1, 4, or 7 and output both.
For example, if the desired temperature is 450, then the program should output 399 and 500. Similarly, if the desired temperature is 375, then the program should output 380 and 369.
SOLUTION:
#include <iostream> using namespace std; int main() { int temperature, hundreds, tenth, ones, minimum, maximum; cout << "Enter a positive integer in bet (0 - 999): "; cin >> temperature; if( temperature >= 1000 || temperature < 0 ){ cout << "please enter valid temperature\n"; return 0; } hundreds = temperature / 100; if(hundreds == 1 || hundreds == 4 || hundreds == 7){ minimum = (hundreds-1) * 100 + 99; maximum = (hundreds+1) * 100; cout << minimum << " && " << maximum << "\n"; return 0; } tenth = (temperature / 10) % 10; if(tenth == 1 || tenth == 4 || tenth == 7){ minimum = hundreds * 100 + (tenth-1) * 10 + 9; maximum = hundreds * 100 + (tenth+1) * 10; cout << minimum << " && " << maximum << "\n"; return 0; } ones = temperature % 10; if(ones == 1 || ones == 4 || ones == 7){ minimum = hundreds * 100 + tenth * 10 + ones-1; maximum = hundreds * 100 + tenth * 10; cout << minimum << " && " << maximum << "\n"; return 0; } cout << temperature << "\n"; return 0; }