Problem: 2.15


 Question 2.15: Write a Java program and compute the sum of the digits of an integer.

Input Data:

Input an integer: 25

Expected Output

The sum of the digits is: 7

Solution:

package com.company;
import java.util.Scanner;
public class Lab2problem15 {
    public static void main(String[] args)
    {
        Scanner Obj =new Scanner(System.in);
        System.out.println("enter the integer value :");
        int n = Obj.nextInt();

        int r = n % 10;

         int s = n /10;

        int sum = r +s;
        System.out.println("the sum of the digits is : " + sum);
    }
}

Previous Post Next Post