Thursday, 10 April 2014

sekilas tentang c# visual studio (console aplication)

Tags

saya postingkan tentang c# semoga bermanfaat ya :)

Cara Menampilkan Hello World pada Bahasa C#

using System;
namespace HelloWorld_0320100005
{
 /// <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.Write("Hello World ! ");
   Console.ReadLine();
  }
 }
}


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

using System;
namespace Variabel_Konstanta_TipeData_0320100005
{
 /// <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 = 0320100005;
   Console.WriteLine(a);
   Console.ReadLine();
  }
 }
}

Contoh Program Operator pada Bahasa C#

using System;
namespace Operator_0320100005
{
 /// <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;
   a=2;
   b=3;
   c=a+b;
   Console.WriteLine(c);
   Console.ReadLine();
  }
 }
}

Contoh Program IF pada Bahasa C#

using System;
namespace If_0320100005
{
 /// <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.Write("Masukkan a : ");
   char a = (char) Console.Read();
   if(Char.IsUpper(a))
    Console.WriteLine("You're Using Uppercase");
   Console.ReadLine();
   if(Char.IsLower(a))
    Console.WriteLine("You're Using Lowercase");
   Console.ReadLine();
  }
 }
}

Contoh Program Loop pada Bahasa C#

using System;
namespace Loop_0320100005
{
 /// <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=0;
   while(x<5)
   {
    x++;
    Console.WriteLine(x);
 
   }
   Console.ReadLine();
  }
 }
}


Contoh Program Array pada Bahasa C#

using System;
namespace array_0320100005
{
 /// <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[] arr = new int[5];
   for (int a=0;a < arr.Length;a++)
    arr[a]= a*a;
   for (int a=0;a < arr.Length;a++)
    Console.WriteLine("arr[{0}] = {1}",a,arr[a]);
   Console.ReadLine();
  }
 }
}

Contoh Program Membaca dan Menulis ke Layar

using System;
namespace MembacadanMenulis_0320100005
{
 /// <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,d;
   Console.Write(" Nilai UAS   = ");
   a = Convert.ToDouble(Console.ReadLine());
   Console.Write(" Nilai UTS   = ");
   b = Convert.ToDouble(Console.ReadLine());
   Console.Write(" Nilai Tugas = ");
   c = Convert.ToDouble(Console.ReadLine());
   Console.WriteLine("\n Anda Telah Menginput :\n");
   Console.WriteLine("\n Nilai UAS   = {0}\n Nilai UTS   = {1}\n Nilai Tugas = {2}",a,b,c);
   Console.ReadLine();

  }
 }
}

Contoh Program Exception Handling pada Bahasa C#

using System;
namespace Exception_Handling_0320100005
{
 /// <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;
   try
   {
    Console.Write(" Masukkan angka = ");
    a= Convert.ToInt16 (Console.ReadLine());
    Console.WriteLine("{0}",a);
    Console.ReadLine();
   }
   catch
   {
    Console.WriteLine(" System Error ");
    Console.ReadLine();
   }
  }
 }
}

Membuat Program C# Sederhana

1. Selisih_Waktu_0320100005

using System;
namespace Selisih_Waktu_0320100005
{
 /// <summary>
 /// Summary description for Class1.
 /// </summary>
 class Class1
 {
  /// <summary>
  /// The main entry point for the application.
  /// </summary>

  public struct JAM
  {
   public int hh;
   public int mm;
   public int ss;
  }
  [STAThread]
  static void Main(string[] args)
  {
   JAM W1;
   JAM W2;
   JAM W3;
   Console.WriteLine("Jam Awal Percakapan : ");
   W1.hh =   Convert.ToInt32(Console.ReadLine());
   W1.mm = Convert.ToInt32(Console.ReadLine());
   W1.ss = Convert.ToInt32(Console.ReadLine());
   Console.WriteLine("Jam Selesai Percakapan : ");
   W2.hh = Convert.ToInt32(Console.ReadLine());
   W2.mm = Convert.ToInt32(Console.ReadLine());
   W2.ss = Convert.ToInt32(Console.ReadLine());
   W3.mm = 0;
   W3.ss = 0;
   if(W2.ss>=W1.ss)
   {
    W3.ss = W2.ss - W1.ss;
   }
   if(W2.ss<W1.ss)
   {
    W3.ss = (W2.ss + 60) - W1.ss;
    W2.mm = W2.mm - 1;
   }
   if(W2.mm>W1.mm)
   {
    W3.mm = W2.mm - W1.mm;
   }
   if(W2.mm<W1.mm)
   {
    W3.mm = (W2.mm + 60) - W1.mm;
    W2.hh = W2.hh - 1;
   }
   W3.hh = W2.hh - W1.hh;
   Console.WriteLine("\nSelisih Waktu = {0}:{1}:{2}",W3.hh,W3.mm,W3.ss);
   Console.ReadLine();
  }
 }
}

2. Faktorial_0320100005

using System;
namespace Faktorial_0320100005
{
 /// <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,fak,k;
   Console.Write(" Masukkan Bilangan : ");
   n = Convert.ToInt32(Console.ReadLine());
   fak=1;
   for (k=1;k<=n;k++)
   {
    fak= fak*k;
   }
   Console.WriteLine(" Hasil faktorial adalah {0}",fak);
   Console.ReadLine();
  }
 }
}

3. Perbandingan_Angka_0320100005

using System;
namespace Perbandingan_Angka_0320100005
{
 /// <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(" 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(a>c)
    {
     Console.WriteLine(" Bilangan terbesar adalah {0} ",a);
     Console.ReadLine();
    }
    else
    {
     Console.WriteLine(" Bilangan terbesar adalah {0} ",c);
     Console.ReadLine();
    }
   }
   else
   {
    if(b>c)
    {
     Console.WriteLine(" Bilangan terbesar adalah {0} ",b);
     Console.ReadLine();
    }
    else
    {
     Console.WriteLine(" Bilangan terbesar adalah {0} ",c);
     Console.ReadLine();
    }
   }
  }
 }
}

4. Kalkulator_0320100005

using System;
namespace Kalkulator_0320100005
{
 /// <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 d,e;
   Console.Write(" Angka 1 : ");
   a = Convert.ToDouble(Console.ReadLine());
   Console.Write(" Operator (+, -, *, /) : ");
   d=Console.ReadLine();
   switch(d)
   {
    case "+" : 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 2 : ");
     b=Convert.ToDouble(Console.ReadLine());
     c=a-b;
     Console.WriteLine(" Jumlah adalah {0}\n",c);
     Console.ReadLine();
     break;
    case "*" : 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 2 : ");
     b=Convert.ToDouble(Console.ReadLine());
     c=a/b;
     Console.WriteLine(" Jumlah adalah {0}\n",c);
     Console.ReadLine();
     break;
   }
  }
 }
}

berbagiyok.cz.cc / belajar_bersama@egiginanjar


EmoticonEmoticon