#include <iostream>
#include <stdio.h>
#include <math.h>
#include <conio.h>
using namespace std;
main ()
{
float A, B, C,E;
float D, x1, x2;
cout << "Nilai A = ";
cin >> A;
cout << "Nilai B = ";
cin >> B;
cout << "Nilai C = ";
cin >> C;
D = (B*B) - (4*A*C);
E = sqrt(D);
cout << "Nilai D = "<<D << endl;
cout << "Akar Dari D = "<<E << endl;
if(D > 0)
{
x1 = (-B+sqrt(D))/(2*A);
x2 = (-B-sqrt(D))/(2*A);
cout << "x1 = "<<x1 << endl;
cout << "x2 = "<<x2 << endl;
}
else if (D < 0)
{
cout << "Nilai D < 0";
}
else
{x1 = -B/2*A;
x2 = x1;
cout << "x1 = "<<x1 << endl;
cout << "x2 = "<<x2 << endl; }
getch();
}
Outputnya :
program dev c++
Senin, 19 Februari 2018
Program C ++ Menentukan Hari Dalam Suatu Bulan
#include <cstdlib>
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int bulan, tahun, hari;
int jum_hari[12]=
{31,28,31,30,31,30,31,31,30,31,30,31};
char b1[10],b2[10],b3[10],b4[10],b5[10],b6[10],b7[10],
b8[10],b9[10],b10[10],b11[10],b12[10];
cout<<"\t\t--------------------------------------------\n";
cout<<"\t\t>>MENENTUKAN JUMLAH HARI DALAM SUATU BULAN<<\n";
cout<<"\t\t--------------------------------------------\n";
cout<<"\n";
cout<<"\t\tKeterangan\n";
cout<<"\t\t-------------------------------------------\n";
cout<<"\t\tBulan 1: Januari | Bulan 7 :Juli\n";
cout<<"\t\tBulan 2: Februari | Bulan 8 :Agustus\n";
cout<<"\t\tBulan 3: Maret | Bulan 9 :September\n";
cout<<"\t\tBulan 4: April | Bulan 10:Oktober\n";
cout<<"\t\tBulan 5: Mei | Bulan 11:November\n";
cout<<"\t\tBulan 6: Juni | Bulan 12:Desember\n";
cout<<"\t\t-------------------------------------------\n";
cout<<"\t\tMasukkan Bulan (1--12) :";
cin>>bulan;
cout<<"\t\tMasukkan Tahun :";
cin>>tahun;
if(bulan==2)
{if(tahun &4==0)
hari=29;
else
hari=28;
}
else
hari=jum_hari[bulan-1];
cout<<"\t\t-------------------------------------------\n";
cout<<"\t\tJumlah hari dalam bulan :";
cout<<bulan<<endl;
cout<<"\t\tPada tahun :";
cout<<tahun<<endl;
cout<<"\t\tDalam Satu Bulan Terdapat :",
cout<<hari;
cout<<"hari\n";
cout<<endl;
system("pause");
return 0;
}
Outputnya :
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int bulan, tahun, hari;
int jum_hari[12]=
{31,28,31,30,31,30,31,31,30,31,30,31};
char b1[10],b2[10],b3[10],b4[10],b5[10],b6[10],b7[10],
b8[10],b9[10],b10[10],b11[10],b12[10];
cout<<"\t\t--------------------------------------------\n";
cout<<"\t\t>>MENENTUKAN JUMLAH HARI DALAM SUATU BULAN<<\n";
cout<<"\t\t--------------------------------------------\n";
cout<<"\n";
cout<<"\t\tKeterangan\n";
cout<<"\t\t-------------------------------------------\n";
cout<<"\t\tBulan 1: Januari | Bulan 7 :Juli\n";
cout<<"\t\tBulan 2: Februari | Bulan 8 :Agustus\n";
cout<<"\t\tBulan 3: Maret | Bulan 9 :September\n";
cout<<"\t\tBulan 4: April | Bulan 10:Oktober\n";
cout<<"\t\tBulan 5: Mei | Bulan 11:November\n";
cout<<"\t\tBulan 6: Juni | Bulan 12:Desember\n";
cout<<"\t\t-------------------------------------------\n";
cout<<"\t\tMasukkan Bulan (1--12) :";
cin>>bulan;
cout<<"\t\tMasukkan Tahun :";
cin>>tahun;
if(bulan==2)
{if(tahun &4==0)
hari=29;
else
hari=28;
}
else
hari=jum_hari[bulan-1];
cout<<"\t\t-------------------------------------------\n";
cout<<"\t\tJumlah hari dalam bulan :";
cout<<bulan<<endl;
cout<<"\t\tPada tahun :";
cout<<tahun<<endl;
cout<<"\t\tDalam Satu Bulan Terdapat :",
cout<<hari;
cout<<"hari\n";
cout<<endl;
system("pause");
return 0;
}
Outputnya :
Program C++ Menghitung luas alas, luas permukaan dan volume kerucut
#include <iostream>
#include <conio.h>
#define phi 3.14
using namespace std;
main(){
float r_alas, r_selimut, t, volume_krucut, luas_alas, luas_selimut;
cout<<"maksukkan jari jari alas = ";
cin>>r_alas;
cout<<"maksukkan jari jari selimut = ";
cin>>r_selimut;
cout<<"masukkan tinggi kerucut = ";
cin>>t;
luas_alas=phi*r_alas*r_alas;
volume_krucut=luas_alas*t;
luas_selimut=phi*r_selimut*r_selimut/3;
cout<<"luas_alas adalah "<<luas_alas<<endl;
cout<<"volume_krucut "<<volume_krucut<<endl;
cout<<"luas permukaan krucut "<<(luas_selimut+luas_alas);
getch();
}
#include <conio.h>
#define phi 3.14
using namespace std;
main(){
float r_alas, r_selimut, t, volume_krucut, luas_alas, luas_selimut;
cout<<"maksukkan jari jari alas = ";
cin>>r_alas;
cout<<"maksukkan jari jari selimut = ";
cin>>r_selimut;
cout<<"masukkan tinggi kerucut = ";
cin>>t;
luas_alas=phi*r_alas*r_alas;
volume_krucut=luas_alas*t;
luas_selimut=phi*r_selimut*r_selimut/3;
cout<<"luas_alas adalah "<<luas_alas<<endl;
cout<<"volume_krucut "<<volume_krucut<<endl;
cout<<"luas permukaan krucut "<<(luas_selimut+luas_alas);
getch();
}
Outputnya :
Program C++ Penjumlahan dan perkalian(hanya menggunakan operator +)
#include <iostream>
#include <conio.h>
using namespace std;
int tambah (int a,int b) {
int x;
x=a+b;
return x;
}
int kali (int a,int b){
int y;
for (int i=1; i<=b; i++) {
y=y+a;
}
return y;
}
main (){
int a,b,x,y;
cout<<"nilai a : ";
cin>>a;
cout<<"nilai b : ";
cin>>b;
x=tambah(a,b);
cout<<"hasil tambah : "<<x<<endl;
y=kali(a,b);
cout<<"hasil kali : "<<y<<endl;
getch ();
}
#include <conio.h>
using namespace std;
int tambah (int a,int b) {
int x;
x=a+b;
return x;
}
int kali (int a,int b){
int y;
for (int i=1; i<=b; i++) {
y=y+a;
}
return y;
}
main (){
int a,b,x,y;
cout<<"nilai a : ";
cin>>a;
cout<<"nilai b : ";
cin>>b;
x=tambah(a,b);
cout<<"hasil tambah : "<<x<<endl;
y=kali(a,b);
cout<<"hasil kali : "<<y<<endl;
getch ();
}
Outputnya :
Program C ++ Menampilkan bilangan ganjil 1-100
#include <iostream>
#include <conio.h>
using namespace std;
int main (){
int a,b,i;
a=-1;
cout<<"bilangan ganjil (1_100) adalah:"<<endl;
for (i=1;i<=50;i++)
{
b=2+a;
cout<<b<<" ";
a=b;
}
getch ();
}
#include <conio.h>
using namespace std;
int main (){
int a,b,i;
a=-1;
cout<<"bilangan ganjil (1_100) adalah:"<<endl;
for (i=1;i<=50;i++)
{
b=2+a;
cout<<b<<" ";
a=b;
}
getch ();
}
Outputnya :
Logika IF dalam C++ dan Contoh Program C++ Menggunakan IF
Untuk lebih jelasnya mengenai perintah IF dalam C++, silahkan Googling aja bila memang belum mampu membeli buku panduan C++ yang lengkap. Untuk saat ini, kang asep sule langsung saja ingin memberikan contoh program c++ menggunakan perintah IF.
Contoh kasus :
- Toko buku "kamseupay" mengadakan promosi.
- Jika membeli buku dengan total pembelian lebih dari atau sama dengan Rp. 100.000, maka mendapat diskon potongan harga sebesar 10% dari total pembelian.
- Jika membeli buku dengan total pembelian kurang dari Rp.100.000, maka tidak mendapat diskon.
- Total bayar = Total pembelian - diskon.
Berikut Sourcodenya :
#include<conio.h>
#include<stdio.h>
#include<iostream>
using namespace std;
main()
{
float total_beli, diskon, total_bayar;
cout<<"Toko Buku Kamseupay"<<endl;
cout<<"==================="<<endl;
cout<<"Total Pembelian = Rp. ";cin>>total_beli;
cout<<endl;
if (total_beli>=100000)
diskon = 0.1 * total_beli;
else
diskon = 0;
cout<<"======================"<<endl;
cout<<"Diskon = Rp. "<<diskon<<endl;
total_bayar=total_beli-diskon;
cout<<"Total Bayar = Rp. "<<total_bayar;
getch();
}
Outputnya :
Program C++ Teller Bank menggunakan Antrian
#include <iostream>
#include <conio.h>
#include <windows.h>
#define max 2
using namespace std;
struct antri
{
int data;
};
struct identitas
{
char nama[20];
char no_rek[15];
char transaksi;
int nominal;
};
main()
{
identitas id[15];
antri ant[15];
int cek=0, y=0, hapus;
char pil;
do {
system("cls");
cout<<"\n\t\t ====================================";
cout<<"\n\t\t || PROGRAM TELLER BANK BOJOKKU GALAK ||";
cout<<"\n\t\t ===================================="<<endl<<endl;
cout<<"1. Masukan Antrian"<<endl;
cout<<"2. Proses Antrian"<<endl;
cout<<"3. Keluar"<<endl;
cout<<endl;
cout<<"Masukkan pilihan Anda (1-3)\t= ";
cin>>pil;
cout<<endl;
if(pil!='1' && pil !='2' && pil !='3' )
{
cout<<"Anda salah memasukkan pilihan\n\n";
system("PAUSE");
}
else
{
if(pil=='1') //PUSH
{
if(cek==15 && y==15)
{
cout<<"ANTRIAN PENUH!!!";
}
cout<<endl;
system("cls");
cout<<"Nama Nasabah\t: ";
cin>>id[cek].nama;
cout<<"Nomor Rekening\t: ";
cin>>id[cek].no_rek;
ulang:
cout<<"Jenis Transaksi\t: \n";
cout<<"\t: A.Debet\n";
cout<<"\t B.Kredit\n"<<"Pilih\t: ";
cin>>id[cek].transaksi;
if(id[cek].transaksi=='a' || id[cek].transaksi=='A' || id[cek].transaksi=='b' || id[cek].transaksi=='B')
{
cout<<"Nominal\t\t: ";
cin>>id[cek].nominal;
}
else
{
cout<<"\n\nAnda Salah Memilih Jenis Transaksi\n\n";
cout<<"\n\n";
goto ulang;
}
cout<<endl;
cek++;
system("cls");
cout<<"Antrian saat ini\t:\n\n";
for(int z=0;z<cek;z++)
{
cout<<"Nomor Antri\t: "<<z+1;
cout<<endl;
cout<<"Nama Nasabah\t: "<<id[z].nama<<endl;
cout<<"Nomor Rekening\t: "<<id[z].no_rek<<endl;
cout<<"Jenis Transaksi\t: ";
if(id[z].transaksi=='a' || id[z].transaksi=='A')
{
cout<<"Debet";
}
else if(id[z].transaksi=='b' || id[z].transaksi=='B')
{
cout<<"Kredit";
}
cout<<endl;
cout<<"Nominal\t\t: "<<id[z].nominal<<endl;
cout<<endl<<endl;
}
cout<<"\n\n";
system("PAUSE");
}
else
{
if(pil=='2') //POP
{
if(cek==0)
cout<<"Antrian kosong";
else
{
for(int z=0;z<cek;z++)
hapus=z;
for(int v=0;v<cek;v++)
ant[v].data=ant[v+1].data;
ant[cek].data=NULL;
cek--;
system("cls");
cout<<"\nAntrian dengan nomor 1 diproses\n";
cout<<"\n\t\tData Nasabah \n\n";
cout<<"Nama Nasabah\t: "<<id[0].nama<<endl;
cout<<"Nomor Rekening\t: "<<id[0].no_rek<<endl;
cout<<"Jenis Transaksi\t: "<<id[0].transaksi<<endl;
cout<<"Nominal\t\t: "<<id[0].nominal<<endl;
cout<<endl;
{
int b;
for(b=0;b<cek;b++)
id[b]=id[b+1];
b--;
}
}
cout<<endl;
if(cek==0)
cout<<"Antrian kosong";
else
cout<<"Nomor Antrian saat ini : ";
for(int z=0;z<cek;z++)
{
cout<<" | ";
cout<<" "<<z+1;
cout<<" | ";
}
cout<<endl<<endl;
system("PAUSE");
}
}
}
}while(pil!='3');
}
Outputnya :
#include <conio.h>
#include <windows.h>
#define max 2
using namespace std;
struct antri
{
int data;
};
struct identitas
{
char nama[20];
char no_rek[15];
char transaksi;
int nominal;
};
main()
{
identitas id[15];
antri ant[15];
int cek=0, y=0, hapus;
char pil;
do {
system("cls");
cout<<"\n\t\t ====================================";
cout<<"\n\t\t || PROGRAM TELLER BANK BOJOKKU GALAK ||";
cout<<"\n\t\t ===================================="<<endl<<endl;
cout<<"1. Masukan Antrian"<<endl;
cout<<"2. Proses Antrian"<<endl;
cout<<"3. Keluar"<<endl;
cout<<endl;
cout<<"Masukkan pilihan Anda (1-3)\t= ";
cin>>pil;
cout<<endl;
if(pil!='1' && pil !='2' && pil !='3' )
{
cout<<"Anda salah memasukkan pilihan\n\n";
system("PAUSE");
}
else
{
if(pil=='1') //PUSH
{
if(cek==15 && y==15)
{
cout<<"ANTRIAN PENUH!!!";
}
cout<<endl;
system("cls");
cout<<"Nama Nasabah\t: ";
cin>>id[cek].nama;
cout<<"Nomor Rekening\t: ";
cin>>id[cek].no_rek;
ulang:
cout<<"Jenis Transaksi\t: \n";
cout<<"\t: A.Debet\n";
cout<<"\t B.Kredit\n"<<"Pilih\t: ";
cin>>id[cek].transaksi;
if(id[cek].transaksi=='a' || id[cek].transaksi=='A' || id[cek].transaksi=='b' || id[cek].transaksi=='B')
{
cout<<"Nominal\t\t: ";
cin>>id[cek].nominal;
}
else
{
cout<<"\n\nAnda Salah Memilih Jenis Transaksi\n\n";
cout<<"\n\n";
goto ulang;
}
cout<<endl;
cek++;
system("cls");
cout<<"Antrian saat ini\t:\n\n";
for(int z=0;z<cek;z++)
{
cout<<"Nomor Antri\t: "<<z+1;
cout<<endl;
cout<<"Nama Nasabah\t: "<<id[z].nama<<endl;
cout<<"Nomor Rekening\t: "<<id[z].no_rek<<endl;
cout<<"Jenis Transaksi\t: ";
if(id[z].transaksi=='a' || id[z].transaksi=='A')
{
cout<<"Debet";
}
else if(id[z].transaksi=='b' || id[z].transaksi=='B')
{
cout<<"Kredit";
}
cout<<endl;
cout<<"Nominal\t\t: "<<id[z].nominal<<endl;
cout<<endl<<endl;
}
cout<<"\n\n";
system("PAUSE");
}
else
{
if(pil=='2') //POP
{
if(cek==0)
cout<<"Antrian kosong";
else
{
for(int z=0;z<cek;z++)
hapus=z;
for(int v=0;v<cek;v++)
ant[v].data=ant[v+1].data;
ant[cek].data=NULL;
cek--;
system("cls");
cout<<"\nAntrian dengan nomor 1 diproses\n";
cout<<"\n\t\tData Nasabah \n\n";
cout<<"Nama Nasabah\t: "<<id[0].nama<<endl;
cout<<"Nomor Rekening\t: "<<id[0].no_rek<<endl;
cout<<"Jenis Transaksi\t: "<<id[0].transaksi<<endl;
cout<<"Nominal\t\t: "<<id[0].nominal<<endl;
cout<<endl;
{
int b;
for(b=0;b<cek;b++)
id[b]=id[b+1];
b--;
}
}
cout<<endl;
if(cek==0)
cout<<"Antrian kosong";
else
cout<<"Nomor Antrian saat ini : ";
for(int z=0;z<cek;z++)
{
cout<<" | ";
cout<<" "<<z+1;
cout<<" | ";
}
cout<<endl<<endl;
system("PAUSE");
}
}
}
}while(pil!='3');
}
Outputnya :
Langganan:
Postingan (Atom)