34. Program that inputs starting and ending numbers and display all prime number ending with digit 7 between the given range in descending order.
Chapter 6:
Looping Structures
Programming Exercise
Problem # 34:
Write a program that inputs starting and ending numbers and display all prime number ending with digit 7 between the given range in descending order.
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int c, e_num, s_num, p;
cout<<"Enter starting number:";
cin>>s_num;
cout<<"\nEnter a number upto which prime numbers are required:";
cin>>e_num;
for(;e_num>s_num;e_num--)
{
p=1;
for(c=2;c<e_num;c++)
if(e_num%c==0)
{
p=0;
break;
}
if(p==1&&(e_num%10==7))
cout<<endl<<e_num<<" is a prime number ending with digit 7.";
}
getch();
}
#include<conio.h>
using namespace std;
int main()
{
int c, e_num, s_num, p;
cout<<"Enter starting number:";
cin>>s_num;
cout<<"\nEnter a number upto which prime numbers are required:";
cin>>e_num;
for(;e_num>s_num;e_num--)
{
p=1;
for(c=2;c<e_num;c++)
if(e_num%c==0)
{
p=0;
break;
}
if(p==1&&(e_num%10==7))
cout<<endl<<e_num<<" is a prime number ending with digit 7.";
}
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.
Very helpful and informative
ReplyDelete