How to sum digits of an integer in java?
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
How to sum 3 digits of an integer?
It is not necessary convert the number to string, use the % operator to get the separate digits of an integer.
something like this:
private int sumTheDigits(int num){
int sum = 0;
while (num > 0) {
sum += num % 10;
num = num / 10;
}
return sum;
}
sum of digits of an integer
What you can do is this:
public static int sumInt(String num) {
int sum = 0;
for (int i = 0; i<num.length(); i++)
sum += num.charAt(i) - '0';
return sum;
}
If you take the character at index i and subtract '0', you get the value of the character as per the ASCII table and it then gets converted implicitly into an interger when you add it to sum.
Sum all digits of a number and show the digits separately in Java
static final Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int number, result;
System.out.println("Enter number:");
number = input.nextInt();
result = 0;
System.out.print("The numbers: ");
//Reverses the number
int reversedNum = 0;
while (number != 0) {
reversedNum = reversedNum * 10 + number % 10;
number = number / 10;
}
//Iterates over the number and prints it out
while (reversedNum > 0) {
System.out.print((reversedNum % 10));
result = result + (reversedNum % 10);
reversedNum = reversedNum / 10;
if (reversedNum > 0) {
System.out.print(" + ");
}
}
System.out.println(" = " + result);
}
This works to print out all the numbers in the correct order without using arrays and strings.
Example:
Enter number:
12345
The numbers: 1 + 2 + 3 + 4 + 5 = 15
BUILD SUCCESSFUL (total time: 5 seconds)
how to calculate the sum of digits for a number in java?
That's because you are sharing same number in both reverse and sum method and when you first call reverse method you go on dividing the number until 0 and hence when you come to sum method, your number is 1 and hence sum is 0.
Inorder to rectify this, i would suggest you define local variable called mynumber like:
public int fill() {
System.out.println("enter the number");
Scanner in = new Scanner(System.in);
num = in.nextInt();
if (num < 0) {
System.out.println("enter a positive number");
}
int myNumber = num;
while (myNumber != 0) {
reverse = reverse * 10;
reverse = reverse + myNumber % 10;
myNumber = myNumber / 10;
}
System.out.println("your reverse number is : " + reverse);
return num;
}
OUTPUT:
enter the number
1234
your reverse number is : 4321
sum of digits: 10
Issue while compare and sum digits of two numbers
Why not make things easier on yourself and just treat the integers as strings for the purpose of summing the digits?
while (scanner.hasNext()) {
int larger = Math.max(scanner.nextInt(), scanner.nextInt());
String sLarger = String.valueOf(larger);
int digitsSum = 0;
for (int i = 0; i < sLarger.length(); i++) {
digitsSum += sLarger.charAt(i) - '0';
}
System.out.printf("Larger number %s sums to %d%n", sLarger, digitsSum);
}
Related Topics
Jersey 415 Unsupported Media Type
How to Fetch Fetchtype.Lazy Associations with JPA and Hibernate in a Spring Controller
Difference Between Year-Of-Era and Week-Based-Year
Jprogressbar Isn't Progressing
Platform Independent Paths in Java
Java Socket/Serialization, Object Won't Update
Maven Parent Pom VS Modules Pom
Disable a Particular Checkstyle Rule for a Particular Line of Code
What Is the Purpose of @Namedarg Annotation in Javafx 8
How to Create Sparksession with Hive Support (Fails with "Hive Classes Are Not Found")
Difference Between List, List<>, List<T>, List<E>, and List<Object>
@Aspectj Pointcut for All Methods of a Class with Specific Annotation
Does Java Have a Hashmap with Reverse Lookup
How to Use .Jar Files in Netbeans
Should I Declare Jackson's Objectmapper as a Static Field
Java Spring Boot: How to Map My App Root ("/") to Index.Html