public static void main(String[] args) {
int num = 321;
int sum = 0;
while (num > 0) {
sum = sum + num % 10;
num = num / 10;
}
System.out.println(sum);
}
Output
6
More Related Contents:
- Including all the jars in a directory within the Java classpath
- How to deal with “java.lang.OutOfMemoryError: Java heap space” error?
- Abstract class in Java
- Convert latitude/longitude point to a pixels (x,y) on mercator projection
- How to get VM arguments from inside of Java application?
- Immutable array in Java
- Android “gps requires ACCESS_FINE_LOCATION” error, even though my manifest file contains this
- Retrieving a List from a java.util.stream.Stream in Java 8
- Java: Text to Speech engines overview [closed]
- Get all variable names in a class
- How to rotate JPEG images based on the orientation metadata?
- How to extract parameters from a given url
- ActionListener for a specific text inside a JTextArea?
- Java, change a cell content as a function of another cell in the same row
- Why put JSP in WEB-INF?
- Java command not found on Linux
- App crashing when trying to use RecyclerView on android 5.0
- Show padding zeros using DecimalFormat
- Why I’m getting StackOverflowError
- How to make a lambda expression define toString in Java 8?
- JTable enter key
- How to slowly change object color from one to another?
- Painted content invisible while resizing in Java
- How do I sort two arrays in relation to each other?
- Image resizing and displaying in a JPanel or a JLabel without loss of quality
- How can I serve static html from spring boot?
- error: unable to load installed packages just now
- How LongAdder performs better than AtomicLong
- RESTEasy Client + NoSuchMethodError
- Disable all default HTTP error response content in Tomcat