Minggu, 16 Desember 2012

Algoritma menggunakan perulangan While,Do..while,and For

1.Dasar Teori Dalam makalah kali ini,kita memakai fungsi looping atau perulangan. 1. While : Pernyataan while akan dijalankan secara terus-menerus selama kondisi bernilai benar(true). Bentuk umumnya adalah : while( boolean_expression ) { statement1; statement2; . . . } 2. Do While : Pernyataan do..while, statement dieksekusi setidaknya satu kali. Bentuk umumnya adalah : do{ statement1; statement2; . . . }while( boolean_expression ); 3. For : Pernyataan for , melakukan eksekusi pengulangan beberapa kali. Bentuk umumnya adalah : for (Initialization; LoopCondition; StepExpression){ statement1; statement2; . . . } 2. Listing Program dan Outputan 1. Menampilkan bilangan 1-10 menggunakan : a) While Coding programnya : package makalah; /** * * @author Rinis */ public class Makalah { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int x=0; while(x<10){ x++; System.out.println(x); } Outputan dari Coding di atas :
b) Do..While Coding programnya : package makalah; /** * * @author Rinis */ public class Makalah { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int x=1; do{ System.out.println(x); x++; } while(x<=10); } Outputan dari Coding di atas :
c) For... Coding programnya : package javaapplication5; /** * * @author Rinis */ public class JavaApplication5 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int a; for (a=1;a<=10;a++){ System.out.println(a); } } } Outputan dari Coding di atas :
2. Menampilkan Bilangan dari 10-1 menggunakan : a) While : Coding Programnya : package javaapplication5; /** * * @author Rinis */ public class JavaApplication5 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int a=10; while(a>0){ System.out.println(a); a--; } Outputan dari Coding di atas :
b) Do..While : Coding programnya: package javaapplication5; /** * * @author Rinis */ public class JavaApplication5 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int a=10; do { System.out.println(a); a--; } while (a>0); } Outputan dari Coding di atas :
c) For... : Coding programnya : package javaapplication5; /** * * @author Rinis */ public class JavaApplication5 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int x; for (x=10;x>0;x--){ System.out.println(x); } Outputan dari Coding di atas :
3. Menapilkan Bilangan kelipatan 3 tanpa kelipatan 5 mulai dari 3 sampai 30 menggunakan : a) While : Coding programnya : package javaapplication5; /** * * @author Rinis */ public class JavaApplication5 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int a=3; while(a<=30){ if(a%5!=0) System.out.println(a); a+=3; } Outputan dari Coding di atas :
b) Do...While : Coding programnya : package javaapplication5; /** * * @author Rinis */ public class JavaApplication5 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int a=3; do { if(a%5!=0) System.out.println(a); a+=3; } while (a<=30); } Outputan dari Coding di atas :
c) For : Coding programnya : package javaapplication5; /** * * @author Rinis */ public class JavaApplication5 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here for(int x=3;x<=30;x+=3){ if(x%5!=0) System.out.println(x); } Outputan dari Coding di atas :
4. Menampilkan bukit bintang menggunakan : a) For : Coding programnya : /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int i=0,j=0; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { System.out.print("*"); } System.out.println(""); } for(i=5;i>0;i--) { for(j=1;j<=i;j++) { System.out.print("*"); } System.out.println(""); } } Outputan dari Coding di atas :
5. Menampilkan Perkalian dari 1-10 menggunakan : a) While: Coding programnya : public class JavaApplication5 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int x=1; while(x<=10){ int i=1; while(i<=10){ System.out.println(i + " X "+x+" = "+x*i); i++; } x++; System.out.println(""); } Outputan dari Coding di atas :
SEMOGA BERMANFAAT :)