# 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