Uploaded by Blaidea Jailor

13-Pattern Programs-30-08-2021 [30-Aug-2021]Material I 30-Aug-2021 Looping Programs - Patterns

advertisement
Topic/Course
Looping
Program - Patterns
Sub-Topic (Example: name of college)
Question 1
Write a program to generate the following patten.
Sample Input:
5
Sample Output:
12345
2345
345
45
5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Scanner;
class Pattern1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 1; i<=n; i++)
{
int count = i;
for(int j = i; j<=n; j++)
{
System.out.print(count);
count++;
}
System.out.println();
}
}
}
Question 2
Write a program to generate the following patten.
Sample Input:
5
Sample Output:
1
24
135
2468
13579
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Scanner;
class Pattern2
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k;
for(int i=1; i<=n; i++)
{
if(i % 2 == 1)
k = 1;
else
k = 2;
for(int j=1; j<=i; j++, k+=2)
{
System.out.print(k);
}
System.out.println();
}
}
}
Question 3
Write a program to generate the following patten.
Sample Input:
5
Sample Output:
12345
23455
34555
45555
55555
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Scanner;
class Pattern3
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 1; i<=n; i++)
{
int count = i;
for(int j = 1; j<=n; j++)
{
System.out.print(count);
if(count <n)
count++;
}
System.out.println();
}
}
}
Question 4
Write a program to generate the following patten.
Sample Input:
5
Sample Output:
12345
23451
34512
45123
51234
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Scanner;
class Pattern4
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 1; i<=n; i++)
{
int count = i;
for(int j = 1; j<=n; j++)
{
System.out.print(count);
count = count%n;
count++;
}
System.out.println();
}
}
}
Question 5
Write a program to generate the following patten.
Sample Input:
5
Sample Output:
1
121
12321
1234321
123454321
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Scanner;
class Pattern6
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=1; i<=n; i++){
for(int k = 1; k<=n-i ; k++) {
System.out.print(" ");
}
for(int j=1; j<=i; j++) {
System.out.print(j);
}
for(int j=i-1; j>=1; j--) {
System.out.print(j);
}
System.out.println();
}
}
}
Question 6
Write a program to generate the following patten.
Sample Input:
5
Sample Output:
1
1 1
1 2 1
1 3 31
1 4 6 4 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Scanner;
class Pattern5
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int line = 1; line <= n; line++) {
for(int space = 1; space<=n-line;space++){
System.out.print(" ");
}
int C=1;
for(int i = 1; i <= line; i++) {
System.out.print(C+" ");
C = C * (line - i) / i;
}
System.out.println();
}
}
}
THANK YOU
Download