Jump to content
  • Advertisement
Sign in to follow this  
mengha

Exercise Hint

This topic is 4749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im working though a book on c++ and currently have got to the question: Write a program that read a decimal digit d and prints a table with two columns: one for positige integers x, less than 100, and one for their squares x2. Only those lines in which the digit d occurs both in x and x2 are to be printed. For example, if d is equal to 2, the table will contain the line: 82 6724 since 82^2 = 6724 and 2 occurs both in 82 and in 6724. The way I completed it is by writing a funtion that turns the number into a char array checks for the number and then prints the table depending on the result. Is there an easier/better way of doing this though?

Share this post


Link to post
Share on other sites
Advertisement
I think you got the easiest solution there is, because it's quite ugly when you use modulo or div. You could try something like this:


void FindDigitInNumber(int big, int digit)
{
int remainder = big;
while(remainder > 0)
{
int mod = remainder % 10;
if (mod == digit)
return true;

remainder = remainder / 10;
}

return false;
}


Of course, it is very much similar to this what atoi does.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!