Add all the digits of a given positive integer until the result has a single digit

Add all the digits of a given positive integer until the result has a single digit

Add all the digits of a given positive integer until the result has a single digit

Program
import java.util.Scanner;

public class Main {
    public static void main(String[] arg) {
      
        Scanner in = new Scanner(System.in);
        System.out.print("Input a positive integer Number: ");
        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\n");  
    }
}


Output:

Input a positive integer Number: 1234
The single digit number is: 1


Input a positive integer Number: 24
The single digit number is: 6



More Questions


47 . Write a java Program to check given matrix is null matrix
48 . Add all the digits of a given positive integer until the result has a single digit
49 . Write a Java program to accept a float value of number and return a rounded float value



For latest job updates join Telegram Channel: https://t.me/sateeshm

Apply For The Job

Apply For Job

Java Material

Python Material

PLSQL Material

.Net Material

Java Practice Programs

400 Java Programs