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