This is the first solution to find the sum of digits from a given integer value using the core logic.
import java.util.Scanner;
public class Sumofdgts {
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner inpInt= new Scanner(System.in);
//Input an Integer
System.out.println("Enter an Integer=");
int numValue=inpInt.nextInt();
System.out.println("The integer is="+numValue);
//Find out the Length
int rem=0, sumDigits=0, numBackUp= numValue;
while (numValue!=0) {
rem=numValue%10;
sumDigits=sumDigits+rem;
numValue=numValue/10;
}
System.out.println("The sum of digits in a given Integer "+numBackUp+" is="+sumDigits);
}
}
Output:
Enter an Integer=
68943
The integer is=68943
The sum of digits in a given Integer 68943 is=30

Leave a comment