[go: up one dir, main page]

0% found this document useful (0 votes)
40 views6 pages

Nested For Loops (REVISION)

The document provides Java programs that utilize nested for-loops to generate various patterns and solve mathematical problems. It includes examples such as a rectangle of stars, right-angled triangle, inverted triangle, number patterns, multiplication tables, Floyd's triangle, perfect numbers, pairs of two-digit numbers that sum to 100, and Pythagorean triplets. Each program is accompanied by its output to illustrate the results.

Uploaded by

harshika.k7913
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views6 pages

Nested For Loops (REVISION)

The document provides Java programs that utilize nested for-loops to generate various patterns and solve mathematical problems. It includes examples such as a rectangle of stars, right-angled triangle, inverted triangle, number patterns, multiplication tables, Floyd's triangle, perfect numbers, pairs of two-digit numbers that sum to 100, and Pythagorean triplets. Each program is accompanied by its output to illustrate the results.

Uploaded by

harshika.k7913
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Programs using Nested for-loops

1. Pattern: Rectangle of Stars


public class StarRectangle {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) { // Rows
for (int j = 1; j <= 5; j++) { // Columns
System.out.print("* ");
}
System.out.println();
}
}
}
Output:
*****
*****
*****
*****
2. Pattern: Right-Angled Triangle
public class RightTriangle {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) { // Rows
for (int j = 1; j <= i; j++) { // Columns
System.out.print("* ");
}
System.out.println();
}
}
}
Output:
*
**
***
****
*****
3. Pattern: Inverted Triangle
public class InvertedTriangle {
public static void main(String[] args) {
for (int i = 5; i >= 1; i--) { // Rows
for (int j = 1; j <= i; j++) { // Columns
System.out.print("* ");
}
System.out.println();
}
}
}
Output:
*****
****
***
**
*
4. Number Pattern
public class NumberPattern {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) { // Rows
for (int j = 1; j <= i; j++) { // Columns
System.out.print(j + " ");
}
System.out.println();
}
}
}
Output:
1
12
123
1234
12345
5. Multiplication Table
public class MultiplicationTable {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) { // Rows
for (int j = 1; j <= 10; j++) { // Columns
System.out.print((i * j) + "\t");
}
System.out.println();
}}}
Output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
...
10 20 30 40 50 60 70 80 90 100
6. Floyd’s Triangle
public class FloydsTriangle {
public static void main(String[] args) {
int rows = 5;
int num = 1;

for (int i = 1; i <= rows; i++) {


for (int j = 1; j <= i; j++) {
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
}
Output:
1
23
456
7 8 9 10
11 12 13 14 15

6. Display All Perfect Numbers between 1 and 500


A perfect number is one where the sum of its proper divisors equals the number.
Example: 28 → 1 + 2 + 4 + 7 + 14 = 28
public class PerfectNumbers {
public static void main(String[] args) {
System.out.println("Perfect numbers from 1 to 500:");
for (int num = 1; num <= 500; num++) {
int sum = 0;
for (int i = 1; i < num; i++) {
if (num % i == 0)
sum += i;
}
if (sum == num)
System.out.println(num);
}}}

7. Print All Pairs of Two-Digit Numbers That Add Up to 100


public class SumTo100 {
public static void main(String[] args) {
System.out.println("Pairs of two-digit numbers whose sum is 100:");
for (int i = 10; i <= 99; i++) {
for (int j = i; j <= 99; j++) {
if (i + j == 100)
System.out.println(i + " + " + j + " = 100");
}}}}

8. Print All Pythagorean Triplets (a² + b² = c²) with a, b, c ≤ 30


public class PythagoreanTriplets {
public static void main(String[] args) {
System.out.println("Pythagorean Triplets where a, b, c <= 30:");

for (int a = 1; a <= 30; a++) {


for (int b = a; b <= 30; b++) {
for (int c = b; c <= 30; c++) {
if (a * a + b * b == c * c)
System.out.println(a + ", " + b + ", " + c);
}
}
}
}
}
}

You might also like