mencari bilangan prima Bahasa C

#include

main()
{
            int bilangan;
            int prima;
            int cekprima();         

            clrscr();
            printf("======================\n");
            printf("MENCARI BILANGAN PRIMA\n");
            printf("======================\n\n");
            printf("Masukkan sebuah bilangan integer : ");
            scanf("%d",&bilangan);
            prima = cekprima(bilangan);
            if (prima == 1)
            {
                        printf("Bilangan %d adalah bilangan prima",bilangan);
            } else {
                        printf("Bilangan %d bukan bilangan prima",bilangan);
            }
}

int cekprima(bil)
int bil;
{
            int bagi=3;
            int batas;
            if (bil == 1)
            {
                        return(0);
            } else if (bil==2||bil==3) {
                        return(1);
            } else if (bil % 2 == 0) {
                        return(0);
            } else {
                        while (batas > bagi)
                        {
                                    if (bil % bagi == 0)
                                    {
                                                printf("Karena habis dibagi %d\n",bagi);
                                                return(0);
                                                break;
                                    }
                                    batas = bil / bagi;
                                    bagi += 2;                 
                        }
                        return(1);
            }
}




II. Mencari bilangan prima dalam rentang 1-1000

#include

main()
{
            int i;
            int prima;
            int cekprima();         

            clrscr();
            printf("======================\n");
            printf("MENCARI BILANGAN PRIMA\n");
            printf("DALAM RENTANG 1-1000\n");
            printf("======================\n\n");
            printf("Bilangan prima yang ditemukan : \n");
            for (i=1;i<=1000;i++)
            {
                        prima = cekprima(i);
                        if (prima == 1)
                        {
                                    printf(" %d",i);
                        }
            }
}

int cekprima(bil)
int bil;
{
            int bagi=3;
            int batas;
            if (bil == 1)
            {
                        return(0);
            } else if (bil==2||bil==3) {
                        return(1);
            } else if (bil % 2 == 0) {
                        return(0);
            } else {
                        while (batas > bagi)
                        {
                                    if (bil % bagi == 0)
                                    {
                                                return(0);
                                                break;
                                    }
                                    batas = bil / bagi;
                                    bagi += 2;                 
                        }
                        return(1);
            }
}
   



Komentar

Postingan populer dari blog ini

Memperbaiki Flashdisk Tidak bisa di Copy

Cara mudah setting DHCP server di Mikrotik

Kumpulan Aplikasi nokia 9300 s80