This Post Contains A C++ Program For Converting A User Entered Number Into A Single Digit With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Do...While-Loop', 'While-Loop' & 'Nested Loops' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.
# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.
C++ Program For Converting A User Entered Number Into A Single Digit.
/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>
/* Start Of Main Program */
void main()
{
/* Declaration Of Variables */
int s, r, a[10], i;
long Num, Temp;
clrscr();
/* Asking For The Input From User */
cout << " \n Enter Any Number : ";
cin >> Num;
Temp=Num;
/* Source Code For Computing Converting User Entered Number, i.e 'Num' Into A Single Digit */
do
{
i=s=0;
while(Num>0)
{
r=Num%10;
a[i]=r;
s=s+r;
i++;
Num=Num/10;
}
--i;
if(i!=0)
{
Num=s;
}
if(i==0 || i==1)
{
cout << " \n Answer After Converting " << Temp << " Into A Single Digit Is : " << s;
break;
}
}while(i>0);
getch();
}
/* End Of Main Program */
Output :
Enter Any Number : 12345
[1+2+3+4+5] = 15 = [1+5] = 6
Answer After Converting 12345 Into A Single Digit Is : 6
# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.
C++ Program For Converting A User Entered Number Into A Single Digit.
/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>
/* Start Of Main Program */
void main()
{
/* Declaration Of Variables */
int s, r, a[10], i;
long Num, Temp;
clrscr();
/* Asking For The Input From User */
cout << " \n Enter Any Number : ";
cin >> Num;
Temp=Num;
/* Source Code For Computing Converting User Entered Number, i.e 'Num' Into A Single Digit */
do
{
i=s=0;
while(Num>0)
{
r=Num%10;
a[i]=r;
s=s+r;
i++;
Num=Num/10;
}
--i;
if(i!=0)
{
Num=s;
}
if(i==0 || i==1)
{
cout << " \n Answer After Converting " << Temp << " Into A Single Digit Is : " << s;
break;
}
}while(i>0);
getch();
}
/* End Of Main Program */
Output :
Enter Any Number : 12345
[1+2+3+4+5] = 15 = [1+5] = 6
Answer After Converting 12345 Into A Single Digit Is : 6
No comments:
Post a Comment