Chapter 6:
Looping Structures
Programming Exercise
Problem # 35:
Write a program that displays all prime numbers between 100 and 500 that are also palindrome.
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int c, p, digit, num, rev, e_num, s_num;
for(s_num=101;s_num<500;s_num++)
{
p=1;
for(c=2;c<s_num;c++)
{
if(s_num%c==0)
{
p=0;
break;
}
}
num=s_num;
rev=0;
while(num!=0)
{
digit=num%10;
rev=(rev*10)+digit;
num=num/10;
}
if(p==1&&(s_num==rev))
cout<<s_num<<" is a prime number and also a palindrome."<<endl;
}
getch();
}
#include<conio.h>
using namespace std;
int main()
{
int c, p, digit, num, rev, e_num, s_num;
for(s_num=101;s_num<500;s_num++)
{
p=1;
for(c=2;c<s_num;c++)
{
if(s_num%c==0)
{
p=0;
break;
}
}
num=s_num;
rev=0;
while(num!=0)
{
digit=num%10;
rev=(rev*10)+digit;
num=num/10;
}
if(p==1&&(s_num==rev))
cout<<s_num<<" is a prime number and also a palindrome."<<endl;
}
getch();
}
Let me know in the comment section if you have any question.
Previous Post:
33. Program to generate all possible combinations of 1, 2, 3 and 4.
Comments
Post a Comment