Contoh Program If – Else


Langsung aja Contoh Penerapan if-else

//Nama  :Ah.Handoyo

//kelas :11D Tpl (siang)

//NPM   : 1412110156

#include <conio.h>

#include <stdio.h>

#include <math.h>

void main ()

{

char pilihan;

printf(“|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|\n”);

printf(“|                  MENU RUMUS MATEMATIKA                                                                 |\n”);

printf(“|_____________________________________________________________|\n”);

printf(“|1. Menghitung Luas Segi Tiga (L=1/2.a.t)                                                                |\n”);

printf(“|2. Menghitung Luas Persegi Panjang (LPp=p.l)                                                      |\n”);

printf(“|3. Menghitung Akar Persamaan Kuadrat (Rumus ABC)(Ax^2+Bx+C=0)             |\n”);

printf(“|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|\n”);

printf(“|Nb: Ketikan pada Pilihan Menu dengan Angka                                                      |\n”);

printf(“|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|\n\n”);

printf(“Pilihan Menu [1..3]->”);

pilihan=getch();

printf(“%c\n”,pilihan);

if (pilihan==’1′)

{   float alas, tinggi, luas;

printf(“|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|\n”);

printf(“Alas Segi Tiga   = “); scanf(“%f”,&alas);

printf(“Tinggi Segi Tiga = “); scanf(“%f”,&tinggi);

luas = alas*tinggi/2;

printf(“Luas Segi Tiga adalah     = %10.2f”,luas);

printf(“\n”);

printf(“|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|\n”);

} else if (pilihan ==’2′)

{   float panjang, lebar,luassp;

printf(“|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|\n”);

printf(“Panjang Persegi Panjang = “); scanf(“%f”,&panjang);

printf(“Lebar Persegi Panjang   = “); scanf(“%f”,&lebar);

luassp = panjang*lebar;

printf(“Luas Persegi Panjang adalah      = %10.2f”,luassp);

printf(“\n”);

printf(“|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|\n”);

} else if(pilihan ==’3′)

{   float a, b, c, d, x1, x2;

printf(“|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|\n”);

printf(“Koefisien A = “); scanf(“%f”,&a);

printf(“Koefisien B = “); scanf(“%f”,&b);

printf(“Koefisien C = “); scanf(“%f”,&c);

d = b*b – 4*a*c;

if (d>=0)

{  x1 = (-b – sqrt (d))/(2*a);

x2 = (-b + sqrt (d))/(2*a);

printf(“Nilai x1 = %10.2f\n”,x1);

printf(“Nilai x2 = %10.2f\n”,x2);

}  else if(d<0)

{  printf(“Akar x1 dan x2 Tidak Dapat Dicari”);

printf(“\n”);

printf(“|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|\n”);

}

}

getch();

}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s