Question on c++ programming language.
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.
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.
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
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