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 4557248
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:28:56+00:00 2026-05-21T17:28:56+00:00

I’m learning C and I am having a hard time understanding loops and the

  • 0

I’m learning C and I am having a hard time understanding loops and the usage of modulo. I know Loops are used to shorten up the program and Modulo’s are used to get out the remainder. My assignment was to “Write a C program to find the sum of an individual positive integer”.

I just spent few hours trying to understand this problem. I experimented too.

    int  n,d=0,s=0;
    printf("\nEnter a number\n\n");
    scanf("%d",&n);

while(n>0) { d = n%10; s = s+d; n = n/10; } printf("\n sum of the individual digits = %d",s);

My questions are:

Can anyone help me understand the flow of this program? Why is Modulo being used? and Why is there a n = n/10

Experiements I’ve done:

When I removed d = n%10; line the output prints out the digits seperatley. thus it’s not calculating.

i.e 123 = 6 –> It’s giving me 136

When I removed the line n = n/10 It’s not showing me an output. The printf statement has a parameter ‘s’

Thanks in Advance!

  • 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-05-21T17:28:56+00:00Added an answer on May 21, 2026 at 5:28 pm

    Taking the modulo in d = n % 10 makes d equal to the last digit of n in base 10. n = n / 10 removes the last digit from n.

    Modulo is essentially taking the remainder, so let’s say n = 123. Then n / 10 is 12 and n % 10 is 3.

    Removing the n = n / 10 means that n doesn’t change between each run of the loop, so the loop condition n > 0 is always true and therefore the loop keeps going until you manually kill the program.

    Here is a trace of the program with n = 123. Initially d and s are both zero.

     while (n > 0) {  // n is 123, which is greater than zero, so we enter the loop
         d = n % 10;  // 123 % 10 is 3, so d is now 3
         s = s + d;   // 0 + 3 is 3, so s is now 3
         n = n / 10;  // 123 / 10 is 12, so n is now 12.
     }                // go back to the top of the loop
     while (n > 0) {  // n is 12, which is still greater than zero
         d = n % 10;  // 12 % 10 is 2, so d is now 2
         s = s + d    // 3 + 2 is 5, so s is now 5
         n = n / 10;  // 12 / 10 is 1, so n is now 1
     }                // go back to the top again
     while (n > 0) {  // n is 1, which is still greater than zero
         d = n % 10;  // 1 % 10 is 1, so d is now 1
         s = s + d;   // 5 + 1 is 6, so s is now 6
         n = n / 10;  // 1 / 10 is 0, so n is now 0
     }                // go back to the top
     while (n > 0) {  // n is 0, which is not greater than zero, so we skip
                      // to after the loop body
     printf("\n sum of the individual digits = %d",s);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I know there's a lot of other questions out there that deal with this
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.