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

  • Home
  • SEARCH
  • 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 3978396
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:02:32+00:00 2026-05-20T05:02:32+00:00

I am trying to create a program that will tell if a number given

  • 0

I am trying to create a program that will tell if a number given to it is a “Happy Number” or not. Finding a happy number requires each digit in the number to be squared, and the result of each digit’s square to be added together.

In Python, you could use something like this:

SQUARE[d] for d in str(n)

But I can’t find how to iterate through each digit in a number in Java. As you can tell, I am new to it, and can’t find an answer in the Java docs.

  • 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-20T05:02:33+00:00Added an answer on May 20, 2026 at 5:02 am

    You can use a modulo 10 operation to get the rightmost number and then divide the number by 10 to get the next number.

    long addSquaresOfDigits(int number) {
        long result = 0;
        int tmp = 0;
        while(number > 0) {
            tmp = number % 10;
            result += tmp * tmp;
            number /= 10;
        }
        return result;
    }
    

    You could also put it in a string and turn that into a char array and iterate through it doing something like Math.pow(charArray[i] - '0', 2.0);

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

Sidebar

Related Questions

I'm trying to create a program that will return a certain number decomposed to
I am trying to create a script that will run a program on each
I am trying to create a program that will do some simple calculations, but
I have been trying to create a Ruby program that will be running online
I am trying to create a java program that will interact with jUDDI to
I'm trying to create a word sorting program that will read the words in
I am trying to create a program that will set up a crontab to
I am trying to create a java program that will take in a directory
I am trying to create a program that will create a vector, generate 100
I am trying to create a Multi-Language WPF program that will load it's languages

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.