C++ Program Code for Digital Root of a Number

Question:

/*The digital root (also called repeated digital sum) of a number is a single digit value obtained by an iterative process of summing digits.
 Digital sum of 65536 is 7, because 6+5+5+3+6=25 and 2+5 = 7. Write a program that takes an integer as input and prints its digital root.
INPUT:
A single integer N
OUTPUT:
Digital root of the number N.
Write the code.*/

Program Code:

#include<iostream>
using namespace std;
int main()
{
int num ,z , x ,count1 =0,count2=0 ,sum =0 ,y ,digital_root=0;
cout<<"Enter any Number :  ";
cin>>num;

z = num;

while(num!=0)
{
num =num / 10;
count1++;
}
cout<<"\nTotal num of digits :  "<<count1<<endl;
for(int i=0;i<count1;i++)
{
x= z %10;
sum = sum + x;
z = z/10;
count2++;

}
for(int j=0;j<count2;j++)
{
y = sum % 10;
digital_root = digital_root + y;
sum = sum / 10;
}
cout<<"\nDigital root of number :  "<<digital_root;

   return 0;
}

Output:


1 comment:

  1. Eas explanation:
    https://dgeekspot.wordpress.com/2015/11/17/digital-root-algorithm/

    ReplyDelete

Please disable your ad blocker to support this website.

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