Java Basic Programs

Hello everyone,

Today i’m gone do some basic programs in java using looping and also explain what is looping and why is it used.

Looping:

while(count<=5) {
System.out.println(a);
a=a*11;
count++;
}

In the above Program a same method is running again and again for three times. So whenever a repeated process is going on we can use Looping .

Looping is useful for time consumption and Without looping depending upon the programs we need to write lot of blocks which will do the same work using looping we can avoid that.

In Looping we have three types:

While Loop, For Loop, and Do While Loop.

While and For Loops are Entry level check whereas Do While Loop is exit level check .

while and For loops will execute only if the given conditional statement is true.

Do While it will execute the method definitely for one time and then check the conditional statement true or false to do the process again.

1.Print one,Eleven, One twenty one.(While Loop)

public class PrintOneElevanonetwentyone {

public static void main(String[] args) {
    int a=1,count=1;
    while(count<=5) {
        System.out.println(a);
        a=a*11;
        count++;
    }
    // TODO Auto-generated method stub

}

Example Program for Do While loop;

public class Factorial54321 {

public static void main(String[] args) {
    int given=5;
    do {
        int fact=1,no=1;
        while(no<=given)
        {
            fact=fact*no;
            no++;
        }
        System.out.println(fact);
        given=given-1;
    } while(given>0);
}
    // TODO Auto-generated method stub

}

Example Program for For loop;

public class ForloopEvenno {

public static void main(String[] args) {
    int no;
    for(no=2;no<=10;no+=2) {
        System.out.println(no);
    }
    // TODO Auto-generated method stub

}

}

Leave a comment

Design a site like this with WordPress.com
Get started