Jumat, 09 November 2012

Soal UTS Algoritma & Pemrograman Looping (Java)



1.      Buatlah program untuk menerima input bilangan bulat positif n dan menampilkan n bilangan bulat positif pertama yang habis dibagi 3 tetapi tidak habis dibagi 2.
Contoh :
Input = 5
Output = 3 9 15 21 27

Jawab :
import java.util.Scanner;

/**
 * @author user
 */
public class s {

 /**
  * @param args the command line arguments
  */
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  int n;

  System.out.print("n = ");
  n = input.nextInt();

  for (int i = 1; i <= n; i++) {
   int x = (i * 6) - 3;
   System.out.print(x + " ");
  }
 }
}





2.      Buatlah program untuk membaca 7 data bilangan bulat psitif dari keyboard. Kemudian menghitung nilai terbesar kedua dari data tersebut dan menampilkan di layar monitor.
Contoh :     data input = 7, 12, 4, 23, 10, 5, 15
                  Data terbesar ke dua = 15

Jawab :
import java.util.Scanner;

import org.apache.log4j.Logger;

/**
 * @author user
 */
public class Exercises {

 /**
  * @param args the command line arguments
  */
 private static final Logger logger = Logger.getLogger(Exercises.class);

 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);

  int variable1;
  int variable2;
  int variable3;
  int variable4;
  int variable5;
  int variable6;
  int variable7;
  int x;

  System.out.print("variable1 = ");
  variable1 = input.nextInt();

  System.out.print("variable2 = ");
  variable2 = input.nextInt();

  System.out.print("variable3 = ");
  variable3 = input.nextInt();

  System.out.print("variable4 = ");
  variable4 = input.nextInt();

  System.out.print("variable5 = ");
  variable5 = input.nextInt();

  System.out.print("variable6 = ");
  variable6 = input.nextInt();

  System.out.print("variable7 = ");
  variable7 = input.nextInt();

  x = variable1;
  if (variable1 > variable2) {
   variable1 = variable2;
   variable2 = x;
  }

  x = variable2;
  if (variable2 > variable3) {
   variable2 = variable3;
   variable3 = x;
  }

  x = variable3;
  if (variable3 > variable4) {
   variable3 = variable4;
   variable4 = x;
  }

  x = variable4;
  if (variable4 > variable5) {
   variable4 = variable5;
   variable5 = x;
  }

  x = variable5;
  if (variable5 > variable6) {
   variable5 = variable6;
   variable6 = x;
  }

  x = variable6;
  if (variable6 > variable7) {
   variable6 = variable7;
   variable7 = x;
  }

  x = variable1;
  if (variable1 > variable2) {
   variable1 = variable2;
   variable2 = x;
  }

  x = variable2;
  if (variable2 > variable3) {
   variable2 = variable3;
   variable3 = x;
  }

  x = variable3;
  if (variable3 > variable4) {
   variable3 = variable4;
   variable4 = x;
  }

  x = variable4;
  if (variable4 > variable5) {
   variable4 = variable5;
   variable5 = x;
  }

  x = variable5;
  if (variable5 > variable6) {
   variable5 = variable6;
   variable6 = x;
  }

  x = variable1;
  if (variable1 > variable2) {
   variable1 = variable2;
   variable2 = x;
  }

  x = variable2;
  if (variable2 > variable3) {
   variable2 = variable3;
   variable3 = x;
  }

  x = variable3;
  if (variable3 > variable4) {
   variable3 = variable4;
   variable4 = x;
  }

  x = variable4;
  if (variable4 > variable5) {
   variable4 = variable5;
   variable5 = x;
  }

  x = variable1;
  if (variable1 > variable2) {
   variable1 = variable2;
   variable2 = x;
  }

  x = variable2;
  if (variable2 > variable3) {
   variable2 = variable3;
   variable3 = x;
  }

  x = variable3;
  if (variable3 > variable4) {
   variable3 = variable4;
   variable4 = x;
  }

  x = variable1;
  if (variable1 > variable2) {
   variable1 = variable2;
   variable2 = x;
  }

  x = variable2;
  if (variable2 > variable3) {
   variable2 = variable3;
   variable3 = x;
  }

  x = variable1;
  if (variable1 > variable2) {
   variable1 = variable2;
   variable2 = x;
  }

  System.out.println("The second large variable = " + variable6);

 }
}





3.    Buatlah program untuk menghitung n? = 1+2+3+……+n, nilai n diinputkan dari keyboard.
Contoh :     input n = 5
                  Output = 15

                  Input  = 10
                  Output = 55

Jawab :
import java.util.Scanner;

import org.apache.log4j.Logger;

/**
 * @author user
 */
public class Exercises {

 /**
  * @param args the command line arguments
  */
 private static final Logger logger = Logger.getLogger(Exercises.class);

 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);

  int value;

  System.out.print("value = ");
  value = input.nextInt();

  int j = 0;
  for (int i = 1; i <= value; i++) {
   j = j + i;
   System.out.print(i + " + " + " ");
  }

  System.out.println();
  System.out.println(j + " ");

 }
}

2 komentar: