# Generic root of a number

Here given code implementation process.

``````//  C program
//  Generic root of a number
#include <stdio.h>

// Find generic root of a given number
void genericRoot(int num)
{
int n = num;
if (n < 0)
{
n = -n;
}
int sum = 0;
// Execute loop until when the value of n is not zero
while (n != 0)
{
sum += n % 10;
// Remove last digit
n /= 10;
if (n == 0 && sum > 9)
{
//  When n is zero but sum is not a form of single digit
//  Assign sum value to n
n = sum;
sum = 0;
}
}
// Display given number
printf("\n Number : %d", num);
printf("\n Result : %d", sum);
}
int main(int argc, char
const *argv[])
{
// Test case
// 221  (2+2+1) => 5
genericRoot(221);
// 786  (7+8+6) => 21 (2+1) => 3
genericRoot(786);
// 19   (1+9)   => 10 (1+0) => 1
genericRoot(19);
return 0;
}``````

#### Output

`````` Number : 221
Result : 5
Number : 786
Result : 3
Number : 19
Result : 1``````
``````/*
Java program
Generic root of a number
*/
public class Simplification
{
// Find generic root of a given number
public void genericRoot(int num)
{
int n = num;
if (n < 0)
{
n = -n;
}
int sum = 0;
// Execute loop until when the value of n is not zero
while (n != 0)
{
sum += n % 10;
// Remove last digit
n /= 10;
if (n == 0 && sum > 9)
{
//  When n is zero but sum is not a form of single digit
//  Assign sum value to n
n = sum;
sum = 0;
}
}
// Display given number
System.out.print("\n Number : " + num);
System.out.print("\n Result : " + sum);
}
public static void main(String[] args)
{
// Test case
// 221  (2+2+1) => 5
// 786  (7+8+6) => 21 (2+1) => 3
// 19   (1+9)   => 10 (1+0) => 1
}
}``````

#### Output

`````` Number : 221
Result : 5
Number : 786
Result : 3
Number : 19
Result : 1``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ program
Generic root of a number
*/

class Simplification
{
public:
// Find generic root of a given number
void genericRoot(int num)
{
int n = num;
if (n < 0)
{
n = -n;
}
int sum = 0;
// Execute loop until when the value of n is not zero
while (n != 0)
{
sum += n % 10;
// Remove last digit
n /= 10;
if (n == 0 && sum > 9)
{
//  When n is zero but sum is not a form of single digit
//  Assign sum value to n
n = sum;
sum = 0;
}
}
// Display given number
cout << "\n Number : " << num;
cout << "\n Result : " << sum;
}
};
int main()
{
// Test case
// 221  (2+2+1) => 5
// 786  (7+8+6) => 21 (2+1) => 3
// 19   (1+9)   => 10 (1+0) => 1
return 0;
}``````

#### Output

`````` Number : 221
Result : 5
Number : 786
Result : 3
Number : 19
Result : 1``````
``````// Include namespace system
using System;
/*
C# program
Generic root of a number
*/
public class Simplification
{
// Find generic root of a given number
public void genericRoot(int num)
{
int n = num;
if (n < 0)
{
n = -n;
}
int sum = 0;
// Execute loop until when the value of n is not zero
while (n != 0)
{
sum += n % 10;
// Remove last digit
n /= 10;
if (n == 0 && sum > 9)
{
//  When n is zero but sum is not a form of single digit
//  Assign sum value to n
n = sum;
sum = 0;
}
}
// Display given number
Console.Write("\n Number : " + num);
Console.Write("\n Result : " + sum);
}
public static void Main(String[] args)
{
// Test case
// 221  (2+2+1) => 5
// 786  (7+8+6) => 21 (2+1) => 3
// 19   (1+9)   => 10 (1+0) => 1
}
}``````

#### Output

`````` Number : 221
Result : 5
Number : 786
Result : 3
Number : 19
Result : 1``````
``````<?php
/*
Php program
Generic root of a number
*/
class Simplification
{
// Find generic root of a given number
public	function genericRoot(\$num)
{
\$n = \$num;
if (\$n < 0)
{
\$n = -\$n;
}
\$sum = 0;
// Execute loop until when the value of n is not zero
while (\$n != 0)
{
\$sum += \$n % 10;
// Remove last digit
\$n = intval(\$n / 10);
if (\$n == 0 && \$sum > 9)
{
//  When n is zero but sum is not a form of single digit
//  Assign sum value to n
\$n = \$sum;
\$sum = 0;
}
}
// Display given number
echo "\n Number : ". \$num;
echo "\n Result : ". \$sum;
}
}

function main()
{
// Test case
// 221  (2+2+1) => 5
// 786  (7+8+6) => 21 (2+1) => 3
// 19   (1+9)   => 10 (1+0) => 1
}
main();``````

#### Output

`````` Number : 221
Result : 5
Number : 786
Result : 3
Number : 19
Result : 1``````
``````/*
Node Js program
Generic root of a number
*/
class Simplification
{
// Find generic root of a given number
genericRoot(num)
{
var n = num;
if (n < 0)
{
n = -n;
}
var sum = 0;
// Execute loop until when the value of n is not zero
while (n != 0)
{
sum += n % 10;
// Remove last digit
n = parseInt(n / 10);
if (n == 0 && sum > 9)
{
//  When n is zero but sum is not a form of single digit
//  Assign sum value to n
n = sum;
sum = 0;
}
}
// Display given number
process.stdout.write("\n Number : " + num);
process.stdout.write("\n Result : " + sum);
}
}

