Hello World
using System;
class InteractiveWelcome
{
static void Main()
{
Console.Write("Hello World");
Console.ReadLine();
string name = Console.ReadLine();
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 pertambahan
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 a,b,c;
Console.Write("masukkan nilai a = ");
a=Convert.ToInt32(Console.ReadLine());
Console.Write("masukkan nilai b = ");
b=Convert.ToInt32(Console.ReadLine());
c=a+b;
Console.WriteLine(c);
Console.ReadLine();
}
}
}
4.If
using System;
namespace ConsoleApplication7
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string a;
int b,c;
Console.Write("Program penentu grade nilai");
Console.Write("\nMasukkan Nilai Anda: ");
a = Console.ReadLine();
b = Int32.Parse(a);
if(b<=30)
{
Console.Write("\nGrade Nilai Anda E");
Console.ReadLine();
}
if(b<=50)
{
Console.Write("Grade Nilai Anda D");
Console.ReadLine();
}
if(b<=69)
{
Console.Write("Grade Nilai Anda C");
Console.ReadLine();
}
if(b<=84)
{
Console.Write("Grade Nilai Anda B");
Console.ReadLine();
}
if(b<=100)
{
Console.Write("Grade Nilai Anda A");
Console.ReadLine();
}
}
}
}
5.Loop
using System;
class ForLoop
{
public static void Main()
{
for (int i=0; i < 150; i++)
{
if (i == 100)
break;
if (i % 2 ==0)
continue;
Console.WriteLine("{0}",i);
}
Console.WriteLine();
Console.ReadLine();
}
}
6.Array
using System;
namespace Array2
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
{
int[] x;
x=new int[100];
string z;
int q,t,c;
Console.WriteLine("Masukkan banyak data");
q=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Masukkan angka data ");
for(t=1;t<=q;t++)
{
Console.WriteLine("Data ke {0}:",t);
x[t-1]=Convert.ToInt32(Console.ReadLine());
}
do
{
Console.WriteLine("\nMasukkan data yang anda ingin cari: ");
c=Convert.ToInt32(Console.ReadLine());
for(t=1;t<=q;t++)
{
if(x[t-1]==c)
break;
}
if(x[t-1]!=c)
{
Console.WriteLine("Data Yang Anda Cari Tidak Ada");
goto f;
break;
}
Console.WriteLine("Data ditemukan pada data {0}",t);
f:
Console.WriteLine("Apakah Anda ingin mencari data lagi?(y/n)");
z=Console.ReadLine();
}
while(z=="y");
Console.ReadLine();
}
}
}
}
7. Menulis dan membaca ke layar
using System;
namespace Input_dan_tampilan_ke_layar
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string g,b;
Console.WriteLine("Tulis Inputan Anda:");
g=Console.ReadLine();
Console.WriteLine("Inputan yang anda masukkan adalah :{0}",g);
g=Console.ReadLine();
Console.WriteLine("Anda ingin melakukannya lagi?(y/t)");
b=Console.ReadLine();
while(b=="y");
}
}
}
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.Penentu lama percakapan
using System;
namespace Penentulamapercakapan_026
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
int j1,m1,d1,j2,m2,d2,j3,m3,d3;
Console.Write("Jam Mulai : ");
j1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Menit Mulai : ");
m1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Detik Mulai : ");
d1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Jam Akhir : ");
j2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Menit Akhir : ");
m2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Detik Akhir : ");
d2 = Convert.ToInt32(Console.ReadLine());
if (j1 <24 && j2 < 24 && m1<60 && m2 <60 && d1 <60 && d2 < 60)
{
if (d2 < d1)
{
d2=d2+60;
m2=m2-1;
d3=d2-d1;
if (m2 < m1 )
{
m2=m2 +60;
j2=j2-1;
m3=m2-m1;
j3=j2-j1;
Console.WriteLine("Lama Bicara : {0} jam {1} menit {2} Detik ",j3,m3,d3);
}
else
{
m3=m2-m1;
j3=j2-j1;
Console.WriteLine("Lama Bicara : {0} jam {1} menit {2} Detik ",j3,m3,d3);
}
}
else
{
d3=d2-d1;
if (m2 < m1 )
{
m2=m2 +60;
j2=j2-1;
m3=m2-m1;
j3=j2-j1;
Console.WriteLine("Lama Bicara : {0} jam {1} menit {2} Detik ",j3,m3,d3);
}
else
{
m3=m2-m1;
j3=j2-j1;
Console.WriteLine("Lama Bicara : {0} jam {1} menit {2} Detik ",j3,m3,d3);
}
}
}
else
{
Console.WriteLine("Error!!!");
}
Console.ReadLine();
}
}
}
2.Faktorial
using System;
namespace Faktorial_0320100026
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
int n,fak,k; ///deklarasi
Console.Write("Besar Nilai yang akan Difaktorial? :");
n=Convert.ToInt32(Console.ReadLine());
k=1;
fak=1;
if (n==0)
{
fak = 1;
}
else
{
for(k=1;k<=n;k++)
{
fak=fak*k;
}
}
Console.WriteLine("{0}",fak);
Console.ReadLine();
}
}
}
3.Perbandingan Angka
using System;
namespace Perbandingan_Angka_0320100026
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
int x,y,z; ///deklarasi
Console.Write("A : ? ");
x = Convert.ToInt32(Console.ReadLine());
Console.Write("B : ? ");
y = Convert.ToInt32(Console.ReadLine());
Console.Write("C : ? ");
z = Convert.ToInt32(Console.ReadLine());
if ( x > y && x > z)
{
Console.WriteLine("Bilangan terbesar adalah {0}",x);
Console.ReadLine();
}
if (y > x && y > z)
{
Console.WriteLine("Bilangan terbesar adalah : {0}",y);
Console.ReadLine();
}
else
{
Console.WriteLine("Bilangan terbesar adalah : {0}",z);
Console.ReadLine();
}
}
}
}
4.Kalkulator
using System;
namespace Kalkulator_0320100026
{
/// <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
//
double a,b,c;
string m,n,o,p;
Console.Write(" Operator [+, -, *, /) = ");
m = Console.ReadLine();
switch(m)
{
case "+" : Console.Write(" Angka 1 = ");
a = Convert.ToDouble(Console.ReadLine());
Console.Write(" Angka 2 = ");
b = Convert.ToDouble(Console.ReadLine());
c = a + b;
Console.WriteLine(" Jumlah Adalah {0}\n", c);
Console.ReadLine();
break;
case "-" : Console.Write(" Angka 1 = ");
a = Convert.ToDouble(Console.ReadLine());
Console.Write(" Angka 2 = ");
b = Convert.ToDouble(Console.ReadLine());
c = a - b;
Console.WriteLine(" Jumlah Adalah {0}\n", c);
Console.ReadLine();
break;
case "*" : Console.Write(" Angka 1 = ");
a = Convert.ToDouble(Console.ReadLine());
Console.Write(" Angka 2 = ");
b = Convert.ToDouble(Console.ReadLine());
c = a * b;
Console.WriteLine(" Jumlah Adalah {0}\n", c);
Console.ReadLine();
break;
case "/" : Console.Write(" Angka 1 = ");
a = Convert.ToDouble(Console.ReadLine());
Console.Write(" Angka 2 = ");
b = Convert.ToDouble(Console.ReadLine());
c = a / b;
Console.WriteLine(" Jumlah Adalah {0}\n", c);
Console.ReadLine();
break;
}
}
}
}