Question On C++ Programming Language.

Asked By 0 points N/A Posted on -
qa-featured

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
Best Answer by Caldwell Janis
Answered By 0 points N/A #116482

Question On C++ Programming Language.

qa-featured

Hi Blossom Serrano,

Please  find below code for your program.

#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

Best Answer
Best Answer
Answered By 0 points N/A #116483

Question On C++ Programming Language.

qa-featured

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

Login/Register to Answer

Related Questions