Sign in to follow this  

Exercise Hint

This topic is 4534 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
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

This topic is 4534 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this