Anda di halaman 1dari 19

Argumen

#include <iostream>
#include <string.h>

using namespace std;

int main(int argc, char* argv[])


{
//program disini
for (int i=0; i< argc; i++)
for (int j=0, n=strlen(argv[i]); j<n; j++)
cout<<"argv ["<< i <<"] [" << j <<"] is: "<<argv[i][j]<<"\n";

return 0;
}

Array
#include <iostream>
using namespace std;

int main()
{
int i, a[4];
a[0]=0;
a[1]=1;
a[2]=5;
a[3]=10;
for(i=0; i<4; i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}

Array0
#include <iostream>
using namespace std;

int main()
{
int sample[10];
int t;

for(t=0; t<10; t++)


sample[t]=t;

for(t=0; t<10; t++)


cout<<"this is sample[" << t << "]" << sample[t] << "\n";
return 0;
}
Array1
int main()
{
int i, min, max, jml=0;
float rata2;
int nums[10];

cout<<"Masukkan 10 angka: ";


for(i=0; i<10; i++)
{
cin>>nums[i];
jml=jml+nums[i];
if (i==0)
min=max=nums[i];
else
{
if(nums[i]<min)
min=nums[i];
if(nums[i]>max)
max=nums[i];
}
}

rata2 = jml/10.0;
cout<< "Rata-rata adalah "<<rata2<<endl;
cout<<"Min : "<<min<<endl;
cout<<"Max : "<<max<<endl;
return 0;
}

ArrayFungsi
#include <iostream>
using namespace std;

void display(int num[10]);

int main()
{
int t[10], i;
for (i=0; i<10; i++);
t[i]=i;
display(t);
cout<<endl;
return 0;
}

void display(int num[10])


{
int i;
for (i=0; i<10; i++)
cout<<num[i]<<' ';
}
ArrayMulti
#include <iostream>
using namespace std;

int main()
{
int t,i,nums[3][4];
for(t=0; t<3; t++)
{
for(i=0; i<4; i++)
{
nums[t][i]=(t*4)+i+1;
cout<<nums[t][i]<<' ';
}
cout<<endl;
}
return 0;
}

Array Pass By Reference


#include <iostream>
using namespace std;

void build_array(int array_variable[50], int length_of_array);

int main()
{
int values[50];
cout<<"The value at location 7 starts as "<<values[7]<<endl;
build_array(values, 50);
cout<<"The value at location 7 is now "<<values[7]<<endl;
return 0;
}

void build_array(int array_variable[50], int length_of_array)


{
for (int i=0; i<length_of_array; i++)
array_variable[i]=i;
}

Array String
#include <iostream>
using namespace std;
int main()
{
char greeting[6]={'H', 'e', 'l', 'l', 'o', '\0'};
cout<<"Greeting message: ";
cout<<greeting<<endl;
return 0;
}
Bintang Diag
#include <iostream>

using namespace std;

int main()
{
int i,j,n;
cout<<"masukkan jumlah baris: ";
cin>>n;

for ( i =0; i < n; i++)


{
for (j = 0; j < n; j++)
{
if ( i == j)
{
cout<<"*";
}
else
cout<<"o";
}
cout<<"\n";
}
return 0;
}

Bintang Jumlah
#include <iostream>

using namespace std;

main()
{
int i,j,n,a;
cout<<"masukkan n="; cin>>n;
for (i=1; i<=n; i++)
{
for (j=1;j<=i;j++)
{
cout <<"*";
a=a+1;
}
cout<<endl;
}
cout<<"Jumlah bintang="<<a<<endl;
}

Constant Reference
#include <iostream>
using namespace std;
void doit(const int &x)
{
// x=5;
}

int main()
{
int z=27;
doit(z);
cout<<"Z is now "<<z<<endl;

return 0;
}

Deret Geo
#include <iostream>
//#include <math.h>

using namespace std;

int main()
{
int i,a,r,n,jumlah=0;
cout<<"Program Deret Bilangan \n";
cout<<"======================\n";
cout<<"Masukkan Bilangan Pertama: ";
cin>>a;
cout<<"Masukkan Banyak Bilangan: ";
cin>>n;
cout<<"======================\n";
cout<<"Deret Bilangan adalah: ";

//jumlah=0;

for(i=1; i<=n; i++)


{
// r=pow((i+1),2);
r=(i+1)*(i+1);
cout<<a<<" ";
jumlah=jumlah+a;
a=a+r;
}
cout<<"\n";
cout<<"Jumlah bilangan:" << jumlah <<"\n";
return 0;
}

Deret Geo2
# include <iostream>
# include <iomanip>

using namespace std;

int main ()
{ int i,sp,r,bs,jd=0;
cout<<"suku pertama="; cin>>sp;
cout<<"rasio="; cin>>r;

cout<<"banyaknya suku="; cin>>bs;


cout<<"deret geometri :";
for (i=1;i<=bs;i++)
{
jd=jd+sp;
cout<<setw(5)<<sp;
sp=sp*r;
}
cout<<endl;
cout<<"jml deret="<<jd;
cout<<"\n";
}

Deret Geo3
#include <iostream>

using namespace std;

int main()
{
int i, sp, r, bs, jd=0;
cout<<"Suku pertama= ";
cin>>sp;
cout<<"Rasio= ";
cin>>r;
cout<<"Banyak suku= ";
cin>>bs;
cout<<"Deret Geometri= ";
for(i=1; i<=bs; i++)
{
jd=jd+sp;
cout<<sp;
cout<<" ";
sp=sp*r;
}
cout<<"\nJumlah deret= "<<jd;
cout<<endl;
}

Durian Mangga
#include <iostream>
using namespace std;

int main()
{
int durian, mangga, buah;
mangga=8;
durian=5;
buah=durian+mangga;
cout<<"Jumlah buah="<<buah;
cout<<endl;
}
Durian Mangga2
#include <iostream.h>
int main()
{
int durian,mangga;
int buah;
cout<<"jml mangga="; cin>>mangga;
cout<<"jml durian="; cin>>durian;
buah=mangga+durian;
cout<<"Jumlah buah="<<buah;
cout<<endl;
}

For Bintang
#include <iostream>

using namespace std;

main()
{
int i,j,n,a;
cout<<"masukkan n="; cin>>n;
for (i=1; i<=n; i++)
{
for (j=1;j<=i;j++)
cout <<"*";
a=i;
a=a+1;
cout<<endl;
}
cout<<"Jumlah bintang="<<a<<endl;
}

Fungsi
#include <iostream>
using namespace std;

int max(int num1, int num2)


{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}

int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
int ret;
// calling a function to get max value.
ret = max(a, b);
cout << "Max value is : " << ret << endl;
return 0;
}

Fungsi 2
// fungsi2.c : demo fungsi dengan input dan do-while
#include <iostream>

using namespace std;

// prototype
int positifInt();

int main(void)
{
int n = positifInt();
cout<<"Terima kasih atas "<<n<"-nya";
cout<<"\n";
}

// meminta input positive dari user


int positifInt(void)
{
int n;
do
{
cout<<"Masukkan integer postif: ";
cin>>n;
}
while (n < 1);
return n;
}

Hello
#include <iostream>

int main() {
std::cout << "Hello World!";
std::cout << std::endl;

return 0;
}

Isi Fibonaci
/* isFibonaci.cpp
* fibonacco.cpp : cek apakah fibonaci
* 04/10/2015
* bagustris, bagustris@yahoo.com
*/

#include <iostream>
#include <math.h>

using namespace std;

bool isPerfectSquare(int x)
{
int s = sqrt(x);
return (s*s == x);
}

bool isFibonacci(int n)
{
// n is fibonacci if one of 5*n*n + 4 or - or both
// is perfect square
return isPerfectSquare(5*n*n + 4) ||
isPerfectSquare(5*n*n - 4);
}

int main()
{
int i;
cout<<"Masukkan bilangan N: ";
cin>>i;
isFibonacci(i)? cout << i << " adalah bilangan Fibonacci\n":
cout << i << " bukan bilangan Fibonacci\n";
return 0;
}

Modulus
// modulus.cpp : demo modulus untuk mengecek ganjil/genap
#include <iostream>

using namespace std;

int main()
{
int num;
cin >> num;
// num % 2 menghitung sisa bagi ketika dibagi 2
if ( num % 2 == 0 )
{
cout << num << " adalah genap\n";
}
else
cout << num << " adalah ganjil\n";
return 0;
}

My Class
#include "myclass.h"

int main()
{
MyClass a;
return 0;
}

Next Prima
#include <iostream>
#include<math.h>

using namespace std;

int next_prime (int n)


{
int p=1;
if(n==2)
p=2;
for (int i=sqrt(n);i>=2;i++)
{
if (n%i==0)
{
p=1;
break;}
else
p=n;
}
if (p==1)
{
next_prime(n +1);
cout<<endl<<n +1;} //while
return p;
}

main()
{
int n, prime;
cout<<"please enter a natural number:";
cin>>n;
prime=next_prime(n);
if (prime==n)
cout<<n<<" is a prime number."<<endl;
else
{
cout<<n<<" is not a prime number"<<endl;
cout<<"the next prime number is "<<prime<<endl;
}
return 0;
}

Non Class
#include<iostream>
using namespace std;

void offsetVector(double &x0, double &x1, double &y0, double &y1,


double offsetX, double offsetY) {
x0 += offsetX;
x1 += offsetX;
y0 += offsetY;
y1 += offsetY;
}

void printVector(double x0, double x1, double y0, double y1) {


cout << "(" << x0 << "," << y0 << ") -> ("
<< x1 << "," << y1 << ")" << endl;
}

int main() {
double xStart = 1.2;
double xEnd = 2.0;
double yStart = 0.4;
double yEnd = 1.6;
offsetVector(xStart, xEnd, yStart, yEnd, 1.0, 1.5);
printVector(xStart, xEnd, yStart, yEnd);
}

Pass By 0
#include <iostream>
using namespace std;

void by_val(int arg) {


arg += 2;
}
void by_ref(int &arg) {
arg += 2;
}

int main()
{
int x = 0;
by_val(x);
cout << x << endl; // prints 0
by_ref(x);
cout << x << endl; // prints 2

int y = 0;
by_ref(y);
cout << y << endl; // prints 2
by_val(y);
cout << y << endl; // prints 2
}

Pass By 1
#include <iostream>
using namespace std;

void Modify(int p, int *q, int *o)


{
p = 27; // passed by value - only the local parameter is modified
*q = 27; // passed by value or reference, check call site to determine
which
*o = 27; // passed by value or reference, check call site to determine
which
}

int main()
{
int a = 1;
int b = 1;
int x = 1;
int *c = &x;
cout<<a<<' '<<b<<' '<<c<<' '<<x<<'\n';
Modify(a, &b, c); // a is passed by value, b is passed by reference
by creating a pointer,
// c is a pointer passed by value
// b and x are changed
cout<<a<<' '<<b<<' '<<c<<' '<<x<<'\n';
return(0);
}

Pass By Reference
#include <iostream>
using namespace std;

void doit(int &x)


{
x=5;
}

int main()
{
int z=27;
doit(z);
cout<<"Z is now "<<z<<endl;

return 0;
}

Pass By Reference 1
#include <iostream>
using namespace std;

void ValTest (int parm1, int parm2)


{
parm1 = 33;
parm2 = 44;
}

void PtrTest (int *parm1, int *parm2)


{
*parm1 = 55;
*parm2 = 66;
}

// Parameters are passed by reference


void RefTest (int &parm1, int &parm2)
{
parm1 = 77;
parm2 = 88;
}

int main ()
{
int val1 = 0, val2 = 0;
int val3 = 0, val4 = 0;
int val5 = 0, val6 = 0;

ValTest(val1, val2);
cout << "val1 = " << val1 << ", val2 = " << val2 << endl; //0 0

PtrTest(&val3, &val4);
cout << "val3 = " << val3 << ", val4 = " << val4 << endl; //55 66
/dir dir

RefTest(val5, val6);
cout << "val5 = " << val5 << ", val6 = " << val6 << endl; //77 88

return(0);
}

Pass By Value
#include <iostream>
using namespace std;

void doit(int x)
{
x=5;
}

int main()
{
int z=27;
doit(z);
cout<<"Z is now "<<z<<endl;
return 0;
}

Pointer 0
#include <iostream>
using namespace std;
int main ()
{
int var1=3;
char var2[10]={'0','1','2','3','4','5','6','7','8','\0'};
cout << "Address of var1 variable: ";
cout << &var1 << endl;
cout << "Address of var2 variable: ";
cout << &var2 << endl;
return 0;
}
Pointer 1
#include <iostream>
using namespace std;

int main()
{
int i,j;
int *p; // pointer to integer
p = &i;
*p = 5;
j = i;
cout<<i<<' '<<j<<' '<<*p<<'\n';
return 0;
}

Pointer 2
#include <iostream>
using namespace std;
int main ()
{
int var = 20;
int *ip;
ip = &var;
// actual variable declaration.
// pointer variable
// store address of var in pointer variable
cout << "Value of var variable: ";
cout << var << endl;
// print the address stored in ip pointer variable
cout << "Address stored in ip variable: ";
cout << ip << endl;
// access the value at the address available in pointer
cout << "Value of *ip variable: ";
cout << *ip << endl;
return 0;
}

Prima Fungsi
#include <iostream>
#include <math.h>

using namespace std;

int isprima(int n)
{
int li;
if (n == 2)
return 1;

if (n % 2 == 0 || n == 1)
return 0;
for(li = 3;li <= sqrt(n);li+=2)
{
if (n%li==0)
return 0;
}
return 1;
}

int main(void)
{
int li, n, i=0;
cout<<"Bilangan prima 1 - ";
cin>>n;

for (li=1;li<=n;li++)
if (isprima(li))
cout<<li<<" ";
cout<<endl;
}

Prima Lima Pertama


/*primaLimaPertama.cpp
* print lima bilangan prima pertama
* 04/19/2015
* bagustris, bagustris@yahoo.com
*/

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
int bPerP, n, c, b=2;
cout<<"Masukkan N: ";
cin>>n;
cout<<"Bilangan prima : ";
if (n>=0)
{
for(c=0; c < n; c++)
{
int p=1; cp=1;
p=p+1;
bPerP=b%p;
if(bPerP==0)
{
cp=cp+1;
b=p;
}
if (cp==2)
cout<<cp<<" ";
}
}
else
cout<<"Input salah";
cout<<"\n";
}
Prima N
#include <iostream.h>
#include <math.h>

using namespace std;

int main()
{
int x=2;
int prime_counter=1;
int prima_sequnce(prime_counter)=x;

Reference
#include <iostream>
using namespace std;

int main()
{
int x;
int& foo = x;
foo = 56;
cout << x <<endl;
}

Rekursi
#include <iostream>

using namespace std;

void recurse ( int count ) {


cout <<count;
recurse ( count + 1 );
}

int main() {
recurse ( 1 );
return 0;
}

Satu Lima
#include <iostream>

using namespace std;

int main()
{
int i,n=5;
for(i=1; i<=n; i++)
cout<<i<<endl;
}

String 0
#include <iostream>
using namespace std;

int main ()
{
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
cout << "Greeting message: ";
cout << greeting << endl;
return 0;
}

String Class
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str1 = "Hello";
string str2 = "World";
string str3;
int len ;
// copy str1 into str3
str3 = str1;
cout << "str3 : " << str3 << endl;
// concatenates str1 and str2
str3 = str1 + str2;
cout << "str1 + str2 : " << str3 << endl;
// total lenghth of str3 after concatenation
len = str3.size();
cout << "str3.size() : " << len << endl;
return 0;
}

Struct
#include <iostream>
#include <cstring>
using namespace std;
void printBook( struct Books book );
struct Books
{
char title[50];
char author[50];
char subject[100];
int
book_id;
};
int main( )
{
struct Books Book1;
struct Books Book2;
// Declare Book1 of type Book
// Declare Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Belajar Pemrorgraman C/C++");
strcpy( Book1.author, "Bagus Tris Atmaja");
strcpy( Book1.subject, "Bahasa Pemrograman");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
// Print Book1 info
printBook( Book1 );
// Print Book2 info
printBook( Book2 );
return 0;
}
void printBook( struct Books book )
{
cout << "Book title : " << book.title <<endl;
cout << "Book author : " << book.author <<endl;
cout << "Book subject : " << book.subject <<endl;
cout << "Book id : " << book.book_id <<endl;
}

Ulang lagi
/* ulangLagi.cpp
* demo while: Ulang lagi dan lagi
* 04/15/2015
* bagustris, bagustris@yahoo.com
*/

#include <iostream>
using namespace std;

int main()
{
char ulang='y';
while (ulang=='y')
{
cout<<"Teknik Fisika ITS"<<endl;
cout<<"Apakah anda ingin mengulan [y/t]: ";
cin>>ulang;
}
}

UTS
#include <iostream>

using namespace std;


main()
{
int i,j,n;
cout<<"Entry Bilangan Bulat: ";
cin>>n;
for(i=0; i<n; i++)
{
for(j=0;j<n; j++)
{
if(j<=((n-1)/2))
{
if(i<=((n-1)/2))
cout<<"o ";
else
cout<<"* ";
}
else
{
if(i<=((n-1)/2))
cout<<"* ";
else
cout<<"o ";
}
}
cout<<"\n";
}
return 0;
}

Anda mungkin juga menyukai