Prime number
Title
Question
How to display prime numbers
Advanced-Cpp Abstract-Class 02-03 min 30-40 sec
Answers:
A meaningful program can be written as follows:
#include<iostream>
using namespace std;int main()
{
int i, chk=0, j;
cout<<"Prime Numbers Between 1 to 100 are:\n";
for(i=1; i<=100; i++)
{
for(j=2; j<i; j++)
{
if(i%j==0)
{
chk++;
break;
}
}
if(chk==0 && i!=1)
cout<<i<<"\n";
chk = 0;
}
return 0;
}
A meaningful program can be written as follows:
#include<iostream>
using namespace std;int main()
{
int i, chk=0, j;
cout<<"Prime Numbers Between 1 to 100 are:\n";
for(i=1; i<=100; i++)
{
for(j=2; j<i; j++)
{
if(i%j==0)
{
chk++;
break;
}
}
if(chk==0 && i!=1)
cout<<i<<"\n";
chk = 0;
}
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the upper limit: ";
cin >> n;
cout << "Prime numbers up to " << n << " are: ";
for (int i = 2; i <= n; i++) {
bool isPrime = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime)
cout << i << " ";
}
return 0;
}
Login to add comment