C program to find the generic root of a number; Through this tutorial, we will learn how to find the generic root of a number using while loop and functions.
Programs to Find Generic Root of a Number in C
- C Program to Find Generic Root of a Number
- C Program to Find Generic Root of a Number using Function
C Program to Find Generic Root of a Number
/* C Program to Calculate Generic Root of a Number */ #include <stdio.h> int main() { int Number, Sum, Reminder; printf("\n Please Enter any number :- "); scanf("%d", &Number); while(Number >= 10) { for (Sum=0; Number > 0; Number= Number/10) { Reminder = Number % 10; Sum=Sum + Reminder; } if(Sum >= 10) { Number = Sum; } else { printf("\n The Generic Root of a Given Number = %d", Sum); return 0; } } }
The output of the above c program; as follows:
Please Enter any number :- 50
Generic Root of a Given Number = 5
C Program to Find Generic Root of a Number using Function
/* C Program to Calculate Generic Root of a Number */ #include <stdio.h> int Sum_Of_Digits (int); int main() { int Number, Sum, Root; printf("\n Please Enter any number\n"); scanf("%d", &Number); while(Number >= 10) { Sum = Sum_Of_Digits (Number); if(Sum >= 10) { Number = Sum; } else { printf("\n Generic Root of a Given Number = %d", Sum); return 0; } } } int Sum_Of_Digits (int Number) { int Reminder, Sum; for (Sum=0; Number > 0; Number = Number/10) { Reminder = Number % 10; Sum = Sum + Reminder; } return Sum; }
The output of the above c program; as follows:
Please Enter any number :- 50 Generic Root of a Given Number = 5