ADVERTISEMENT
ADVERTISEMENT

Java program to implement nested loop using for loop

Here, we have to implement a nested loop using for loop.
Submitted by Nidhi, on March 08, 2022

Problem Solution:

In this program, we will use the for loop to print tables from 2 to 5.

Program/Source Code:

The source code to implement the nested loop using the for loop is given below. The given program is compiled and executed successfully.

// Java program to implement nested loop 
// using the for loop

public class Main {
  public static void main(String[] args) {
    int cnt1 = 0;
    int cnt2 = 0;

    for (cnt1 = 2; cnt1 <= 5; cnt1++) {
      for (cnt2 = 1; cnt2 <= 10; cnt2++) {
        System.out.print((cnt1 * cnt2) + " ");
      }
      System.out.println();
    }
  }
}

Output:

2 4 6 8 10 12 14 16 18 20 
3 6 9 12 15 18 21 24 27 30 
4 8 12 16 20 24 28 32 36 40 
5 10 15 20 25 30 35 40 45 50

Explanation:

In the above program, we created a public class Main. It contains a static method main().

The main() method is an entry point for the program. Here, we used a nested for loop to print tables from 2 to 5.

Java Basic Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.