Contoh-Contoh Coding Sederhana Dalam Bahasa C#
Coding C#
1. Hello World
using System;
namespace Hello
{
class Demonstrator
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.ReadLine();
}
}
}
2. Variabel, Konstanta Dan Tipe Data
using System;
namespace Variabel__Konstanta__dan_Tipe_data
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int d,e,f; //Deklarasi Tipe Data Integer dengan variabel d,e, dan f
e = 2; //Deklarasi Konstanta dari variabel e, yaitu 2
Console.Write("Input Angka : ");
d = Convert.ToInt32(Console.ReadLine()); //Rubah tipe data dari string ke integer
f = d % e;
if(f==0)
{
Console.WriteLine("Angka {0} adalah bilangan genap",d);
}
if(f!=0)
{
Console.WriteLine("Angka {0} adalah bilangan ganjil",d);
}
Console.ReadLine();
}
}
}
3. Operator, Program Operator perkalian sederhana
using System;
namespace Operator
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int a,b,c;
Console.Write("Input Nilai Pertama: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Input Nilai Kedua: ");
b = Convert.ToInt32(Console.ReadLine());
c = a * b;
Console.WriteLine("Hasil Nilai: {0}", c);
Console.ReadLine();
}
}
}
4. IF, Program menentukan bilangan ganjil atau genap.
using System;
namespace IF
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int a,b;
Console.Write("Input Nilai: ");
a = Convert.ToInt32(Console.ReadLine());
b = a % 2;
if(b == 0)
{
Console.WriteLine("\nAngka {0} Merupakan Bilangan Genap",a);
}
else
{
Console.WriteLine("\nAngka {0} Merupakan Bilangan Ganjil",a);
}
Console.ReadLine();
}
}
}
5. Loop, program menghitung bilangan faktorial
using System;
namespace Loop
{
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();
}
}
}
6. Array, Program melihat data Array
using System;
namespace Array
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int a,b,c;
int[] array;
array = new int[100];
Console.Write("Input Jumlah Data : ");
a = Convert.ToInt32(Console.ReadLine());
for(b=0;b<=a-1;b++)
{
Console.Write("Input Data ke {0} : ",b+1);
array[b] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("\n\nMenampilkan Data");
Console.Write("\nInput Data yang anda cari : ");
c = Convert.ToInt32(Console.ReadLine());
for(b=0;b<=a-1;b++)
{
if(array[b]==c)
{
Console.WriteLine("Data yang anda cari ada di urutan ke {0}",b+1);
Console.ReadLine();
}
}
}
}
}
7. Input - Output
using System;
namespace InputOutput
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string a,b,c;
Console.Write("Nama\t: ");
a = Console.ReadLine();
Console.Write("NIM\t: ");
b = Console.ReadLine();
Console.Write("Prodi\t: ");
c = Console.ReadLine();
Console.WriteLine("\n\nMenampilkan Data");
Console.WriteLine("Nama\t: {0}", a);
Console.WriteLine("NIM\t: {0}", b);
Console.WriteLine("Prodi\t: {0}", c);
Console.ReadLine();
}
}
}
8. Exceptional Handling
Exceptional Handling Berlaku apabila user melakukan kesalahan dalam menginput. Dalam Contoh dibawah, bila user menginput huruf atau kata, maka program akan menampilkan pesan error.
using System;
namespace Exceptional_Handing
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int a,b,c;
try
{
Console.Write("Input Nilai Pertama: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Input Nilai Kedua: ");
b = Convert.ToInt32(Console.ReadLine());
c = a * b;
Console.WriteLine("{0} * {1} = {2}",a,b,c);
Console.ReadLine();
}
catch
{
Console.WriteLine("Maaf, Anda Salah memasukkan Input");
Console.ReadLine();
}
}
}
}
Tugas harian
1. Selisih Jam
using System;
namespace Selisih_Waktu_020
{
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());
Console.WriteLine("Menit Awal Percakapan : ");
jam1.mnt = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Detik Awal Percakapan : ");
jam1.dtk = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Jam Selesai Percakapan : ");
jam2.jam = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Menit Selesai Percakapan : ");
jam2.mnt = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Detik Selesai Percakapan : ");
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<jam1.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<jam1.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
using System;
namespace Faktorial_020
{
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
using System;
namespace Perbandingan_Angka_020
{
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<a[b])
{
c = a[b];
}
}
Console.WriteLine("Bilangan Terbesar Adalah {0}",c);
Console.ReadLine();
}
}
}
using System;
namespace Hello
{
class Demonstrator
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.ReadLine();
}
}
}
2. Variabel, Konstanta Dan Tipe Data
using System;
namespace Variabel__Konstanta__dan_Tipe_data
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int d,e,f; //Deklarasi Tipe Data Integer dengan variabel d,e, dan f
e = 2; //Deklarasi Konstanta dari variabel e, yaitu 2
Console.Write("Input Angka : ");
d = Convert.ToInt32(Console.ReadLine()); //Rubah tipe data dari string ke integer
f = d % e;
if(f==0)
{
Console.WriteLine("Angka {0} adalah bilangan genap",d);
}
if(f!=0)
{
Console.WriteLine("Angka {0} adalah bilangan ganjil",d);
}
Console.ReadLine();
}
}
}
3. Operator, Program Operator perkalian sederhana
using System;
namespace Operator
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int a,b,c;
Console.Write("Input Nilai Pertama: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Input Nilai Kedua: ");
b = Convert.ToInt32(Console.ReadLine());
c = a * b;
Console.WriteLine("Hasil Nilai: {0}", c);
Console.ReadLine();
}
}
}
4. IF, Program menentukan bilangan ganjil atau genap.
using System;
namespace IF
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int a,b;
Console.Write("Input Nilai: ");
a = Convert.ToInt32(Console.ReadLine());
b = a % 2;
if(b == 0)
{
Console.WriteLine("\nAngka {0} Merupakan Bilangan Genap",a);
}
else
{
Console.WriteLine("\nAngka {0} Merupakan Bilangan Ganjil",a);
}
Console.ReadLine();
}
}
}
5. Loop, program menghitung bilangan faktorial
using System;
namespace Loop
{
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();
}
}
}
6. Array, Program melihat data Array
using System;
namespace Array
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int a,b,c;
int[] array;
array = new int[100];
Console.Write("Input Jumlah Data : ");
a = Convert.ToInt32(Console.ReadLine());
for(b=0;b<=a-1;b++)
{
Console.Write("Input Data ke {0} : ",b+1);
array[b] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("\n\nMenampilkan Data");
Console.Write("\nInput Data yang anda cari : ");
c = Convert.ToInt32(Console.ReadLine());
for(b=0;b<=a-1;b++)
{
if(array[b]==c)
{
Console.WriteLine("Data yang anda cari ada di urutan ke {0}",b+1);
Console.ReadLine();
}
}
}
}
}
7. Input - Output
using System;
namespace InputOutput
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string a,b,c;
Console.Write("Nama\t: ");
a = Console.ReadLine();
Console.Write("NIM\t: ");
b = Console.ReadLine();
Console.Write("Prodi\t: ");
c = Console.ReadLine();
Console.WriteLine("\n\nMenampilkan Data");
Console.WriteLine("Nama\t: {0}", a);
Console.WriteLine("NIM\t: {0}", b);
Console.WriteLine("Prodi\t: {0}", c);
Console.ReadLine();
}
}
}
8. Exceptional Handling
Exceptional Handling Berlaku apabila user melakukan kesalahan dalam menginput. Dalam Contoh dibawah, bila user menginput huruf atau kata, maka program akan menampilkan pesan error.
using System;
namespace Exceptional_Handing
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int a,b,c;
try
{
Console.Write("Input Nilai Pertama: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Input Nilai Kedua: ");
b = Convert.ToInt32(Console.ReadLine());
c = a * b;
Console.WriteLine("{0} * {1} = {2}",a,b,c);
Console.ReadLine();
}
catch
{
Console.WriteLine("Maaf, Anda Salah memasukkan Input");
Console.ReadLine();
}
}
}
}
Tugas harian
1. Selisih Jam
using System;
namespace Selisih_Waktu_020
{
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());
Console.WriteLine("Menit Awal Percakapan : ");
jam1.mnt = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Detik Awal Percakapan : ");
jam1.dtk = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Jam Selesai Percakapan : ");
jam2.jam = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Menit Selesai Percakapan : ");
jam2.mnt = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Detik Selesai Percakapan : ");
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<jam1.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<jam1.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
using System;
namespace Faktorial_020
{
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
using System;
namespace Perbandingan_Angka_020
{
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<a[b])
{
c = a[b];
}
}
Console.WriteLine("Bilangan Terbesar Adalah {0}",c);
Console.ReadLine();
}
}
}
4. Kalkulator Sederhana
using System;
namespace Kalkulator_020
{
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();
}
}
}
Trimakasih Dan Semoga Bermanfaat :D #Ntv
Comments
Post a Comment