Advertisement
SaisPas

Untitled

Oct 14th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. // Prime.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. // The task is to create a function called isPrime, which accepts an integer as parameter and returns 1 if it is a prime number and 0 it is not
  5. //also there should be a program reading an iteger from the user which shows how many numbers to be entered
  6. //the function has to determine if the entered number is integer
  7. //if so it should output yes otherwise no
  8.  
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. bool isPrime(int x) {
  13.     // In case number is 1 or 2, return true
  14.     if (x == 1 || x == 2)
  15.         return true;
  16.  
  17.     // If it's divisible by i, then not prime
  18.     for (int i = 2; i <= x / 2; i++) {
  19.         if (x % i == 0)
  20.             return false;
  21.     }
  22.     // If loop does finish and we get to here, then it's prime.
  23.     return true;
  24. }
  25.  
  26. int main()
  27. {  
  28.     int n, a;
  29.     //to check for positive number because it cannot loop negative number
  30.     do {
  31.         cout << "Enter number of repetition " << endl;
  32.         cin >> n;
  33.     } while (n < 0);
  34.     //loops how many numbers it will check
  35.     while (n > 0)
  36.     {
  37.         cout << "Enter a number to check: " << endl;
  38.         cin >> a;
  39.         // to call the function and and check if entered number is prime
  40.         if (isPrime(a) == true)
  41.             cout << "Yes" << endl;
  42.         else
  43.             cout << "No" << endl;
  44.         n--;
  45.     }
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement