Kumpulan Program Kecil pada C#

Standar

1. Cara Menampilkan Hello World pada Bahasa C#

using System;

namespace Hello_World_0320100021
{
class Class1
{
[STAThread]
static void Main(string[] args)
{

Console.WriteLine(“Hello, World”);
Console.ReadLine();
}
}
}

2. Contoh Program Variabel, Konstanta, Tipe Data pada Bahasa C#

using System;
namespace Variable_Constanta_Tipe_Data_0320100021
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int a=10,b;

b=a*10;
Console.WriteLine(“Angkanya Adalah : {0}”,b);
Console.ReadLine();
}
}
}

3. Contoh Program Operator pada Bahasa C#

using System;

namespace Operator_0320100021
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
double a,b;
Console.Write(“Masukkan Angka : “);
a = Convert.ToInt32(Console.ReadLine());
b=a/2;
Console.WriteLine(“\nHasilnya bila dibagi 2 : {0} “,b);
Console.ReadLine();
}
}
}

4. Contoh Program IF pada Bahasa C#

using System;

namespace If_0320100021
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string a;
Console.WriteLine(“Masukkan Jenis Kelamin Anda (p/l) : “);
a = Console.ReadLine();

if (a==”p”)
{
Console.WriteLine(“Good Morning!!!”);
}
if (a==”l”)
{
Console.WriteLine(“Good Morning!!!”);
}
if (a!=”p” && a!=”l”)
{
Console.WriteLine(“??????????”);
}
Console.ReadLine();
}
}
}

5. Contoh Program Loop pada Bahasa C#

using System;

namespace Loop
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int n;
string s;
do
{
Console.Write(“masukan bilangan : “);
n=Convert.ToInt32(Console.ReadLine());
    
if (n%2==0)
{
Console.Write(“\n{0} adalah bilangan genap”, n);
}
if (n%2!=0)
{
Console.Write(“\n{0} adalah bilangan ganjil”, n);
}
Console.Write(“\n\nMau Coba Lagi?? “);
s=Console.ReadLine();
Console.Write(“\n”);
}
while(s==”y” || s==”Y”);
}
}
}

6. Contoh Program Array pada Bahasa C#

using System;

namespace Array_0320100021
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int x,y;
int[,] angka = new int[8,8];

for(x=0;x<=3;x++)
{
for(y=0;y<=x;y++)
{
angka[x,y]=0;
}
}
y=0;
for(x=0;x<=3;x++)
{
angka[x,y]=1;
y=y+1;
}
for(x=3;x>=0;x–)
{
angka[x,y]=1;
y=y+1;
}
for(x=0;x<4;x++)
{
for(y=0;y<8;y++)
{
Console.Write(“{0}”,angka[x,y]);
}
Console.Write(“\n”);
}
Console.ReadLine();
}
}
}

7. Contoh Program Input dan Output di Layar

using System;

namespace Input_Output_0320100021
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string NIM,prodi,nama,tgl,alamat,pilih;
do
{
Console.WriteLine(“Daftar diri mahasiswa”);
Console.Write(“\nNIM    : “);
NIM=Console.ReadLine();
Console.Write(“Prodi  : “);
prodi=Console.ReadLine();
Console.Write(“Nama   : “);
nama=Console.ReadLine();
Console.Write(“Alamat : “);
alamat=Console.ReadLine();
Console.WriteLine(“\nNIM    : {0}”,NIM);
Console.WriteLine(“Prodi  : {0}”,prodi);
Console.WriteLine(“Nama   : {0}”,nama);
Console.WriteLine(“Alamat : {0}”,alamat);

Console.Write(“Data anda sudah benar[y/t]? “);
pilih=Console.ReadLine();
Console.WriteLine(“”);
}
while(pilih==”t”);
Console.WriteLine(“Terima kasih, {0}”,nama);
Console.ReadLine();
}
}
}

8. Contoh Program Exception Handling pada Bahasa C#

using System;

