﻿ Java exercises: Add all the digits of a given positive integer until the result has a single digit - w3resource # Java Exercises: Add all the digits of a given positive integer until the result has a single digit

## Java Basic: Exercise-108 with Solution

Write a Java program to add all the digits of a given positive integer until the result has a single digit.

Pictorial Presentation: Sample Solution:

Java Code:

``````import java.util.Scanner;
public class Example108 {
public static void main(String[] arg) {
Scanner in = new Scanner(System.in);
System.out.print("Input a positive integer: ");
int n = in.nextInt();
if (n>0)
{
System.out.print("The single digit number is: "+(n == 0 ? 0 : (n % 9 == 0 ? 9 : n % 9)));
}
System.out.println("\n");
}
}
```
```

Sample Output:

```Input a positive integer: 25
The single digit number is: 7
```

Flowchart: Java Code Editor:

What is the difficulty level of this exercise?

﻿

## Java: Tips of the Day

initial:

Returns all the elements of an array except the last one. Use Arrays.copyOfRange() to return all except the last one

```public static <T> T[] initial(T[] elements) {
return Arrays.copyOfRange(elements, 0, elements.length - 1);
}
```

Ref: https://bit.ly/3zEKw9u