Write a program that prints out as many primes as required that are larger than
ID: 3547096 • Letter: W
Question
Write a program that prints out as many primes as required that are larger than a value (including starting number) given by a user. Note that the starting number must be positive (i.e. program should accept only values that are greater than 0 otherwise, ask user again to enter a positive number). The program should loop until the user enters an acceptable number. Now, it prints the first prime (strictly) larger than the number. Ask the user if he wants to print the next prime, if so print it out. Repeat until the user does not want any more primes. There is no file generation required for this task. Just send the code.
Explanation / Answer
here's the program
it's also optimised
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,c,k=0,n;
int b[16]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53};
cout<<"Enter boundary number ";
cin>>n;
for(a=2;a<2809;a++)
{
c=0;
for(int i=0;i<16&&i<a;i++)
if(a>b[i]&&a%b[i]==0)
c++;
if(a<n)
if(c==0&&k<n)
{
cout<<a<<" ";
k++;
}
}
getch();
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.