In this post, we will learn **how to calculate the generic root of a number using C** Programming language. But before that let’s learn about the **generic root of a number**.

**Generic root** of a number is the sum of all the digits of a given number until we get a single digit output. For example, if a number is** 428**, then it’s generic root will be **4 + 2 + 8 = 14**, then **1 + 4 = 5**.

The below program asks the user to enter a number, then it calculates the generic root of the entered number using the following approaches.

- Using Loops
- Using Functions

So, without further ado, let’s begin this tutorial.

**C Program to Calculate Generic Root of a Number**

// C Program to Calculate Generic Root of a Number #include <stdio.h> int main(){ int num, rem, sum; // Asking for input printf("Enter a number: "); scanf("%d", &num); while (num >= 10){ for (sum = 0; num > 0; num = num / 10){ rem = num % 10; sum = sum + rem; } if (sum >= 10){ num = sum; } else{ printf("Generic root of the entered number is: %d", sum); break; } } return 0; }

**Output**

```
Enter a number: 246
Generic root of the entered number is: 3
```

**How Does This Program Work ?**

int num, rem, sum;

In the above program, we have declared** three integer data type** variables named** num**, **rem **and **sum**.

// Asking for input printf("Enter a number: "); scanf("%d", &num);

The user is asked to enter a number. This number gets stored in the** num** named variable.

while (num >= 10){ for (sum = 0; num > 0; num = num / 10){ rem = num % 10; sum = sum + rem; } }

The formula to calculate the generic tree of a number is to sum all digits of the number until we get a number less than **10**.

Suppose, the user enters a number** 246**, then:

**1st While Loop Iteration: (246 >= 10)**, here the condition is true. So, the following loop continues:

**1st For Loop Iteration**: for (sum = 0; 246 > 0; 246 / 10)- rem = num % 10 = 246 / 10 = 6
- sum = sum + rem = 0 + 6 =
**6**

**2nd For Loop Iteration:**for (sum = 6; 24 > 0; 24 / 10)- rem = num % 10 = 24 / 10 = 4
- sum = sum + rem = 6 + 4 =
**10**

**3rd For Loop Iteration:**for (sum = 10; 2 > 0; 2 / 10)- rem = num % 10 = 2 / 10 = 2
- sum = sum + rem = 10 + 2 =
**12**

Now, since **sum = 12 > 10**, the while loop performs a second iteration such that the value of *num = sum*.

**2nd While Loop Iteration: (12 >= 10)**, here the condition is true. So,

**1st For Loop Iteration:**for (sum = 0; 12 > 0; 12 / 10)- rem = num % 10 = 12 % 10 = 2
- sum = sum + rem = 0 + 2 =
**2**

**2nd For Loop Iteration:**for (sum = 2; 1 >0; 1 / 10)- rem = num % 10 = 1 % 10 = 1
- sum = sum + rem = 2 + 1 =
**3**

We get **sum = 3**.

After that the loop terminates.

if (sum >= 10){ num = sum; } else{ printf("Generic root of the entered number is: %d", sum); break; }

Now, since the** sum >= 10** condition is** false**, the else statement continues and the generic root of the entered number is displayed on the screen using** printf() **function.

**C Program to Calculate Generic Root of a Number Using Function**

// C Program to Calculate Generic Root of a Number Using Functions #include <stdio.h> int genericRoot(int n){ int rem, sum; for (sum = 0; n > 0; n = n / 10){ rem = n % 10; sum = sum + rem; } return sum; } int main(){ int num, sum; // Asking for input printf("Enter a number: "); scanf("%d", &num); // Calling out user-defined function while (num >= 10){ sum = genericRoot(num); if (sum >= 10){ num = sum; } else{ printf("Generic root is: %d", num, sum); break; } } return 0; }

**Output**

```
Enter a number: 456
Generic root is: 6
```

**Conclusion**

I hope after going through this post, you understand** how to calculate the generic root of a number using C** Programming language.

If you have any query regarding the program, then contact us in the comment section. We will be delighted to assist you.

**Also Read:**