134k views
4 votes
4.14 LAB: Countdown until matching digits In C++ Write a program that takes in an integer in the range 20-98 as input. The output is a countdown starting from the integer, and stopping when both output digits are identical. Ex: If the input is: 93 the output is: 93 92 91 90 89 88 Ex: If the input is: 77 the output is: 77 Ex: If the input is: 15 or any value not between 20 and 98 (inclusive), the output is: Input must be 20-98 For coding simplicity, follow each output number by a space, even the last one. Use a while loop. Compare the digits; do not write a large if-else for all possible same-digit numbers (11, 22, 33, …, 88), as that approach would be cumbersome for larger ranges.

User Maurycy
by
6.0k points

1 Answer

2 votes

Answer:

Here is the C++ program:

#include <iostream> // to use input output functions

using namespace std; // to identify objects like cin cout

int main() { //start of main function

int number; //an integer value

cout<<"Enter an integer: "; //prompts user to enter a number

cin >> number; // reads integer value from user

if (number > 98 || number < 20) { //if number is not between 20 and 98

cout<<"Input must be 20-98"; } //display this message if number is not between 20 and 98

else { // if number is in range 20-98

while(number%11){ // loop keeps taking mod of input number with 11 until matching digits found

cout<<number<<" "; //displays the numbers

number--; } // decrements the value of number by 1

cout<<number<<" "; } } // displays the matching digits

Step-by-step explanation:

I will explain the program with an example:

Let suppose the number = 90

The if condition evaluate to false because 90 is in the range of 20-98 so the else part executes which has a while loop that works as following:

while(number % 11)

At first iteration:

This while(number % 11) is basically equivalent to :

while(number % 11 != 0)

while(90 % 11)

The % operator is the modulo operator which returns the remainder of the division. So when 90 is divided by 11 the remainder is 2. This is not equal to 0. So the statement under while condition executes:

cout<<number<<" "; This statement displays the number on output screen. Since number = 90 so 90 is printed. " " adds a space with the number

90

Next the value of number is decremented to 1 so number = 89

At second iteration:

while(number % 11) becomes:

while(89 % 11)

When 89 is divided by 11 the remainder is 1. This is not equal to 0. So the statement under while condition executes:

cout<<number<<" "; This statement displays the number on output screen. Since number = 89 so 89 is printed. " " adds a space with the number

90 89

Next the value of number is decremented to 1 so number = 88

At third iteration:

while(number % 11) becomes:

while(88 % 11)

When 89 is divided by 11 the remainder is 0. So the while loop breaks because the condition while(number % 11 != 0) evaluates to false. So the statement in while loop does not execute and program control moves to the following statement:

cout<<number<<" "; This statement displays that number whose digits match on the output screen with a space. So the output of the entire program is:

90 89 88

The program along with the output using example given in the question i.e. input is 93 is attached in a screenshot.

4.14 LAB: Countdown until matching digits In C++ Write a program that takes in an-example-1
User Bar Akiva
by
5.1k points