Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,152 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,872 people online right now. Registration is fast and FREE... Join Now!




Using while loops to find palindromes?

 
Reply to this topicStart new topic

Using while loops to find palindromes?, The assignment requires that the output shows that an input number is

arizonaguy1990
post 4 Oct, 2008 - 04:52 PM
Post #1


New D.I.C Head

*
Joined: 4 Oct, 2008
Posts: 1

The code can only have while loops, though here i have included a function for powers before main, only because i forgot how to use the math library and using power functions (i believe its only double). If i can somehow eliminate the need for this beginning function, and fix the while loops to where the program recognizes all positive integers as palindromes, it would make it run smoother and with more accuracy. * Oh and it has to be in the C language*

Here is the assignment:

-Write a program to check if a given positive integer is a palindrome or not. A palindrome number is one that reads
the same forward and backwards. Your program MUST use while statements only. Call this program
palindrome-number.c.
Example: 12321 is a palindrome number. 123 is not.


CODE

#include <stdio.h>

int power(int a, int b) {
int  i = 0;
int result = a;
for (i = 0; i < b - 1; i++) {
result = result * a;
}
return result;
}

main ()

{
int init;
int x;
int y;
int first;
int last;
int p;

printf("Give me a positive integer:\n");
scanf("%d", &x);

while (x < 0)
{
printf("Not a positive integer, try again:\n");
scanf("%d",&x);
}
init = x;

while (x > 9)

{
p=1;

while (x / power(10,p) > 9)
{
p++;
}
first = x / power(10,p);
last = x%10;
x = x - power(10, p);
x = x / 10;

while (first != last) {
printf("%d is not a palindrome.", init);
break;
}
}
printf("%d is a palindrome.", init);
}



This post has been edited by arizonaguy1990: 4 Oct, 2008 - 08:19 PM
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 4 Oct, 2008 - 08:10 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,012



Thanked 171 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Well here is are two ways you can do it. The first is treating it as a string and the second is using the math.

cpp

#include <iostream>
using namespace std;

bool isPalindrome(int number);

int main() {
char text[20];

cout << "Enter a string: ";
cin >> text;

int start = 0;
int end = strlen(text) - 1;

bool palindrome = true;

// While loop comparing characters at start and end
while (start < end) {
if (text[start] != text[end]) {
palindrome = false;
break;
}

start++;
end--;
}

// Check the flag to see if palindrome was found
if (palindrome == false) {
cout << "Not a palindrome" << endl;
}
else { cout << "Palindrome" << endl; }

return 0;
}


cpp

#include <iostream>
using namespace std;

bool isPalindrome(int number);

int main() {
int anumber;

cout << "Enter an integer: ";
cin >> anumber;

// Call our function to test if it is a palindrome
if (isPalindrome(anumber)) {
cout << "Palindrome" << endl;
}
else { cout << "Not a palindrome" << endl; }


return 0;
}

// Use a while loop to test for a palindrome. Returns true or false.
bool isPalindrome(int number) {

int rev=0,rem;

int num = number;
while (num > 0)
{
rem=num%10;
rev=rev*10+rem;
num /= 10;
}

if (rev == number) { return true; }

return false;
}


So either one will work. It is up to you on how you want to do it. You can use these as a guide to figure out some ways of doing it. Enjoy!

"At DIC we be palindrome tossing code ninjas... here catch skyhawk! Damn man, you dropped it. You suck!" decap.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 01:36PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month