Member Avatar for GARO

This is what I have so far. It's wrong in a bunch of ways. I can't seem to find whats wrong. It worked well on paper though :D So... I need some help. Can't use strings. It's giving me wrong palindromes. The numbers have to be prime and palindrome.

void makePalindrome(int prime, int count)
{
  int x = prime;
  int revPrime = prime;
  int n = prime;
      while(n>0)
      {
         revPrime = (prime%10) * pow(10.0, count-1) + (prime/10);
        prime = revPrime;
         n = n/10;    
        
      } if(revPrime == x)
        {
           cout << x << " <-prime palindromes-> " << revPrime << endl;
        }

}

Recommended Answers

All 2 Replies

i dont understand your logic but here i am giiving mine.

this is not so efficient but will do the same work.

#include <cstdlib>
#include <iostream>
#include<cstdio>
using namespace std;
int prime (int num);
int pal(int num);
void makepals(int num);
int Pals[100000];
int cnt =0;

int main(int argc, char *argv[])
{
double num;
int i;
FILE *fp;
for (num =0; ( int )num < 1000; num++)
    makepals((int)num);
  fp= fopen ("primes.txt","w");// 106 th prime 
  
    fprintf(fp,"%s=%d\n\n","COUNT",cnt);
for (i=0; i<cnt;i++)
   // cout <<"Pals["<< i <<"] = " << Pals[i]<<endl;
  fprintf(fp,"%d\n",Pals[i]);
  fclose(fp);
     system("PAUSE");
    return EXIT_SUCCESS;
}

int prime (int num)
{
    int i;
    for( i= 2; i < num; i++ )
        if(num %i == 0 )
             break;
             
     if ( i == num )
          return 1;
     else
         return 0;
}

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

if (temp == rev)
    return 1;
else
    return 0;
}  

void makepals(int num)
{
 
 if(prime( num ))
   if (pal(num ))
       Pals[cnt++]=num;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.