Find prime no. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Find prime no.

using while loop

7th Nov 2016, 8:48 AM
Anand
Anand - avatar
4 Answers
+ 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(); }
7th Nov 2016, 9:23 AM
Harish Nandoliya
Harish Nandoliya - avatar
+ 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; }
7th Nov 2016, 9:56 AM
Megatron
Megatron - avatar
+ 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; }
7th Nov 2016, 10:03 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
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; }
7th Nov 2016, 9:21 AM
kamal joshi
kamal joshi - avatar