Kumpulan Source Code Pemrograman Menggunakan C++
10 minute read
Pada kesempatan kali ini, saya akan membagikan kumpulan pemrograman dasar bagi pemula, khususnya bagi mahasiswa baru jurusan Teknologi Informasi, Sistem Informasi, Teknik Informatika, Ilmu Komputer, Matematika atau siapapun yang ingin belajar coding menggunakan bahasa C++ ini. Semua
source code disini merupakan hasil dari saya menimbah ilmu di bangku perkuliahan.
Untuk mengetahui hasilnya, kalian bisa mencobanya langsung di aplikasi yang kalian gunakan, atau bisa menggunakan Compiler Online
berikut source code-nya,
Membuat Luas Segitiga
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <cstdlib>
using namespace std;
float LS(float A, float T) {
float luas;
luas = A * T / 2;
return (luas);
}
int main() {
float alas, tinggi, luas;
cout << " Masukan alas segitiga : ";
cin >> alas;
cout << " Masukan tinggi segitiga : ";
cin >> tinggi;
luas = LS(alas, tinggi);
cout << " Luas segitiga : " << alas << " x " << tinggi << " / " << " 2 " << " = " << luas << endl;
getch();
}
Membuat Faktorial
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <cstdlib>
using namespace std;
long int faktorial(long int a);
main() {
long int faktor;
long int n;
cout << " Masukan Nilai Faktorial ";
cin >> n;
faktor = faktorial(n);
cout << " " << n << "! = " << faktor << endl;
getch();
}
long int faktorial(long int a) {
if (a == 1 || a == 0) {
return (1);
} else if (a > 1) {
return (a * faktorial(a - 1));
} else {
getch();
}
}
Membuat 3 Perkalian
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <cstdlib>
using namespace std;
int tambah(int e, int o, int f) {
if (f == 0) {
return 0;
}
if (f == 1) {
return (e * o);
} else
return ((e * o) + tambah(e, o, (f - 1)));
}
int main() {
int x, y, z;
ulang:
cout << "Angka 1 : ";
cin >> x;
cout << "Angka 2 : ";
cin >> y;
cout << "Angka 3 : ";
cin >> z;
cout << "Hasil Perkalian 3 angka tersebut: ";
cout << tambah(x, y, z);
cout << endl;
goto ulang;
getch();
}
Membuat Switch - Case - Default
#include <cstdlib>
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
main() {
char Kode;
cout << " Masukan Kode Barang [A..D] : ";
cin >> Kode;
switch (Kode) {
case 'a':
case 'A':
cout << " Kode " << Kode << " Adalah Kode Untuk Barang Elektronik ";
break;
case 'b':
case 'B':
cout << " Kode " << Kode << " Adalah Kode Untuk Barang Kosmetik ";
break;
case 'c':
case 'C':
cout << " Kode " << Kode << " Adalah Untuk Barang Alat Tulis Kantor ";
break;
case 'd':
case 'D':
cout << " Kode " << Kode << " Adalah Untuk Barang Alat-Alat Dapur ";
break;
default:
cout << " Kode " << Kode << " Tidak Ada Dalam Daftar ";
break;
}
getch();
}
Membuat Looping While
#include <cstdlib>
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
main() {
int i = 1;
while (i <= 10) {
cout << i << " ";
++i;
}
getch();
}
Membuat Looping For
#include <cstdlib>
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
main() {
int i;
for (i = 1; i <= 100; i += 8) {
cout << i << " ";
}
getch();
}
Membuat Looping Do - While
#include <cstdlib>
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
main() {
int i = 2;
do {
cout << i << " ";
i += 2;
}
while (i <= 30);
getch();
}
Membuat Looping Do - Break - While
#include <cstdlib>
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
main() {
int i = 2;
do {
if (i >= 100)
break;
cout << i << " ";
}
while (++i);
getch();
}
Membuat Looping For - Nested
#include <cstdlib>
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
main() {
int a, b;
for (a = 1; a <= 10; a++) {
for (b = a; b <= 10; b++) {
cout << a << " ";
}
cout << endl;
}
getch();
}
Membuat For - Continue
#include <cstdlib>
#include <stdio.h>
#include <conio.h>
#include<iostream>
using namespace std;
main() {
int a;
for (a = 1; a <= 10; ++a) {
if (a == 6 || a == 8)
continue;
cout << a << " ";
}
getch();
}
Membuat For - Break
#include <cstdlib>
#include <stdio.h>
#include <conio.h>
#include<iostream>
using namespace std;
main() {
int a = 3, b = 2, c = 1, d;
cout << " Bilangan A | Bilangan B | Bilangan C" << endl;
cout << " ===========================================" << endl;
for (d = 1; d <= 10; ++d) {
a += b;
b += c;
c += 2;
cout << " " << a << " | " << b << " | " << c << endl;
if (c == 13)
break;
}
getch();
}
Membuat Pertukaran Angka
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <stdio.h>
using namespace std;
void TukarBilangan(int X, int Y) {
int Z = Y;
X = Y;
Y = Z;
cout << " Didalam fungsi : " << endl;
cout << " Bilangan ke-1 : " << X << endl;
cout << " Bilangan ke-2 : " << Y << endl;
cout << endl;
}
int main() {
int Bilangan1, Bilangan2;
cout << " Masukan bilangan pertama : ";
cin >> Bilangan1;
cout << " Masukan bilangan kedua : ";
cin >> Bilangan2;
cout << " Keadaan awal" << endl;
cout << " Bilangan ke-1 : " << Bilangan1 << endl;
cout << " Bilangan ke-2 : " << Bilangan2 << endl;
cout << endl;
TukarBilangan(Bilangan1, Bilangan2);
cout << " Keadaan akhir" << endl;
cout << " Bilangan ke-1 : " << Bilangan1 << endl;
cout << " bilangan ke-2 : " << Bilangan2 << endl;
cout << endl;
getch();
}
Membuat If - Else
#include <cstdlib>
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
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();
}
Membuat Bubble Sort
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <stdio.h>
using namespace std;
int main() {
int a[7];
int j, k, c, temp;
cout << "Memasukan nilai pada elemen array : " << endl;
for (c = 0; c < 7; c++) {
cout << "A[" << c << "] = ";
cin >> a[c];
}
cout << "\nNilai elemen array sebelum diurutkan : " << endl;
for (j = 0; j < 6; j++) {
for (k = 7; k > 0; k--) {
if (a[k] < a[k - 1]) {
temp = a[k];
a[k] = a[k - 1];
a[k - 1] = temp;
}
}
}
cout << "\nNilai elemen array setelah diurutkan : " << endl;
for (c = 0; c < 7; c++) {
cout << "A[" << c << "] = " << a[c] << endl;
}
getch();
}
Membuat Array Satu Dimensi (1D)
Indeks Nilai
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int nilai[5] = {
56,
67,
99,
72,
67
};
int a;
for (a = 0; a < 5; a++) {
cout << " Nilai Array Index Ke-" << a << " = ";
cout << nilai[a] << endl;
}
getch();
}
Pilih Hari
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main() {
string hari[7] = {
"Senin",
"Selasa",
"Rabu",
"Kamis",
"jumat",
"Sabtu",
"Minggu"
};
int pil;
for (int i = 0; i < 7; i++) {
cout << i + 1 << " : " << hari[1] << endl;
}
cout << "Pilih No.Berapa ? ";
cin >> pil;
switch (pil) {
case 1:
cout << hari[0];
break;
case 2:
cout << hari[1];
break;
case 3:
cout << hari[2];
break;
case 4:
cout << hari[3];
break;
case 5:
cout << hari[4];
break;
case 6:
cout << hari[5];
break;
case 7:
cout << hari[6];
break;
}
}
Membuat Array Dua Dimensi (2D)
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main() {
int a, b, jual[4][4];
system("cls");
for (a = 1; a <= 3; a++) { //nomor
for (b = 1; b <= 3; b++) { //tahun
cout << " Data Ke-" << a << " " << b << endl;
cout << " Jumlah Penjualan : ";
cin >> jual[a][b];
}
}
cout << " Data Penjalan Pertahun\n";
cout << "-----------------------\n";
cout << " No 2001 2002 2003\n";
cout << "-----------------------\n";
for (a = 1; a <= 3; a++) {
cout << " " << setiosflags(ios::left) << setw(5) << a;
for (b = 1; b <= 3; b++) {
cout << " " << setiosflags(ios::left) << setw(4);
cout << jual[a][b];
cout << " ";
}
cout << endl;
}
cout << "-----------------------\n";
getch();
}
Itulah kesemua
source code
coding-an C++ yang mungkin bisa bermanfaat buat kalian semua, terutama bagi kalian yang sedang belajar bahasa pemrograman C++ ini.
1 comment