Find the Generic root of a number

Problem statement:- Program to Find the Generic root of a number.

Data requirement:-

   Input Data:- num

  Output Data:-sum

  Additional Data:- r

Program in C

Here is the source code of the C Program to Find the Generic root of a number.

Code:

/*Write a C program to Find the Generic root of a number.*/

#include <stdio.h>
int main ()
{
  int num, sum, r;
  printf ("\nEnter a number:");
  scanf ("%d", &num);
  while (num > 10)
    {
      sum = 0;
      while (num)
{
  r = num % 10;
  num = num / 10;
  sum += r;
}
      if (sum > 10)
num = sum;
      else
break;
    }
  printf ("\nGeneric root of the number is %d", sum);
  return 0;
}

Input/Output:
Enter a number:
25
Generic root of the number is 7

Program in C++

Here is the source code of the C++ Program to Find the Generic root of a number.

Code:

/*Write a C++ program to Find the Generic root of a number.*/

#include <iostream>
using namespace std;
int main ()
{
  int num, sum, r;
  cout<<"Enter a number:";
  cin>>num;
  while (num > 10)
    {
      sum = 0;
      while (num)
{
  r = num % 10;
  num = num / 10;
  sum += r;
}
      if (sum > 10)
num = sum;
      else
break;
    }
  cout<<"Generic root of the number is "<<sum;
  return 0;
}

Input/Output:
Enter a number:
35
Generic root of the number is 8

Program in Java

Here is the source code of the Java Program to Find the Generic root of a number.

Code:

/*Write a JAVA program to Find the Generic root of a number.*/

import java.util.Scanner;
public class GenericRoot {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num, sum=0, r;
System.out.println("Enter a number:");
  num=cs.nextInt();
  while (num > 10)
    {
      sum = 0;
      while (num!=0)
{
  r = num % 10;
  num = num / 10;
  sum += r;
}
      if (sum > 10)
num = sum;
      else
break;
    }
  System.out.println("Generic root of the number is "+sum);
cs.close();
}
}

Input/Output:
Enter a number
72
Generic root of the number is 9

Program in Python

Here is the source code of the Python Program to Find the Generic root of a number.

Code:

'''Write a Python program to Find the Generic root of a number.''

print("Enter a number:")
num = int(input())
while num > 10:
    sum = 0
    while num:
        r=num % 10
        num= num / 10
        sum+= r
    if sum > 10:
        num = sum
    else:
        break
print("Generic root of the number is ", int(sum)) 

Input/Output:
Enter a number:
112
Generic root of the number is  4


Recommended Question :



Post a Comment

3 Comments

  1. True division "/" vs. Floor divison "//". What do you think of a small Python code improvement? Single line in nested while loop could be changed to: num = num // 10. We want num to keep as integer after division. Right? Floor division "//" is a normal division operation except that it returns the largest possible integer.

    ReplyDelete
    Replies
    1. In Python 2.7: By default, division operator will return integer output. To get the result in double, multiply the dividend or divisor by 1.0.
      In Python 3:
      // => used for integer output (100//35 => 2)
      / => used for double output (100/35 => 2.857142857142857)

      Delete
  2. The last line can be: print("Generic root of the number is "+ str(sum)). New part is "+ str(sum)" instead of existing "int(sum)" that doesn't work for all inputs. It's OK for input 112 (result=4). But, for the input 456, current code returns wrong result=8 instead of 6. Therefore, "int(sum)" has no effect.

    ReplyDelete

Please do not Enter any spam link in the comment box