Вы находитесь на странице: 1из 5

(COM612102) ALGORITMA DAN PEMROGRAMAN

TUMING 4

BUDI SAFTA NUGRAHA | 1517051133


email: budi_safta@outlook.co.id

PROGRAM STUDI S1 ILMU KOMPUTER


JURUSAN ILMU KOMPUTER
Universitas Lampung, Jln. Prof. Dr. Sumantri Brojonegoro No. 1 35145, Indonesia

Question
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)

Write a program that presents the user w/ a choice of your 5 favorite beverages (Coke, Water,
Sprite, ... , Whatever).
Then allow the user to choose a beverage by entering a number 1-5.
Output which beverage they chose.

If you program uses if statements instead of a switch statement, modify it to use a switch
statement.
If instead your program uses a switch statement, modify it to use if/else-if statements.

Modify the program so that if the user enters a choice other than 1-5 then it will output "Error.
choice was not valid, here is your money back."

Budi Safta Nugraha, Ilmu Komputer, Universitas Lampung


Source Code C++

/*
* Hello.cpp
*
* Created on: 17 Oktober 2015
* Author: Budi Safta Nugraha | 1517051133
*/
#include <iostream>

using namespace std;

int main(){

int Minuman;
cout<<"======PILIH MINUMAN KESUKAAN ANDA=======\n\n";
cout<<"Masukan angka Minuman : ";
cin>>Minuman;
switch(Minuman)
{
case 1:cout<<"Cocacola";break;
case 2:cout<<"Nestea";break;
case 3:cout<<"Cimory";break;
case 4:cout<<"Milo";break;
case 5:cout<<"Rootbeer";break;
default :cout<<"Error!!!choice was not valid, here is your money back ";break;

return 0;
}

return 0;
}

Budi Safta Nugraha, Ilmu Komputer, Universitas Lampung


Screenshoot Output Program

Budi Safta Nugraha, Ilmu Komputer, Universitas Lampung


Modify the program to if-else
Source Code C++

/*
* Hello.cpp
*
* Created on: 17 Oktober 2015
* Author: Budi Safta Nugraha | 1517051133
*/
#include <iostream>

using namespace std;

int main(){

int x;

cout<<"Pilih Minuman Anda: ";cin>>x;


if(x==1){
cout<<"Minuman yang anda pilih adalah CocaCola\n";
}
else
if(x==2){
cout<<"Minuman yang anda pilih adalah Nestea\n";
}
else
if(x==3){
cout<<"Minuman yang anda pilih adalah Cimory\n";
}
else
if(x==4){
cout<<"Minuman yang anda pilih adalah Milo\n";
}
else
if(x==5){
cout<<"Minuman yang anda pilih adalah Rootbeer\n";
}
else
cout<<"Error!!!choice was not valid, here is your money back\n\n ";

return 0;
}

Budi Safta Nugraha, Ilmu Komputer, Universitas Lampung


Screenshoot Output Program

Budi Safta Nugraha, Ilmu Komputer, Universitas Lampung

Вам также может понравиться