- Mencetak Kalimat Hello World !!!
namespace Hello
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Demonstrator
{
/// <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. Menggunakan Variabel, Konstanta dan Type Data.
using System;
namespace variabel
{
/// <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;
a=29;
Console.WriteLine(a);
Console.ReadLine();
}
}
}
3. Program Operator
using System;
namespace operator1
{
/// <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("jadi a+b = {0}",c);
Console.ReadLine();
}
}
}
4. Penggunaan 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
//
int a,b,c;
Console.Write("masukkan nilai a= ");
a=Convert.ToInt32(Console.ReadLine());
b=a%2;
if(b==0)
{
Console.WriteLine("{0} = Bilangan Genap",a);
Console.ReadLine();
}
else
{
Console.WriteLine("{0} = Bilangan Ganjil",a);
Console.ReadLine();
}
}
}
}
5. Contoh Looping dalam Sebuah Program
using System;
namespace looping
{
/// <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,i,j;
Console.WriteLine("Input Tinggi Segitiga = ");
n=Convert.ToInt32(Console.ReadLine());
for(i=1; i<=n; i++)
{
Console.Write("\n");
for(j=i; j>0; j--)
{
Console.Write(" * ");
}
}
for(i=1; i<=n; i++)
{
Console.Write("\n");
for(j=i+1; j<=n; j++)
{
Console.Write(" * ");
}
}
Console.ReadLine();
}
}
}
6. Array
using System;
namespace Array
{
/// <summary>
/// </summary>
class Class1
{
/// <summary>
/// </summary>
[STAThread]
static void Main(string[] args)
{
string[] array1;
array1 = new string[50];
int w,t;
string[] array;
array = new string[50];
Console.Write("Banyak Data Mahasiswa : ");
w = Convert.ToInt32(Console.ReadLine());
for(t=1;t<=w;t++)
{
Console.Write("\n\nNama : ");
array1[l] = (Console.ReadLine());
Console.Write("\nProdi : ");
array[l] = Console.ReadLine();
}
Console.WriteLine("\n");
Console.WriteLine("Nama\t\tProdi");
Console.WriteLine("\n");
for(t=1;t<=w;t++)
{
Console.WriteLine("{0}\t\t{1}",array1[l],array[l]);
}
Console.ReadLine();
}
}
}
7. Menulis dan Menampilkan ke Layar
using System;
namespace menulis_dan_menampilkan_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)
{
//
// TODO: Add code to start application here
//
string a,b;
int c;
Console.WriteLine("Biodata Diri");
Console.Write("First Name : ");
a=Convert.ToString(Console.ReadLine());
Console.Write("Last Name : ");
b=Convert.ToString(Console.ReadLine());
Console.Write("Birth Date(ddmmyy) : ");
c=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\n");
Console.WriteLine("{0} {1}",a,b);
Console.WriteLine("{0}",c);
Console.ReadLine();
}
}
}
8. Exception & Handling
using System;
namespace exception_dan_handling_029
{
/// <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
//
try
{
int a,b,c;
Console.Write("A = ? ");
a=Convert.ToInt32(Console.ReadLine());
Console.Write("B = ? ");
b=Convert.ToInt32(Console.ReadLine());
Console.Write("C = ? ");
c=Convert.ToInt32(Console.ReadLine());
if(a<b)
{
if(b<c)
{
Console.WriteLine("Bilangan Terbesar Adalah {0}",c);
}
if(b>c)
{
Console.WriteLine("Bilangan Terbesar Adalah {0}",b);
}
}
if(b<c)
{
if(c<a)
{
Console.WriteLine("Bilangan Terbesar Adalah {0}",a);
}
if(c>a)
{
Console.WriteLine("Bilangan Terbesar Adalah {0}",c);
}
}
}
catch
{
Console.WriteLine("Silahkan Input Ulang");
Console.ReadLine();
}
}
}
}
0 komentar:
Posting Komentar