C++ Program that inputs one five-digit number separates the number into its individual digits and prints the digits separated from one another by three spaces each. [Hint: Use combinations of integer division and the remainder operation.] For example, if the user types in 42139, the program should print: 4 2 1 3 9

/* C++ Program that inputs one five-digit number separates the number into its individual digits and prints the digits separated from one another by three spaces each. [Hint: Use combinations of integer division and the remainder operation.] For example, if the user types in 42139, the program should print: 4   2   1   3   9 */
#include <iostream>
using namespace std;
int main() 
{
    int num, digit1, digit2, digit3, digit4, digit5;
    cout <<"Enter a 5 digit whole number: ";
    cin >> num; 
    digit5 = num % 10;   
    num = num / 10;    
    digit4 = num % 10;   
   	num = num / 10;    
    digit3 = num % 10;   
    num = num / 10;    
    digit2 = num % 10;  
    num = num / 10;
    digit1 = num % 10;   	
    cout << digit1 <<"  "<< digit2 <<"  " << digit3 <<"  " << digit4 <<"  " << digit5 <<"\n";
    return 0;
}

Output :
Enter a 5 digit whole number: 42139
4 2 1 3 9



Leave a comment