C Program to Find Generic Root of a Number


Lets write a C program to calculate Generic Root of a user input number.

Generic Root: of a number is sum of all the digits of the number until we get a single-digit output.

Related Read:
while loop in C programming
Calculate Sum of Digits: C Program

For Example: If user input number is 12345, then we add all the individual digits of the number i.e., 1 + 2 + 3 + 4 + 5 = 15. We got 15. Now we add individual digits of number 15 i.e., 1 + 5 = 6. So Generic Root of number 12345 is 6.

Source Code: C Program to Find Generic Root of a Number

 
#include < stdio.h >

int main()
{
    int num, temp, rem, sum = 0;

    printf("Enter a number\n");
    scanf("%d", &num);

    temp = num;

    while(temp > 0)
    {
        rem  = temp % 10;
        sum  = sum + rem;
        temp = temp / 10;

        if(temp == 0)
        {
            if(sum > 9)
            {
                temp = sum;
                sum  = 0;
            }
        }
    }

    printf("Generic Root of %d is %d\n", num, sum);

    return 0;
}

Output 1:
Enter a number
12345
Generic Root of 12345 is 6

Output 2:
Enter a number
123
Generic Root of 123 is 6

Output 3:
Enter a number
15937
Generic Root of 15937 is 7

Output 4:
Enter a number
222222
Generic Root of 222222 is 3

Output 5:
Enter a number
99999
Generic Root of 99999 is 9

C Program to Calculate Generic Root of a Number


[youtube https://www.youtube.com/watch?v=W3gYEahIZY8]

YouTube Link: https://www.youtube.com/watch?v=W3gYEahIZY8 [Watch the Video In Full Screen.]


Logic To Find Generic Root of a Number

If user entered number is 1234.
Num = 1234;
temp = 1234;
sum = 0;

Iteration 1
rem = temp % 10;
rem = 1234 % 10;
rem = 4;

sum = sum + rem
sum = 0 + 4;
sum = 4;

temp = temp / 10;
temp = 1234 / 10;
temp = 123;

Iteration 2
rem = temp % 10;
rem = 123 % 10;
rem = 3;

sum = sum + rem
sum = 4 + 3;
sum = 7;

temp = temp / 10;
temp = 123 / 10;
temp = 12;

Iteration 3
rem = temp % 10;
rem = 12 % 10;
rem = 2;

sum = sum + rem
sum = 7 + 2;
sum = 9;

temp = temp / 10;
temp = 12 / 10;
temp = 1;

Iteration 4
rem = temp % 10;
rem = 1 % 10;
rem = 1;

sum = sum + rem
sum = 9 + 1;
sum = 10;

temp = temp / 10;
temp = 1 / 10;
temp = 0;

Now inside while loop we keep checking for the condition temp = 0. Once this condition is true, we check if the value present in sum is greater than 9. i.e., we check if variable sum has double digit number. If that is true, then we copy the number present in variable sum to variable temp and we assign 0 to sum. Now the while loop continues until the variable sum has single digit number.

Once variable sum has single digit number in it, control exits the while loop and we out put the value of sum as Generic Root of the user entered number.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *