zein69

prime

Nov 17th, 2023
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool cek(int i)
  5. {
  6.     if(i <= 1){
  7.         return false;
  8.     };
  9.     for (int j = 2; j <= i/2; ++j) {
  10.         if(i % j == 0){
  11.             return false;
  12.         };
  13.     };
  14.     return true;
  15. }
  16.  
  17. int main() {
  18.     int input, inc = 1;
  19.     cout<<"Masukkan Nomor : ";
  20.     cin>>input;
  21.     if(input <= 0){//a3
  22.         cout<<"Bilangan Tidak Valid";//ya
  23.     }else{//tidak
  24.         do{
  25.             for (int i = 1; i <= input; i++) {
  26.                 inc++;
  27.                 if(cek(i)){
  28.                     cout<<i<<" ";
  29.                 }else{
  30.                     input++;
  31.                 };
  32.             }
  33.         }
  34.         while(inc <= input);
  35.         cout<<"\nBilangan diatas adalah bilangan prima";
  36.     };
  37.     return 0;
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment