Header Ads Widget

Write a C++ program to receive a five-digit no and display as like 24689: 2 4 6 8 9

 Write  a C++ program to receive a five-digit no and display as like 24689: 2 4 6 8 9

Given a five digit number , write a program program to receive a five-digit no and display as like 24689: 2 4 6 8 9 .

Sample input

24986

Sample output

6 8 9 4 2

 Write  a C++ program to receive a five-digit no and display as like 24689: 2 4 6 8 9

The objective of the code is to print the digits of the number by space separated .

Algorithm:

  1. Start the program.
  2. Prompt the user to enter a five-digit number.
  3. Read the number from the standard input and store it in a variable.
  4. Print the original number with a colon and a space.
  5. Use a loop to extract each digit of the number in reverse order.
  6. Print each digit separated by a space.
  7. Exit the program.

Code(C++):-

#include <iostream>

int main() {
int num, digit;
std::cout << "Enter a five-digit number: ";
std::cin >> num;
std::cout << num << ": ";
for (int i = 0; i < 5; i++) {
digit = num % 10;
std::cout << digit << " ";
num /= 10;
}
return 0;
}

Output:

Enter a five-digit number: 24986:
6 8 9 4 2

Explanation:

The program prompts the user to enter a five-digit number and reads it from the standard input. The original number is printed with a colon and a space.

The program uses a loop to extract each digit of the number in reverse order. This is done by taking the modulus of the number by 10 to get the rightmost digit, printing the digit, and then dividing the number by 10 to remove the rightmost digit. The loop continues until all five digits have been printed.

Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-