0

Whenever I try and run this my compiler shows a random error and a bunch of binary numbers. I have no idea what is wrong. Am I missing something? Its a program to check if a number is a palindrome.

#include <stdio.h>
#include <math.h>

int palindromecheck (int number)
{
   int numbercount = 0;
   int n[100];
   int j;
   int sum = 0;
   int savenumber;
  	savenumber = number;

      for (j = 0; number != 0; ++j)
      {
      	if (number < 10)
         	n[j] = number;
         else
         {
      		n[j] = number % 10;
				number = number/10;
         }
         ++numbercount;
      }
      for (j = 0; numbercount != 0; ++j)
      {
        	sum += n[j]*(pow(10, (numbercount-1)));
         --numbercount;
      }
   if (sum == savenumber)
   	return (1);
   else
   	return (0);
}

main ()
{
float a;

printf("Enter number: ");
scanf("%i", &a);
if (palindromecheck(a))
	printf("palindrome");
else
	printf("not palindrome");

}
2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by Ancient Dragon
0

WOOOOOOOOOOOOOOOOOOOOO JUST FOUND THE PROBLEM.
needed a number = 0 after n[j] = number;

Thanks anyways :DDDDDDDDDDDDDDDDDDDDDDDD

0

A couple problems:
1) line 11: you have to set number to 0 so that the loop will stop

2) line 27: change data type to int because that's what palindromecheck() expects as its parameter.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.