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