Arithmetic Progression Matrix
This program prints a matrix of arithmetic progression values using nested for loop.
Problem Statement
Write a Java program that prints a 3x3 matrix of arithmetic progression values starting from 2 with difference 2.
Source Code
| 1 | public class APMatrix { |
| 2 | public static void main(String[] args) { |
| 3 | int a = 2, d = 2, num = a; |
| 4 | for(int i=1; i<=3; i++) { |
| 5 | for(int j=1; j<=3; j++) { |
| 6 | System.out.print(num + " "); |
| 7 | num += d; |
| 8 | } |
| 9 | System.out.println(); |
| 10 | } |
| 11 | } |
| 12 | } |
Program Output
2 4 6 8 10 12 14 16 18
Explanation
The outer loop controls rows, inner loop prints AP values sequentially. Each iteration increments num by difference d. This demonstrates how nested loops can generate arithmetic progression in matrix form. Shows how nested for loops can generate AP matrices.