Sie sind auf Seite 1von 13

PRINT PATTERN 3 45 678 9101112

For any input number N Print the following code – For below code N=4

3
45
678
9101112

PREREQUISITE:

Basic knowledge in Java programming, usage of loops.

ALGORITHM:

1. Take input from user i.e number of lines required (N value).


2. Take two loops one for each line (say ‘i’) and other for each digit in a particular line
(say ‘j’). i starts from 1 and j starts from 1.
3. Take a result variable (say ‘a’) and initialize it with input value taken from user.
4. Here ‘i’ loop is used to access each line from 1 to n and ‘j’ loop is used to print values
in each line. j loop is executed until it reaches i value.
5. Print ‘a’ value and post increment until the j loop reaches a value equal to i.
6. go to next line.
7. Repeat the ‘i’ loop until it reaches n.

CODE IN JAVA:

1
2
3 import java.lang.*;
4 import java.io.*;
class Pattern
5 {
6 public static void main(String[] args)throws IOException
7 {
8 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,i,j,a,t;
9 System.out.print("Enter N value:");
10n=Integer.parseInt(br.readLine());
11System.out.print("Enter initial Value:");
12a=Integer.parseInt(br.readLine());
13for(i=1;i<=n;i++)
{
14for(j=1;j<=i;j++)
15{
16System.out.print(a++);
17}
18System.out.println();
}
19}
20}
21
22
TAKING INPUT:

DISPLAYING THE OUTPUT:

PRINT PATTERN 3 44 555 6666


For any input number N Print the following code – For below code N=4

3
44
555
6666
555
44
3

PREREQUISITE:

Basic knowledge in Java programming, usage of loops.

ALGORITHM:

1. Take input from user i.e number of lines required (N value) and take initial value a.
2. Take two loops one for each line (say ‘i’) and other for each digit in a particular line
(say ‘j’). i starts from 1 and j starts from 1.
3. Here ‘i’ loop is used to access each line from 1 to n and ‘j’ loop is used to print values
in each line. j loop is executed until it reaches i value.
4. Print ‘a’ value until the j loop reaches i value and increment a value got to next line.
5. Decrement a value by 2 and repeat the 2,3 steps and in the 4th step instead of
increment now for decrement.
6. Repeat the ‘i’ loop until it reaches 1.

CODE IN JAVA:
import java.lang.*;
import java.io.*;
class Pattern
{
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,i,j,a;
System.out.print("Enter starting value:");
a=Integer.parseInt(br.readLine());
System.out.print("Enter N value:");
n=Integer.parseInt(br.readLine());
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(a);
}
System.out.println();
a++;
}
a=a-2;
for(i=(n-1);i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(a);
}
System.out.println();
a--;
}
}
}

TAKING INPUT:
DISPLAYING THE OUTPUT:

PRINT PATTERN 3 45 678 9101112 678


For any input number N Print the following code – For below code N=4

3
45
678
9101112
678
45
3

PREREQUISITE:

Basic knowledge in Java programming, usage of loops.

ALGORITHM:

1. Take input from user i.e number of lines required (N value) and take initial value a.
2. Take two loops one for each line (say ‘i’) and other for each digit in a particular line
(say ‘j’). i starts from 1 and j starts from 1.
3. Here ‘i’ loop is used to access each line from 1 to n and ‘j’ loop is used to print values
in each line. j loop is executed until it reaches i value.
4. The Pattern is divided in to tow parts increment and decrement part.
5. In increment part print ‘a’ value post increment it until the j loop reaches i and go to
next line.
6. Before implementing decrement part, assign ‘a’ value to a-((n*2)-1 and implement
decrement loop from n-1 to 1
7. Before going to next line assign ‘a’ value to a-((i*2)-1 and go to next line.
8. Repeat the ‘i’ loop until it reaches 1.

CODE IN JAVA:
1
2
3
4 import java.lang.*;
import java.io.*;
5 class Pattern
6 {
7 public static void main(String[] args)throws IOException
8 {
9 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,i,j,a,t=0;
10System.out.print("Enter starting value:");
11a=Integer.parseInt(br.readLine());
12System.out.print("Enter N value:");
13n=Integer.parseInt(br.readLine());
for(i=1;i<=n;i++)
14{
15for(j=1;j<=i;j++)
16{
17System.out.print(a++);
18}
System.out.println();
19}
20a=a-((n*2)-1);
21for(i=(n-1);i>=1;i--)
22{
23for(j=1;j<=i;j++)
{
24System.out.print(a++);
25}
26System.out.println();
27a=a-((i*2)-1);
}
28}
29}
30
31
32

TAKING INPUT:

DISPLAYING THE OUTPUT:


PRINT PATTERN 3 45 678 9101112 678
For any input number N Print the following code – For below code N=4

3
45
678
9101112
678
45
3

PREREQUISITE:

Basic knowledge in Java programming, usage of loops.

ALGORITHM:

1. Take input from user i.e number of lines required (N value) and take initial value a.
2. Take two loops one for each line (say ‘i’) and other for each digit in a particular line
(say ‘j’). i starts from 1 and j starts from 1.
3. Here ‘i’ loop is used to access each line from 1 to n and ‘j’ loop is used to print values
in each line. j loop is executed until it reaches i value.
4. The Pattern is divided in to tow parts increment and decrement part.
5. In increment part print ‘a’ value post decrement it until the j loop reaches i and go to
next line by assigning ‘a’ value to a-((i*2)+1).
6. Before implementing decrement part, assign ‘a’ value to a-((n*2)-1 and implement
decrement loop from n-1 to 1
7. Go to next line.
8. Repeat the ‘i’ loop until it reaches 1.

CODE IN JAVA:
import java.lang.*;
import java.io.*;
class Pattern
{
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,i,j,a;
System.out.print("Enter starting value:");
a=Integer.parseInt(br.readLine());
System.out.print("Enter N value:");
n=Integer.parseInt(br.readLine());
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(a--);
}
a=a+((i*2)+1);
System.out.println();
}
a=a-((n*2)+1);
for(i=(n-1);i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(a--);
}
System.out.println();
}
}
}

