Sabtu, 05 Maret 2011

LATIHAN C# 020311_0320100029

0 komentar
1.Selesih Waktu_0320100029

using System;


namespace Selisih_Waktu_0320100029
{
                 /// <summary>
                /// </summary>
               class Class1
              {
                         /// <summary>
                        /// The main entry point for the application.
                       /// </summary>
                      public struct JAM
                     {
                                public int hh;
                                public int mm;
                                public int ss;
                     }
                     static void Main(string[] args)
                    {
                                JAM w1;
                                JAM w2;
                                JAM w3;
                               Console.WriteLine("Jam Mulai 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} jam {1} menit {2} detik",w3.hh,w3.mm,w3.ss);
                                Console.ReadLine();
                  }
        }

 }

2. Faktorial_0320100029

using System;

namespace Faktorial_0320100029
{
               /// <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 Angka : ");
                                        n=Convert.ToInt32(Console.ReadLine());
                                        if(n==0)
                                       {
                                                       Console.WriteLine("Faktorial Adalah 1");
                                        }
                                        else
                                       {
                                                       fak=1;
                                                       for(k=1;k<=n;k++)
                                                      {
                                                                   fak=fak*k;
                                                      }
                                                      Console.WriteLine("Faktorial Adalah {0}",fak);
                                                      Console.ReadLine();
                                       }
 

                      }
         }
}

3. Perbandingan Angka_0320100029


using System;

namespace perbandingan_angka_0320100029
{
               /// <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(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);
                                              }
                                }

                               Console.ReadLine();
 

                      }
           }
}

 4. Kalkulator_0320100029

 using System;

namespace Kalkulator_0320100029
{
                 /// <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
                                                //
                                               decimal a,b,c;
                                               char operator1;
                                               Console.Write("Angka 1 : ");
                                               a=Convert.ToDecimal(Console.ReadLine());
                                               Console.Write("Operator(+, -, *, /): ");
                                               operator1=Convert.ToChar(Console.ReadLine());
                                               Console.Write("Angka 2 : ");
                                               b=Convert.ToDecimal(Console.ReadLine());

                                              if(operator1=='+')
                                             {
                                                            c=a+b;
                                                            Console.WriteLine("Jumlah adalah {0} ",c);
                                              }
                                              if(operator1=='-')
                                              {
                                                              c=a-b;
                                                              Console.WriteLine("Jumlah adalah {0} ",c);
                                               }
                                               if(operator1=='*')
                                              {
                                                               c=a*b;
                                                               Console.WriteLine("Jumlah adalah {0} ",c);
                                              }
                                              if(operator1=='/')
                                             {
                                                                 c=a/b;
                                                                 Console.WriteLine("Jumlah adalah {0} ",c);
                                              }
                                              Console.ReadLine();
                           }
              }
}









Kamis, 03 Maret 2011

Kumpulan Program Sederhana C#_0320100029

0 komentar
  1. Mencetak Kalimat Hello World !!!
using System;

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();
                             }
              }
       }
}