0% found this document useful (0 votes)
4 views2 pages

Java Number Triangle and Factorials

The document contains two Java programs: NumberTriangle and FactorialList. NumberTriangle prints a triangle of numbers from 1 to the current row number, while FactorialList calculates and prints the factorial of numbers from 1 to 10. Each program includes explanations of its structure and functionality.

Uploaded by

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

Java Number Triangle and Factorials

The document contains two Java programs: NumberTriangle and FactorialList. NumberTriangle prints a triangle of numbers from 1 to the current row number, while FactorialList calculates and prints the factorial of numbers from 1 to 10. Each program includes explanations of its structure and functionality.

Uploaded by

Ashok B P
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

public class NumberTriangle {

public static void main(String[] args) {


int rows = 5; // Total number of rows

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


// Print numbers from 1 to i
for (int j = 1; j <= i; j++) {
[Link](j + " ");
}
// Move to the next line
[Link]();
}
}
}

Program Explanation:
public class NumberTriangle

Defines a public class named NumberTriangle.

public static void main(String[] args)

This is the main method, which is the entry point of any Java program.

int rows = 5;

A variable rows is declared and set to 5, which represents how many lines the
triangle will have.

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

This outer loop runs from i = 1 to i = 5.

Each iteration of this loop represents a single row in the triangle.

for (int j = 1; j <= i; j++)

This inner loop runs from j = 1 up to the current value of i.

It prints the numbers from 1 to i on the same line.

[Link](j + " ");

Prints the value of j followed by a space, staying on the same line.

[Link]();

After the inner loop finishes, this statement moves the cursor to the next line to
begin a new row.

public class FactorialList {


public static void main(String[] args) {
int number = 1; // Start from 1

// Loop through numbers from 1 to 10


while (number <= 10) {
int fact = 1; // To store factorial result
int i = number; // Temp variable to calculate factorial

// Inner while loop to calculate factorial


while (i > 0) {
fact = fact * i; // Multiply current value of i
i--; // Decrement i
}

// Print the factorial result for current number


[Link]("Factorial of " + number + " is: " + fact);

number++; // Move to the next number


}
}
}

Program Explanation:
public class FactorialList

Defines the class containing our program.

int number = 1;

Initializes the starting number for which factorial will be calculated.

while (number <= 10)

Outer while loop runs from number = 1 to number = 10.

Inside the loop:

int fact = 1;
Initializes factorial to 1 for the current number.

int i = number;
Sets a temporary variable i equal to the current number.

while (i > 0)
Inner loop multiplies fact by i and decrements i until i becomes 0. This calculates
the factorial (e.g., 4*3*2*1).

[Link](...)

Prints the factorial result for the current number.

number++

Moves to the next number (e.g., from 1 to 2, then 2 to 3, etc.).

You might also like