157k views
4 votes
To compile a program by which a 4-digit natural number is entered from the interval [1000.. 9999]. 2 new 2-digit numbers are formed from this number. The first number is formed from the 1st and 4th digits of the entered number. The second number is formed from the 2nd - 3rd digit of the entered number. Display whether the 1st new number is displayed on the screen e is less than <, equal to = or greater than the 2nd number. Example: 3332 Output: less (32<33) Example: 1144 Output: equals (14=14) Example: 9875 Output: greater (95>87)

1 Answer

3 votes

Answer:

#include <iostream>

using namespace std;

int main()

{

// Prompt the user to enter a 4-digit natural number

cout << "Enter a 4-digit natural number: ";

// Read the number from the standard input stream

int number;

cin >> number;

// Check if the number is in the specified range

if (number < 1000 || number > 9999)

{

// If the number is not in the specified range, print an error message and exit

cout << "Error: the entered number is not a 4-digit natural number." << endl;

return 1;

}

// Compute the first new number by concatenating the 1st and 4th digits of the entered number

int firstNumber = (number / 1000) + (number % 10) * 10;

// Compute the second new number by concatenating the 2nd and 3rd digits of the entered number

int secondNumber = ((number / 100) % 10) + ((number / 10) % 10) * 10;

// Compare the first

User Sridhar Iyer
by
5.9k points