{
public static void main(String[] args)
{
int j = 1;
int i = 1;
do
{ Nilai j = 55
++i;
j = j + i;
}
while (i<=9); System.out.println("Nilai j =" +j); } } • public class Do2 { public static void main(String[] args) { int i = 0; int y = 1; do i = 30 Nilai y = 0 { y = y * i; i = i + 5; } while (i<=25); System.out.print(" i = " +i); System.out.println( " Nilai y = " +y); } } • import java.util.Scanner; public class ganjil { public static void main (String [] utuh) { System.out.println("Menu Program : "); System.out.println("a. Menghitung Luas Lingkaran"); System.out.println("b. Menampilkan deret dan jumlahnya"); System.out.println("c. Menampilkan Pola"); System.out.print("Pilihan : "); Scanner pilihan = new Scanner (System.in); String str = pilihan.next(); char pilih = str.charAt(0); switch (pilih) { case 'a' : System.out.print("Masukkan besar diameter : "); Scanner diameter = new Scanner (System.in); float d = diameter.nextFloat(); final double phi = 3.14; float r = d/2; double luas = phi * r * r; System.out.println("Luas = phi x r^2"); System.out.println(" = " +phi+ " x " +r+ "^2"); System.out.println(" = " +luas); break; case 'b' : System.out.print("Masukkan banyaknya deret : "); Scanner deret = new Scanner (System.in); int n = deret.nextInt(); System.out.print("Deret : "); double hasil = 0; int akhir = 0; for (int i=1; i
System.out.print(" ");
}
if (i==1) {
System.out.print(" atau ");
}
else {
for (int j=1; j<7; j++) { System.out.print(" "); } } // Pattern II for (int j=a; j>=1; j--) {
System.out.print(" ");
}
for (int j=i; j>=1; j--) {
System.out.print(j+ " ");
}
if (i==1) {
System.out.print(" atau ");
}
else {
for (int j=1; j<7; j++) { System.out.print(" "); } } // Pattern III for (int j=1; j<=a+1; j++) { System.out.print(j+ " "); } for (int j=2; j<=i; j++) { System.out.print(" "); } if (i==1) { System.out.print(" atau "); } else { for (int j=1; j<7; j++) { System.out.print(" "); } } // Pattern IV for (int j=2; j<=i; j++) { System.out.print(" "); } for (int j=a+1; j>=1; j--) {
System.out.print(j+ " ");
}
System.out.println();
}
break;
default : System.out.println("Input anda salah, silakan jalankan ulang program.");
}
}
}
Menu Program :
a. Menghitung Luas Lingkaran
b. Menampilkan deret dan jumlahnya
c. Menampilkan Pola
Pilihan :a
Masukkan besar diameter : 10
Luas = phi x r^2
=3.14x5.0^2
= 78.5
Menu Program :
a. Menghitung Luas Lingkaran
b. Menampilkan deret dan jumlahnya
c. Menampilkan Pola
Pilihan :b
Masukkan banyaknya deret:10
Deret : 3.14 + 9.42 + 15.70 + 21.98 + 28.26 + 34.54 + 40.82 + 47.10 + 53.38 + 59
.66 = 314.00
Menu Program :
a. Menghitung Luas Lingkaran
b. Menampilkan deret dan jumlahnya
c. Menampilkan Pola
Pilihan :c
Masukkan nilai n : 1
1 atau 1 atau 1 atau 1
Menu Program :
a. Menghitung Luas Lingkaran
b. Menampilkan deret dan jumlahnya
c. Menampilkan Pola
Pilihan :c
Masukkan nilai n : 2
1 atau 1 atau 12 atau 2 1
1 2 21 1 1
Menu Program :
a. Menghitung Luas Lingkaran
b. Menampilkan deret dan jumlahnya
c. Menampilkan Pola
Pilihan :c
Masukkan nilai n : 3
1 atau 1 atau 123 atau 32 1
1 2 21 12 21
123 321 1 1
• import java.util.Scanner;
public class genap {
public static void main (String [] utuh) {
System.out.println("Menu Program : ");
System.out.println("a. Menghitung Keliling Lingkaran");
System.out.println("b. Menampilkan deret dan jumlahnya");
System.out.println("c. Menampilkan Pola");
System.out.print("Pilihan : ");
Scanner pilihan = new Scanner (System.in);
String str = pilihan.next();
char pilih = str.charAt(0);
switch (pilih) {
case 'a' :
System.out.print("Masukkan besar diameter : ");
Scanner diameter = new Scanner (System.in);
float d = diameter.nextFloat();
final double phi = 3.14;
float r = d/2;
double keliling = 2 * phi * r;
System.out.println("Keliling = 2 x phi x r");
System.out.println(" = 2 x " +phi+ " x " +r);
System.out.println(" = " +keliling);
break;
case 'b' :
System.out.print("Masukkan banyaknya deret : ");
Scanner deret = new Scanner (System.in);
int n = deret.nextInt();
System.out.print("Deret : ");
double hasil = 0;
for (int i=1; i
System.out.print(" ");
}
if (i==1) {
System.out.print(" atau ");
}
else {
for (int j=1; j<7; j++) { System.out.print(" "); } } // Pattern II for (int j=a; j>=1; j--) {
System.out.print(" ");
}
for (int j=i; j>=1; j--) {
System.out.print(j+ " ");
}
if (i==1) {
System.out.print(" atau ");
}
else {
for (int j=1; j<7; j++) { System.out.print(" "); } } // Pattern III for (int j=1; j<=a+1; j++) { System.out.print(j+ " "); } for (int j=2; j<=i; j++) { System.out.print(" "); } if (i==1) { System.out.print(" atau "); } else { for (int j=1; j<7; j++) { System.out.print(" "); } } // Pattern IV for (int j=2; j<=i; j++) { System.out.print(" "); } for (int j=a+1; j>=1; j--) {
System.out.print(j+ " ");
}
System.out.println();
}
break;
default : System.out.println("Input anda salah, silakan jalankan ulang program.");
}
}
}
Menu Program :
a. Menghitung Keliling Lingkaran
b. Menampilkan deret dan jumlahnya
c. Menampilkan Pola
Pilihan : a
Masukkan besar diameter : 12
Keliling = 2 x phi x r
= 2 x 3.14 x 6.0
= 37.68
Menu Program :
a. Menghitung Keliling Lingkaran
b. Menampilkan deret dan jumlahnya
c. Menampilkan Pola
Pilihan : b
Masukkan banyaknya deret : 12
Deret : 6.28 + 12.56 + 18.84 + 25.12 + 31.40 + 37.68 + 43.96 + 50.24 + 56.52 + 6
2.80 + 69.08 + 75.36 = 489.84
Menu Program :
a. Menghitung Keliling Lingkaran
b. Menampilkan deret dan jumlahnya
c. Menampilkan Pola
Pilihan : c
Masukkan nilai n : 4
1 atau 1 atau 1 2 3 4 atau 4 3 2 1
1 2 2 1 1 2 3 3 2 1
1 2 3 3 2 1 1 2 2 1
1 2 3 4 4 3 2 1 1 1
Menu Program :
a. Menghitung Keliling Lingkaran
b. Menampilkan deret dan jumlahnya
c. Menampilkan Pola
Pilihan : c
Masukkan nilai n : 5
1 atau 1 atau 1 2 3 4 5 atau 5 4 3 2 1
1 2 2 1 1 2 3 4 4 3 2 1
1 2 3 3 2 1 1 2 3 3 2 1
1 2 3 4 4 3 2 1 1 2 2 1
1 2 3 4 5 5 4 3 2 1 1 1
• public class model_1
{
public static void main(String[] args)
{
System.out.println("Model 1");
for (int row=1;row<=10;row++) { for (int col=1;col<=row;col++) System.out.print(col + " " ); System.out.println(); } } } Model 1 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 • public class model_1_2 { public static void main(String[] args) { System.out.println("Model 1&2"); for (int row=1;row<=10;row++) { for (int col=1;col<=row;col++) System.out.print(col + " "); System.out.println(); } for (int row=1;row<=10;row++) { for (int col=1;col<=10-row;col++) System.out.print(col + " "); System.out.println(); } } } Model 1&2 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 • public class model_2 { public static void main(String[] args) { System.out.println("Model 2"); for (int row=1;row<=10;row++) { for (int col=1;col<=11-row;col++) System.out.print(col + " "); System.out.println(); } } } Model 2 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 • public class model_3 { public static void main(String[] args) { System.out.println("Model 3"); for (int row=1;row<=10;row++) { for (int col=1;col <= 10-row;col++) System.out.print(" "); for (int col=row;col>=1;col--)
System.out.print(col + " ");
System.out.println();
}
System.out.println();
}
}
Model 3
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
• public class model_4
{
public static void main(String[] args)
{
System.out.println("Model 4");
for (int row=1;row<=6;row++) { for (int col=1;col <= 7-row;col++) System.out.print(col + " "); System.out.println(); for (int col=1;col<=row;col++) System.out.print(" "); } } } Model 4 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 • public class model_a { public static void main(String[] args) { System.out.println("Model a"); for (int i=1;i<=6;i++) System.out.print(i + " "); System.out.println(); } } Model a 1 2 3 4 5 6 • public class model_b { public static void main(String[] args) { System.out.println("Model b"); for (int i=1;i<=6;i++) System.out.println(i + " " ); System.out.println(); } } Model b 1 2 3 4 5 6 • //import java.util.Scanner; //import javax.swing.JOptionPane; public class piramida { public static void main(String[] args) { for (int row=0;row<=7;row++) { for (int col=1;col<=7-row;col++) System.out.print(" "); for (int col=0;col <=row;col++) if (Math.pow(2, col)<16) System.out.print(" " + (int)Math.pow(2, col)); else if (Math.pow(2, col)<128) System.out.print(" " + (int)Math.pow(2, col)); else System.out.print(" " + (int)Math.pow(2, col)); for (int col=row - 1; col>=0; col--)
if (Math.pow(2, col)<16)
System.out.print(" " + (int)Math.pow(2, col));
else
System.out.print(" " + (int)Math.pow(2, col));
System.out.println();
}
}
}
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
• import java.util.Scanner;
class pola{
public static void main (String []args)
{
int n;
System.out.println("Masukkan nilai n");
Scanner Scan = new Scanner(System.in);
n = Scan.nextInt();
for(int i=1; i<=n; i++){
for(int j=1; j<=i; j++){
System.out.print(0+j + " ");
}
System.out.println();
}
}
}
Masukkan nilai n
2
1
1 2
Masukkan nilai n
4
1
1 2
1 2 3
1 2 3 4
• public class While
{
public static void main(String[] args)
{
int i = 0;
int x = 1;
while (i<=25)
{
i = i+5;
x = x*i;
System.out.print("i=" +i);
System.out.println(" Nilai x = " +x);
}
}
}
i=5 Nilai x = 5
i=10 Nilai x = 50
i=15 Nilai x = 750
i=20 Nilai x = 15000
i=25 Nilai x = 375000
i=30 Nilai x = 11250000
• public class While2
{
public static void main(String[] args)
{
int j = 1;
int i = 1;
while (i<=9)
{
++i;
j=j+i;
System.out.print("i = " +i );
System.out.println(" Nilai x = " +j );
}
}
}
i = 2 Nilai x = 3
i = 3 Nilai x = 6
i = 4 Nilai x = 10
i = 5 Nilai x = 15
i = 6 Nilai x = 21
i = 7 Nilai x = 28
i = 8 Nilai x = 36
i = 9 Nilai x = 45
i = 10 Nilai x = 55
0 comments:
Post a Comment