Advertisement
agusbd

ajik

Mar 13th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void deretprima(int);
  5. int deret_prima(int);
  6. int main(){
  7. int x;
  8. cout<<"masukan jumlah deret ";cin>>x;
  9. deretprima(x);
  10. cout<<endl<<"S = "<<deret_prima(x);
  11.  
  12.  
  13. }
  14.  
  15.  
  16.  
  17.  
  18. int deret_prima(int x){
  19.  
  20.  if(x==1)
  21.     return 2;
  22.  
  23.  int jumlah_deret=0,jumlah_mod;
  24.  int i=2;
  25.  bool kondisi;
  26. while(jumlah_deret<x){
  27.     jumlah_mod=0;
  28.     for(int j=1;j<=i;j++){
  29.  
  30.         if (i%j==0)
  31.             jumlah_mod++;
  32.     }
  33.  
  34.     if(jumlah_mod<=2)
  35.         {kondisi=true;}
  36.     else
  37.         {kondisi = false;}
  38.  
  39.     if(kondisi){
  40.         jumlah_deret++;
  41.  
  42.  
  43.  }
  44.  
  45. i++;
  46.  
  47.  
  48. }
  49. return i-1+deret_prima(x-1);
  50. }
  51.  
  52. void deretprima(int x){
  53.  
  54. int jumlah_deret=0,jumlah_mod;
  55.  int i=2;
  56.  bool kondisi;
  57. while(jumlah_deret<x){
  58.     jumlah_mod=0;
  59.  
  60.     for(int j=1;j<=i;j++){
  61.  
  62.         if (i%j==0)
  63.             jumlah_mod++;
  64.     }
  65.  
  66.     if(jumlah_mod<=2)
  67.         {kondisi=true;}
  68.     else
  69.         {kondisi = false;}
  70.  
  71.     if(kondisi){
  72.         jumlah_deret++;
  73.         cout<<i<<" ";
  74.  }
  75.    i++;
  76.  
  77. }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement