You are on page 1of 4

Nama : Ahmad Naufal Syahbana

Kelas : INFORMATIKA B-PAGI


NIM : 210602062

 LATIHAN 04 OPERATOR PERCABANGAN


JAWABAN :

1. PROGRAM MENENTUKAN AKAR KUADRAT

import java.util.Scanner;
public class Nopal{
public static void main(String[] args) {
int a, b, c;
double D, x1, x2;

Scanner keyboard = new Scanner(System.in);

System.out.println("AYO KITA MENCARI AKAR PERSAMAAN..!!");


System.out.println("Contoh : a:1 b:-2 c:-8");
System.out.println("==========++++==========");
System.out.print("Masukkan Nilai a: ");
a = keyboard.nextInt();
System.out.print("Masukkan Nilai b: ");
b = keyboard.nextInt();
System.out.print("Masukkan Nilai c: ");
c = keyboard.nextInt();
System.out.println("==========++++==========");

System.out.println("Persamaan yang terbentuk:");


System.out.print(a + "x^2 ");
if(b < 0)
{
System.out.print("- " + (-b) + "x ");
}else if(b > 0)
{
System.out.print("+ " + b + "x ");
}
if(c < 0)
{
System.out.println("- " + (-c) + " = 0");
}else if(c > 0)
{
System.out.println("+ " + c + " = 0");
}else
{
System.out.println(" = 0");
}
D = b * b - (4 * a * c);
x1=(-b)+ (Math.pow(D, 0.5))/2*a;
x2=(-b)+ (Math.pow(D, 0.5))/2*a;
System.out.println("Determinan = " + D);
if(D < 0)
{ System.out.println("==========++++==========");
System.out.println("Akar-akar persamaan meupakan bilangan imajiner");
}else if(D == 0)
{
x1 = -b /(2 * a);
System.out.println("Memiliki akar persamaan x1 = x2 = " + x1);
}else
{
x1 = (-b + Math.sqrt(D)) / (2 * a);
x2 = (-b - Math.sqrt(D)) / (2 * a);
System.out.println("==========++++==========");
System.out.println("Memiliki akar persamaan x1 = " + x1 + " dan x2 = " + x2);
}
}
}

OUTPUT 1 : OUTPUT 2 :

2. PROGRAM MENENTUKAN BILANGAN GANJIL 11-188 DENGAN FOR, WHILE, DO-WHILE


 FOR

import java.util.Scanner; OUTPUT :


public class Nopal {
public static void main(String[] args) {
int x;
for (x=11;x < 188x+=2){
System.out.println("Bilangan Ganjil dari 11 sampai 188 ="+x);
System.out.printf("\n");
}
}
}
OUTPUT :
 WHILE
import java.util.Scanner;
public class Nopal {
public static void main(String[] args) {
int x=11;
while (x<=188){
System.out.println("Bilangan Ganjil dari 11 sampai 188 ="+x);
System.out.printf("\n");
x = x+2;
}

 DO-WHILE
import java.util.Scanner; OUTPUT :
public class Nopal {
public static void main(String[] args) {
int x=11;
do{
System.out.println("Bilangan Ganjil dari 11 sampai 188 ="+x);
System.out.printf("\n");
x=x+2;}
while (x<188);
}
}

3. PROGRAM MENAMPILKAN ANGKA HARI SEBANYAK N YANG DIINPUT

import java.util.Scanner;
public class Nopal {
public static void main(String[] args) {
int a, b, c;
Scanner keyboard = new Scanner(System.in);
System.out.print("Masukan Jumlah n : ");
a =keyboard.nextInt();
b =a%7;
System.out.println("==========++++==========");
System.out.println("Maka Angkanya Adalah = ");
for (c=a/7; c>0; c--){ OUTPUT :
System.out.printf("\n");
System.out.print("1 2 3 4 5 6 7 ");
}
if (b>0){
for (c=1; c<=b ; c++) {
System.out.printf("%d ",c);
}
}
System.out.printf("\n\n");
System.out.println("==========++++==========");
}
}
4. PROGRAM MENAMPILKAN JUMLAH BILANGAN DENGAN BINTANG (*)
import java.util.Scanner;
public class Nopal {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Masukkan N : ");
int n = input.nextInt(); OUTPUT :
System.out.println("==========++++==========");
System.out.println();
int counter = 0;
for (int i = 0; i < n * n; i++) {
counter++;
if (i % 2 != 0) {
System.out.print("-");
} else {
System.out.print("*");
}

if (counter == n) {
System.out.println();
counter = 0;
}
}
System.out.printf("\n");
System.out.println("==========++++==========");
}
}

5. PROGRAM MENAMPILKAN JUMLAH BILANGAN DENGAN BINTANG (*)

import java.util.Scanner;
OUTPUT :
public class Nopal {
public static void main(String[] args) {
int a, b, c;
Scanner keyboard = new Scanner(System.in);
System.out.print("Masukan Jumlah n: ");
c=keyboard.nextInt ();
System.out.println("==========++++==========");

for (a=1;a<=c;a++){
for (b=1;b<=a;b++){
System.out.print("*");
}
System.out.println();
}
System.out.printf("");
System.out.println("==========++++==========");
}
}

You might also like