Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The file below is the starter file for the question: import java.util.Scanner; p

ID: 3813815 • Letter: T

Question

The file below is the starter file for the question:

import java.util.Scanner;

public class SumOfDigits {
public static void main(String[] args) {
System.out.println("Please enter an integer: ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int sumOfDigits = sumOfDigits(num);
System.out.printf("The sum of digits of %d is %d ", num, sumOfDigits);
  
input.close();
}
  
public static int sumOfDigits(int num) {
// Your code here

In the file, provide code for the method

public static int sumOfDigits(int num)

that computes and returns the sum of the digits of the given integer num. Run the program to test the correctness of your code for the method. The following are some sample runs:

Please enter your height in centimeters:

The sum of the digits of 123 is 6

Please enter your height in centimeters:

The sum of the digits of 2140 is 7

Please enter your height in centimeters:

The sum of the digits of 2145360 is 21

Please also provide comments for each line of the program.

must also be written in java C++

Explanation / Answer

Code for the method sumOfDigits is

public static int sumOfDigits(int num)
{ int sum = 0;
while(num !=0)
{
int lastdigit = num % 10;

sum += lastdigit;

num /= 10;
  
}

return sum;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote