Home » » PEMROGRAMAN 2

PEMROGRAMAN 2

Written By illy arief on Sabtu, 05 Maret 2011 | 15.06

Latihan :
1. Hello World
using System;

namespace Hello
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }
    }
}

2. Variabel, Konstanta dan Tipe Data
using System;

namespace Variabel
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            int x;
            Console.WriteLine("Masukkan Bilangan :");
            x = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Bilangan yang diinput : {0}",x);
            Console.ReadLine();
        }
    }
}

3. Operator
using System;

namespace Operator
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            int a,b,c,hitung;
            Console.WriteLine("\t\t\tM E N G H I T U N G  O P E R A T O R");
            Console.WriteLine("\t\t\t1. Menghitung Penjumalahan");
            Console.WriteLine("\t\t\t2. Menghitung Pengurangan");
            Console.WriteLine("\t\t\t3. Menghitung Perkalian");
            Console.WriteLine("\t\t\t4. Menghitung Pembagian");
            Console.WriteLine("\nMasukkan pilihan anda :");
            hitung = Convert.ToInt32(Console.ReadLine());
            switch(hitung)
            {
                case 1 :
                    Console.WriteLine("\nMasukkan Bilangan ke-1 :");
                    a = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Masukkan Bilangan ke-2 :");
                    b = Convert.ToInt32(Console.ReadLine());
                    c = a + b;
                    Console.WriteLine("Hasilnya adalah : {0}",c);
                    Console.ReadLine();
                    break;
                case 2 :
                    Console.WriteLine("\nMasukkan Bilangan ke-1 :");
                    a = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Masukkan Bilangan ke-2 :");
                    b = Convert.ToInt32(Console.ReadLine());
                    c = a - b;
                    Console.WriteLine("Hasilnya adalah : {0}",c);
                    Console.ReadLine();
                    break;
                case 3 :
                    Console.WriteLine("\nMasukkan Bilangan ke-1 :");
                    a = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Masukkan Bilangan ke-2 :");
                    b = Convert.ToInt32(Console.ReadLine());
                    c = a * b;
                    Console.WriteLine("Hasilnya adalah : {0}",c);
                    Console.ReadLine();
                    break;
                case 4 :
                    Console.WriteLine("\nMasukkan Bilangan ke-1 :");
                    a = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Masukkan Bilangan ke-2 :");
                    b = Convert.ToInt32(Console.ReadLine());
                    c = a / b;
                    Console.WriteLine("Hasilnya adalah : {0}",c);
                    Console.ReadLine();
                    break;
            }
        }
    }
}

4. IF, menentukkan bilangan genap atau ganjil
using System;

namespace IF_Genap
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            int x;
            Console.WriteLine("Masukkan Bilangan Bulat :");
            x = Convert.ToInt32(Console.ReadLine());
            if (x%2==0)
            {
                Console.WriteLine("Bilangan {0} adalah genap",x);
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("Bilangan {0} adalah ganjil",x);
                Console.ReadLine();
            }
        }
    }
}

5. Loop, mengulang kata
using System;

namespace Loop
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            int y;
            for (y=1;y<=5;y++)
            {
                Console.WriteLine("Hallo Programmer");
                Console.ReadLine();
            }
        }
    }
}

6. Array, memasukkan bilangan
using System;

namespace Array
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            int []y = new int [5];
            int i;
                for (i=0;i<=4;i++)
                {
                    Console.Write("Input Bilangan : ");
                    y[i] = Convert.ToInt32(Console.ReadLine());
                }
            for (i=0;i<=4;i++)
            {
                Console.WriteLine("Bilangan ke-{0} = {1}",i+1,y[i]);
            }
            Console.ReadLine();
        }
    }
}

7. Menulis dan mencetak ke layar
using System;

