Array 1 Dimensi

namespace o_array1d
{
class Program
{
public static void Main(string[] args)
{
//deklarasi variable
byte jumlahBarang;
int total = 0;

//deklarasi array
string[] namaBarang;
int[] harga;
byte[] qty;
int[] subTotal;

//input jumlah barang
Console.Write(“Jumlah Barang = “);
jumlahBarang=Convert.ToByte(Console.ReadLine());
Console.WriteLine();

//inisialisasi array
namaBarang = new string[jumlahBarang];
harga = new int[jumlahBarang];
qty = new byte[jumlahBarang];
subTotal = new int[jumlahBarang];

//input nama barang, harga, qty, sub total
for (int i = 0;i<jumlahBarang;i++)
{
Console.WriteLine(“Barang ke-{0}”,(i+1).ToString());
Console.Write(“Nama Barang = “);
namaBarang[i] = Convert.ToString(Console.ReadLine());

Console.Write(“Harga = “);
harga[i] = Convert.ToInt32(Console.ReadLine());

Console.Write(“Qty = “);
qty[i] = Convert.ToByte(Console.ReadLine());

subTotal[i] = Convert.ToInt32(harga[i] * qty[i]);
Console.WriteLine();

}

//menghitung total
for (int i = 0;i<jumlahBarang;i++)
{
total=total+subTotal[i];
}

//output
Console.WriteLine();
Console.WriteLine(“Nama Barang\tHarga\t\tQty\tSub Total”);
for (int i = 0;i<jumlahBarang;i++)
{
Console.WriteLine(“{0}\t\t{1}\t\t{2}\t{3}”,namaBarang[i],harga[i],qty[i],subTotal[i]);
}

Console.WriteLine(“\t\t\t\tTotal = {0}”,total.ToString());

Console.ReadKey(true);
}
}
}

ini hasilnya

Iklan

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s