OK, looks like I didn't think this through. Doing this in one loop without an array and with no helper functions seems to be trickier than I thought (if it's even possible).
It's still possible to do this in one loop with no array, but with some math functions, but it's a bit more involved than what I had in mind.
Breaking a three-digit integer. Is this a good way?
Well:
It works by the way. :)
#include <iostream>int main(){ int num, i = 0; std::cout << "Enter a number: "; std::cin >> num; do { if( i == 0 ) { i = 1; do { i *= 10; } while( num / i != 0 ); i /= 10; } std::cout << num / i % 10 << " "; i /= 10; } while( i != 0 ); system("pause"); return 0;}
It works by the way. :)
@sheep19: Well done!
BTW, your loops don't need to be nested, you can just write them as two separate loops, one after the other.
BTW, your loops don't need to be nested, you can just write them as two separate loops, one after the other.
Thank you! :)
I know it was meant for the the other exercise, but ok, it's no big deal (although two separated loops would have cleaner syntax).
Thanks to everyone for the help, I really appreciate it!
-------------------------------- ---------------------------------
Ok, now for the next one. Is 4564 palindrom? or only 4554?
Quote:Original post by Gage64
As a similar exercise, write a program that reads in a number and checks to see if it's a palindrom, i.e., reads the same from backwards. For example, 123 is not a palindrom, while 1221 is.
The program should not use an array or any other type of storage container, should not use log10(), and should only use one loop. Once again, it should work for any number.
I know it was meant for the the other exercise, but ok, it's no big deal (although two separated loops would have cleaner syntax).
Thanks to everyone for the help, I really appreciate it!
-------------------------------- ---------------------------------
Ok, now for the next one. Is 4564 palindrom? or only 4554?
Quote:Original post by sheep19
Is 4564 palindrom?
4564 in reverse is 4654 which is not equal to 4564, so it's not a palindrom.
Quote:Original post by sheep19
Ok, now for the next one. Is 4564 palindrom? or only 4554?
A palindrome (with an 'e' on the end by the way) is a sequence that reads the same front to back as it does back to front. So 4564 is not a palindrome, whereas 4554, HANNAH and 2 are all palindromes, although I believe Gage64 asked for an integer only solution.
Ok, here it is:
#include <iostream>int main(){ int num, num2, i = 0; std::cout << "Enter a number: "; std::cin >> num; num2 = num; bool pal = false; do { if( i == 0 ) { i = 1; do { i *= 10; } while( num / i != 0 ); i /= 10; } if( num / i % 10 == num2 % 10 ) pal = true; else pal = false; num2 /= 10; i /= 10; } while( i != 0 ); if( pal ) std::cout << num << " is palindrom." << "\n"; else std::cout << num << " is not palindrom." << "\n"; system("PAUSE"); return 0;}
Quote:Original post by sheep19
Ok, here it is:
*** Source Snippet Removed ***
There's a flaw, if two digits don't compare as equal (i.e. not a palindrome) then the loop continues and pal can potentially be changed back to true. Your program essentially just compares the first and last digits, ignoring all others. Try 1241, it's not a palindrome but your program says it is.
Here's my attempt, don't look if you don't want to know [smile]
[Edited by - dmatter on July 5, 2008 12:36:47 PM]
// scroll down...#include <iostream>bool palindrome(unsigned int number){ static const unsigned int radix = 10; unsigned int forwards = number; unsigned int backwards = 0; while (number > 0) { backwards = (backwards * radix) + (number % radix); number /= radix; } return forwards == backwards;}int main(){ unsigned int number; std::cin >> number; if (!palindrome(number)) std::cout << "Not "; std::cout << "Palindrome" << std::endl;}
[Edited by - dmatter on July 5, 2008 12:36:47 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement