Latihan Looping Java Menggunakan FOR
Contoh 1
class latihanfor {
public static void main(String[]args) {
int a, u, x;
for (a=9;a>=1;a--) {
x=a*a;
System.out.println();
for (u=a;u<=x;u+=a) {
System.out.print(u+" ");
}
}
System.out.println();
}
}
int a, u, x;
for (a=9;a>=1;a--) {
x=a*a;
System.out.println();
for (u=a;u<=x;u+=a) {
System.out.print(u+" ");
}
}
System.out.println();
}
}
contah 2
public class For {
public stattic void main(String[] args){
int b=0;
for(int i=1; i<6; i++){
int j=i*2;
System.out.print(j+”+”);
b=b+j;
if(j==10){
System.out.print(“=”);
System.out.print(a);
}
}
}
Hasil
2+4+6+8+10+=30
public stattic void main(String[] args){
int b=0;
for(int i=1; i<6; i++){
int j=i*2;
System.out.print(j+”+”);
b=b+j;
if(j==10){
System.out.print(“=”);
System.out.print(a);
}
}
}
Hasil
2+4+6+8+10+=30
Tidak ada komentar:
Posting Komentar