namespace Exception_Handling_0320100021
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int jlmh;
START:
try
{
Console.Write(“Berapa anggota keluarga anda : “);
jlmh=Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine(“\nError: Input integer type data only”);
Console.ReadLine();
goto START;
}
Console.WriteLine(“\nTipe data anda benar!!!”);
Console.ReadLine();
}
}
}

Membuat Program C# Sederhana

1. Selisih_Waktu_0320100021

using System;

namespace Selisih_Waktu_0320100021
{
class Class1
{
public struct JAM
{
public int jam;
public int mnt;
public int dtk;
}
static void Main(string[] args)
{
JAM jam1;
JAM jam2;
JAM jam3;
Console.WriteLine(“Jam Awal Percakapan : “);
jam1.jam = Convert.ToInt32(Console.ReadLine());
jam1.mnt = Convert.ToInt32(Console.ReadLine());
jam1.dtk = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“Jam Selesai Percakapan : “);
jam2.jam = Convert.ToInt32(Console.ReadLine());
jam2.mnt = Convert.ToInt32(Console.ReadLine());
jam2.dtk = Convert.ToInt32(Console.ReadLine());
jam3.mnt = 0;
jam3.dtk = 0;
if(jam2.dtk>=jam1.dtk)
{
jam3.dtk = jam2.dtk – jam1.dtk;
}
if(jam2.dtk
{
jam3.dtk = (jam2.dtk + 60) – jam1.dtk;
jam2.mnt = jam2.mnt – 1;
}
if(jam2.mnt>jam1.mnt)
{
jam3.mnt = jam2.mnt – jam1.mnt;
}
if(jam2.mnt
{
jam3.mnt = (jam2.mnt + 60) – jam1.mnt;
jam2.jam = jam2.jam – 1;
}
jam3.jam = jam2.jam – jam1.jam;
Console.WriteLine(“\nSelisih Waktu = {0}:{1}:{2}”,jam3.jam,jam3.mnt,jam3.dtk);
Console.ReadLine();
}
}
}

2. Faktorial_0320100021

using System;

namespace Faktorial_0320100021
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int a,b;
long c;
c = 1;
Console.Write(“Input Angka : “);
a = Convert.ToInt32(Console.ReadLine());
for(b=1;b<=a;b++)
{
c = c * b;
}
Console.WriteLine(“\n\nNilai Faktorial Dari {0} adalah {1}”,a,c);
Console.ReadLine();
}
}
}



3. Perbandingan_Angka_0320100021

using System;

namespace Perbandingan_Angka_0320100021
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int b,c;
int[] a;
a = new int[3];
Console.Write(“A = ? “);
a[0] = Convert.ToInt32(Console.ReadLine());
Console.Write(“B = ? “);
a[1] = Convert.ToInt32(Console.ReadLine());
Console.Write(“C = ? “);
a[2] = Convert.ToInt32(Console.ReadLine());
c = a[0];
for(b=1;b<=2;b++)
{
if(c
{
c = a[b];
}
}
Console.WriteLine(“Bilangan Terbesar Adalah {0}”,c);
Console.ReadLine();
}
}
}

4. Kalkulator_0320100021

using System;

namespace Kalkulator_0320100021
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int a,c,d;
decimal e;
char b;
Console.Write(“Angka 1 : “);
a = Convert.ToInt32(Console.ReadLine());
Console.Write(“Operator (+, -, *, /) : “);
b = Convert.ToChar(Console.ReadLine());
Console.Write(“Angka 2 : “);
c = Convert.ToInt32(Console.ReadLine());
switch(b)
{
case ‘+’ : d = a + c;
Console.Write(“Jumlah adalah {0}”,d);
break;
case ‘-‘ : d = a – c;
Console.Write(“Jumlah adalah {0}”,d);
break;
case ‘*’ : d = a * c;
Console.Write(“Jumlah adalah {0}”,d);
break;
case ‘/’ : e = Convert.ToDecimal(a) / Convert.ToDecimal(c);
Console.Write(“Jumlah adalah {0}”,e);
break;
}
Console.ReadLine();
}
}
}

http://muh-gunandar.blogspot.com/2011/03/kumpulan-tugas-c-manajemen.html

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s