CONTOH PROGRAM DASAR C DAN C++ (CIN, COUT, DEFINE, INTEGER, FLOAT, CHAR)
Bagi teman-teman yang sedang belajar. Kumpulan program C dan C++ ini dapat teman-teman gunakan untuk memahami konsep dasar dari pemprograman C dan C++. Semoga kumpulan program dasar ini dapat membantu teman-teman dalam belajar.
1. Kasus 1
#include <iostream>
using namespace std;
int main () {
char greeting[6] = {'H' , 'e', 'l', 'l', 'o', '\0'};
int number;
cout<<"greeting message: ";
cout<<greeting<<endl;
cout<<"input number:";
cin>>number;
return 0;
}
2. Kasus 2
#include <iostream>
/*using namespace std;*/
int main () {
char greeting[6] = {'H' , 'e', 'l', 'l', 'o', '\0'};
int number;
std::cout<<"greeting message: ";
std::cout<<greeting<<std::endl;
std::cout<<"input number:";
std::cin>>number;
return 0;
}
3. Kasus 3
#include <iostream>
/*using namespace std;*/
namespace tes
{
char greeting[6] = {'H' , 'e', 'l', 'l', 'o', '\0'};
}
int main () {
int number;
std::cout<<"greeting message: ";
std::cout<<tes::greeting<<std::endl;
std::cout<<"input number:";
std::cin>>number;
return 0;
}
4. Kasus 4
#include <iostream>
using namespace std;
//variable declaration;
extern int a, b;
extern int c;
extern float f;
int main () {
//variable definition
int a, b;
int c;
float f;
//actual initialization
a=10;
b=20;
c=a+b;
cout <<c<< endl;
f=70.0/3.0;
cout <<f<< endl ;
return 0;
}
5. Kasus 5 (C++)
#include <stdio.h>
#define length 10
#define width 5
#define newline '\n'
int main () {
int area;
area = length * width;
printf ("value of area : %d", area);
printf ("%c", newline);
return 0;
}
6. Kasus 6
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
float hours, rate, gaji_total;
cout<<"Masukkan Jumlah Jam Kerja = ";
cin>>hours;
cout<<"Masukkan Gaji Kerja Perjam = ";
cin>>rate;
gaji_total=hours*rate;
cout<<"Jumlah Gaji Total = "<< gaji_total;
return 0;
}
7. Kasus 7
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
float x, y, sum;
cout<<"Masukkan Nilai Bilangan 1 = ";
cin>>x;
cout<<"Masukkan Nilai Bilangan 2 = ";
cin>>y;
sum=x+y;
cout<<"Jumlah Bilangan 1 + Bilangan 2 = "<<sum;
return 0;
}
8. Kasus 8
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
float jumlah_es, jumlah_ayam, harga_total;
cout<<"Masukkan Jumlah Ayam Goreng = ";
cin>>jumlah_es;
cout<<"Masukkan Jumlah Es Teh = ";
cin>>jumlah_ayam;
cout<<"Harga Ayam Goreng = 15.000 \n";
cout<<"Harga Es Teh = 2.000 \n";
harga_total=2000*jumlah_es+15000*jumlah_ayam;
cout<<"Harga Total = "<<harga_total;
return 0;
}
9. Kasus 9
#include <iostream>
using namespace std;
int main()
{
char nama_depan[30],nama_belakang[30], nim[30];
float nt1, nt2, nt3, nt4, nt5, average;
//nt = nilai tugas
cout<<"Masukkan Nama : ";
cin>>nama_depan>>nama_belakang;
cout<<"Masukkan NIM : ";
cin>>nim;
cout<<"Masukkan Nilai Tugas 1 : ";
cin>>nt1;
cout<<"Masukkan Nilai Tugas 2 : ";
cin>>nt2;
cout<<"Masukkan Nilai Tugas 3 : ";
cin>>nt3;
cout<<"Masukkan Nilai Tugas 4 : ";
cin>>nt4;
cout<<"Masukkan Nilai Tugas 5 : ";
cin>>nt5;
average=(nt1+nt2+nt3+nt4+nt5)/5;
cout<<"\n";
cout<<"==============================================\n";
cout<<"Nama : "<<nama_depan<<nama_belakang<<"\n";
cout<<"NIM : "<<nim<<"\n";
cout<<"Nilai Tugas 1 : "<<nt1<<"\n";
cout<<"Nilai Tugas 2 : "<<nt2<<"\n";
cout<<"Nilai Tugas 3 : "<<nt3<<"\n";
cout<<"Nilai Tugas 4 : "<<nt4<<"\n";
cout<<"Nilai Tugas 5 : "<<nt5<<"\n";
cout<<"Nilai Rata-rata : "<<average<<"\n";
return 0;
}
10. Kasus 10
#include <iostream>
using namespace std;
int main()
{
float r;
float L, phi;
phi=3.14;
cout<< "masukkan jari-jari=";
cin>>r;
L=phi*r*r;
cout<<"L"<<"="<<phi<<"*"<<r<<"*"<<r<<"\n";
cout<<"luas lingkaran=";
cout<<L;
return 0;
}
11. Kasus 11
#include<iostream>
using namespace std;
int main()
{
int vari;
float varf;
vari=10;
varf=10.001;
cout<<"nilai awal vari = "<<vari<<"\n";
cout<<"nilai awal varf = "<<varf<<"\n";
cout<<"\n";
vari=vari/3;
varf=varf/3;
cout<<"hasil setelah dibagi"<<"\n";
cout<<"vari"<<vari<<"\n";
cout<<"varf"<<varf<<"\n";
return 0;
}
Baca Juga :
Baca Juga :