C++ Program For Separating Digits From the Number

Question Types:

Write a program that asks the user to enter an five digits positive integer and outputs digits of this number in separate lines.

Program Code:

#include<iostream>
using namespace std;
int main()
{
int number ,a[5] , x ,j;
cout<<"Enter 5 digit number :  ";
cin>>number;
for(int i = 0;i <5;i++)
{
x = number % 10;
a[i] = x;
number  = number / 10;
}
cout<<"Digits are :  ";
cout<<a[4]<<","<<a[3]<<","<<a[2]<<","<<a[1]<<","<<a[0];
cout<<endl;
cout<<"\n\n or using for loop : ";
for(int i=0 ;i<5;i++)
{
cout<<a[i]<<" : ";
}
return 0;
}

Output:

.exe file

No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.