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

One of the algorithms that computer security people are fond of is adding up a s

ID: 3704172 • Letter: O

Question

 One of the algorithms that computer security people are fond of is adding up a       sequence of digits to verify that a file has not been modified.  Frequently the      sequence of digits is the number of bytes in a file.   Let's keep this simple and insist      that the file size must be five digits.   Write a java program to prompt the user to enter       the file size in bytes.   File size will range from 00001 to 99999.   Your program      should then add up the individual digits in the file size.                    sum = digit1 + digit2 + digit3 + digit4 + digit5
      Hint: this is a good time to use the % and / operators!
      Your program should then display the sum, as well as a message if the sum is over 35.
      Sample run:               Enter the file size in bytes: 87156  Enter               The sum is 27.        
                     Enter the file size in bytes: 98779 Enter                    The sum is 40.  The sum is over 35.

Explanation / Answer

FileSizeSumTest.java

import java.util.Scanner;

public class FileSizeSumTest {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter the file size in bytes: ");

int n = scan.nextInt();

int sum = 0;

while(n>0) {

sum+=n%10;

n/=10;

}

if(sum >35) {

System.out.println("The sum is "+sum+" The sum is over 35.");

} else {

System.out.println("The sum is "+sum);

}

}

}

Output:

Enter the file size in bytes:
98779
The sum is 40 The sum is over 35.

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