Rabu, 12 Maret 2014

Contoh Codingan kalkulator C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace kalkulator
{
    public partial class kalkulator : Form
    {
        string operand1 = string.Empty;
        string operand2 = string.Empty;
        string result;
        char operation;
        int a = 0;

        public kalkulator()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e) //angka 1
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "1";
        }

        private void button2_Click(object sender, EventArgs e) // angka 2
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "2";
        }

        private void button3_Click(object sender, EventArgs e) //angka 3
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "3";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "4";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "5";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "6";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "7";
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "8";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "9";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += "0";
        }

        private void button15_Click(object sender, EventArgs e) // .
        {
            if (a <= 0)
            {
                textBox1.Text = string.Empty;
            }
            a++;
            textBox1.Text += ".";
        }

        private void button13_Click(object sender, EventArgs e) //  +
        {
            operand1 = textBox1.Text;
            operation = '+';
            textBox1.Text = string.Empty;

        }

        private void button11_Click(object sender, EventArgs e) // *
        {
            operand1 = textBox1.Text;
            operation = '*';
            textBox1.Text = string.Empty;

        }

        private void button12_Click(object sender, EventArgs e) // (“/”)
        {
            operand1 = textBox1.Text;
            operation = '/';
            textBox1.Text = string.Empty;

        }

        private void button14_Click(object sender, EventArgs e) // -
        {
            operand1 = textBox1.Text;
            operation = '-';
            textBox1.Text = string.Empty;

        }

        private void button16_Click(object sender, EventArgs e) // =
        {
            operand2 = textBox1.Text;
            double opr1, opr2;
            double.TryParse(operand1, out opr1);
            double.TryParse(operand2, out opr2);

            switch (operation)
            {
                case '+':
                    a = 0;
                    result = (opr1 + opr2).ToString();
                    textBox1.Text = result.ToString();
                    break;

                case '-':
                    a = 0;
                    result = (opr1 - opr2).ToString();
                    textBox1.Text = result.ToString();
                    break;

                case '*':
                    a = 0;
                    result = (opr1 * opr2).ToString();
                    textBox1.Text = result.ToString();
                    break;

                case '/':
                    if (opr2 != 0)
                    {
                        a = 0;
                        result = (opr1 / opr2).ToString();
                        textBox1.Text = result.ToString();
                    }
                    else
                    {
                        a = 0;
                        textBox1.Text = "tidak bisa di bagi 0 !!";
                    }
                    break;
            }
        }

        private void button17_Click(object sender, EventArgs e) // C
        {
            textBox1.Text = "0";
        }
    }

}

Gambar Kalkulator


Contoh Codingan Anagram C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace anagram
{
    class Program
    {
        static int size;
        static int count;
        static char[] arrChar = new char[100];

        static void Main(string[] args)
        {
            // Mendapatkan kalimat dari pengguna.
            Console.Write("Masukkan kalimat : ");
            String input = Console.ReadLine();
            Console.Write("\n\n");
            // Mendapatkan ukuran kalimat.
            size = input.Length;
            count = 0;
            for (int j = 0; j < size; j++)
                // Menaruh kalimat di array.
                arrChar[j] = input[j];
            doAnagram(size); // Menyusun anagram.
            Console.ReadLine();
        }

        // Metoda yang melakukan anagramisasi.
        private static void doAnagram(int newSize)
        {
            // Jika ukuran terlalu kecil
            // (dasar rekursif).
            if (newSize == 1)
                return;
            // Untuk tiap posisi.
            for (int j = 0; j < newSize; j++)
            {
                // Sisa anagram.
                doAnagram(newSize - 1);
                // Jika selesai, tampilkan!
                if (newSize == 2)
                    displayWord();
                // Lakukan rotasi kalimat.
                rotate(newSize);
            }
        }

        // Melakukan rotasi.
        private static void rotate(int newSize)
        {
            int j;
            int position = size - newSize;
            // Simpan huruf pertama.
            char temp = arrChar[position];
            for (j = position + 1; j < size; j++)
                // Geser huruf yang lain ke kiri.
                arrChar[j - 1] = arrChar[j];
            // Meletakkan huruf pertama ke kanan.
            arrChar[j - 1] = temp;
        }

        // Menampilkan ke layar Console.
        private static void displayWord()
        {
            if (count < 99)
                Console.Write(" ");
            if (count < 9)
                Console.Write(" ");
            Console.Write(++count + " ");
            for (int j = 0; j < size; j++)
                Console.Write(arrChar[j]);
            Console.Write(" ");
            if (count % 6 == 0)
                Console.Write("");
        }
    }

}


Gambar : Output


Contoh codingan BILANGAN PRIMA C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace bilanganPrima
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Program Sederhana Bilangan Prima menggunakan C#");
            Console.Write("Masukkan Batas Bilangan Prima : ");

            bool bilanganprima = true;

            int bilangan = int.Parse(Console.ReadLine());

            if (bilangan >= 2)
            {
                for (int i = 2; i <= bilangan; i++)
                {
                    for (int p = 2; p < i; j++)
                    {
                        if ((i % p) == 0)
                        {
                            bilanganprima = false;
                            break;
                        }
                    }

                    if (bilanganprima)
                        Console.WriteLine("Bilangan " + i + " adalah bilangan prima");
                    bilanganprima = true;
                }
            }
            else
                Console.WriteLine("Tidak ada bilangan prima yang bisa dituliskan");
            Console.ReadLine();
        }
    }
}




        Gambar : codingan 




                                                                     Gambar  : Output