Pemrograman 2: C# (C-Sharp)
Tanggal 28 Februari 2011
1) Hello World !
================
using System;
namespace HelloWorld
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Console.WriteLine("Hello, World !");
Console.ReadLine();
}
}
}
2) Variabel, Konstanta, Tipe Data
=================================
using System;
namespace VariabelKonstantaTipedata
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
int a=5,b;
b=a+1;
Console.WriteLine("{0}",b);
Console.ReadLine();
}
}
}
3) Operator
===========
using System;
namespace Operator
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
int pilihan,a,b,c;
string jawab;
START:
Console.Write("1. Penambahan\n2. Pengurangan\n3. Pembagian\n4. Perkalian\nPilih nomor: ");
pilihan = Convert.ToInt32(Console.ReadLine());
if (pilihan == 1)
{
Console.Write("Masukkan bilangan pertama: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Masukkan bilangan kedua: ");
b = Convert.ToInt32(Console.ReadLine());
c = a + b;
Console.WriteLine("Penambahan dari {0} dan {1} adalah {2}",a,b,c);
}
if (pilihan == 2)
{
Console.Write("Masukkan bilangan pertama: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Masukkan bilangan kedua: ");
b = Convert.ToInt32(Console.ReadLine());
c = a - b;
Console.WriteLine("Pengurangan dari {0} dan {1} adalah {2}",a,b,c);
}
if (pilihan == 3)
{
Console.Write("Masukkan bilangan pertama: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Masukkan bilangan kedua: ");
b = Convert.ToInt32(Console.ReadLine());
c = a / b;
Console.WriteLine("Pembagian dari {0} dan {1} adalah {2}",a,b,c);
}
if (pilihan == 4)
{
Console.Write("Masukkan bilangan pertama: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Masukkan bilangan kedua: ");
b = Convert.ToInt32(Console.ReadLine());
c = a * b;
Console.WriteLine("Perkalian dari {0} dan {1} adalah {2}",a,b,c);
}
if (pilihan>4)
{
Console.WriteLine("Anda salah memasukkan kode");
}
Console.Write("Ingin mengulang lagi? [y/t]");
jawab = Console.ReadLine();
if (jawab == "y")
{
goto START;
}
}
}
}
4) If
======
using System;
namespace If
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
string jawaban;
Console.Write("Apa gender anda[p/l]? ");
jawaban = Console.ReadLine();
if(jawaban=="l")
{
Console.Write("Selamat Pagi, Mas");
}
if(jawaban=="p")
{
Console.Write("Selamat Pagi, Mbak");
}
if(jawaban != "p" && jawaban != "l")
{
Console.Write("Selamat Pagi ... Ah ,gender apaan nih?");
}
Console.ReadLine();
}
}
}
5) Array
========
using System;
namespace Array
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
int a,b;
int[,] angka = new int[4,4];
for(a=0;a<4;a++)
{
for(b=0;b<=a;b++)
{
angka[a,b]=0;
}
}
b=0;
for(a=0;a<4;a++)
{
angka[a,b]=1;
b=b+1;
}
for(a=0;a<4;a++)
{
for(b=0;b<4;b++)
{
Console.Write("{0}",angka[a,b]);
}
Console.Write("\n");
}
Console.ReadLine();
}
}
}
6) Loop
=======
using System;
namespace Loop
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
int a,b;
for(a=0;a<5;a++)
{
for(b=0;b<=a;b++)
{
Console.Write("* ** *");
}
Console.Write("\n");
}
Console.ReadLine();
}
}
}
7) Input & Output
=================
using System;
namespace InputOutput
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
string username,password,nama,NIM,jawab;
do
{
Console.WriteLine("Daftarkan keanggotaan perpustakaan");
Console.Write("Username: ");
username=Console.ReadLine();
Console.Write("Password: ");
password=Console.ReadLine();
Console.Write("Nama : ");
nama=Console.ReadLine();
Console.Write("NIM : ");
NIM=Console.ReadLine();
Console.WriteLine("Username: {0}",username);
Console.WriteLine("Password: {0}",password);
Console.WriteLine("Nama : {0}",nama);
Console.WriteLine("NIM : {0}",NIM);
Console.Write("Data anda sudah benar[y/t]? ");
jawab=Console.ReadLine();
Console.WriteLine("");
}
while(jawab=="t");
Console.WriteLine("Terima kasih ,{0} Anda telah terdaftar di perpustakaan POLMAN",nama);
Console.ReadLine();
}
}
}
8) Exception Handling
=====================
using System;
namespace ExceptionHandling_028
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
int umur;
START:
try
{
Console.Write("Berapa umur anda? ");
umur=Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("Error: Input integer type data only");
Console.ReadLine();
goto START;
}
Console.WriteLine("Tipe data benar!");
Console.ReadLine();
}
}
}
Pemrograman 2: C# (C-Sharp)
Tanggal 2 Maret 2011
1) Selisih Waktu
================
using System;
namespace Selisih_waktu_028
{
public struct waktu
{
public int hh,mm,ss;
public waktu(int p1, int p2, int p3)
{
hh = p1;
mm = p2;
ss = p3;
}
}
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
waktu W1 = new waktu();
waktu W2 = new waktu();
waktu W3 = new waktu();
Console.Write("Jam awal Percakapan: ");
W1.hh=Convert.ToInt32(Console.ReadLine());
Console.Write("Menit awal Percakapan: ");
W1.mm=Convert.ToInt32(Console.ReadLine());
Console.Write("Detik awal Percakapan: ");
W1.ss=Convert.ToInt32(Console.ReadLine());
Console.Write("Jam akhir Percakapan: ");
W2.hh=Convert.ToInt32(Console.ReadLine());
Console.Write("Menit akhir Percakapan: ");
W2.mm=Convert.ToInt32(Console.ReadLine());
Console.Write("Detik akhir Percakapan: ");
W2.ss=Convert.ToInt32(Console.ReadLine());
if(W2.ss>=W1.ss)
W3.ss=W2.ss-W1.ss;
else
{
W3.ss=(W2.ss+60)-W1.ss;
W2.mm=W2.mm-1;
}
if(W2.mm>=W1.mm)
W3.mm=W2.mm-W1.mm;
else
{
W3.mm=(W2.mm+60) - W1.mm;
W2.hh=W2.hh - W1.hh;
}
W3.hh=W2.hh-W1.hh;
Console.WriteLine("Lama Percakapan adalah {0}:{1}:{2}",W3.hh,W3.mm,W3.ss);
Console.ReadLine();
}
}
}
2) Faktorial
============
using System;
namespace Faktorial_028
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
int n,k,fak;
Console.Write("masukkan angka: ");
n = Convert.ToInt32(Console.ReadLine());
if (n==0)
fak = 1;
else
{
fak=1;
for(k=1;k<=n;k++)
{
fak=fak*k;
}
}
Console.WriteLine("Faktorial dari {0} adalah {1}",n,fak);
Console.ReadLine();
}
}
}
3) Perbandingan angka
=====================
using System;
namespace Perbandingan_angka_028
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
int x,c,ulang=1,a=0;
int[] angka = new int[3];
for(x=0;x<3;x++)
{
Console.Write("Masukkan angka ke {0}: ",x+1);
angka[x]=Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("A = {0}",angka[0]);
Console.WriteLine("B = {0}",angka[1]);
Console.WriteLine("C = {0}",angka[2]);
while(ulang==1)
{
a=0;
for(x=0;x<2;x++)
{
if(angka[x]>angka[x+1])
{
c=angka[x+1];
angka[x+1]=angka[x];
angka[x]=c;
a=1;
}
}
if(a==0)
ulang=0;
}
Console.WriteLine("Bilangan terbesar adalah {0}",angka[2]);
Console.ReadLine();
}
}
}
4) Kalkulator
=============
using System;
namespace Kalkulator_028
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
int pilihan;
Console.Write("1. Penambahan\n2. Pengurangan\n3. Pembagian\n4. Perkalian\nPilih nomor: ");
pilihan = Convert.ToInt32(Console.ReadLine());
if (pilihan == 1)
{
int a,b;
Console.Write("Masukkan bilangan pertama: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Masukkan bilangan kedua: ");
b = Convert.ToInt32(Console.ReadLine());
int c;
c = a + b;
Console.WriteLine("Angka 1: {0}",a);
Console.WriteLine("Operator (+, -, *, /) : +");
Console.WriteLine("Angka 2: {0}",b);
Console.WriteLine("Jumlah adalah {0}",c);
}
if (pilihan == 2)
{
int a,b;
Console.Write("Masukkan bilangan pertama: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Masukkan bilangan kedua: ");
b = Convert.ToInt32(Console.ReadLine());
int c;
c = a - b;
Console.WriteLine("Angka 1: {0}",a);
Console.WriteLine("Operator (+, -, *, /) : -");
Console.WriteLine("Angka 2: {0}",b);
Console.WriteLine("Jumlah adalah {0}",c);
}
if (pilihan == 3)
{
Console.Write("Masukkan bilangan pertama: ");
double a,b;
a = Convert.ToDouble(Console.ReadLine());
Console.Write("Masukkan bilangan kedua: ");
b = Convert.ToDouble(Console.ReadLine());
double c;
c = a / b;
Console.WriteLine("Angka 1: {0}",a);
Console.WriteLine("Operator (+, -, *, /) : /");
Console.WriteLine("Angka 2: {0}",b);
Console.WriteLine("Jumlah adalah {0}",c);
}
if (pilihan == 4)
{
int a,b;
Console.Write("Masukkan bilangan pertama: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Masukkan bilangan kedua: ");
b = Convert.ToInt32(Console.ReadLine());
int c;
c = a * b;
Console.WriteLine("Angka 1: {0}",a);
Console.WriteLine("Operator (+, -, *, /) : *");
Console.WriteLine("Angka 2: {0}",b);
Console.WriteLine("Jumlah adalah {0}",c);
}
if (pilihan>4)
{
Console.WriteLine("Anda salah memasukkan kode");
}
Console.ReadLine();
}
}
}
ok, just checking. Radix
BalasHapus