# Programming in Java | NPTEL 2022 | Week 8 Programming Assignments Solutions

### Programming in Java NPTEL Week 8 Programming Assignment 1 Solutions

Write a program which willÂ print a pyramid of “*” ‘s of height “n” and print the number of “*” ‘s in the pyramid.

``````import java.util.*;
public class Pattern1 {
public static void main(String[] args) {
Scanner inr = new Scanner(System.in);
int n = inr.nextInt();
// Add the necessary code in the below space

int a = 0, sum = 0;
for(int i=1; i<=n; ++i) {
a=0;
for(int j=1; j<=n-i; ++j) {
System.out.print("  ");
}
while(a!=2*i-1) {
System.out.print("* ");
sum++;
++a;
}
System.out.println();
}
System.out.println(sum);
}
}
``````

### Programming Assignment 2 Solution

Write a program which willÂ print a pascalÂ  pyramid ofÂ  “*” ‘s of height “l” .

``````import java.util.*;
public class Pattern2 {
public static void main(String[] args) {
Scanner inr = new Scanner(System.in);
int l = inr.nextInt();
// Add the necessary code in the below space

int space = l - 1;

for(int i=0; i<l; i++) {
for(int j=0; j<space; j++) {
System.out.print(" ");
}
for(int j=0; j<=i; j++) {
System.out.print("* ");
}
System.out.println();
space--;
}
}
}
``````

### Programming Assignment 3 Solution

Write a program which willÂ print a pyramid of “numbers” ‘s of height “n” and print the sum of all number’s in the pyramid.

`````` import java.util.*;
public class Pattern3 {
public static void main(String[] args) {
Scanner inr = new Scanner(System.in);
int n = inr.nextInt();
// Add the necessary code in the below space
int num, sum = 0;
for(int i=1; i<=n; i++) {
num=1;
for(int j=1; j<=n-i; j++) {
System.out.print("  ");
}
while(num <= 2*i-1) {
System.out.print(num + " ");
sum = sum + num;
num++;
}
System.out.println();
}
System.out.println(sum);
}
}``````

### Week 8: Programming Assignment 4 Solution

Write a program toÂ print symmetric Pascal’s triangle of “*” ‘s ofÂ  height “l” of odd length . If input “l” is even then your program will print “Invalid line number”.

``````import java.util.*;
public class Pattern4 {
public static void main(String[] args) {
Scanner inr = new Scanner(System.in);
int l = inr.nextInt();
// Add the necessary code in the below space

int ul=0, ll=0;
if (l%2!=0) {
ul = (l/2)+1;
ll=l-ul;
for(int i=1;i<=ul;i++) {
for(int j=1;j<=(ul-i); j++) {
System.out.print(" ");
}

for(int j=1;j<=i; j++){
System.out.print("* ");
}
System.out.println();
}

int c=ll;
for(int i=1;i<=ll; i++) {
for(int j=c;j<ll; j++) {
System.out.print(" ");
}

for(int j=1;j-1<=ll-i; j++) {
System.out.print(" *");
}
c--;
System.out.println();
}
}
else {
System.out.print("Invalid line number");
}

}
}``````

### Week 8: Programming Assignment 5 Solution

Write a program toÂ display any digit(n) from 0-9 represented as a “7 segmentÂ  display”.Â

``````import java.util.*;
public class Pattern5 {
public static void main(String[] args) throws Exception {
Scanner inr = new Scanner(System.in);
int n = inr.nextInt();
// Add the necessary code in the below space

if(n==2 || n==3 || n==5 || n==6 || n==7 || n==8 || n==9 || n==0)
System.out.println(" _ ");
else
System.out.println("  ");

if(n==1 || n==7)
System.out.println("  |");
else if(n==2 || n==3)
System.out.println(" _|");
else if(n==4 || n==8 || n==9)
System.out.println("|_|");
else if(n==5 || n==6)
System.out.println("|_ ");
else
System.out.println("| |");

if(n==1 || n==4 || n==7)
System.out.println("  |");
else if(n==3 || n==5 || n==9)
System.out.println(" _|");
else if(n==6 || n==8 || n==0)
System.out.println("|_|");
else
System.out.println("|_ ");
}
}
``````

For discussion about any question, join the below comment section. And get the solution of your query. Also, try to share your thoughts about the topics covered in this particular quiz.