Sabtu, 09 Juni 2012



Berikut ini adalah contoh program yang menggunakan Switch Case untuk program pilihan.


#include<stdio.h>
#include<conio.h>
main()
{
float s,r,a,t,p,l,hasil;
int x;
char nama;
printf("\n\n\n");
printf("====================================================================\n\n");
printf("\t\tPROGRAM UNTUK MENGHITUNG LUAS BANGUN DATAR\n\n\n");
printf("====================================================================\n\n");
menu:
printf("((MENU PILIHAN LUAS BANGUN DATAR))");
printf("\n");;
printf("1. Menghitung Luas Persegi\n");
printf("2. Menghitung Luas Lingkaran\n");
printf("3. Menghitung Luas Luas Segitiga\n");
printf("4. Menghitung Luas Persegi Panjang\n");
printf("5. Exit\n");
printf("\n");;
printf("Pilih Angka (1-2-3-4-5)\n");
scanf("%s",&nama);
switch(nama)
{
float hasil;
case '1' :
{
printf("\n\n");
printf("====================================================================\n\n");
printf("\t\tProgram Menghitung Luas Persegi\n\n");
printf("====================================================================\n\n");
printf("Sisi Persegi = ");
scanf("%f",&s);
printf("\n\n");
hasil=s*s;
printf("====================================================================\n\n");
printf("Rumus Luas Segiempat = Sisi x Sisi\n\n");
printf("%.1f * %.1f = %.1f\n\n",s,s,hasil);
printf("====================================================================\n\n");
printf("Maka Luas Persegi = %.1f\n\n",hasil);
printf("====================================================================\n\n");
printf("\t\t Terima kasih sudah mencoba\n\n");
printf("====================================================================\n\n");
printf("\n\n");
goto menu;
}
case '2' :
{
printf("\n\n");
printf("====================================================================\n\n");
printf("\t\tProgram Menghitung Luas Lingkaran\n\n");
printf("====================================================================\n\n");
printf("Jari-Jari Lingkaran (r) = ");
scanf("%f",&r);
hasil=3.142857*r*r;
printf("\n\n");
printf("====================================================================\n\n");
printf("Rumus Luas Lingkaran = 22/7 x Jari-Jari x Jari-Jari\n\n");
printf("%.2f * %.2f * %.2f = %.2f\n\n",3.142857,r,r,hasil);
printf("====================================================================\n\n");
printf("Maka Luas Lingkaran = %.2f\n\n",hasil);
printf("====================================================================\n\n");
printf("\t\t Terima kasih sudah mencoba\n\n");
printf("====================================================================\n\n");
printf("\n\n");
goto menu;
}
case '3' :
{
printf("\n\n");
printf("====================================================================\n\n");
printf("\t\tProgram Menghitung Luas Segitiga\n\n");
printf("====================================================================\n\n");
printf("Alas Segitiga = ");
scanf("%f",&a);
printf("\n\n");
printf("Tinggi Segitiga = ");
scanf("%f",&t);
printf("\n\n");
hasil=a*t/2;
printf("====================================================================\n\n");
printf("Rumus Luas Segitiga = 1/2 x Alas x Tinggi\n\n");
printf("%.1f * %.1f * %.1f = %.1f\n\n",0.5,a,t,hasil);
printf("====================================================================\n\n");
printf("Maka Luas Segitiga = %.1f\n\n",hasil);
printf("====================================================================\n\n");
printf("\t\t Terima kasih sudah mencoba\n\n");
printf("====================================================================\n\n");
printf("\n\n");
goto menu;
}
case '4' :
{
printf("\n\n");
printf("====================================================================\n\n");
printf("\t\tProgram Menghitung Luas Persegi Panjang\n\n");
printf("====================================================================\n\n");
printf("Panjang = ");
scanf("%f",&p);
printf("\n\n");
printf("Lebar = ");
scanf("%f",&l);
printf("\n\n");
hasil=p*l;
printf("====================================================================\n\n");
printf("Rumus Luas Segiempat = Panjang x Lebar\n\n");
printf("%.1f * %.1f = %.1f\n\n",p,l,hasil);
printf("====================================================================\n\n");
printf("Maka Luas Segi Empat = %.1f\n",hasil);
printf("====================================================================\n\n");
printf("\t\t Terima kasih sudah mencoba\n\n");
printf("====================================================================\n\n");
printf("\n\n");
goto menu;
}
case '5' :
printf("exit\n");
break;
getch();
}
}


Berikut adalah contoh program untuk mencari nilai faktorial dalam bahasa C.


#include<stdio.h>
#include<conio.h>
main ()
{
int n,i,fak;
printf("\n\n\n");
printf("====================================================================\n\n");
printf("\t\tPROGRAM UNTUK MENGHITUNG NILAI FAKTORIAL\n\n\n");
printf("====================================================================\n\n");
printf ("\tSilahkan Masukan Nilai/Angka : "); scanf ("%d", &n);
printf("\n\n");
fak=1;
for (i=n;i>0;i--)
fak*=i;
printf ("\tMaka Bilangan Faktorialnya adalah : %d\n ",fak);
printf("\n\n");
printf("====================================================================\n\n");
printf("\t\t Terima kasih sudah mencoba\n\n");
printf("====================================================================\n\n");
printf("\n\n\n\n");
getch();
}


Berikut adalah contoh program dengan main program dan fungsi program dalam bahasa C. Terdapat program fungsi untuk garis, penjumlahan, pengurangan, perkalian dan pembagian.


