Case study
Menu Pilihan
-------------
1. Hitung luas lingkaran
2. Hitung luas persegi
3. Hitung luas segitiga
4. Exit
-------------
Pilihan anda (1/2/3/4) :
Misalnya pilihan 1
menghitung luas lingkaran
masukan jari-jari :
Luas Lingkaan :
There is a saying "if you don't know, you won't love". Therefore, before we step into the completion stage, it would be good if we first get to know the 3 basic concepts that we will later use to form the program above:
A. Repetition
In C++, loops are used to execute one or more commands, and are performed repeatedly for certain conditions.
In general, repetition consists of 3 components, in other words it has 3 characteristics:
- Initial Value, which serves as initialization.
- Value Modifier, to determine how many times the loop will be executed.
- Condition, a statement/specific condition to make a decision on program execution.
There are several types of loops that we can use in C++ programming, including the following:
- for
- do...while
- while
Meanwhile, for the case study above, I will use while, so that the program will be executed during a certain while, the way to write it is as follows:
while ( syarat ) {
pernyataan ke-n;
Perubah Nilai;
}
Example:
while (i <= 10) {
if (i%2 == 0) {
cout << i << " ";
i++;
}
}
B. Branching
In C++ branching is used to solve problems and make decisions from several statements. There are several types of branches that we can use in C++ programming, including the following:
- if
- if -- else
- switch -- case
For the case study above, I will use if -- else and switch -- case, so that it contains the meaning "if the condition is true, then do statement 1, otherwise do statement 2 if statement 1 does not meet the requirements". The way to write it is as follows:
if (kondisi) {
pernyataan 1;
}
else {
pernyataan 2;
}
Example:
if ( tot_beli >= 50000 ) {
potongan = 0.2 * tot_beli;
}
else {
potongan = 0.05 * tot_beli;
}
While switch -- case is essentially the same as if -- else, but switch -- case can only check data of type char and int, other than that cannot. Here's how to write it:
switch (syarat) {
case kontanta-1:
pernyataan 1;
break;
case konstanta-2:
perintah 2;
break;
default:
perintah;
}
Example:
switch (Hari) {
case 1:
cout << "Ahad";
break;
case 2:
cout << "Senin";
break;
default:
cout << "Selasa";
}
C. Keyword
In C++ programming, keywords are used to form certain functions, while the way they are declared varies, because there are some keywords that need #include <file> so that they can be recognized by the compiler, but there are also some that don't need to.
There are several types of keywords that we can use in C++ programming, including the following:
- break
- continue
- goto
- gotoxy
- exit
However, since we are encouraged to refer to structured programming, we try to avoid goto as far as possible. Meanwhile, for the case study above, I will try to use break and exit;
Break
Used to exit a statement either in the form of checking or looping, but most often used in switch -- case branching, so break is used to exit the case. How to write can be seen in the example of the switch -- case structure.
Exit
It is a statement that functions to exit the program and this statement requires an #include <stdlib.h> in the header, so that the statement can be read by the compiler. Here's how to write it:
exit(7); then it means that the exit function is given the value 7, so if we enter the value 7 then the program will exit.
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
void main() {
pernyataan1;
pernyataan1;
exit(7);
}
Solution:
Using if-else
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
/**-------------------------
NIM : 12131249
Blog : bundet.com
---------------------------**/
void main() {
unsigned int pilihan;
cout << "MENU PILIHAN" << endl;
cout << "------------" << endl;
cout << "1. hitung luas lingkaran" << endl;
cout << "2. hitung luas persegi" << endl;
cout << "3. hitung luas segitiga" << endl;
cout << "4. exit" << endl;
cout << "------------" << endl;
while (pilihan>=0){
cout << "pilihan anda (1/2/3/4) : "; cin >> pilihan;
if (pilihan== 1) {
cout << "-------------------------------------"<< endl;
cout << "Menghitung Luas Lingkaran" << endl;
float r;
cout << "Masukan jari-jari : "; cin >> r;
float Ll = 3.14*r*r;
cout << "Luas Lingkaran : " << Ll << endl;
cout << "-------------------------------------"<< endl;
}
else if (pilihan== 2){
cout << "-------------------------------------"<< endl;
cout << "Menghitung Luas Persegi" << endl;
float p;
cout << "Masukan Panjang : "; cin >> p;
float l;
cout << "Masukan Lebar : "; cin >> l;
float Lp = p*l;
cout << "Luas Persegi : " << Lp << endl;
cout << "-------------------------------------"<< endl;
}
else if (pilihan== 3){
cout << "-------------------------------------"<< endl;
cout << "Menghitung Luas Segitiga" << endl;
float a;
cout << "Masukan Alas : "; cin >> a;
float t;
cout << "Masukan Tinggi : "; cin >> t;
float Ls = 0.5*a*t;
cout << "Luas Segitiga : " << Ls << endl;
cout << "-------------------------------------"<< endl;
}
else if (pilihan==4) {
exit(4);
}
else {
cout << "Kode Yang Anda Masukan Salah...!!!"<< endl;
}
}
getch();
}
Using switch-case
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
/**-------------------------
NIM : 12131249
Blog : bundet.com
---------------------------**/
void main() {
unsigned int pilihan;
float r, Ll, p, l, Lp, a, t, Ls;
cout << "MENU PILIHAN" << endl;
cout << "------------" << endl;
cout << "1. hitung luas lingkaran" << endl;
cout << "2. hitung luas persegi" << endl;
cout << "3. hitung luas segitiga" << endl;
cout << "4. exit" << endl;
cout << "------------" << endl;
while (pilihan>=0){
cout << "pilihan anda (1/2/3/4) : "; cin >> pilihan;
switch(pilihan) {
case 1:
cout << "-------------------------------------"<< endl;
cout << "Menghitung Luas Lingkaran" << endl;
cout << "Masukan jari-jari : "; cin >> r;
Ll = 3.14*r*r;
cout << "Luas Lingkaran : " << Ll << endl;
cout << "-------------------------------------"<< endl;
break;
case 2:
cout << "-------------------------------------"<< endl;
cout << "Menghitung Luas Persegi" << endl;
cout << "Masukan Panjang : "; cin >> p;
cout << "Masukan Lebar : "; cin >> l;
Lp = p*l;
cout << "Luas Persegi : " << Lp << endl;
cout << "-------------------------------------"<< endl;
break;
case 3:
cout << "-------------------------------------"<< endl;
cout << "Menghitung Luas Segitiga" << endl;
cout << "Masukan Alas : "; cin >> a;
cout << "Masukan Tinggi : "; cin >> t;
Ls = 0.5*a*t;
cout << "Luas Segitiga : " << Ls << endl;
cout << "-------------------------------------"<< endl;
break;
case 4:
exit(4);
break;
default:
cout << "Kode Yang Anda Masukan Salah...!!!"<< endl;
}
}
getch();
}
Hope it is useful!
Netizens
Question 1
IBNU CAHYADI17 Dec 2014, 21:26:00 = I'm getting more confused, my assignment never gets finished, bro...., please complete the tutorial, I mean the script, because I found an error in #include
CHARTEN T Aug 31 2015, 18:14:00 = thanks
ILHAM NUZULI 23 Sep 2015, 11:22:00 = Sir, is that from Notepad++?
CIPU CIMOT Sep 23 2015, 17:55:00 = if you use codeblocks include, why is there an error, bro?
JAFAR DJAWAS 5 Oct 2015, 08:51:00 = Is it the same if I use dev c++, sir?
PERLIN HAREFA 23 Oct 2015, 01:00:00 = how to make a program if the program content is selling
1.pen >>price, quantity, total; 2.jacket >> price, size, total;
make it in 1 program, how do you do it bro?
ARY SURJANTO Oct 23, 2015, 12:45:00 = try it first, bro...
IDAN WILDAN Nov 30, 2015, 19:43:00 = Bro, please enlighten me. I have an assignment. Determining the class of several types of sizes
MUHAMMAD PRAMODA Dec 12, 2015, 05:10:00 = Bro, I want to ask. Why not just use switch-case? It can be simpler and doesn't require a lot of data to be declared. Is that right?
SATRIA PERDANA 27 Dec 2015, 06:53:00 = Bro, I want to ask, how to make a C++ salary calculation program using modular with loops/branches.. that's the assignment given to me by the lecturer but I don't understand the meaning of the question above. Thanks.
SATRIA PERDANA Jan 23 2016, 15:59:00 = alright, thx a lot bro
ANDRY ANTO 30 Dec 2015, 16:55:00 = Good afternoon, bro Wawan, for your article, is there an example of making a program that combines all functions, for example if-else combined with switch-case and so on? Is there a title? If there is, what is the title? I'm looking for it. Thanks Andri
EVARIA BOO STORE Jan 7, 2016, 14:43:00 = Sir.....can you help me...I have a task to create a C++ program that contains the following:\
Types of requirements 1) Data Types / Variables Minimum 3 different types 2) Constants Minimum 2 different types 3) Selection/branching Minimum 1 line with if ... else ... or with switch\
4) Loops Minimum 2 of 3 types
- while ... loop
- do ... while
- for ...
5) Minimum Array 1 row\
How do I make the program..
TISMA HERMAYANTI 3 Jan 2017, 18:27:00 How do I join the Gatewan forum, bro?
AZTEC10 Feb 2016, 21:13:00 if only every Indonesian would share like this I am sure the stupidity of this nation will fade but with one condition that we get used to starting to use a hook to catch fish and not keep asking for fish.....try and share your mistakes... success to future Indonesian intellectuals.....Salute
AHMAD SUSANTO Feb 27, 2016, 20:59:00 = What is the getch function for, bro? I'm a newbie, you know.
ARIEF HIDAYAT Mar 8 2016, 05:56:00 = really cool
MUHAMMAD SYAHBANDI ST, 27 Mar 2016, 13:13:00 = Thanks bro.. are you an ITB student bro?
SYAIFUL AZIZ May 23, 2016, 09:06:00 = Uncle, how do I apply a flowchart to Borlan C++? For example, I already have a flowchart and I want to create a program in Borlan C++.
EXO.XOXO KISS AND HUG May 29, 2016, 22:19:00 bro, please help me. If the output is like this:
1 C++PROGRAMMING 2 C++PROGRAMMING 3 C++PROGRAMMING 4 C++PROGRAMMING 5 C++PROGRAMMING
What is the formula for that?
NITA ELVIDA Jul 18, 2016, 17:10:00 = HOW TO COMBINE SELECT CASE WITH IF GO TO?
UNKNOWN 6 Aug 2016, 19:01:00 = Sir, I want to ask, I am confused about using the header #include or and is the function of printf the same as cout? thank you sir
ANDI GHALIB 7 Dec 2016, 22:11:00 = Bro, please help me with my problem. Create a program consisting of:
- Recurrence
- Array
- Conditional statement\
FADA MULYA Jan 6, 2017, 12:20:00 = Min, how do you insert if into switch? But use the #include header
AHMAD RIFAI 6 Jan 2017, 15:33:00 = Great Gan, Thanks U very Much, Your program became the basic concept for my program for my final assignment, finally my assignment is finished
TRUE VILLAGERS 6 Mar 2017, 20:24:00 = please make a flowchart for this programming min
ROBERT1 Apr 8, 2017, 14:31:00 = Can anyone help me by giving a coding example? My assignment is: Create a simple program using a combination of pointers and the while command. Please help me.
AGUNG HERMAWAN Jul 13, 2017, 21:50:00 = How is the flowchart, bro?
MUZLIH Jul 27, 2017, 18:57:00 = Bro, I want to ask, I have a condition, if X>=60 or X>=-60 then the current is off, and immediately End, so when it is reversed to condition <60 or <-60, the current is still Off, how do I code it bro? thanks
M.RAMDAN ANGGADIAKSA Oct 3, 2017, 10:46:00 = I want to ask, I have 2 c++ scripts, one is login and the other is a flat shape area script, how do I combine the 2 scripts into one? So after logging in, the area calculation process starts immediately. Thanks
ARI RAMDHANI 22 Oct 2017, 11:21:00 = I want to join, bro
DDDD9 Dec 2017, 19:33:00 Bro, please help me create the program coding bro... Thursday, December 14th, collected\
1) Using the switch case or goto concept (choose the one you can), create a program that displays a menu consisting of: 1. Biodata, 2. Employee Salary Calculation Program and 3. Exit. 2) For menu 1. Biodata, contains your biodata consisting of name, student ID number, date of birth, local, department, faculty. 3) For menu 2. Employee Salary Calculation Program which has the following provisions: a) Basic monthly salary is Rp. 1,800,000; b) Position Allowance:
- Group 1: 5%*basic salary
- Group 2: 10%*basic salary
- Group 3: 15%*basic salary
4) Overtime honorarium of Rp. 15,000/hour, where the number of mandatory working hours is 8 hours for 5 days within 1 month (4 weeks) so that the mandatory working hours total 160 hours. Excess working hours are calculated as overtime hours. So the total amount of overtime honorarium = overtime hours of Rp. 15,000. 5) The amount of honorarium received = basic salary position allowance + total overtime honorarium. 6) The input screen consists of:
- Your PT.NIM Employee Salary Calculation Program
- Employee Name :
- Job Class (1/2/3) :
- Number of Working Hours:
7) The output screen is:
- An employee named...
- The honorarium received consists of...
- Position Allowance Rp....
- Overtime honorarium Rp....
- Basic salary Rp...
- Total salary Rp...
8) For Menu 3. Exit, if selected, the program stops or exits the program.
Response 1
ok bro, done
you are welcome..
Hello ILHAM N. No, the editor I use is Borland C++. To execute the program above, please use Borland C++.
Hi Cipu Cimot, Different editor applications have different libraries, so the #include above is Borland C++'s library, so it's natural that an error occurs. If Cipu Cimot uses another application, please use the Help menu to find out the basic usage instructions (starter guide). thanks
If you use dev c++, there are several commands that need to be changed, bro, such as how to display/print output, besides that the library is also not the same... so the answer is, "yes" but it needs to be adjusted to the compiler used...
I agree with Mr. Ary Surjanto, happy studying, Mr. Perlin Harefa and good luck.
Just try it first, bro. The tip is to use a flowchart to conceptualize the program.
Hello Muhammad P. Yup, the discussion above only explains the differences in how to use 2 different branching methods, the problem of usage is just a matter of taste and purpose of use, please feel free, that's all, Thank you.
Hello Satria P. Modular (function), so to create a salary calculation program, then break it down into several parts (special assignments), which will later be put into several functions, the function is placed under the main function (void main {..}), where this function/module will later have a special task in the salary calculation program structure. Use a function with a return value.
Examples of special functions:
double jmlhlembur(double total){
penghitungan;
return total;
}
After that, don't forget to call these functions into the main function, for example:
void main{
double x;
jmlhlembur(x);
getch();
}
Okay, happy studying and good luck.
Hi Andry A. such program combinations are in our article section at "C++ REPEAT PROGRAM COLLECTION" Thank you
Hello EVARIA BOO STORE, If you want to ask something or want to discuss by bringing up a separate topic, at length and involving file attachments, then please join the GATEWAN FORUM, thank you,
Hii TISMA HERMAYANTI, for now we only accept emails with google hosting (gmail, google.co.id, or google.com). Please apply on the sign up page. Thank you
Hello Mr. AZTEC, thank you sir for your appreciation. That's right sir, in order to practice the contents of the Preamble to the 1945 Constitution, paragraph 4 "Enlightening the Life of the Nation"
getch is taken from the conio.h header file, which functions to display characters on the screen. So when you run a program and there is a need to display characters on the screen either via keyboard or via source code, then you need getch().
Hello ARIEF HIDAYAT, thank you.
Hello MUHAMMAD SYAHBANDI ST you're welcome, amen
The method is, after you can use a flowchart, the next step is to learn the basics of Borland C++ programming, get to know and master the various instructions. If SYAIFUL AZIZ already understands the use of instructions very well, I am sure that realizing your flowchart will be easier.
Oh, that's easy, bro. It's actually simple. The formula is just "Learn, Try and Practice."
Hi NITA ELVIDA, it depends on what the case study was like, but what is clear is that it is very possible.
header is a library provided by the compiler, the library contains c++ commands that we will use, we do not need to include all libraries. So whatever commands we will use in the program, then it determines what libraries we need to include. printf with cout have the same function, namely to print output to the screen.
I will help with prayer and technical support only, if you experience problems in certain parts. Sorry ANDI GHALIB, because it is your responsibility, so don't give up, please try it first. Happy learning, Ganbate!
switch(pilihan) {
case 1:
if(){
..statement..Anda
}
else{
..statement..Anda
}
break;
The name itself is header, it is clear that its placement is at the very top. It cannot be included in switches, functions, branches, or loops. The point is that it cannot be disturbed, its placement is at the very top.
Good job bro AHMAD RIFAI! Congratulations! Glad to hear that, regards.
Hii True Villagers, fyi
int x=0;
bool arus;
for (x>=60 || x>=-60){
arus= false;
}
for (x<=60 || x<=-60){
arus= false;
}
Use method/function, so each code is entered into the function. For example void login {login script here}, then void luasBangunDatar {script buildun rata here}. Then the two functions above are called in the main function with conditions. Example:
void main {
if (login == ture){
luasBangunDatar;
}
}
Please go ahead bro
- Edited
In a country with four seasons, there are several factors that determine a season. For example, when the humidity is between 0-10 and the temperature is below 10 C , the weather is cloudy, then it is certain that it is winter. Another thing is when the humidity is 10-20, the temperature is from 10 to 15 C and the weather is cloudy, then it is autumn when the leaves turn orange and fall in groups. Well, summer is when the humidity is highest, starting from 10-30. The temperature also rises drastically from 15 in early summer to 30 C. The weather is also sunny at that time. And the last is spring, where after winter freezes many places, spring comes with a humidity of 10-25. The temperature also tends to be warm starting from 10 to 23 C. And the weather is sunny like in summer.
CREATE A PROGRAM
- Based on the four seasons situation above
- Accept ten times input based on the table below.
- Apply the concept of branching and looping
- When the input does not meet all the conditions
- branching, make the output “data not found”
Understanding C++ Loops
Loops are used to run one or more commands repeatedly under certain conditions.
In a loop, it generally consists of 3 components, namely:
- Initial Value/Initialization, namely determining the initial value in the loop
- Loop Conditions, if the value meets certain conditions, the loop will continue, otherwise the loop will be stopped.
- Value Changer, during the loop the value will be changed continuously
C++ Loops
Look at the following diagram:
Recurrence Flow Chart
There are several types of loops that can be used in the C++ programming language, including the following statements:
- for
- do .. while
- while
1. Statement for
The for statement can be translated as "do a loop as long as the value meets the loop conditions, to run the command (in the block) with the value to be changed as many times as the value changes".
It has the following general form:
for (inisialisasi; syarat perulangan; perubah nilai) {
Pernyataan 1; Pernyataan N
}
Example Program 1:
#include<iostream.h>
#include<conio.h>
/**
*bundet.com
*Cetak angka 1 s.d 9
*/
void main() {
for (int i = 1; i < 10; i++) {
cout << i << " ";
}
getch();
}
Program Execution Results:
1 2 3 4 5 6 7 8 9
Example Program 2:
#include<iostream.h>
#include<conio.h>
/**
*bundet.com
*Cetak angka 9 s.d 1
*/
void main() {
for (int i = 9; i > 0; i--) {
cout << i << " ";
}
getch();
}
Program Execution Results
9 8 7 6 5 4 3 2 1
2. While statement
The while statement can be translated as "as long as the condition meets the loop conditions, execute the commands in the block repeatedly".
It has the following general form:
inisialisasi;
while (syarat perulangan) {
pernyataan 1;
pernyataan N;
perubah nilai;
}
Example Program 3:
#include<iostream.h>
#include<conio.h>
/**
*bundet.com
*Mencetak bilangan bulat
*/
void main() {
int i = 1;
while (i < 10) {
if (i%2 == 0)
cout << i << " "; i++;
}
getch();
}
Program Execution Results:
2 4 6 8
Example Program 4:
#include<iostream.h>
#include<conio.h>
/**
*bundet.com
*Mencetak bilangan bulat
*dan menentukan bilangan genap
*/
void main() {
int nilai = 1;
while (nilai%2 != 0) {
cout << "Masukkan bilangan genap ";
cin >> nilai;
}
cout << "Angka " << nilai << " termasuk bilangan genap";
getch();
}
Program Execution Results:
Masukkan bilangan genap 3
Masukkan bilangan genap 5
Masukkan bilangan genap 7
Masukkan bilangan genap 8
Angka 8 termasuk bilangan genap
3. Do -- while statement
In the do -- while statement will execute the command first, then will perform the test at the end of the loop. Look at the diagram below.
It has the following general form:
inisialisasi;
do {
pernyataan 1;
pernyataan N;
perubah nilai;
}
while(syarat perulangan);
Flow Chart do -- while Statement
From the diagram above, it can be concluded that, in the do -- while loop, at least one command will be executed even if the loop condition does not meet the requirements.
Example Program 5:
#include<iostream.h>
#include<conio.h>
/**
*bundet.com
*Mencetak bilangan bulat
*dan menentukan bilangan genap atau ganjil
*/
void main() {
char jawab;
int angka;
do {
cout << "Masukkan Angka : ";
cin >> angka;
cout << "Angka " << angka << " adalah ";
cout << ( (angka % 2 == 1) ? "ganjil" : "genap" );
cout << "\nCoba lagi (Y/T) ? "; cin >> jawab;
}
while (jawab == 'y' || jawab == 'Y'); getch();
}
Program Execution Results:
Masukkan Angka : 5
Angka 5 termasuk ganjil Coba lagi (Y/T) ? y
Masukkan Angka : 8
Angka 8 termasuk genap Coba lagi (Y/T) ? t
4. Break Statement
The break statement is used to exit a loop. Usually this break command is stored in a branch.
Example Program 6:
#include<iostream.h>
#include<conio.h>
/**
*bundet.com
*Menampilkan bilangan bulat
*/
void main() {
int i = 0;
while (i < 10) {
if (i == 7)
break;
cout << i << " "; i++;
}
getch();
}
Program execution results:
0 1 2 3 4 5 6 7
5. Continue statement
The continue statement is used to direct execution to the next iteration/loop while ignoring other commands/statements below it.
Example Program 7:
#include<iostream.h>
#include<conio.h>
/**
*bundet.com
*Mencetak bilangan ganjil
*/
void main() {
int i = 0;
while (i < 10) {
i++;
if (i%2 == 0) continue;
cout << i << " ";
}
getch();
}
Program execution results:
1 3 5 7 9
6. Nested Loop
Nested loop is a loop that is placed in another loop. In this Nested loop can use the statement for, while, do -- while or a combination of the three statements.
Example Program 8:
#include<iostream.h>
#include<conio.h>
#define MAX 5
/**
*bundet.com
*Display bangun segitiga
*/
void main() {
int i = 0;
while (i < MAX) {
for (int j = 0; j <= i; j++) {
cout << '*';
}
cout << endl; i++;
}
getch();
}
Program execution results:
*
**
***
****
*****
Example Program 9:
#include<iostream.h>
#include<conio.h>
/**
*bundet.com
*Display bangun segitiga terbalik
*/
void main() {
for (int i = 5; i > 0; i--) {
for (int j = i; j > 0; j--) {
cout << '*';
}
cout << endl;
}
getch();
}
Program execution results:
*****
****
***
**
*
Source:
© STMIK El Rahma Yogyakarta. Compiled by Mr. Eko Riswanto, ST, M.Cs,
Question 1
DWIKY GUNTARA Nov 28, 2015, 14:49:00 Bro, can you help me complete this program:
1
121
1223221
1223334333221
GUSLILA SARI 12 Dec 2015, 20:48:00 Wan can you help me complete this program,
1
121
1223221
1223334333221
RINALDI AGUSTIAN 4 Oct 2016, 23:57:00 please help me if the numbers that appear are 1,2,4,5,6,8,9,10, what should I do? Please help me.
EDDO RENALDY 21 Mar 2017, 11:46:00 the program can't run bro, there's an error in the header, please help bro
FIRMAN JAYA HALAWA 19 Aug 2017, 14:44:00 good bro.. thanks for the material
IIN 13 Dec 2017, 21:14:00 thank you for the complete article
Response 1
Yes, just cout << that display. Be diligent in studying, don't give up yet.
How come it's the same as the one above? heheheh
Try it first, bro, don't give up, it's okay to make a few mistakes, because you're just learning, I'll help you when you make a mistake. If I help you make it, I don't think so, because then you won't learn.
Using Dev C++, right, bro? Okay, please include a screenshot of the error, or what the error sounds like, so we can help with debugging. Because so far, the scripts above are still running smoothly on Borland C++ for now, bro EDDO RENALDY, I'll wait for the error capture.
Hi FIRMAN JAYA HALAWA, ok, welcome to the bundet
Hi IIN, ok, welcome to the bundet
Understanding C++ Branching
Branching is used to solve problems to make a decision among several existing statements.
C++ Branching
1. If statement
The if statement has the meaning, "If the condition is true, then the command will be executed and if it does not meet the conditions then it will be ignored". From this definition, it can be seen from the following flow diagram:
Flow Chart if
General form of if statement:
if (kondisi) {
// pernyataan
}
Example Program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Contoh pernyataan if
*/
void main() {
if (true) {
cout << "Perintah ini akan dijalankan\n";
}
if (!true) {
cout << "Perintah ini TIDAK akan dijalankan\n";
}
if (1+1 == 2 && 100 > 99) {
cout << "Anda benar\n";
}
getch();
}
In the example above, the command will be executed if the final condition is true. Here is an example of a program to determine the amount of discount from purchasing goods with the following criteria:
- There is no discount if the total purchase is less than Rp. 50,000,-
- If the total purchase is more than or equal to Rp. 50,000,- the discount received is 20% of the total purchase.
Another example of using the if statement in the Shopping Program
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Contoh penggunakan if dalam program Belanja
*/
void main() {
double tot_beli, potongan = 0, jum_bayar = 0;
cout << "Total pembelian Rp. ";
cin >> tot_beli;
if ( tot_beli >= 50000 ) {
potongan = 0.2 * tot_beli;
}
cout << "Besarnya potongan Rp. " << potongan << endl;
jum_bayar = tot_beli - potongan;
cout << "Jumlah yang harus dibayarkan Rp . " << jum_bayar;
getch();
}
Execution Results:
Total pembelian Rp. 70000
Besarnya potongan Rp. 14000
Jumlah yang harus dibayarkan Rp. 56000
2. If - else statement
The if statement has the meaning, "If the condition is true, then command-1 will be executed and if it does not meet the conditions then command-2 will be executed". From this definition, it can be seen from the following flow diagram:
Flow Chart if-else
General form of if-else statement:
if (kondisi) {
// pernyataan 1
}
else {
// pernyataan 2
}
The following is an example of a program to determine the amount of discount from purchasing goods with the following criteria:
- If the total purchase is less than Rp. 50,000,- then the discount received is 5% of the total purchase.
- If the total purchase is more than or equal to Rp. 50,000,- the discount received is 20% of the total purchase.
Example Program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Contoh penggunakan if-else
*/
void main() {
double tot_beli, potongan = 0, jum_bayar = 0;
cout << "Total pembelian Rp. ";
cin >> tot_beli;
if ( tot_beli >= 50000 ) {
potongan = 0.2 * tot_beli;
}
else {
potongan = 0.05 * tot_beli;
}
cout << "Besarnya potongan Rp. " << potongan << endl;
jum_bayar = tot_beli - potongan;
cout << "Jumlah yang harus dibayarkan Rp . " << jum_bayar;
getch();
}
Execution Results:
Total pembelian Rp. 49000
Besarnya potongan Rp. 2450
Jumlah yang harus dibayarkan Rp. 46550
3. Nested if statement
Nested if is an if statement that is inside another if statement. The general form of writing a nested if statement is as follows:
if (kondisi1) {
if (kondisi2) {
// perintah
}
else {
// perintah
}
}
else {
if (kondisi3) {
// perintah
}
else {
// perintah
}
}
Sample case:
A company provides commission to salesmen with the following conditions:
- If the salesman can sell goods up to Rp. 2,000,000, - then he will be given a service fee of Rp. 500,000, - plus a commission of Rp. 10% of the income earned that day.
- If the salesman can sell goods above Rp. 2,000,000, - then he will be given a service fee of Rp. 500,000, - plus a commission of Rp. 15% of the income earned that day.
- If the salesman can sell goods above Rp. 5,000,000, - then he will be given a service fee of Rp. 1,000,000, - plus a commission of Rp. 20% of the income earned that day.
Example program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Contoh penggunakan nested if
*Hitung total pendapatan
*/
void main() {
long pendapatan, jasa = 0, komisi = 0, total = 0;
cout << "Pendapatan hari ini Rp. ";
cin >> pendapatan;
if ( pendapatan > 0 && pendapatan <= 2000000) {
jasa = 500000;
komisi = 0.1 * pendapatan;
}
else {
if ( pendapatan <= 5000000 ) {
jasa = 500000;
komisi = 0.15 * pendapatan;
}
else {
jasa = 1000000;
komisi = 0.2 * pendapatan;
}
}
total = komisi + jasa;
cout << "Uang jasa Rp. " << jasa << endl;
cout << "Uang Komisi Rp. " << komisi << endl;
cout << "------------------------------" << endl;
cout << "Hasil Total Rp. " << total << endl;
getch();
}
Execution Results:
Pendapatan hari ini Rp. 3500000
Uang jasa Rp. 500000
Uang Komisi Rp. 524999
------------------------------
Hasil Total Rp. 1024999
4. Compound if-else Statements
The form of a nested compound if-else is actually similar to a nested if. The advantage of using nested if-else compared to nested if is that the writing form is simpler. The general writing form is as follows:
if (kondisi1) {
// perintah 1
}
else if (kondisi2) {
// perintah 2
}
else {
// perintah N
}
The following is a simplification of the previous program case example using compound if-else.
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Penggunakan if-else majemuk
*Hitung total pendapatan
*/
void main() {
long pendapatan, jasa = 0, komisi = 0, total = 0;
cout << "Pendapatan hari ini Rp. ";
cin >> pendapatan;
if ( pendapatan > 0 && pendapatan <= 2000000) {
jasa = 500000; komisi = 0.1 * pendapatan;
}
else if ( pendapatan <= 5000000 ) {
jasa = 500000; komisi = 0.15 * pendapatan;
}
else {
jasa = 1000000; komisi = 0.2 * pendapatan;
}
total = komisi + jasa;
cout << "Uang jasa Rp. " << jasa << endl;
cout << "Uang Komisi Rp. " << komisi << endl;
cout << "------------------------------" << endl;
cout << "Hasil Total Rp. " << total << endl;
getch();
}
5. Switch - case statement
The switch -- case statement has the same use as nested if -- else , but to check data of character or integer type. In general, the writing format is as follows:
switch (ekspresi) {
case kontanta-1:
// pernyataan 1 break;
case konstanta-2:
// perintah 2 break;
default:
// perintah
}
Example Program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Penggunakan switch-case
*Menentukan hari
*/
void main() {
int nHari;
cout << "Masukkan No Hari [1..7] : ";
cin >> nHari;
cout << "Ini adalah hari ";
switch (nHari) {
case 1:
cout << "Ahad";
break;
case 2:
cout << "Senin";
break;
case 3:
cout << "Selasa";
break;
case 4:
cout << "Rabu";
break;
case 5:
cout << "Kamis";
break;
case 6:
cout << "Jum'at";
break;
case 7:
cout << "Sabtu";
break;
default:
cout << "Kiamat :))";
}
getch();
}
Execution Results:
Masukkan No Hari [1..7] : 5
Ini adalah hari Kamis
The break statement indicates that you are ready to exit the switch. If this statement is not present, the program will continue to the other branches.
Another example in the gender determination program
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Penggunakan switch-case
*Menentukan jenis kelamin
*/
void main() {
char jkl;
cout << "Gender Anda (L/P) : ";
cin >> jkl;
cout << "Anda adalah ";
switch (jkl) {
case 'p':
case 'P':
cout << "Perempuan";
break;
case 'l':
case 'L':
cout << "Laki-laki";
break;
default:
cout << "Complecated";
}
getch();
}
Execution Results:
Gender Anda (L/P) : l
Anda adalah Laki-laki
Hope this is useful & happy learning!
Source:
© STMIK El Rahma Yogyakarta. Compiled by Mr. Eko Riswanto, ST, M.Cs,
C++ Loop Program Collection
Substance:
- for
- do .. while
- while
- Combination of for and do .. while
- Creating Fields (Recursion and Branching -> switch)
1. Looping Using "for"
Finding Average, Maximum and Minimum
Program:
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
//bundet.com
//MENCARI RATA - RATA, MAKSIMUM DAN MINIMUM
void main()
{
randomize();
int data, mak, min,tot=0;
float rata;
for(int i=1;i<=5;i++)
{
data=random(100);
cout<<"Data masuk : "<<data<<endl;
if (i==1)
mak=min=data;
else
{
if (mak<data)
mak=data;
if (min>data)
min=data;
}
tot=tot+data;
}
rata=tot/5;
cout<<"Rata-rata : "<<rata<<endl;
cout<<"Masimum : "<<mak<<endl;
cout<<"Minimum : "<<min<<endl;
getch();
}
Counter 1st Run
Counter 2nd Run
Program:
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
//bundet.com
//COUNTER
void main()
{
for(int i=1;i<=1000;i++)
{
cout<<setw(5)<<i;
if(i%10==0)
cout<<endl;
if(i%100==0)
{
cout<<"Tekan enter untuk melanjutkan...";
getch();
clrscr();
}
}
getch();
}
Counter with Interval, 1st Run
Counter with Interval, 2st Run
Program:
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
//bundet.com
//COUNTER DENGAN INTERVAL
void main()
{
int brs=0,klm=0;
for(int i=1;i<=1000;i+=3)
{
cout<<setw(5)<<i;
klm++;
if(klm==10)
{
cout<<endl;
brs++;
klm=0;
}
if(brs==10)
{
cout<<"Tekan enter untuk melanjutkan...";
getch();clrscr();
brs=0;
}
}
getch();
}
2. Looping Using do .. while
Finding Average, Maximum and Minimum
Program:
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
//bundet.com
//MENCARI RATA-RATA, MAKSIMUM DAN MINIMUM
void main()
{
randomize();
int data, mak, min,tot=0,i=1;
float rata;
do
{
data=random(100);
cout<<"Data masuk : "<<data<<endl;
if (i==1)
mak=min=data;
else
{
if (mak<data)
mak=data;
if (min>data)
min=data;
}
tot=tot+data;
i++;
}while(i<=5);
rata=tot/5;
cout<<"Rata-rata : "<<rata<<endl;
cout<<"Masimum : "<<mak<<endl;
cout<<"Minimum : "<<min<<endl;
getch();
}
3. Looping Using while
Finding Average, Maximum and Minimum
Program:
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
//bundet.com
//MENCARI RATA-RATA, MAKSIMUM DAN MINIMUM
void main()
{
randomize();
int data, mak, min,tot=0,i=1;
float rata;
while(i<=5)
{
data=random(100);
cout<<"Data masuk : "<<data<<endl;
if (i==1)
mak=min=data;
else
{
if (mak<data)
mak=data;
if (min>data)
min=data;
}
tot=tot+data;
i++;
}
rata=tot/5;
cout<<"Rata-rata : "<<rata<<endl;
cout<<"Masimum : "<<mak<<endl;
cout<<"Minimum : "<<min<<endl;
getch();
}
4. Combination of for and do .. while
Payment Note
Program:
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
//bundet.com
//NOTA PEMBAYARAN
void main()
{
cout<<"NOTA PENJUALAN\n";
cout<<"GATEWAN MARKET\n";
cout<<"Jl. Parangtritis No 10 Yogyakarta\n";
cout<<"===================================================================\n";
cout<<"| No. | Nama Barang | Jml | Hg Sat | Jml Hg | Diskon | Tot Hg |\n";
cout<<"===================================================================\n";
for(int i=1;i<=15;i++)
{
cout<<"| |\n";
}
cout<<"===================================================================\n";
cout<<"| TOTAL Bayar \n";
cout<<"===================================================================\n";
int i=1; char jw;
float jml,hgsat,jmlhg,disk,hgdisk,totdisk,totsemua,tothg;
char nmbrg[30];
do
{
gotoxy(3,6+i);cout<<i;
gotoxy(8,6+i);cin>>nmbrg;
gotoxy(22,6+i);cin>>jml;
gotoxy(28,6+i);cin>>hgsat;
jmlhg=jml*hgsat;
gotoxy(37,6+i);cout<<jmlhg;
gotoxy(46,6+i);cin>>disk;
hgdisk=(disk/100)*jmlhg;
gotoxy(50,6+i);cout<<hgdisk;
tothg=jmlhg-hgdisk;
totdisk=totdisk+hgdisk;
totsemua=totsemua+tothg;
gotoxy(55,6+i);cout<<tothg;
gotoxy(50,23);cout<<totdisk;
gotoxy(55,23);cout<<totsemua;
gotoxy(30,1);cout<<"Input data lagi [y/t]? ";cin>>jw;
i++;
gotoxy(30,1);clreol();
}while(jw=='y');
getch();
}
5. Create Fields (Repetition and Branching -> switch)
Bottom Diagonal
Top Diagonal
Cross
Left Inverted Right Triangle
Right Angled Triangle
Right Inverted Right Triangle
Program:
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
//bundet.com
//Membuat Bidang
void main()
{
int pilih;
do{
clrscr();
cout<<"Menu Pilihan "<<endl;
cout<<"1. Diagonal bawah"<<endl;
cout<<"2. Diagonal atas"<<endl;
cout<<"3. Silang"<<endl;
cout<<"4. atas "<<endl;
cout<<"5. bawah "<<endl;
cout<<"6. bawah kiri"<<endl;
cout<<"7. bawah kanan"<<endl;
cout<<"8. Keluar"<<endl;
cout<<"Pilihan anda : ";cin>>pilih;
switch(pilih)
{
case 1:
{
for(int i=1;i<=10;i++)
{ for(int j=1;j<=10;j++)
if(i==j)
cout<<"*";
else
cout<<" ";
cout<<endl;
}
getch();
break;
}
case 2:
{
for(int i=1;i<=10;i++)
{ for(int j=1;j<=10;j++)
if(i+j==11)
cout<<"*";
else
cout<<" ";
cout<<endl;
}
getch();
break;
}
case 3:
{
for(int i=1;i<=10;i++)
{ for(int j=1;j<=10;j++)
if((i==j)||(i+j==11))
cout<<"*";
else
cout<<" ";
cout<<endl;
}
getch();
break;
}
case 4:
{
for(int i=1;i<=10;i++)
{ for(int j=1;j<=10;j++)
if(i+j<=11)
cout<<"*";
else
cout<<" ";
cout<<endl;
}
getch();
break;
}
case 5:
{
for(int i=1;i<=10;i++)
{ for(int j=1;j<=10;j++)
if(i+j>=11)
cout<<"*";
else
cout<<" ";
cout<<endl;
}
getch();
break;
}
case 6:
{
for(int i=1;i<=10;i++)
{ for(int j=1;j<=10;j++)
if(i>j)
cout<<"*";
else
cout<<" ";
cout<<endl;
}
getch();
break;
}
case 7:
{
for(int i=1;i<=10;i++)
{ for(int j=1;j<=10;j++)
if(i<j)
cout<<"*";
else
cout<<" ";
cout<<endl;
}
getch();
break;
}
}
}while(pilih!=8);
getch();
}
Creating a Rectangle
Program:
#include <iostream.h>
#include <conio.h>
//bundet.com
//MEMBUAT PERSEGI PANJANG
void main()
{
int i,j,n;
cout<<"program membuat persegi"<<endl;
cout<<"======================="<<endl;
cout<<endl;
cout<<"Masukkan ukuran persegi : "; cin>>n;
for (i=1;i<=n;i++)
{
if ((i==1)||(i==n))
{
for (j=1;j<=n;j++)
cout<<"*";
cout<<endl;
}
else
{
for (j=1;j<=n;j++)
{
if ((j==1)||(j==n))
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
}
getch();
}
Hope this is useful & happy learning!
Question 1
DONI JULIANSA Oct 15, 2015, 11:04:00 super, thank you
LALU WATON SALENDRA Nov 3, 2015, 23:49:00 Thank you very much, very helpful. KEEP SUCCESSFUL.
FATHUR RIZAL 19 Nov 2015, 08:13:00 thank you very much
CRUSADER 12 Nov 21, 2015, 10:07:00 For conio.h library is not available in my geany. Is there any other way without using conio.h library
MPEBB GUARDIOLLA25 Dec 2015, 22:23:00 sorry sir, please enlighten me, for example input1: 1. input2: 5 and output'y: (1.2).(2.3).(3.4).(4.5) what is the source code for all of that like sir???
TIO RAHADITYA L Mar 19, 2016, 19:24:00 Can't be downloaded anymore?
ATMA SWIFT5 Jun 2016, 13:24:00 to display output
5
54
543
5432
54321
how to use while?
JUNIAR HASLIZA Oct 12, 2016, 18:14:00 Very helpful, thank you
ALEXANDER SIMBOLON1 Nov 2016, 10:03:00 How about a question like this, bro?
- Create an algorithm/program to print the numbers 1,2,...,n, with the value n inputted via the keyboard.
- Accumulate the numbers 1,2,...,n and store them in the total variable.
- Calculate the average of numbers using the formula average = total / n\
NASRIANG JHY Nov 19, 2016, 10:14:00 How do you make a program like this?
1
3 4
5 6 7
7 8 9 10
9 10 11 12 13
please help newbie #fast response
FIKRI TAUPAN20 Nov 2016, 08:10:00 I want to ask, I have a program for javelin throwing like this:
printf("\nDAFTAR PESERTA\n");
int i,total;
for(i=0; i
the problem is when added up, the a-value is 0. please help me, sis. tq
KIKI SUKANDI25 Dec 2016, 14:21:00 link to download the application, bro, I want to try this program
INDRIANI28 Dec 2016, 16:20:00 Create an algorithm and program to display the reverse of a string entered by the user.
- Example if input string: Denpasar
- Reverse output of string : rasapneD
if to make this program how String in C++ language always ends with NULL character (\0). Based on this fact, make an algorithm and program that asks for input of a string and then counts the number of characters contained in the string.
MOCHAMAD RIZKY SUBAGJA May 20, 2017, 16:25:00 Bro, why does the #include error keep appearing on my laptop's Dev C++? Is the problem with the laptop or the Dev C++ apk?
MEGA RAYHAN 23 May 2017, 19:33:00 Thank you very much, permission to save for UKK material
ALWI ABDILLAH 28 Nov 2017, 20:27:00 thank you very much, it is very helpful, permission to save the material to make assignments
KEVIN QUDRATA Jan 9, 2018, 16:12:00 very helpful, thank you
FERNANDO MARDI Apr 5, 2018, 00:13:00 Bro, please tell me how to calculate the number of repetitions after using repetitions.
Response 1
Welcome Doni J, ..you're welcome
Welcome Then WAS, you're welcome, amen
Hi Fathur R., you're welcome, bro, good luck.
Hi Crusder, You can't find conio.h because it is a C header file used by the MS-DOS compiler to generate user interface text. Compilers targeted for non-DOS operating systems, such as Linux, Win32 and OS/2, provide different files for this kind of function. Please use #include <curses.h>
to get the same function as conio.h Thanks,
Please try it first, we can help you with corrections, thank you.
Sorry, because our github is under reconstruction so all source code hosted on github is experiencing problems, but look now we have fixed it
Hi Atma, please try it first, we will help you correct it or just some of it.
Hi Juniar, welcome. You're welcome.
Please try it first, bro. If there is an error or problem with the program you have created, God willing, I will help you.
If you want to ask something on a different topic or something that is not related to the post above, then I suggest you join our forum because only in this forum do questions on different topics have the highest priority level for responding.
Please try it first, bro, later if there is an error or problem with the program you have created, God willing I will help. If you want to ask something with a different topic or something that is not related to the post above, then my suggestion is to join our forum because only in that forum do questions with different topics have the highest priority level to be responded to.
Please search and download Borland C++
Interesting assignment miss , happy studying
Different compilers, bro, the programs above are made using Borland C++ not Dev C++. Can you attach a screenshot of the error? Who knows, I might be able to help.
Hii MEGA RAYHAN, you're welcome, bro
hii ALWI ABDILLAH, ready, please be happy, hopefully useful, good luck.
Hii FERNANDO MARDI, is there an example of a looping program? Please show me a fragment of the looping program first, then I will try to help modify it to find out the number of loops.
C++ Looping & Branching Program Collection
Substance:
- Student Grade Calculation Program Version 0.1
- Student Grade Calculation Program Version 0.2
- Determining Prime Numbers or Not Version 0.1
- Determining Prime Numbers or Not Version 0.2
- Determining Prime Numbers or Not Version 0.3
1. Student Grade Calculation Program Version 0.1
This version displays a table with a flexible number of rows.
Program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Menghitung Nilai Mahasiswa Versi 0.1
*/
void main()
{
char nama[30], namamk[30];
int jml,uts,uas,tugas;
char hrf;
float rata;
cout<<"Nama : ";cin>>nama;
cout<<"Jml MK : ";cin>>jml;
cout<<"================================================================\n";
cout<<"| No | Nama MK | UTS | UAS | Tugas | Rata2 | Huruf |\n";
cout<<"================================================================\n";
for(int i=1;i<=jml;i++)
{
gotoxy(1,5+i);cout<<"|";
gotoxy(3,5+i);cout<<i;
gotoxy(6,5+i);cout<<"|";
gotoxy(8,5+i);cin>>namamk;
gotoxy(16,5+i);cout<<"|";
gotoxy(18,5+i);cin>>uts;
gotoxy(22,5+i);cout<<"|";
gotoxy(24,5+i);cin>>uas;
gotoxy(28,5+i);cout<<"|";
gotoxy(30,5+i);cin>>tugas;
gotoxy(36,5+i);cout<<"|";
rata=(uts+uas+tugas)/3;
gotoxy(38,5+i);cout<<rata;
gotoxy(44,5+i);cout<<"|";
if(rata>=80)
hrf='A';
else if(rata>=70)
hrf='B';
else if(rata>=60)
hrf='C';
else if(rata>=50)
hrf='D';
else
hrf='E';
gotoxy(46,5+i);cout<<hrf;
gotoxy(52,5+i);cout<<"|";
}//end for
cout<<"\n================================================================\n";
getch();
}
2. Student Grade Calculation Program Version 0.2
This version displays a table with a fixed row size,
Program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Menghitung Nilai Mahasiswa Versi 0.2
*/
void main()
{
char nama[30], namamk[30];
int jml,uts,uas,tugas;
char hrf;
float rata;
cout<<"Nama : ";cin>>nama;
cout<<"Jml MK : ";cin>>jml;
cout<<"================================================================\n";
cout<<"| No | Nama MK | UTS | UAS | Tugas | Rata2 | Huruf |\n";
cout<<"================================================================\n";
for(int i=1;i<=10;i++)
{
gotoxy(1,5+i);cout<<"|";
gotoxy(6,5+i);cout<<"|";
gotoxy(16,5+i);cout<<"|";
gotoxy(22,5+i);cout<<"|";
gotoxy(28,5+i);cout<<"|";
gotoxy(36,5+i);cout<<"|";
gotoxy(44,5+i);cout<<"|";
gotoxy(52,5+i);cout<<"|";
}//end for
cout<<"\n================================================================\n";
for(int i=1;i<=jml;i++)
{
gotoxy(3,5+i);cout<<i;
gotoxy(8,5+i);cin>>namamk;
gotoxy(18,5+i);cin>>uts;
gotoxy(24,5+i);cin>>uas;
gotoxy(30,5+i);cin>>tugas;
rata=(uts+uas+tugas)/3;
gotoxy(38,5+i);cout<<rata;
if(rata>=80)
hrf='A';
else if(rata>=70)
hrf='B';
else if(rata>=60)
hrf='C';
else if(rata>=50)
hrf='D';
else
hrf='E';
gotoxy(46,5+i);cout<<hrf;
}//end for
getch();
}
3. Determining Prime Numbers or Not Version 0.1
This version displays the results directly
Program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Menentukan Bilangan Prima atau Bukan Versi 0.1
*/
void main()
{
int bil;
int prima=1;
cout<<"Masukkan bilangan : ";cin>>bil;
for(int i=2;i<bil;i++)
{
if(bil%i==0)
prima=0;
}
if(prima==1)
cout<<"Bilangan prima";
else
cout<<"Bukan prima";
getch();
}
4. Determining Prime Numbers or Not Version 0.2
This version will show the results as well as the process,
Program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Menentukan Bilangan Prima atau Bukan Versi 0.2
*/
void main()
{
int jml=0,bil;
cout<<"Masukkan bilangan : ";cin>>bil;
for(int i=1;i<=bil;i++)
{
if(bil%i==0)
{
jml++;
cout<<bil<<"%"<<i<<"="<<bil%i<<endl;
}
}
if(jml==2)
cout<<"Bilangan prima";
else
cout<<"Bukan prima";
getch();
}
5. Determining Prime Numbers or Not Version 0.3
This version will display prime numbers with a specified limit,
Program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Menentukan Bilangan Prima atau Bukan Versi 0.3
*/
void main()
{
int prima=1;
int batas;
cout<<"Masukkan batas : ";cin>>batas;
for(int i=2;i<=batas;i++)
{
for(int j=2;j<i;j++)
{
if(i%j==0)
prima=0;
}
if(prima==1)
cout<<i<<" ";
prima=1;
}
getch();
}
Hope this is useful & happy learning!
Question 1
ALDY HERNANVEZ May 2, 2015, 21:44:00 Thank you very much.. But, can I ask for the flowchart too?
Response 1
You're welcome, Aldy H. For the flow chart, please develop it yourself. Good luck and happy learning.
Question 2
Assalamualaikum sir, I want to ask if the problem is like this, what is the source code? If the input = 5 The result is 1 a 2 b 3 a 1 b 2 c 1 a 2 b 3 a 1 b 2 c 1 a 2 b 3
If input = 3 The result is 1 a 2 a 1 b 1 a 1
Please enlighten me
Response 2
First, identify the pattern. If we pay attention, the rule will be like this:
Input 5:
- 123, 12, 123, 12, 123. (5x)
- ab, abc, ab, abc, ab. (5x)\
in principle, each number or letter has the same number of members, only their positions are reversed if NUMBER(3,2..etc) if LETTER(2,3..etc).
Now you just have to make a mechanism to display it, whether you want to put it in an array, or just in a variable. Now to do this you need to use nested loops and branches. Please do it, gradually do it for the pattern for NUMBERS first, if successful continue to LETTERS, after that they are merged in a variable or in an array, it's up to you.
Input 3
- 1a2, a1b, 1a1, (3x)
- 1a2, a1b, 1a1, a2a, (if 4x)\
well from there we can conclude that the output is the result of a mix of NUMBER and LETTER patterns, so we can create a role like this:
Conclusion
From the questions above, we can broadly make rules with the following pattern:
ANGKA_insert_huruf, HURUF_insert_angka, ANGKA_insert_huruf, HURUF_insert_angka, ...dst.
Also create a condition, if the input < 5, then use function A, otherwise use function B.
okay i guess that's it, happy studying, keep going!
Branching, Array & Transfer by Value Notes
Task 1
Create a sales form
Output display:
Warung Gorengan
===============================
Masukan Nama Anda : Doni
Masukan Kode (MD, CD, EF) ; MD
===============================
Nama Pembeli : Doni
Kode Item : MD
Nama : Mendowan
Harga : 2000
===============================
Masukkan Jumlah Beli : 20
===============================
Total Bayar : Rp. 40000
Task2
Energy drink distributors suspect that marketing areas affect sales. Marketing areas include areas (residential, plantation, agriculture, mining, economy, industry)
strcmp
used to compare strings / text input.
Example:
if(strcmp(kd,"MD")==0) the number 0, a parameter that the comparison results are the same or there are no values that are more or less.
Procedures are for display only, unlike functions.
All C++ programming that is executed is void main, so if the function is not called in main, it will not run.
getch -->> is used to stop the program, and is located in the conio.h header __ to stop the program without removing its display...
Switch Function Exercise - Case
code:
#include <iostream.h>
#include <conio.h>
void main(){
int nilai;
cout<< "masukan nilai test : ";
cin>>nilai;
switch (nilai/10)
{
case 10:
case 9:
case 8:
cout << 'A' <<endl;break;
case 7:
cout << 'B' <<endl;break;
case 6:
case 5:
cout << 'C' <<endl;break;
case 4:
case 3:
cout << 'D' <<endl;break;
case 2:
case 1:
case 0:
cout << 'E' <<endl;break;
default:
cout << "Salah, nilai diluar jangkauan "<<endl;
}
getch();
}
output:
Repetition Function Exercises
code:
#include <iostream.h>
#include <conio.h>
double ulang(int A, int B);
void main(){
int awal, akhir;
cout << "masukan nilai awal : ";
cin>>awal;
cout << "masukan nilai akhir : ";
cin>>akhir;
ulang(awal, akhir);
getch();
}
double ulang(int A, int B){
int i;
for(i=A;i<B;i++){
cout << i << endl;
}
}
output:
Multiplication Function Exercises
code:
#include <iostream.h>
#include <conio.h>
double hasil (int A, int B);
void main(){
int x, y;
double z;
cout << "masukan Nilai x : ";
cin >> x;
cout << "masukan Nilai y : ";
cin >> y;
z = hasil(x,y);
cout << "hasil perkaliannya adalah : ";
cout << x << " . " << y << " : " << z;
getch();
}
double hasil (int A, int B){
return (A * B);
}
output:
Transfer by Value Exercise (passing parameters)
code:
#include <iostream.h>
#include <conio.h>
/*contoh program transfer by value*/
int tambah(int x);
void main()
{
int a, hasil;
cout<<"masukan bilangan : ";
cin>>a;
cout<<"a awal= "<<a<<endl;
hasil = tambah(a);
cout<<"a akhir= "<<a<<endl;
cout<<"hasil : "<<hasil;
getch();
}
int tambah(int x)
{
cout<<"x awal = "<<x<<endl;
x = x + 2;
cout<<"x akhir = "<<x<<endl;
return x;
}
output:
Array Practice
code:
output:
IF - ELSE Branching Exercises
code:
#include <conio.h>
#include <iostream.h>
void main(){
int usia;
cout <<"Masukan Usia Anda : ";
cin >> usia;
if (usia < 15){
cout << "anda sudah baligh";
}
else{
cout << "Anda belum cukup umur";
}
getch();
}
//jika usia lebih dari 15 tahun tampilkan pesan "anda sudah baligh"
//namun jika kurang dari 15 tampilkan pesan "Anda belum cukup umur"
output:
Data Type Check Exercise
code:
#include <iostream.h> //pustaka untuk mengaktifkan cout, endl, dll.
#include <conio.h> //pustaka untuk mengaktifkan fungsi getch(), dll.
#include <stdlib.h>
#include <limits>
void main(){
cout << "\n Character Types \n" << endl;
cout << "Ukuran memori : " << sizeof(char) << " byte." << endl;
cout << "Signed char min: " << SCHAR_MIN << endl;
cout << "Signed char max: " << SCHAR_MAX << endl;
cout << "Unsigned char min: 0" << endl;
cout << "Unsigned char max: " << UCHAR_MAX << endl;
cout << "\nShort Int Types" << endl;
cout << "Ukuran memory (dalam satuan byte): " << sizeof(short) << " bytes." << endl;
cout << "Signed short min: " << SHRT_MIN << endl;
cout << "Signed short max: " << SHRT_MAX << endl;
cout << "Unsigned short min: 0" << endl;
cout << "Unsigned short max: " << USHRT_MAX << endl;
cout << "\nInt Types" << endl;
cout << "Ukuran memory (dalam satuan byte): " << sizeof(int) << " bytes." << endl;
cout << "Signed int min: " << INT_MIN << endl;
cout << "Signed int max: " << INT_MAX << endl;
cout << "Unsigned int min: 0" << endl;
cout << "Unsigned int max: " << UINT_MAX << endl;
cout << "\nLong Int Types" << endl;
cout << "Ukuran memory (dalam satuan byte): " << sizeof(long) << " bytes." << endl;
cout << "Signed long min: " << LONG_MIN << endl;
cout << "Signed long max: " << LONG_MAX << endl;
cout << "Unsigned long min: 0" << endl;
cout << "Unsigned long max: " << ULONG_MAX << endl;
getch();
}
output:
That's all and happy studying