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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:04:01+00:00 2026-06-08T08:04:01+00:00

Following is the code for reducing a given number to a single digit by

  • 0

Following is the code for reducing a given number to a single digit by adding the digits of the number recursively.

For example if the input is 845 the output is 8. 8+4+5 = 17 -> 1+7 = 8 (output)

#include <stdio.h>
#define TRUE 1

int reduceToSingle(int numb);

int main() 
{
    int numb;
    scanf("%d",&numb);
    printf("Original = %d Single digit = %d\n", numb, reduceToSingle(numb));

    return TRUE;
} 

int reduceToSingle(int numb)
{
    int sum = 0, digit = 0;
    for (digit = numb % 10; numb != 0; numb = numb / 10)
    {
        digit = numb % 10;
        sum += digit;
    }

    if (sum > 9)
        reduceToSingle(sum);
    else
        return sum;
}

In the above code in the if (sum > 9) block I haven’t returned the function value. I just called the function instead. Logically this function should give an incorrect value. But when I ran the above program in my system I got the correct sum of digits in output. I am unable to comprehend the logic behind this behaviour.

  • 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-08T08:04:03+00:00Added an answer on June 8, 2026 at 8:04 am

    It’s just undefined behavior and I’m sure you got a warning. It happens to work – tweak the compiler settings or change the compiler altogether and it won’t anymore.

    In this case I suspect eax isn’t clobbered so you get the expected value, i.e. the last value returned by any of the calls. So when you call reduceToSingle, it will eventually reach return (when sum <= 9). From then on the value of eax will trickle down to the original caller.

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

Sidebar

Related Questions

Following code is generated by a for loop. <form action=saveresponse.php method=POST name=mainForm> <input class=cbox_yes
following code doesn't work with input: 2 7 add Elly 0888424242 add Elly 0883666666
Having the following code: #include <iostream> struct A { int x; A(){} ~A(){std::cout <<~A(<<x<<)\n;}
Following code is an example of text placed into a textarea from a database.
I pasted the following code in google closure for reducing the size,selected the option
Following code produces a nested array as a result for keys containing three items:
Following code takes like 2500 milliseconds on an i7-*3.4 GHz windows-7 64-bit computer to
Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
Following code: <%= render 'shared/error_messages', f.object %> where f.object is instance of a class
Following code gets executed whenever I want to persist any entity. Things seems to

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.