Objective
Objective
To write a Java program that prints a right triangle pattern using asterisks (*).
Requirements:
Java Development Kit (JDK)
Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or
NetBeans
Description:
This lab aims to create a simple Java program that prints a right triangle pattern. The
pattern consists of asterisks, where the number of n is defined in the program. Each
n line contains a number of asterisks equal to the n number.
Problem:
Use the for loop to output the right triangle for n lines. The example for n= 3
Code:
package patternprinting;
import java.util.Scanner;
public class RightTrianglestr {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n;
System.out.print("enter the numbers line: ");
n=input.nextInt();
for(int i=1;i<=n;i++)
{for(int j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
}
} output
Conclusion:
The program successfully prints a right triangle pattern with the specified number of
lines. This exercise demonstrates the use of nested loops in Java for pattern printing.
Objective:
To write a Java program that prints a left-aligned triangle pattern using asterisks (*).
Requirements:
Java Development Kit (JDK)
Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or
NetBeans
Description:
This lab aims to create a simple Java program that prints a left-aligned triangle
pattern. The pattern consists of asterisks, with the number of n defined in the
program. Each row contains a number of asterisks equal to the n number and spaces
to left-align them.
Problem:
Use the for loop to output the left triangle for n lines. The example for n= 3
Code:
package patternprinting;
import java.util.Scanner;
public class LeftTrianglestr {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n;
System.out.println("enter the numbers of line: ");
n=input.nextInt();
for(int i=1;i<=n;i++){
for(int space=3;space>=i;space--){
System.out.print(" ");
}
for(int j=1;j<=i;j++)
{ System.out.print("*");
}
System.out.println();
}
}
}
output
Conclusion:
The program successfully prints a left-aligned triangle pattern with the specified
number of n line. This exercise demonstrates the use of nested loops in Java for
pattern printing and proper alignment.
Objective:
To write a Java program that prints a pyramid pattern using asterisks (*).
Requirements:
Java Development Kit (JDK)
Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or
NetBeans
Description:
This lab aims to create a simple Java program that prints a pyramid pattern. The
pattern consists of asterisks, with the number of rows defined in the program. Each
row contains a certain number of asterisks and spaces to form a symmetrical
pyramid shape.
Problem:
Use the for loop to output the pyramid for n lines. The example for n= 3
Code:
package patternprinting;
import java.util.Scanner;
public class PyramidStar {
public static void main(String[] args){
Scanner input= new Scanner(System.in);
int n;
System.out.print("enter the lines: ");
n=input.nextInt();
for (int i = 1; i <=n; i++){
for (int sp=i;sp<n;sp++){
System.out.print(" ");
}
for (int j=1;j<=(2*i-1);j++){
System.out.print("*");
}
System.out.println();
}
}
} output
Conclusion:
The program successfully prints a pyramid pattern with the specified number of n
lines. This exercise demonstrates the use of nested loops in Java for pattern
printing and proper alignment.