Write a program to read an integer and reduce it to a single digit
import java.util.Scanner; public class DigitReduce { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter a Number:-"); long num=sc.nextLong(); System.out.println(" " + calculateDigit(num)); } private static long calculateDigit(long num) { return (num/10 == 0 ? num : calculateDigit(num/10 + num % 10)); } } Output: Enter a Number:- 4789 1 Enter a Number:- 3348 9
No comments:
Post a Comment