In this blog post, we learn how to write a C program to find the generic root of a number?. We will write the C program to find the generic root of a number. Write a C program to input a number from the user and find the generic root of a number. How to display the generic root of a number. How to find the generic root of a number in C programming. Logic to find the generic root of a number in the C program.
Example,
The mathematical formula to calculate the generic root is nothing but the calculating sum of all digits in a given number until we get a single-digit output (less than 10)
Generic Root of 98765 = 9 + 8 + 7 + 6 + 5 => 35 => 8
C program to find the generic root of a number:
The below program ask the user to enter the value. After getting the value from the user it will find the generic root of a number.
#include <stdio.h>
int main()
{
int num, sum, rem;
printf("Please Enter any 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 Given num = %d", sum);
break;
}
}
return 0;
}
Output:
Please Enter any number = 123
Generic Root of Given num = 6
You can also calculate the genric root of a number by modulo division 9. There are two conditions, calculate num % 9 to get the root if the result is 0, then the root is 9.
#include <stdio.h>
int main()
{
int num, genericRoot;
printf("Please Enter any number = ");
scanf("%d", &num);
genericRoot = (1+((num-1)%9));
printf("Generic Root of a given Number = %d", genericRoot);
return 0;
}
Output:
Please Enter any number = 123
Generic Root of Given num = 6