function main()
{
// Test case
// 221  (2+2+1) => 5
// 786  (7+8+6) => 21 (2+1) => 3
// 19   (1+9)   => 10 (1+0) => 1
}
main();``````

#### Output

`````` Number : 221
Result : 5
Number : 786
Result : 3
Number : 19
Result : 1``````
``````#   Python 3 program
#   Generic root of a number

class Simplification :
#  Find generic root of a given number
def genericRoot(self, num) :
n = num
if (n < 0) :
n = -n

sum = 0
#  Execute loop until when the value of n is not zero
while (n != 0) :
sum += n % 10
#  Remove last digit
n = int(n / 10)
if (n == 0 and sum > 9) :
#   When n is zero but sum is not a form of single digit
#   Assign sum value to n
n = sum
sum = 0

#  Display given number
print("\n Number : ", num, end = "")
print("\n Result : ", sum, end = "")

def main() :
#  Test case
#  221  (2+2+1) => 5
#  786  (7+8+6) => 21 (2+1) => 3
#  19   (1+9)   => 10 (1+0) => 1

if __name__ == "__main__": main()``````

#### Output

`````` Number :  221
Result :  5
Number :  786
Result :  3
Number :  19
Result :  1``````
``````#   Ruby program
#   Generic root of a number

class Simplification
#  Find generic root of a given number
def genericRoot(num)
n = num
if (n < 0)
n = -n
end

sum = 0
#  Execute loop until when the value of n is not zero
while (n != 0)
sum += n % 10
#  Remove last digit
n /= 10
if (n == 0 && sum > 9)
#   When n is zero but sum is not a form of single digit
#   Assign sum value to n
n = sum
sum = 0
end

end

#  Display given number
print("\n Number : ", num)
print("\n Result : ", sum)
end

end

def main()
#  Test case
#  221  (2+2+1) => 5
#  786  (7+8+6) => 21 (2+1) => 3
#  19   (1+9)   => 10 (1+0) => 1
end

main()``````

#### Output

`````` Number : 221
Result : 5
Number : 786
Result : 3
Number : 19
Result : 1``````
``````/*
Scala program
Generic root of a number
*/
class Simplification
{
// Find generic root of a given number
def genericRoot(num: Int): Unit = {
var n: Int = num;
if (n < 0)
{
n = -n;
}
var sum: Int = 0;
// Execute loop until when the value of n is not zero
while (n != 0)
{
sum += n % 10;
// Remove last digit
n = (n / 10).toInt;
if (n == 0 && sum > 9)
{
//  When n is zero but sum is not a form of single digit
//  Assign sum value to n
n = sum;
sum = 0;
}
}
// Display given number
print("\n Number : " + num);
print("\n Result : " + sum);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Simplification = new Simplification();
// Test case
// 221  (2+2+1) => 5
// 786  (7+8+6) => 21 (2+1) => 3
// 19   (1+9)   => 10 (1+0) => 1
}
}``````

#### Output

`````` Number : 221
Result : 5
Number : 786
Result : 3
Number : 19
Result : 1``````
``````/*
Swift 4 program
Generic root of a number
*/
class Simplification
{
// Find generic root of a given number
func genericRoot(_ num: Int)
{
var n: Int = num;
if (n < 0)
{
n = -n;
}
var sum: Int = 0;
// Execute loop until when the value of n is not zero
while (n  != 0)
{
sum += n % 10;
// Remove last digit
n /= 10;
if (n == 0 && sum > 9)
{
//  When n is zero but sum is not a form of single digit
//  Assign sum value to n
n = sum;
sum = 0;
}
}
// Display given number
print("\n Number : ", num, terminator: "");
print("\n Result : ", sum, terminator: "");
}
}
func main()
{
// Test case
// 221  (2+2+1) => 5
// 786  (7+8+6) => 21 (2+1) => 3
// 19   (1+9)   => 10 (1+0) => 1
}
main();``````

#### Output

`````` Number :  221
Result :  5
Number :  786
Result :  3
Number :  19
Result :  1``````
``````/*
Kotlin program
Generic root of a number
*/
class Simplification
{
// Find generic root of a given number
fun genericRoot(num: Int): Unit
{
var n: Int = num;
if (n < 0)
{
n = -n;
}
var sum: Int = 0;
// Execute loop until when the value of n is not zero
while (n != 0)
{
sum += n % 10;
// Remove last digit
n /= 10;
if (n == 0 && sum > 9)
{
//  When n is zero but sum is not a form of single digit
//  Assign sum value to n
n = sum;
sum = 0;
}
}
// Display given number
print("\n Number : " + num);
print("\n Result : " + sum);
}
}
fun main(args: Array < String > ): Unit
{
// Test case
// 221  (2+2+1) => 5
// 786  (7+8+6) => 21 (2+1) => 3
// 19   (1+9)   => 10 (1+0) => 1
}``````

#### Output

`````` Number : 221
Result : 5
Number : 786
Result : 3
Number : 19
Result : 1``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.