answersLogoWhite

0

String num = Integer.toString(12345); // Insert your Integer here.

int sum = 0;

for(int i = 0; i < num.length(); i++){

sum += Character.digit(num.charAt(i), 10);

}

System.out.println(sum); // Simply prints the sum of the digits to standard out.

///// ALTERNATE SOLUTION:

int num = 12345; // Insert your Integer here.

int sum = 0;

while (num > 0){

sum += num % 10;

num /= 10;

}

System.out.println(sum); // Simply prints the sum of the digits to standard out.

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

How to write a java program to extract the last digit from the input?

Assuming you've entered a multi-digit number whole number (an integer), then take the modus (%) of the number and 10. E.g., if the number input was 1234, then 1234 % 10 is 4. Thus the final digit is 4. Note that modus 10 is the same as dividing the number by 10 and taking the remainder.


To find strong number in java?

sir , what is perfect no?


Can you get a Java program to find the cube of a number?

Yes


Write a java program to print the last digit in Fibonacci series?

Just generate the Fibonacci numbers one by one, and print each number's last digit ie number%10.


How do you write a Java program to accept a 3-digits integer number and print out the highest digit from the derived input?

The tricky part is getting the individual digits. There are basically two ways to do this: 1) Convert the number to a string, and use string manipulation to get the individual digits. 2) Repeatedly divide the number by 10. The digit is the remainder (use the "%" operator). To actually get the highest digit, initially assume that the highest digit is zero (store this to a variable, called "maxDigit" or something similar). If you find a higher digit, replace maxDigit by that.


How do you find out whether Java is installed on Linux?

Enter "java -version" into a terminal. If Java is installed, it will tell you the version number. If it is not installed, it will say "command not found."


Where can one find a Java development kit?

Java Development Kits are best found on the Java website. There are a number of programmes that can be downloaded. It is also possible to get Java Development Kit downloads from the Oracle website.


How do you find a system number in a JAVA program?

If by system number you mean the version number of the Java language on the client computer, you can useSystem.getProperty("java.version"); .Or, if you want the Java virtual machine version number you can use System.getProperty("java.vm.version"); .Finally, if you want the version number of the operating system, you can use System.getProperty("os.version"); .


Which math function is used to find the square root of a number using java?

Math.sqrt(number) function is used to find the square root of a number.. try it


Example of inclusive or operator in java?

This is often used to specify several possibilities For example, assuming you have the last digit of a number in a variable "lastDigit":if (lastDigit 5)// In this case, the number is divisible by 5


How do you write a java program to find the square root of a number?

You can use the Math.sqrt() method.


Head first java covers which java?

Head First Java book covers, core java 1.5 [Tiger].