Rabu

kalkulator menggunakan switch case

#include <iostream.h>
#include <conio.h>

main(){

int bil1,bil2,pil;
float hsl;
cout<<"Pilihlah menu di bawah ini :\n";
cout<<"[1] Penjumlahant\n";
cout<<"[2] Pengurangan\n";
cout<<"[3] Perkalian\n";
cout<<"Masukkan pilihan anda : ";
cin>>pil;

switch (pil)

  {  case 1:
          cout<<"Input bil 1 = ";cin>>bil1;
          cout<<"Input bil 2 = ";cin>>bil2;
      hsl=bil1+bil2;
      cout<<"Hasil penjumlahannya = "<<hsl<<endl<<endl;
      break;
  case 2:
          cout<<"Input bil 1 = ";cin>>bil1;
          cout<<"Input bil 2 = ";cin>>bil2;
      hsl=bil1-bil2;
      cout<<"Hasil pengurangannya = "<<hsl<<endl<<endl;
      break;
  case 3:
          cout<<"Input bil 1 = ";cin>>bil1;
          cout<<"Input bil 2 = ";cin>>bil2;
      hsl=bil1*bil2;
      cout<<"Hasil perkaliannya = "<<hsl<<endl<<endl;
      break;
  default :
      cout<<"Maaf blm terdaftar";
   }
getch();
}

Jumat

Definition of one-dimensional array



Is a variable that stores a set of data that has the same type and the element to be accessed only through an index or subscript.

The general form of declaration:

nama_array [jumlah_eleman];

* An array of two-dimensional

Is a variable that stores a set of data that has the same type and the element to be accessed via 2 index or subscript of the index row and column index.

The general form of declaration:

nama_array [jumlah_eleman_baris] [jumah_eleme_kolom];

* Multidimensional arrays
Is a variable that stores a set of data that has the same type and the element to be accessed through many indexes or subscripts. This array is used for automatic, 2-dimensional arrays are also included into a multidimensional array.

Array dua dimensi

Two-dimensional array is an array that has two subscripts of rows and columns.
The general form of two-dimensional array declaration:
Tipe_data nama_array [banyak_baris] [banyak_kolom];

Examples of programs:

program array multi dimensi*/
#  include<stdio.h>

main()
{
int i,j;
int x[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};

for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
printf(“%6d”,x[i][j]);
printf(“\n”);
}
printf(“\n”);
}

/* program array multi dimensi*/
#include<stdio.h>

main()
{

int x[3][4],i,j;

for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
{
printf(“input data matrik [%i][%i]:”,i,j);
scanf(“%i”,&x[i][j]);
}
}

printf(“\nOutput data matrik\n”);

for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
printf(“%6i”,x[i][j]);
printf(“\n”);
}
}

#include <stdio.h>
main()
{
int A[3][2], B[3][2],C[3][2], i, j;
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(“input data matrik A[%i][%i] :”,i,j);
scanf(“%i”,&A[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(“input data matrik B[%i][%i] :”,i,j);
scanf(“%i”,&B[i][j]);
}
}

for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
C[i][j]=A[i][j]+B[i][j];
}
}
printf(“\n matrik penjumlahan A+B\n”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
printf(“%6i”,C[i][j]);
printf(“\n\n”);
}

This is an example program to input data / values ​​and find the average by using arrays.
#include<stdio.h>
#include<string.h>
main()
{
int i,data;
char nama[30];
float nilai[100];
float rata;

printf(“Bayaknya data : “);
scanf(“%d”,&data);

for(i=0; i<data; i++)
{
printf(“Data mahasiswa ke-[%d]\n”,i+1);
printf(“Nama  = “);
scanf(“%s”,&nama);
printf(“Nilai = “);
scanf(“%f”,&nilai[i]);
}
{
float jum=0;
for(i=0; i<data; i++)
jum+=nilai[i];
rata=jum/data;
}
printf(“\n”);
printf(“Daftar Nilai Mahasiswa\n”);
printf(“================================\n”);
printf(“Nama mahasiswa           Nilai  \n”);
printf(“================================\n”);
for(i=0; i<data; i++)
{
printf(“\n%s                “,nama);
printf(“%1.f”,nilai[i]);
}
printf(“\n================================\n”);
printf(“rata-rata adalah = %.1f”,rata);
}

Array Satu dimensi


  • Listing



  • Logika LaArray.java
The example above is an example program satudimensi array.
The first step arrays can be declared indirectly.
  Example: int [] x;
Only later in the initialization directly.
Example: x = new int [3];
After that, we declare each element of the variable x as well as fill in each element.
Example: x [0] = 20;
                 x [1] = 10;
                 x [2] = 30;
To get the output or output print give the print command,
Example: System.out.println ("The value of x [0]:" + x [0]);
System.out.println ("Nilaix [1]:" + x [1]);
System.out.println ("Nilaix [2]:" + x [2]);
  • Output