Looping pada Netbeans

Menggunakan class For


public class For{
                public static void main(String[] args){
                for (int i=2; i<=10; i+=2)
                                System.out.print(i+” “);
}
}

Hasilnya
2 4 6 8 10

Menggunakan class While

public class While {
                public static void main(String[] args){
                int n=3;
                while(n<20)
                {
                                System.out.println(n+ “ “); n+=3;
                }
                }
}

Hasilnya : 3 6 9 12 15 18

Cotoh program for dengan hasil jumlahnya
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(b);
                }
}
}

Hasil
2+4+6+8+10+=30

contoh 2 for dengan hasil jumlah

public class For {
    public static void main (String[] args)
    {
        int i;
        int jum=0;
            for(i=2;i<=10;i+=2){
            System.out.print(i+"+");
            jum=jum+i;
        }
        System.out.print("="+jum);
    }
}

Posting Komentar

Designed by OddThemes | Distributed by Gooyaabi Templates