How do you find in java the sum of the first and last digit of 3 digit number?

Java

1 answer

Answer

1022225

2026-08-06 21:40

+ Follow

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.

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.