0% found this document useful (0 votes)
32 views

Assignment 15 Class 9

The document contains code snippets for printing different patterns using loops in Java. The patterns include Pascal's triangle with 6 rows, a star pattern with 5 rows, a number pattern with increasing numbers in each row from 1 to 5, and two separate patterns - one with descending numbers from 5 to 1 and stars, and another with descending numbers from 5 to 1. The code uses for loops with nested for loops to iterate through rows and columns to print the patterns by outputting numbers or characters like stars and hashes.

Uploaded by

Sneha Bhatia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Assignment 15 Class 9

The document contains code snippets for printing different patterns using loops in Java. The patterns include Pascal's triangle with 6 rows, a star pattern with 5 rows, a number pattern with increasing numbers in each row from 1 to 5, and two separate patterns - one with descending numbers from 5 to 1 and stars, and another with descending numbers from 5 to 1. The code uses for loops with nested for loops to iterate through rows and columns to print the patterns by outputting numbers or characters like stars and hashes.

Uploaded by

Sneha Bhatia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

12.

WAP to print the Pascal's triangle as below


1

1 1
1 2 1

3 3 1

1 4 6 4 1

1 5 10 10 5 1

Ans. public class Pascal

public static void main()

int r, i, k, number=1, j;
r=6;
for(i=0;i<r;i++)

for(k=r; k>i; k--)

System.out.print("")

number =1;
forj=0j<=ij++)
System.out.print(number+ " ");

nurzber =
number* (i-j)/G + 1);

System.out.printin();
17. Write a program to print the following pattern:

Ans. class Star

public static void main()

int i.j
fori=1;i<=5;i++)

forj=1j<=ij++)

System.out.print("*");

System.out.println();
20. Write a program to print the following pattern:
1
12
123
1234
12345
Ans. public class Pattern

public static void main()

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

for(int j=1j<=ij++)

System.out.printj+" ");

System.out.println();
18. Write two separate programs to generate the following patterns using iteration (loop)
(a)
statements
(b) 5 4 3 2 1 [2015]
4 3 2
5 4 3
# 5 4
5 [15]
Ans. (a) The program is as follows:
public class PatternA {
public static void main() {
for(int i=1; i<=5; i++) {
for(int j=1; j<=i; j++{
ifj%21=0)
System.out.print("*");
else
System.out.print("#*);
System.out.println();

b) The program is as follows:


public class PatternB {
public static void main() {
for(int i 1;i <= 5; i++) {
for(int j 5; j >= is j-) {
=

System.out.print);

System.out.println);

You might also like