Lab Digital Assignment – 2
Fall Semester 2020-21
Name – Tanay Dubey
Registration Number – 19BCT0185
Course Code and Name – CSE1007 Java Programming
Faculty Name – Gopichand G
Slot – L27 + L28
1. Write a Java Program to Print a Daimond Pattern of “*”.
Soln)
Code
package com.company;
import java.util.Scanner;
public class Pattern1 {
public static void main(String args[]) {
int n, i, j, space = 1;
System.out.print("Enter the number of rows: ");
Scanner s = new Scanner(System.in);
n = s.nextInt();
space = n - 1;
for (j = 1; j<= n; j++) {
for (i = 1; i<= space; i++) {
System.out.print(" ");
}
space--;
for (i = 1; i <= 2 * j - 1; i++) {
System.out.print("*");
}
System.out.println("");
}
space = 1;
for (j = 1; j<= n - 1; j++) {
for (i = 1; i<= space; i++) {
System.out.print(" ");
}
space++;
for (i = 1; i<= 2 * (n - j) - 1; i++) {
System.out.print("*");
}
System.out.println("");
}
}
}
Output
2. Write a Java Program to Print a Inverted Daimond Pattern of
“#”.
Soln)
Code
package com.company;
import java.util.Scanner;
public class Pattern2 {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int rows = sc.nextInt();
for (int i= 0; i<= rows-1 ; i++) {
for (int j=0; j <i; j++) {
System.out.print(" ");
}
for (int k=i; k<=rows-1; k++) {
System.out.print("#" + " ");
} System.out.println("");
}
for (int i= rows-1; i>= 0; i--) {
for (int j=0; j< i ;j++) {
System.out.print(" ");
}
for (int k=i; k<=rows-1; k++) {
System.out.print("#" + " ");
}
System.out.println("");
}
sc.close();
}
}
Output
3. Write a Java Program to Print a Pyramid of Numbers
As Pascal’s Triangle.
Soln)
Code
package com.company;
import java.util.Scanner;
public class Pattern3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the No. of Rows:");
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int number = 1;
System.out.printf("%" + (n - i) * 2 + "s", "");
for (int j = 0; j <= i; j++) {
System.out.printf("%4d", number);
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}
}
Output
4. Write a Java Program to Print a Inverted Daimond Pattern of
Numbers.
Soln)
Code
package com.company;
import java.util.Scanner;
public class Pattern4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter no. of Rows: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int k = i; k <= n; k++) {
System.out.print(k+" ");
}
System.out.println();
}
for (int i = n-1; i >= 1; i--) {
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int k = i; k <= n; k++) {
System.out.print(k+" ");
}
System.out.println();
}
}
}
Output
5. Write a Java Program to Print Right Angled Triangle Pattern of
English Alphabets with 5 rows and Letter of choice to start
with Incrementation and with Same Letter in each row.
Soln)
Code
package com.company;
import java.util.Scanner;
public class Pattern5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter any No. Between 65 and 91 :");
int alphabet = sc.nextInt();
for (int i = 0; i<= 5; i++) {
for (int j = 0; j <= i; j++) {
System.out.print((char) alphabet + " ");
}
alphabet++;
System.out.println();
}
}
}
Output
6. Write a Java Program to Print Right Angled Triangle Pattern of
English Alphabets with 5 rows and Letter of choice to start
with Incrementation.
Soln)
Code
com.company;
import java.util.Scanner;
public class Pattern6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter any No. Between 65 and 91 :");
int alphabet = sc.nextInt();
for (int i = 0; i <= 5; i++) {
for (int j = 0; j <= i; j++) {
System.out.print((char) (alphabet + j) + " ");
}
System.out.println();
}
}
}
Output