#include<stdio.h>
float a,b,c;
float tambah(float a,float b)
{
c=a+b;
return(c);
}
float kurang(float a,float b)
{
c=a-b;
return(c);
}
float kali(float a,float b)
{
c=a*b;
return (c);
}
float bagi(float a,float b)
{
c=a/b;
return(c);
}
void garis(){
printf("====================================================================\n\n");
}
main()
{
char nama;
menu:
system("cls");
printf("\n\n");
garis();
printf("\t\tPROGRAM OPERASI HITUNG MATEMATIKA\n\n\n");
garis();
printf("\t\t((SISTEM OPERASI PERHITUNGAN))\n\n\a");
printf("\t1. Penjumlahan\n");
printf("\t2. Pengurangan\n");
printf("\t3. Perkalian\n");
printf("\t4. Pembagian\n");
printf("\t5. Exit\n\n");
garis();
printf("\tPilih Angka (1-2-3-4-5)\n");
scanf("%s",&nama);
printf("\n\n");
switch(nama)
{
case '1':
{
garis();
printf("\t\tProgram Operasi Penjumlahan\n\n");
garis();
printf("\tMasukan nilai a = ");
scanf("%f",&a);
printf("\tMasukan nilai b = ");
scanf("%f",&b);
printf("\tMaka nilai a + b = %.0f\n\n\a",tambah(a,b));
garis();
printf("\t\t Terima kasih sudah mencoba\n\n");
garis();
printf("\tTekan (1) Kembali ke Menu atau (0) Exit\n",c);
scanf("%f",&c);
if(c==1)
goto menu;
else
goto exit;
}
case '2':
{
garis();
printf("\t\tProgram Operasi Pengurangan\n\n");
garis();
printf("\tMasukan nilai a = ");
scanf("%f",&a);
printf("\tMasukan nilai b = ");
scanf("%f",&b);
printf("\tMaka nilai a - b = %.0f\n\n\a",kurang(a,b));
garis();
printf("\t\t Terima kasih sudah mencoba\n\n");
garis();
printf("\tTekan (1) Kembali Menu atau (0) Exit\n",c);
scanf("%f",&c);
if(c==1)
goto menu;
else
goto exit;
}
case '3':
{
garis();
printf("\t\tProgram Operasi Perkalian\n\n");
garis();
printf("\tMasukan nilai a = ");
scanf("%f",&a);
printf("\tMasukan nilai b = ");
scanf("%f",&b);
printf("\tMaka nilai a x b = %.0f\n\n\a",kali(a,b));
garis();
printf("\t\t Terima kasih sudah mencoba\n\n");
garis();
printf("\tTekan (1) Kembali ke Menu atau (0) Exit\n",c);
scanf("%f",&c);
if(c==1)
goto menu;
else
goto exit;
}
case '4':
{
garis();
printf("\t\tProgram Operasi Pembagian\n\n");
garis();
printf("\tMasukan nilai a = ");
scanf("%f",&a);
printf("\tMasukan nilai b = ");
scanf("%f",&b);
printf("\tMaka nilai a / b = %.2f\n\n\a",bagi(a,b));
garis();
printf("\t\t Terima kasih sudah mencoba\n\n");
garis();
printf("\tTekan (1) Kembali ke Menu atau (0) Exit\n",c);
scanf("%f",&c);
if(c==1)
goto menu;
else
goto exit;
}
exit:
case '5' :
printf("exit\a");
break;
}
}




Berikut adalah program array dalam bahasa C untuk program penjumlahan secara diagonal.

#include<stdio.h>
void garis()

{
printf("====================================================================\n\n");
}
main()
{
garis();
printf("\tPROGRAM PENJUMLAHAN SECARA DIAGONAL MATRIX ORDO 3 X 3\n\n\n");
garis();
int i,j,sum=0;
int A[3][3]={3,1,1,2,6,8,4,3,3};
for(i=0;i<3;i++)
printf("\t\t|\t"); 
for(j=0;j<3;j++)
printf("%d\t\a",A[i][j]); 
printf("|\n\n"); 
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if (i==j) sum+=A[i][j]; 
printf ("\t\tJumlah diagonalnya adalah = %d\n\n",sum); 
printf ("\t\t[1][1] + [2][2] + [3][3] = %d\n\n",sum); 
garis(); 
printf("\t\t Terima kasih sudah mencoba\n\n"); 
garis(); 
getch(); 
}


Berikut adalah salah satu program array dalam bahasa C untuk mencari bilangan genap di dalam sebuah matrix.


#include<stdio.h>
void garis()

{
printf("====================================================================\n\n");
}
main ()
{
garis();
printf("\tPROGRAM MENCARI BILANGAN GENAP DALAM MATRIX\n\n\n");
garis();
int i,j,genap=0,jumlah=0,A[3][3]={1,2,3,4,5,6,7,8,9};
for (i=0;i< 3;i++) 

printf ("\t\t|\t"); 
for (j=0;j< 3;j++) 
printf ("%d\t",A[i][j]); 
printf ("|\n\n"); 
}
for (i=0;i< 3;i++) 
{
for (j=0;j< 3;j++) 
{
if (A[i][j]%2==0) 
{
genap = genap + 1;
jumlah += A[i][j]; 
}
printf ("\tBanyaknya nilai genap dari matrik diatas adalah = %d\n\n",genap); 
printf ("\tMaka penjumlahan dari nilai genap adalah = %d\n\n",jumlah); 
garis(); 
printf("\t\t Terima kasih sudah mencoba\n\n"); 
garis(); 
getch(); 
}
 
Twitter Facebook Dribbble Tumblr Last FM Flickr Behance