1. Menampilkan
Bilangan Prima
public class BilanganPrima {
/**
*
@param args the command line arguments
*/
public static void main(String[] args) {
int input = 100;
for (int i = 2; i < input; i++) {
boolean isPrima = true;
for (int j = 2; j < i; j++) {
if(i%j==0){
isPrima = false;
break;
}
}
if(isPrima==true){
System.out.println(i+",");
}
}
}
}
2. IF-Else
import javax.swing.JOptionPane;
public class ifelse
{
public
static void main ( String[] args ){
String
huruf = "";
int
angka;
huruf
= JOptionPane.showInputDialog
("masukkan
huruf");
angka
= Integer.parseInt (huruf);
String
msg;
if
(angka > 60){
msg
= "Hello " + angka + ":-)";
}
else
{
msg
= "Hello " + angka + ":-(";
}
JOptionPane.showMessageDialog
(null, msg);
}
}
3. Menampilkan
Bilangan Genap
Berikut Pseudocode (alogaritma) Java :
public class BilanganGenap
{
public static void main(String[] args)
{
int i;
for (i=0; i<=20; i=i+2)
{
System.out.print(i + ". ");
System.out.println("Bilangan Genap");
}
}
}
4. Menampilkan
Bilangan Ganjil
Program untuk menampilkan bilangan ganjil 1
- 10 di aplikasi java ( console )
dengan
menggunakan FOR
class ganjil{
public static void main(String args[]){
int
i;
for(i=0;i<=10;i++){
if(i%2==1){
System.out.println(i);
}
}
}
}
dengan
menggunakan Do-While
class ganjil{
public static void main(String args[]){
int
i=0;
do{
if(i%2==1){
System.out.println(i);
}
i++;
}while(i<10);
}
}
5. Rata-rata dengan
JOPtionPane
import javax.swing.JOptionPane;
public class coba
{
public
static void main ( String[] args ){
String
angka1="";
angka1=JOptionPane.showInputDialog
("masukkan angka 1=");
int a=Integer.parseInt(angka1);
String angka2="";
angka2=JOptionPane.showInputDialog
("masukkan angka2=");
int b=Integer.parseInt(angka2);
String angka3="";
angka3=JOptionPane.showInputDialog
("masukkan angka3=");
int c=Integer.parseInt(angka3);
int angka=(a+b+c)/3;
String
msg;
if
(angka > 60){
msg
= "Hello " + angka + ":-)";
}
else
{
msg
= "Hello " + angka + ":-(";
}
JOptionPane.showMessageDialog
(null, msg);
}
}
6. Penjumlahan
JOPtionPane
import javax.swing.JOptionPane;
public class jajal
{
public
static void main ( String[] args ){
int opsi=JOptionPane.YES_OPTION;
double jumlah=0;
while(opsi==JOptionPane.YES_OPTION){
jumlah+=Double.parseDouble(JOptionPane.showInputDialog(null,"masukkan
nilai"));
opsi=JOptionPane.showConfirmDialog(null,"Tambah
data lagi?","Konfirmasi",JOptionPane.YES_NO_OPTION);
}
JOptionPane.showMessageDialog
(null,
"jumlah="+jumlah,"Hasil",JOptionPane.INFORMATION_MESSAGE);
}
}
7. Mencari Luas
Lingkaran
public class Lingkaran
{
public
static void main( String[] args){
int
r = 10;
double
phi =3.14;
double
L=phi*r*r;
double
K=2*phi*r;
System.out.println(
"Luas Lingkaran"+ L);
System.out.println
("Keliling Lingkaran" + K);
}
}
8. Rata-rata
public class rata
{
public
static void main( String[] args){
int
a =10;
int
b =20;
int
c =45;
float
total =a+b+c;
float
Rerata =total/3;
System.out.println(
"number 1 = "+ a);
System.out.println
("number 2 = " + b);
System.out.println
("number 3 = " + c);
System.out.println
("averege="+Rerata);
}
}
9. Input Nama
JOPtionPane
import javax.swing.JOptionPane;
public class Input
{
public static void main ( String[] args ){
String name ="";
name =JOptionPane.showInputDialog
("pleas
enter your name");
String msg = "Hello " + name +
"!";
JOptionPane.showMessageDialog (null,msg);
}
}
10. Program bintang
Bintang 1
public class bintang{
public
static void main(String [] args) {
int
x,y;
for(x=0;x<=5;x++){
for(y=1;y<=x;y++){
System.out.print("*");
}
System.out.println();
}
}
}
Bintang2
public class bintang2{
public
static void main(String [] args){
int
x,y,z;
for
(x = 5; x > 0; x--){
for (y = x-1;y > 0;y--){
System.out.print ("
");
}
for (z = x;z < 5+1;z++){
System.out.print("*
");
}
System.out.println();
}
}
}
11. KUIS
import javax.swing.JOptionPane;
public class kuis
{
public
static void main(String[] args){
int
x = Integer.parseInt(JOptionPane.showInputDialog("masukan nilai
awal"));
int
y = Integer.parseInt(JOptionPane.showInputDialog("Masukan Nilai
Akhir"));
do
{
System.out.println(x);
x++;
}while
(x<y);
}
}
Tidak ada komentar:
Posting Komentar