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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:41:57+00:00 2026-06-10T07:41:57+00:00

I have to make a recursive function that finds the number of ones from

  • 0

I have to make a recursive function that finds the number of ones from 0 to n recursively.

So f(16) = 9

(1,10,11,12,13,14,15,16)

This is obviously homework so I would appreciated if you did NOT post any code, just the reasoning behind it.

What I’ve reasoned so far is that if you do %10 of a number it will tell you if the least significant is a one, also if you do an integer division by 10 you lose that digit.

So I’m guessing the approach will be can be checking if number%10 == 1 and then calling the function with f(n/10), but then I get lost in the actual implementation.

I would appreciate if you could comment what approach would you use, it has to be recursive just because it’s home work, the procedural approach was trivial.

  • 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-10T07:41:58+00:00Added an answer on June 10, 2026 at 7:41 am

    You’ve got two parts to your problem.

    1. Find numbers of ones in number.
    2. Find numbers of ones in all the numbers less than it (but more than zero).

    Part one first:

    If the right-most digit is 1, then number % 10 == 1.
    If the number is > 9 you need to check other digits, you can do this by doing the same test on the number after integer-divide 10. If the number <= 9, then that would give you zero.

    So your OnesInNumber function is a bit like:

    1. If number == 0, return 0.
    2. Otherwise call OnesInNumber on number / 10.
    3. If number % 10 == 1 add 1 to that result.
    4. Return the result.

    This will for example, give you 1 when called on 10, 1, 12, 303212, give you 2 when called on 11, and so on.

    Your OnesInZeroUntil function is then like:

    1. If number <= 0, return 0.
    2. Otherwise call OnesInZeroUntil on number - 1.
    3. Add OnesInNumber(number) to this.
    4. Return the result.

    So you’ve a recursive function that works out the number of 1 in a number, and another recursive function that works out the number of 1 in every number up to that one, building on that first function.

    That’d be enough to write a quick 2 functions in, had you not requested that we don’t.

    (Tip: If your teacher isn’t already requiring it, see if you can work out how to do this without recursion. Every recursive function can be re-written as a non-recursive form, and it’s a practical skill to be able to do that some people teaching recursion don’t seem to cover).

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

Sidebar

Related Questions

I have a function that gives me a number as result: Now to make
I have to make a recursive function that will compute the sum of the
I have a function here that is recursive, but I would instead like to
I have a recursive jQuery function that has something like this: parentItem.find('> div:first-child >
Using this sample : I have make my own Fragment that holds tabhost and
I have a javascript object that I would like to update with data from
I have a javascript function that walks a tree recursively. It has two flag
I am trying to make a recursive ruby function that returns true if a
I have this function that iterates through a directory and is supposed to read
MATLAB is a pass by value language. I have a recursive function that processes

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.