Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int isPrime(int N)
- {
- if(N == 1) return 0;
- if( N == 2) return 1;
- for(int i = 2; i*i <= N; ++i)
- {
- if(N%i == 0)
- return 0;
- }
- return 1;
- }
- void displayPrime(int l, int r)
- {
- for(int i = l; i <= r; ++i)
- {
- if(isPrime(i))
- cout << i << " ";
- }
- }
- int main()
- {
- printf("Prime Numbers\n");
- printf("Please choose what you want\n");
- printf("1. Checking if a number is Prime\n");
- printf("2. Printing all prime numbers of given range\n");
- int x, N, l, r;
- cin >> x;
- if(x == 1){
- cout << "Enter a number: ";
- cin >> N;
- if(isPrime(N)) cout << "The number is Prime" << endl;
- else cout << "The number is non-Prime" << endl;
- }
- else if(x == 2) {
- cout << "Enter starting number: ";
- cin >> l;
- cout << "Enter ending number: ";
- cin >> r;
- displayPrime(l, r);
- }
- else cout << "Invalid. Please choose option 1 or 2." << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement