Example 1: Java program to print the numbers using for loop
KW.java
class KW 
{
    public static void main(String args[]) 
    {
        for(int i=0;i<=10;i++)
            System.out.println(i);
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW 0 1 2 3 4 5 6 7 8 9 10 kodingwindow@kw:~$
Example 2: Java program to print the numbers using for loop
KW.java
class KW 
{
    public static void main(String args[])
    {
        for(int i=-5;i<=5;i++)
            System.out.print(i+" ");
            System.out.println();
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW -5 -4 -3 -2 -1 0 1 2 3 4 5 kodingwindow@kw:~$
Example 3: Java program to print the numbers using for loop
KW.java
class KW 
{
    public static void main(String args[])
    {
        for(int i=5;i>=-5;i--)
            System.out.print(i+" ");
            System.out.println();
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW 5 4 3 2 1 0 -1 -2 -3 -4 -5 kodingwindow@kw:~$
Example 4: Java program to print the numbers using for loop
KW.java
class KW
{
    public static void main(String args[])
    {
        int i;
        for(i=1;i<=10;i++)
        {
            System.out.println(i);
            if(i==5)
                break;
        }
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW 1 2 3 4 5 kodingwindow@kw:~$
Advertisement