write a Java program to input a three digit number and display it in reverse order. Also calculate the sum.
Share
write a Java program to input a three digit number and display it in reverse order. Also calculate the sum.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a three digit number:");
int num = scanner.nextInt();
int reversedNum = reverseNumber(num);
int sum = calculateSum(num);
System.out.println("The reversed number is: " + reversedNum);
System.out.println("The sum of the digits is: " + sum);
}
private static int reverseNumber(int num) {
return Integer.parseInt(new StringBuilder(String.valueOf(num)).reverse().toString());
}
private static int calculateSum(int num) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
}