Here is the simple code(in C++) to find the SUM OF DIGITS of the number, until the sum is not in a single digit.
Example:
number = 56
56==> 5+6=11
11==> 1+1=2
and 2 is a single digit number.
Best example you can try in this code (Dev-C++) is : 2147483647 and observe the answer.
#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
int n,nc,rem,sum=0;
int sum1=10;
cout<<"enter the number :";
cin>>n;
nc=n;
while(sum1>9)
{
sum=0;
rem=0;
while(nc>0)
{
rem=nc%10;
sum=sum+rem;
nc=nc/10;
sum1=sum;
}
nc=sum1;
cout<<"\n the sum of digits of "<<n<<" is ="<<sum1;
n=nc;
}
getch();
return 0;
}
Output:
Example:
number = 56
56==> 5+6=11
11==> 1+1=2
and 2 is a single digit number.
Best example you can try in this code (Dev-C++) is : 2147483647 and observe the answer.
#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
int n,nc,rem,sum=0;
int sum1=10;
cout<<"enter the number :";
cin>>n;
nc=n;
while(sum1>9)
{
sum=0;
rem=0;
while(nc>0)
{
rem=nc%10;
sum=sum+rem;
nc=nc/10;
sum1=sum;
}
nc=sum1;
cout<<"\n the sum of digits of "<<n<<" is ="<<sum1;
n=nc;
}
getch();
return 0;
}
Output:
No comments:
Post a Comment