C++ | Digital Root Of A Number.
Hello peeps! Lord Hypersonic greets you! I am here to present you a new and amazing and simple c++ program which can find/calculate the digital root of a number.
What is the digital root?
Digital root is a sum of all the digits of a number, for example digital root of 23 is 5(2+3).
How Program works?
1. First, it asks the user to enter the number (the number should be positive and not in decimals). Input is taken in string variable input.2. Then character array A of size equal to the length of the input is declared. At each index of the array, it stores the digits of the number given by the user.
3. It is obtained using a loop. Loop start from i = 0 to i = length of input number. Then copy the value of the input at index "i" to the array A at index "i".
4. Now we declare integer array I of size equal to the length of the number. Then a loop is started from i = 0 to i = length of number. Then copy the integer value of A at index i to I of index i.
It can be achieved like this:-
if A at index i is equal to 0 then I at index i is equal to 0
We have done this because values stored in array A are in character and we need integer values for the array I.
5. Then we take the sum of these numbers by adding the values of array I. Initially the value of the sum is equal to zero. Then it stores the value of the sum of all the numbers.
We do it like this:
5.1 start a loop from i = 0 to i = length of number.
5.2 value of the sum is equal to the value of sum + value of I at index i.
6. After finding the digital sum with the process given above. Finally, it prints the value of sum as result.
Source code:-
/*
Program: Digital Root Finder
Description: This program find the digital root of a number. Digital root is the sum of digits of the number.
Author: Lord Hypersonic
Email: lordhypersonic.522@gmail.com
Website: www.lordhypersonic.blgspot.in
*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
cout<<"Enter number: "; cin>>input; //asking for input
int length=input.length(),sum=0; //declaring length and sum
char A[length]; //declaring character array A
for(int i=0; i<length; i++) // loop for separating digits
{
A[i]=input[i];
}
int I[length]; //declaring integer array I
for (int i=0; i<length; i++) // Loop for converting character to integer
{
if(A[i]=='0') I[i]=0; //If A at index i is equal to 0 then I at index i is equal to 0
if(A[i]=='1') I[i]=1; //If A at index i is equal to 1 then I at index i is equal to 1
if(A[i]=='2') I[i]=2; //If A at index i is equal to 2 then I at index i is equal to 2
if(A[i]=='3') I[i]=3; //If A at index i is equal to 3 then I at index i is equal to 3
if(A[i]=='4') I[i]=4; //If A at index i is equal to 4 then I at index i is equal to 4
if(A[i]=='5') I[i]=5; //If A at index i is equal to 5 then I at index i is equal to 5
if(A[i]=='6') I[i]=6; //If A at index i is equal to 6 then I at index i is equal to 6
if(A[i]=='7') I[i]=7; //If A at index i is equal to 7 then I at index i is equal to 7
if(A[i]=='8') I[i]=8; //If A at index i is equal to 8 then I at index i is equal to 8
if(A[i]=='9') I[i]=9; //If A at index i is equal to 9 then I at index i is equal to 9
}
for(int i=0; i<length; i++) // Loop for adding all the digits
{
sum=sum+I[i]; // value of sum is equal = value of sum + value of I at index i
}
cout<<"Digital root: "<<sum<<endl; // Displaying the result.
return 0;
}
Program: Digital Root Finder
Description: This program find the digital root of a number. Digital root is the sum of digits of the number.
Author: Lord Hypersonic
Email: lordhypersonic.522@gmail.com
Website: www.lordhypersonic.blgspot.in
*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
cout<<"Enter number: "; cin>>input; //asking for input
int length=input.length(),sum=0; //declaring length and sum
char A[length]; //declaring character array A
for(int i=0; i<length; i++) // loop for separating digits
{
A[i]=input[i];
}
int I[length]; //declaring integer array I
for (int i=0; i<length; i++) // Loop for converting character to integer
{
if(A[i]=='0') I[i]=0; //If A at index i is equal to 0 then I at index i is equal to 0
if(A[i]=='1') I[i]=1; //If A at index i is equal to 1 then I at index i is equal to 1
if(A[i]=='2') I[i]=2; //If A at index i is equal to 2 then I at index i is equal to 2
if(A[i]=='3') I[i]=3; //If A at index i is equal to 3 then I at index i is equal to 3
if(A[i]=='4') I[i]=4; //If A at index i is equal to 4 then I at index i is equal to 4
if(A[i]=='5') I[i]=5; //If A at index i is equal to 5 then I at index i is equal to 5
if(A[i]=='6') I[i]=6; //If A at index i is equal to 6 then I at index i is equal to 6
if(A[i]=='7') I[i]=7; //If A at index i is equal to 7 then I at index i is equal to 7
if(A[i]=='8') I[i]=8; //If A at index i is equal to 8 then I at index i is equal to 8
if(A[i]=='9') I[i]=9; //If A at index i is equal to 9 then I at index i is equal to 9
}
for(int i=0; i<length; i++) // Loop for adding all the digits
{
sum=sum+I[i]; // value of sum is equal = value of sum + value of I at index i
}
cout<<"Digital root: "<<sum<<endl; // Displaying the result.
return 0;
}
IDE I USE: CODE::BLOCKS
Hope you enjoy! And if you have done this with another method then share it with us in the comments so that everyone will able to learn.
WORKING OF PROGRAM:-
Post a Comment