Hi everyone, I have wirte a programme to display the Palindromic prime, i.e 2,3,5,7,11,101,131,151..........
but I don't know what's wrong with my code, it can only display the number up to 11 and then no response. Could yuou help me to check out what's wrong? thank you so much.
Here is the code:
-----------------------------------------------------------------------------------------

#include <iostream>
#include <iomanip>
using namespace std;



int main()
{
	int a;
	cin>>a;
	int prime=2;
	int i=1;
	int start;
	int end;
	

	while(i<=a)
	{
		bool isprime = true;
		start=prime;
		while(start>=10)
		{
			start=prime/10;
		}
		end=prime%10;
		cout<<"("<<start<<")"<<"("<<end<<")"<<endl;

		for(int j=2; j<prime; j++)
	{
		if((prime%j==0)||(start!=end))
		{
			isprime = false;
			break;
		}
	}

		if(isprime)
		{
			if(i%10==0)
			{
			cout<<setw(8)<<prime<<endl;
		    
		}
			else
		{
			cout<<setw(8)<<prime;
			}
			i++;
		}
		
		prime++;
		
	}
	cout<<endl;
	system("pause");
	return 0;
}

Recommended Answers

All 3 Replies

Use following program

int a = 100;
            int prime=2;
            int i=2;
            int start;
            int end;
            while(i<=a)
            {
                bool isprime = true;
                start=prime;
                end=prime%10;
                for(int j=2; j<i; j++)
                {
                    if((i%j==0))
                    {
                        isprime = false;
                        break;
                    }
                }
                if (isprime)
                {
                    prime = i;
                    Console.WriteLine(prime.ToString() + " - Prime");
                }
                
                i++;
                
            }
commented: How does someone learn if you do their homework for them. You've been here long enough to now we frown on this. -2

Console.WriteLine(prime.ToString() + " - Prime"); You left your C# hat on... :) Only kidding.

He/She also needs to check the numbers to see if they are palindromes too. OP was trying to just check the first and last digits but for a 4+ digit number this will probably yield incorrect results. If possible use a stringstream to parse the number and work your way to the middle of the string (or work your way through the number by division and compare the digits that way--which might get tricky for large numbers-- if you have an upper bound you could create an array with length # of digits).

Shows the next prime palindromic number to the number you input.

#include<iostream.h>
#include<conio.h>
#include<process.h>

void main()
{
clrscr();
unsigned long a,b[10],z=10;
int i=0,j=0,k=0,m=0,c=1,g=2,f=6;
cin>>a;
if (a==1)
{
  cout<<a;
  cout<<"\nnot prime";
  getch();
  exit(0);
}
b:while(k==0)
{
  b[i]=((a+j)%z);
  if (b[i]==(a+j))
  {
    if(c==2)
    {
      goto a;
    }
    c++;
  }
  b[i]=b[i]/(z/10);
  i++;
  z=z*10;
}
a:for(m=0;m<i;m++)
  {
    if (b[m]!=b[i-m-1])
    {
      j++;
      i=0;
      c=1;
      g=1;
      z=10;
      goto b;
    }
    else if(b[m]==b[i-m-1])
    {
       g=0;
       continue;
    }
  }
if(g==0)
{
  for(int l=2;l<b[i]/2;l++)
  {
    if(b[i]%l==0)
    {
      j++;
      i=0;
      c=1;
      g=1;
      z=10;
      goto b;
    }
    else
    {
      f=1;
      continue;
    }
  }
}
c:if (f==1)
  {
    cout<<endl<<"no. is prime and palindromic\n";
  }
cout<<b[i];
getch();
}
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.