## Question on c++ programming language.

Asked By 0 points N/A Posted on -

How to write a program that takes a five digit integer from the user and displays the individual digits on the screen? Please remember that the input number will be a single variable and not an array.

SHARE
Answered By 0 points N/A #116482

## Question on c++ programming language.

Hi Blossom Serrano,

#include <iostream.h>

int main()
{

int no;
cout<<"please enter the five digit no = ";
cin>>no;
cout<<endl;

for (int i=5;i>0;i–)
{
cout << i << " the digit of no = " << no % 10 << endl;
no = no / 10;
}

return 0;

}

Thanks,

Sophia Taj

Answered By 5 points N/A #116483

## Question on c++ programming language.

C/C++ provides to operators division operator (/) and modulus operator (%). The division operator returns the quotient and the remainder operator returns the remainder in a division. Using these operators and any of while and for loop, you can separate the digits from the number.

To separate digits from a number follow these steps:

1.   First input the number into a variable, or you can declare a variable and initialize it with the five digit number say int num=56894.

2.   As it is five digits number, so we need to divide the number by 10000 to get the quotient which will be our first digit.

3.   For this declare and initialize a variable say  int divisor = 10000

4.   Also declare a variable say int digit, which will store the separated digits.

5.   Here you can use any loop of your choice for loop or while loop.

6.   I use while loop. Inside make loop make a condition while(div >=1)

7.   Now inside the loop, we apply the logic.

digit = num/div         // This will give first digit 5.

num = num % div        //  This will give the remainder 6894.

divisor = divisor/10     // it will make divisor=1000

8.   For each iteration of loop we get next digit 6,8,9,4 until the condition becomes false.

Hopefully this will give you some idea, how it works.

Thanks