Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8705233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:18:35+00:00 2026-06-13T03:18:35+00:00

The example in the book asks the user to enter any positive number. Then

  • 0

The example in the book asks the user to enter any positive number. Then the program will add the individual digits separately and print the total. For example if the user enters the number 7512 the program is designed to add 7 + 5 + 1 + 2 and then print the total.

I’ve written out the way I understand how the code works. Is this correct? Is my understanding of this loop correct with each step, or am I missing any calculations? What happens during the 4th loop when there is no remainder in 7 % 10?

1st  run of loop ... sum = sum + 7512 % 10 which is equal to 2 
                        n = 7512 / 10 which which equals to 751 

2nd run of loop ... sum = 2 + 751 % 10 which is equal to 1 
                         n = 751 / 10  which is equal to 75 

3rd run of loop ...  sum = 3 + 75 % 10 which is equal to 5 
                          n  = 75 / 10 which is equal to 7

4th run of loop ...  sum = 8 + 7 % 10   <------? 

import acm.program.*; 

   public class DigitSum extends ConsoleProgram{

   public void run() {
   println("This program will add the integers in the number you enter.");
   int n = readInt("Enter a positive integer: ");
   int sum = 0;
   while (n > 0) {
        sum += n % 10;
        n /= 10; 
    }
    println("The sum of the digits is" + sum + ".");    
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:18:36+00:00Added an answer on June 13, 2026 at 3:18 am

    The result of the operation 7 % 10 is 7, the remainder when you divide 7 by 10. The last iteration of the loop is to add 7 to the prior value. The next division step inside the loop (n /= 10;) takes n to 0, which is what ends the loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in CakePHP book example here I try: cake bake all then I choose 1
Reading the Scala by Example book and there is this example when Martin explains
The output of my test gives com.example.book.Book : null . When I debug the
I am implementing Schnorr identification protocol. I have an example from book Handbook of
For class specific new_handler implementation, i came across the following example in book effective
This is an example from a book, a bit modified. It seems like sessions
I am trying out an example from the book Foundations of python network programming,
I am trying to get an example from the book Professional Android Programming with
thank you for looking i got this example from my book but i cant
I'm learning JavaScript and there's an example in the book I'm using that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.