namespace membaca_dan_menulis_ke_layar
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            string a,b,c,d,e,f;
            Console.WriteLine("=======Input Buku=======");
            Console.Write("Judul Buku\t: ");
            a = Convert.ToString(Console.ReadLine());
            Console.Write("Pengarang\t: ");
            b = Convert.ToString(Console.ReadLine());
            Console.Write("Penerbit\t: ");
            c = Convert.ToString(Console.ReadLine());
            Console.WriteLine("\n\n=======Input Peminjaman=======");
            Console.Write("Nama Peminjaman\t: ");
            d = Convert.ToString(Console.ReadLine());
            Console.Write("Alamat\t\t: ");
            e = Convert.ToString(Console.ReadLine());
            Console.Write("No. Telepon\t: ");
            f = Convert.ToString(Console.ReadLine());
            Console.WriteLine("\n\n=======Data Buku Yang Telah Dipinjam=======");
            Console.Write("Judul Buku\t: {0}",a);
            Console.Write("\nPengarang\t: {0}",b);
            Console.Write("\nPenerbit\t: {0}",c);
            Console.WriteLine("\n\n=======Data Peminjam Yang Meminjam=======");
            Console.Write("Nama Peminjam\t: {0}",d);
            Console.Write("\nAlamat\t\t: {0}",e);
            Console.Write("\nNo. Telepon\t: {0}",f);
            Console.ReadLine();
        }
    }
}


8. Exception Handling
using System;

namespace Exception_handling
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            object y=null;
            int x;
            try
            {
                x=(int)y;
            }
            catch(Exception err)
            {
                Console.WriteLine(err);
                x=10;
            }
            finally
            {
                Console.WriteLine("Thank You !");
            }
            Console.ReadLine();
        }
    }
}

Tugas :
1. Selisih_Waktu_0320100008
using System;

namespace Selisih_waktu_0320100008
{
    class Class1
        {
            public struct jam
            {
                public int h;
                public int m;
                public int s;
            }
            static void Main(string[] args)
            {
                jam h1;
                jam h2;
                jam h3;
                Console.WriteLine("Jam Awal Percakapan : ");
                h1.h = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Menit Awal Percakapan : ");
                h1.m = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Detik Awal Percakapan : ");
                h1.s = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Jam Selesai Percakapan : ");
                h2.h = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Menit Selesai Percakapan : ");
                h2.m = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Detik Selesai Percakapan : ");
                h2.s = Convert.ToInt32(Console.ReadLine());
                h3.m = 0;
                h3.s = 0;
                if(h2.s>=h1.s)
                {
                    h3.s = h2.s - h1.s;
                }
                if(h2.s<h1.s)
                {
                    h3.s = (h2.s + 60) - h1.s;
                    h2.m = h2.m - 1;
                }
                if(h2.m>h1.m)
                {
                    h3.m = h2.m - h1.m;
                }
                if(h2.m<h1.m)
                {
                    h3.m = (h2.m + 60) - h1.m;
                    h2.h = h2.h - 1;
                }
                h3.h = h2.h - h1.h;
                Console.WriteLine("\nSelisih Waktu = {0}:{1}:{2}",h3.h,h3.m,h3.s);
                Console.ReadLine();
            }
        }
}

2. Faktorial_0320100008
using System;

namespace Faktorial_0320100008
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            int n,k,fak;
            Console.WriteLine("Input Bilangan =");
            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("Hasil faktorial adalah = {0}",fak);
        Console.ReadLine();
        }
    }
}

3. Perbandingan_angka_0320100008
using System;

namespace Faktorial_0320100008
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            int n,k,fak;
            Console.WriteLine("Input Bilangan =");
            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("Hasil faktorial adalah = {0}",fak);
        Console.ReadLine();
        }
    }
}

4. Kalkulator_0320100008
using System;

namespace Kalkulator_0320100008
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            int x,y,z;
            char operasi;
            Console.WriteLine("Angka 1 = ");
            x = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Operator (+,-,*,-) :");
            operasi = Convert.ToChar(Console.ReadLine());
            Console.WriteLine("Angka 2 = ");
            y = Convert.ToInt32(Console.ReadLine());
            switch(operasi)
            {
                case '*' :
                    z = x * y;
                    Console.WriteLine("Jumlah adalah {0}",z);
                    Console.ReadLine();
                    break;
                case '+' :
                    z = x + y;
                    Console.WriteLine("Jumlah adalah {0}",z);
                    Console.ReadLine();
                    break;
                case '-' :
                    z = x - y;
                    Console.WriteLine("Jumlah adalah {0}",z);
                    Console.ReadLine();
                    break;
                case '/' :
                    z = x / y;
                    Console.WriteLine("Jumlah adalah {0}",z);
                    Console.ReadLine();
                    break;
            }
        }
    }
}
Share this article :

0 komentar :

Posting Komentar

 
Support : Your Link | Your Link | Your Link
Copyright © 2013. PEMROGRAMAN 2 - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger