pemrograman c++ : membuat menu dengan “switch case”

program

#include <stdio.h>

main()

{

char pilih;

int nilai,nilai2,hasil=0;

printf(“********menu*********\n”);

printf(” 1.penambahan\n”);

printf(” 2.pengurangan\n”);

printf(” 3.perkalian\n”);

printf(” 4.pembagian\n”);

printf(” 5.sisa bagi\n”);

printf(” 6.selesai\n”);

printf(“**********************\n”);

do

{

printf(“masukkan pilihan: “);

scanf(” %c”,&pilih);

switch (pilih)

{

case ‘1’:

printf(“masukkan bilangan1: “);

scanf(” %d”,&nilai2);

printf(“masukkan bilangan2: “);

scanf(” %d”,&nilai);

hasil=nilai2+nilai;

printf(“hasil: %d\n”,hasil);

break;

case’2′:

printf(“masukkan bilangan1: “);

scanf(” %d”,&nilai2);

printf(“masukkan bilangan2: “);

scanf(” %d”,&nilai);

hasil=nilai2-nilai;

printf(“hasil: %d\n”,hasil);

break;

case’3′:

printf(“masukkan bilangan1: “);

scanf(” %d”,&nilai2);

printf(“masukkan bilangan2: “);

scanf(” %d”,&nilai);

hasil=nilai2*nilai;

printf(“hasil: %d\n”,hasil);

break;

case’4′:

printf(“masukkan bilangan1: “);

scanf(” %d”,&nilai2);

printf(“masukkan bilangan2: “);

scanf(” %d”,&nilai);

if (nilai!=0)

{hasil=nilai2/nilai;

printf(“hasil: %d\n”,hasil);}

else

printf(“error pembagian dengan 0”);

break;

case’5′:

printf(“masukkan bilangan1: “);

scanf(” %d”,&nilai2);

printf(“masukkan bilangan2: “);

scanf(” %d”,&nilai);

if (nilai!=0)

hasil=nilai2%nilai;

else

printf(“error pembagian dengan 0”);

printf(“hasil: %d\n”,hasil);

break;

case’6′:

break;

default:

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

};

}while (pilih!=’6′);

return 0;

}

 

Hasil program

clip_image002

clip_image004

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: