15 CONTOH PROGRAM C YANG MENGGUNAKAN TIPE DATA DAN OPERATOR
Selamat pagi sobat semua. bagi sahabat yang sedang belajar bahasa C/C++ berikut saya share contoh program C yang menggunakan tipe data dan operator didalamnya. soal ini dapat sobat gunakan dalam latihan.
1. Kasus 1 : ukuran tipe data
#include <iostream>
using namespace std;
int main()
{
cout<<"Size
Of char : "<<sizeof(char)<<endl;
cout<<"Size
Of int : "<<sizeof(int)<<endl;
cout<<"Size
Of short int : "<<sizeof(short int)<<endl;
cout<<"Size
Of long int : "<<sizeof(long int)<<endl;
cout<<"Size
Of float : "<<sizeof(float)<<endl;
cout<<"Size
Of double : "<<sizeof(double)<<endl;
cout<<"Size
Of wchar_t : "<<sizeof(wchar_t)<<endl;
return
0;
}
2. Kasus 2 : operator aritmatika
//initialization
//of variables
#include <iostream>
using namespace std;
int main()
{
int
a=5;
int
b=(3);
int
c{2};
int
result;
a=
a+b;
result
= a - c;
cout<<result;
return
0;
}
3. Kasus 3 : tipe data string
#include <iostream>
#include <string>
using namespace std;
int main()
{
string
mystring;
mystring
= "this is the initial string content";
cout<<mystring<<endl;
mystring
= "this is a different string content";
cout<<mystring<<endl;
return
0;
}
4. Kasus 4 : operator aritmatika
#include <iostream>
using namespace std;
const double pi = 3.14159;
const char newline = '\n';
int main()
{
double
r=5.0; // radius
double
circle;
circle
= 2 * pi * r;
cout<<circle;
cout<<newline;
}
5. Kasus 5 : deklarasi konstanta
#include <iostream>
using namespace std;
int main()
{
const
int side=50;
int
area;
area
= side * side;
cout<<"The
area of the square with side : " << side<<" is:
"<<area;
return
0;
}
6. Kasus 6 : deklarasi konstanta
#include <iostream>
using namespace std;
#define pi 3.14159
#define newline '\n'
int main()
{
double
r=5.0;
double
circle;
circle
= 2 * pi * r;
cout<<circle;
cout<<newline;
}
7. Kasus 7 : tipe data signed dan unsigned
#include <iostream>
using namespace std;
/* This program shows the
* signed and unsigned int
*/
int main()
{
short
int i;
short
unsigned int j;
j=50000;
i=j;
cout<<i<<"
"<<j;
return
0;
}
8. Kasus 8
#include <iostream>
using namespace std;
int main()
{
int
a, b=3;
a=b;
a+=2;
cout<<a;
return
0;
}
9. Kasus 9 : operator aritmatika
#include <iostream>
using namespace std;
int main()
{
int
a, b, c;
a=2;
b=7;
c=(a>b)?
a: b;
cout<<c<<'\n';
return
0;
}
10. Kasus 10 : penggunaan define
#include <iostream>
using namespace std;
#define length 10
#define width 5
#define newline '\n'
int main()
{
int
area;
area
= length * width;
cout<<area;
cout<<newline;
return
0;
}
11. Kasus 11 : penggunaan increment
#include <iostream>
using namespace std;
int main()
{
int
c; //mendeklarasikan variabel c
//mengisikan
nilai ke dalam variabel c dengan nilai 5
c=5;
//melakukan
pre-increment
cout<<"Nilai
C awal : "<<c<<endl;
cout<<"Nilai
++C : "<<++c<<endl;
cout<<"Nilai
C akhir : "<<c<<endl;
cout<<'\n';
//mengubah
nilai yang terdapat dalam variabel c dengan nilai 10
c=10;
//melakukan
post increment
cout<<"Nilai
C awal : "<<c<<endl;
cout<<"Nilai
++C : "<<++c<<endl;
cout<<"Nilai
C akhir : "<<c<<endl;
return
0;
}
12. Kasus 12 : penggunaan if
#include <iostream>
using namespace std;
int main()
{
int
x=5;
int
y=7;
if(!x==y)
cout<<"x
does not equal y";
else
cout<<"x
equals y";
return
0;
}
13. Kasus 13 : operator logika
#include <iostream>
using namespace std;
int main()
{
bool
a = true;
bool
b = false;
bool
c = true;
cout<<"(a
&& b) : "<< (a&&b);
cout<<"\n
(a && c) : "<< (a&&c);
cout<<"\n
(a && b && c) : "<< (a&&b&&c);
cout<<"\n
(a || b) : "<< (a||b);
cout<<"\n
(a || c) : "<< (a||c);
cout<<"\n
(a || b || c) : "<<(a||b||c);
cout<<"\n
!a : "<< !a;
cout<<"\n
!b : "<< !b;
cout<<"\n
!c : "<< !c;
cout<<"\n
!(b && a) : "<<!(b&&a);
//getch
}
14. Kasus 14 : menghitung volume bola, kerucut, balok dan limas dengan if.
#include <iostream>
using namespace std;
int main()
{
int
pil;
float
p1, p2, l1, l2, t1, t2, t3, r1, r2;
float
v, lp,v_kerucut, v_bola, v_limas;
float
phi=3.14159;
//p1
= panjang balok, p2= panjang alas limas
//t1=tinggi
balok, t2 tinggi kerucut, t3 = tinggi limas
//r1=jari
jari kerucut, r2=jari-jari bola
//proses
input p,l,t
cout<<"Daftar
Paket Program Program :";
cout<<"\n
1. Menghitung Volume dan Luas Permukaan Balok";
cout<<"\n
2. Menghitung Volume Kerucut";
cout<<"\n
3. Menghitung Volume Bola";
cout<<"\n
4. Menghitung Volume Limas";
cout<<"\n
Masukkan Nomor Pilihan = ";
cin>>pil;
if(pil=1){
cout<<"\n
===================================";
cout<<"\n
Masukkan nilai panjang balok = ";
cin>>p1;
cout<<"\n
Masukkan nilai lebar balok = ";
cin>>l1;
cout<<"\n
Masukkan nilai tinggi balok = ";
cin>>t1;
//Balok
p1 l t1 v lp
v=p1*l1*t1;
lp=(2*p1*l1)+(2*p1*t1)+(2*l1*t1);
cout<<"\n
===================================";
cout<<"\n
Volume balok = "<<v;
cout<<"\n
Luas permukaan balok = "<<lp;
}
else
if(pil=2){
cout<<"\n
===================================";
cout<<"\n
Masukkan nilai jari-jari kerucut = ";
cin>>r1;
cout<<"\n
Masukkan nilai tinggi kerucut = ";
cin>>t2;
//kerucut
r1, t2 v_kerucut
v_kerucut
= ( phi * r1 * r1 * t2 )/3 ;
cout<<"\n
===================================";
cout<<"\n
Volume kerucut = "<<v_kerucut;
}
else
if(pil=3){
cout<<"\n
===================================";
cout<<"\n
Masukkan nilai jari-jari bola = ";
cin>>r2;
//bola
r2, v_bola
v_bola
= (4 * phi * r2 * r2 * r2 )/3 ;
cout<<"\n
===================================";
cout<<"\n
Volume bola = "<<v_bola;
}
else
{
cout<<"\n
===================================";
cout<<"\n
Masukkan nilai panjang alas limas = ";
cin>>p2;
cout<<"\n
Masukkan nilai lebar alas limas = ";
cin>>l2;
cout<<"\n
Masukkan nilai tinggi limas = ";
cin>>t3;
//limas
p2, l2, t3, v_limas
v_limas
= ((p2*l2) * t3)/3 ;
cout<<"\n
===================================";
cout<<"\n
Volume limas = "<<v_limas;
cout<<"\n
===================================";
}
return
0;
}
15. Kasus 15 : menghitung volume bola, kerucut, balok dan limas.
#include <iostream>
using namespace std;
int main()
{
float
p1, p2, l1, l2, t1, t2, t3, r1, r2;
float
v, lp,v_kerucut, v_bola, v_limas;
float
phi=3.14159;
//p1
= panjang balok, p2= panjang alas limas
//t1=tinggi
balok, t2 tinggi kerucut, t3 = tinggi limas
//r1=jari
jari kerucut, r2=jari-jari bola
//proses
input p,l,t
cout<<"\n
===================================";
cout<<"\n
Masukkan nilai panjang balok = ";
cin>>p1;
cout<<"\n
Masukkan nilai lebar balok = ";
cin>>l1;
cout<<"\n
Masukkan nilai tinggi balok = ";
cin>>t1;
cout<<"\n
===================================";
cout<<"\n
Masukkan nilai jari-jari kerucut = ";
cin>>r1;
cout<<"\n
Masukkan nilai tinggi kerucut = ";
cin>>t2;
cout<<"\n
===================================";
cout<<"\n
Masukkan nilai jari-jari bola = ";
cin>>r2;
cout<<"\n
===================================";
cout<<"\n
Masukkan nilai panjang alas limas = ";
cin>>p2;
cout<<"\n
Masukkan nilai lebar alas limas = ";
cin>>l2;
cout<<"\n
Masukkan nilai tinggi limas = ";
cin>>t3;
//proses
perhitungan
//Balok
p1 l t1 v lp
v=p1*l1*t1;
lp=(2*p1*l1)+(2*p1*t1)+(2*l1*t1);
//kerucut
r1, t2 v_kerucut
v_kerucut
= ( phi * r1 * r1 * t2 )/3 ;
//bola
r2, v_bola
v_bola
= (4 * phi * r2 * r2 * r2 )/3 ;
//limas
p2, l2, t3, v_limas
v_limas
= ((p2*l2) * t3)/3 ;
//proses
output
cout<<"\n
===================================";
cout<<"\n
Volume balok = "<<v;
cout<<"\n
Luas permukaan balok = "<<lp;
cout<<"\n
===================================";
cout<<"\n
Volume kerucut = "<<v_kerucut;
cout<<"\n
===================================";
cout<<"\n
Volume bola = "<<v_bola;
cout<<"\n
===================================";
cout<<"\n
Volume limas = "<<v_limas;
cout<<"\n
===================================";
return
0;