Rabu, 29 Mei 2013

PROGRAM VEKTOR DALAM BAHASA JAVA


PROGRAM VEKTOR DALAM BAHASA JAVA
I
import java.io.*;
class Yogi_Pratama
{
public static void main(String[] args)throws Exception
{
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));

int[]jum=new int[10];
int[]v1=new int[10];
int[]v2=new int[10];
int[] krg = new int[10];
int[] dot = new int[10];
int kali = 0;

System.out.print("\nMasukkan Jumlah Elemen Vektor = ");
int n=Integer.parseInt(input.readLine());
System.out.println("\nVektor 1");

for (int i=0;i<n;i++)
{
System.out.print("Elemen[1,"+i+"] = ");
v1[i]=Integer.parseInt(input.readLine());
}

System.out.println("\nVektor 2");
for (int i=0; i<n; i++)
{
System.out.print("Elemen [2,"+i+"] = ");
v2[i] = Integer.parseInt(input.readLine());
}

System.out.print("\nPenjumlahan Vektor : [");
for (int i=0; i<n; i++)
{
jum[i] = v1[i]+v2[i];
System.out.print(jum[i]);
if (i != n-1)
{
System.out.print(",");
}
}
System.out.println("]");
;

System.out.print("Pengurangan Vektor : [");
for (int i=0; i<n; i++)
{
krg[i]= v1[i]-v2[i];
System.out.print(krg[i]);
if (i != n-1)
{
System.out.print(",");
}
}
System.out.println("]");

System.out.print("Perkalian Vektor : ");
for (int i=0; i<n; i++)
{
dot[i] = v1[i]*v2[i];
kali = kali+dot[i];
}
System.out.println(kali);
}
}

II

import java.io.*;
class Tugas_Yogipratama
{
public static void main (String [] args) throws Exception
{
BufferedReader input = new BufferedReader (new InputStreamReader(System.in));

double []v1 = new double [10];
double []v2 = new double [10];

System.out.print ("masukan nama jumlah elemen vektor: ");
int n=Integer.parseInt(input.readLine());
double jum;
System.out.println("vektor 1");
for(int i=0;i<n;i++)
{
System.out.print("elemen [1,"+i+"]= ");
v1[i]=Integer.parseInt(input.readLine());
}
System.out.println("\nvektor 2");
for (int i=0;i<n;i++)
{
System.out.println("elemen [1,"+i+"]= " + Math.random());
v2[i]=Math.random();
}
System.out.print("\npenjumlahan vektor : [");

for(int i=0;i<n;i++)
{
jum=(v1[i]+v2[i]);222222
System.out.print (jum);
if (i!=n-1)
{
System.out.print("||");
}
}
System.out.print("]");
int temp=0;
System.out.print("\n");
}

}



Tidak ada komentar:

Posting Komentar