Skip to content

How to add two integers in one line

Hello im new to java and in my class my prof wants us to create a program that will compute the sum of two numbers. (Use methods) For example: Input an integer: 95 //Expected Output: The sum is 14 //(9+5=14)

im really lost on this one because i really don’t know how to do this please help


If you use Java you can use this code:

int num = 95;
int sum = 0;

while (0 != num) {
    // add the last digit of the given number to the sum
    sum = sum + (num % 10);
    // remove the last digit of the given number
    num = num / 10;


(The code in JavaScript is very similar)