Q:

C program to find the generic root of a number

belongs to collection: C Programming on Numbers

0

In this exercise, 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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Programming on Numbers

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program to calculate the square of a number usin... >>
<< C Program to find given number is the sum of first...