Lompat ke konten Lompat ke sidebar Lompat ke footer

PROGRAM C PERULANGAN - Program Menampilkan Bilangan Prima


Contoh Kasus : 
Bantulah Nadia untuk membuat program menampilkan n deret bilangan prima.

Masukan : Sebuah angka bulat
Keluaran : n deret bilangan prima

Test Case

Masukan :
5
Keluaran :
2 3 5 7 11

Program 1

[code hl="1, 4, 7"] #include<stdio.h> int main(){ int byk,i,count=1,angka=2; scanf("%d",&byk); printf("%d ",angka); while(count<byk){ angka++; for(i=2;i<angka;i++){ if (angka%i==0){ break; } else{ if (i==angka-1){ printf("%d ",angka); count++; break; } } } } return 0; } [/code]

Program 2

[code hl="1, 4, 7"] #include<stdio.h> int main() { //deklarasi int angka,i,prima; //penginputan scanf("%d",&angka); prima = 0; //proses perulangan for(i=1;i<=angka+1;i++){ if(i == 1){ prima = i+1; } else if(i>= 1){ prima=i*2-1; if(prima>3 && prima%3==0) {continue;} } printf("%d ",prima); } } [/code]

Program 3

[code hl="1, 4, 7"] #include <stdio.h> int main() { int n, prima = 1, index = 0; scanf("%d", &n); if (n == 1) { printf("2"); } for (int i = 2; i < n * n; i++) { prima = 1; for (int j = 2; j < i; j++) { if (i % j == 0) { prima = 0; } } if (prima) { printf("%d ", i); index++; if (index == n) { break; } } } return 0; } [/code]

Program 4

[code hl="1, 4, 7"] #include <stdio.h> int main() { int n, prima = 1, index = 0; scanf("%d", &n); if (n == 1) { printf("2"); } for (int i = 2; i < n * n; i++) { prima = 1; for (int j = 2; j < i; j++) { if (i % j == 0) { prima = 0; } } if (prima) { printf("%d ", i); index++; if (index == n) { break; } } } return 0; } [/code]

Program 5

[code hl="1, 4, 7"] #include <stdio.h> int main (){ int prim; int batas=1; int n=1; printf("Input Banyak Bilangan Prima yang diinginkan = "); scanf("%d",&prim); while(batas != (prim+1)){ if(n==2){ batas +=1; printf("%d ",n); }else if(n%2==1 && n > 1){ int benar = 0; for(int i = 2;i<n;i++){ if(n%i==0){ benar=1; break; } } if(benar == 0){ batas+=1; printf("%d ",n); } } n ++; } return 0; } [/code]

Student Terpelajar
Student Terpelajar Content Creator, Video Creator and Writer

Posting Komentar untuk "PROGRAM C PERULANGAN - Program Menampilkan Bilangan Prima"