TAKING INPUT:

DISPLAYING THE OUTPUT:


PRINT PATTERN 2 33 444 5555 5555 444
For any input number N Print the following code – For below code N=4

2
33
444
5555
5555
444
33
2

PREREQUISITE:

Basic knowledge in Java programming, usage of loops.

ALGORITHM:

1. Take input from user i.e number of lines required (N value) and take initial value a.
2. Take two loops one for each line (say ‘i’) and other for each digit in a particular line
(say ‘j’). i starts from 1 and j starts from 1.
3. Here ‘i’ loop is used to access each line from 1 to n and ‘j’ loop is used to print values
in each line. j loop is executed until it reaches i value.
4. Print ‘a’ value until the j loop reaches i value and increment a value got to next line.
5. Decrement a value by 1 and repeat the 2,3 steps and in the 4th step instead of
increment now for decrement.
6. Repeat the ‘i’ loop until it reaches 1.

CODE IN JAVA:

1 import java.lang.*;
2 import java.io.*;
3 class Pattern
{
4 public static void main(String[] args)throws IOException
5 {
6 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
7 int n,i,j,a;
System.out.print("Enter starting value:");
8 a=Integer.parseInt(br.readLine());
9 System.out.print("Enter N value:");
10n=Integer.parseInt(br.readLine());
11a=a-1;
12for(i=1;i<=n;i++)
{
13for(j=1;j<=i;j++)
14{
15System.out.print(a);
16}
17System.out.println();
a++;
18}
19a=a-1;
20for(i=n;i>=1;i--)
21{
22for(j=1;j<=i;j++)
{
23System.out.print(a);
24}
25System.out.println();
26a--;
27}}
28}
29
30
31
32
33
34

TAKING INPUT:

DISPLAYING THE OUTPUT:

PRINT PATTERN 2 34 567 891011 891011


For any input number N Print the following code – For below code N=4

2
34
567
891011
891011
567
34
2

PREREQUISITE:

Basic knowledge in Java programming, usage of loops.

ALGORITHM:

1. Take input from user i.e number of lines required (N value) and take initial value a.
2. Take two loops one for each line (say ‘i’) and other for each digit in a particular line
(say ‘j’). i starts from 1 and j starts from 1.
3. Here ‘i’ loop is used to access each line from 1 to n and ‘j’ loop is used to print values
in each line. j loop is executed until it reaches i value.
4. Print ‘a’ value and post increment it until the j loop reaches i value and increment a
value got to next line.
5. Decrement a value by n and repeat the 2,3 steps where i loop from n to 1 and in the
4th step before going to next line assign ‘a’ value to a-((i*2)-1.
6. Repeat the ‘i’ loop until it reaches 1.

CODE IN JAVA:

1 import java.lang.*;
2 import java.io.*;
class Pattern
3 {
4 public static void main(String[] args)throws IOException
5 {
6 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,i,j,a;
7 System.out.print("Enter starting value:");
8 a=Integer.parseInt(br.readLine());
9 System.out.print("Enter N value:");
10n=Integer.parseInt(br.readLine());
11a=a-1;
for(i=1;i<=n;i++)
12{
13for(j=1;j<=i;j++)
14{
15System.out.print(a++);
16}System.out.println();
17}
18a=a-n;
19for(i=n;i>=1;i--)
20{
for(j=1;j<=i;j++)
21{
22System.out.print(a++);
23}
24System.out.println();
25a=a-((i*2)-1);
}
26}
27}
28
29
30
31
32
33

TAKING INPUT:

DISPLAYING THE OUTPUT:

PRINT PATTERN 3 54 876 1211109 876


For any input number N Print the following code – For below code N=4

3
54
876
1211109
876
54
3

PREREQUISITE:
Basic knowledge in Java programming, usage of loops.

ALGORITHM:

1. Take input from user i.e number of lines required (N value) and take initial value a
and decrement it by 1.
2. Take two loops one for each line (say ‘i’) and other for each digit in a particular line
(say ‘j’). i starts from 1 and j starts from 1.
3. Here ‘i’ loop is used to access each line from 1 to n and ‘j’ loop is used to print values
in each line. j loop is executed until it reaches i value.
4. The Pattern is divided in to tow parts increment and decrement part.
5. In increment part print ‘a’ value post decrement it until the j loop reaches i and go to
next line by assigning ‘a’ value to a-((i*2)+1). Copy the value of a in every loop to t.
6. Before implementing decrement part, assign ‘a’ value to t+n and implement
decrement loop from n to 1 and post decrement a.
7. Go to next line.
8. Repeat the ‘i’ loop until it reaches 1.

CODE IN JAVA:
import java.lang.*;
import java.io.*;
class Pattern
{
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,i,j,a,t=0;
System.out.print("Enter starting value:");
a=Integer.parseInt(br.readLine());
System.out.print("Enter N value:");
n=Integer.parseInt(br.readLine());
a=a-1;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(a--);
t=a;
}
System.out.println();
a=a+((i*2)+1);
}
a=t+n;
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(a--);
}
System.out.println();
}
}
}
TAKING INPUT:

DISPLAYING THE OUTPUT:

Das könnte Ihnen auch gefallen