0
Find prime no.
using while loop
4 Réponses
+ 2
//check the accepted number is prime number or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,c=1,i;
clrscr();
printf("enter the number= ");
scanf("%d",&n);
i=2;
while(i<=n-1)
{
if(n%i==0)
{
c=0;
}
i++;
}
if(c==0)
{
printf("%d is Not a prime number",n);
}
else
{
printf("%d is Prime number",n);
}
getch();
}
+ 2
#include<iostream.h>
#include<math.h>
int main()
{
int no,flag,x=2;
cout<<"Enter number";
cin>>no;
while(x<=no)
{
int i=2;
flag=0;
while(i<=floor(sqrt(x)))
{
if(x%i==0)
flag=1;
i++;
}
if(flag==0)
cout<<x<<endl;
x++
}
return 0;
}
+ 1
#include <iostream>
using namespace std;
int main() {
int i,n,j,flag;
n=5;
i=2;
flag=1;
while(i<=n)
{
j=2;
while(j<=i-1)
{
if(i%j==0){
flag=0;
break;
}
j++;
}
if(flag){
cout << "your prime number " << i << endl;
}
i++;
}
return 0;
}
0
#include <iostream>
using namespace std;
int isPrime(int no)
{
if(no == 1 || no == 0)
return 0;
int i=2;
while(i<no){
if(no % i == 0)
return 0;
i++;
}
return 1;
}
int main() {
int no;
cin>>no;
if(isPrime(no))
cout<<"prime"<<endl;
else
cout<<"not prime ";
return 0;
}