13. Program that inputs a number from the user and displays all prime numbers which are less that the input number using any loop.
Chapter 6:
Looping Structures
Programming Exercise
Problem # 13:
Write a program that inputs a number from the user and displays all prime numbers which are less that the input number using any loop.
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int c, n, num, p;
cout<<"Enter a number upto which prime numbers are required:";
cin>>n;
for(num=2;num<n;num++)
{
p=1;
for(c=2;c<=num/2;c++)
if(num%c==0)
{
p=0;
break;
}
if(p==1)
cout<<num<<" ";
}
return 0;
}
#include<conio.h>
using namespace std;
int main()
{
int c, n, num, p;
cout<<"Enter a number upto which prime numbers are required:";
cin>>n;
for(num=2;num<n;num++)
{
p=1;
for(c=2;c<=num/2;c++)
if(num%c==0)
{
p=0;
break;
}
if(p==1)
cout<<num<<" ";
}
return 0;
}
Let me know in the comment section if you have any question.
Previous Post:
12. Program that calculates and prints the average of several integers. Assume that the last value read is sentinel 9999.
Comments
Post a Comment