Source code of javaforloop.java
class javaforloop{
public static void main(String[] args){
for(int i=1;i<=15;i++){
System.out.println(" "+i);
}
}
}
output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
In the below source code we are going to print the number in increasing order, Each time, it prints the number from new line with next number.
/*Program for print 1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
*/
class javaforloop4{
public static void main(String[] args){
for(int i=1;i<=5;i++){
for(int j=i;j>=1;j--){
System.out.print(j);
}
System.out.println(" ");
}
}
}
Output:
1
21
321
4321
54321
In the below source code we are going to print the number from 4-1 in decreasing order, each time number decreases by 1 and print from new line.
/*Program for print 4 3 2 1
3 2 1
2 1
1
*/
class javaforloop5{
public static void main(String[] args){
for(int i=4;i>=1;i--){
for(int j=i;j>=1;j--){
System.out.print(" "+j);
}
System.out.println(" ");
}
}
}
Output
4321
321
21
1
In the below source code we are going to print the number from 1-3 in decreasing order, each time number decreases by 1and print from new line.
/*Program for print 1 2 3
2 3
3
*/
class javaforloop6{
public static void main(String[] args){
for(int i=1;i<=4;i++){
for(int j=i;j<4;j++){
System.out.print(" "+j);
}
System.out.println(" ");
}
}
}
Output
1 2 3
2 3
3
In the below source code we are going to print the number from 1-4 in increasing order, each time number increases by same number and print from new line.
/*Program for print 1
2 2
3 3 3
4 4 4 4
*/
class javaforloop7{
public static void main(String[] args){
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++){
System.out.print(" "+i);
}
System.out.println(" ");
}
}
}
Output
1
2 2
3 3 3
4 4 4 4
In the below source code we are going to print the number from 1 in increasing order, each time number increases by same number and print from new line.
/*Program for print 1
1 1
1 1 1
1 1 1 1
*/
class javaforloop8{
public static void main(String[] args){
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++){
System.out.print(" "+"1");
}
System.out.println(" ");
}
}
}
Output
1
1 1
1 1 1
1 1 1 1
In the below source code we are going to print the number from 1-4 in increasing order, each time number increases by same number and print from new line and every new line the odd number print with + operator and the even number print with - operator.
/*Program for print 1
-2 -2
3 3 3
-4 -4 -4 -4
*/
class javaforloop9{
public static void main(String[] args){
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++){
if(i%2==0)
{
System.out.print(-i);
}
else
{
System.out.print(" "+i);
}
System.out.print(" ");
}
System.out.println();
}
}
}
Output
1
-2 -2
3 3 3
-4 -4 -4 -4
In the below source code we are going to print the number from 1-5 in increasing order, each time number increases by next number and print from new line.
/*Program for print 1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
*/
class javaforloop3{
public static void main(String[] args){
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print(j);
}
System.out.println(" ");
}
}
}
Output
1
12
123
1234
12345
In the below source code we are going to print the even number up to 15.
//program for print 2 4 6 8 10 12 14
class javaforloop2{
public static void main(String[] args){
for(int i=1;i<=15;i++){
if(i%2==0){
System.out.print(" "+i);
}
}
}
}
Output
2 4 6 8 10 12 14
In the below source code we are going to print the odd number up to 15.
//Program for print 1 3 5 7 9 11 13 15
class javaforloop1{
public static void main(String[] args){
for(int i=1;i<=15;i++){
if(i%2==1){
System.out.print(" "+i);
}
}
}
}
Output
1 3 5 7 9 11 13 15