Public Group

# Exercise Hint

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

## 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 on other sites
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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634058
• Total Posts
3015288
×