AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Java code for the following pattern

Write the code to print the following pattern.

1                           1 
1 2                       2 1 
1 2 3                   3 2 1 
1 2 3 4               4 3 2 1 
1 2 3 4 5           5 4 3 2 1 
1 2 3 4 5 6       6 5 4 3 2 1 
1 2 3 4 5 6 7   7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 

corejava x 336
programming x 153
Posted On : 2017-12-19 12:16:00.0
profile Shaik Abdulgani - anyforum.in Shaik Abdulgani
600
up-rate
6
down-rate

Answers


public class NumberPattern {
private static int max=8;

public static void main(String[] args) {
for(int i=1;i<=max;i++){
/*
*Since there was a change in sequence in last line. 8 is coming only once, So added the condition. We can Remove it if no sequence change in all steps.*
*/
System.out.println(printNumbers(i)+printSpace(i)+((max==i)?" "+printReverseNumbers(i-1):printReverseNumbers(i)));
}
}

public static String printNumbers(int i){
StringBuilder sb=new StringBuilder();
int j=1;
while(j<=i){
sb.append(j+" ");
j++;
}
return sb.toString().trim();
}

public static String printReverseNumbers(int i){
StringBuilder sb=new StringBuilder();
int j=i;
while(j>=1){
sb.append(j+" ");
j--;
}
return sb.toString();
}

public static String printSpace(int i){
StringBuilder sb=new StringBuilder();
int j=0;
while(j<(4*(max-i)-1)){
sb.append(" ");
j++;
}
return sb.toString();
}
}

Posted On : 2017-12-19 22:57:04
Satisfied : 0 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596128622628
Reply This Thread
up-rate
5
down-rate

Let me know if these codes are satisfied or not.


Pattern 1 :
----------------------------------
1                           1 
1 2                       2 1 
1 2 3                   3 2 1 
1 2 3 4               4 3 2 1 
1 2 3 4 5           5 4 3 2 1 
1 2 3 4 5 6       6 5 4 3 2 1 
1 2 3 4 5 6 7   7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 


import java.util.Scanner;
class Pattern2
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("::Enter Number to Dispaly the Pattern::");
int N=s.nextInt();
System.out.println("::Pattern for "+N+" is::");
for(int i=1;i<=N;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(" "+j);
}
for(int k=0;k<(4*(N-i)-1);k++)
{
System.out.print(" ");
}
for(int j=i;j>=1;j--)
{
if(j==N)
{
System.out.print(" ");
}
else
{
System.out.print(j+" ");
}
}
System.out.println("");
}
}
}



Pattern 2:
-------------------------------
1 1
1 2 2 1
1 2 3 3 2 1
1 2 3 4 4 3 2 1
1 2 3 4 5 5 4 3 2 1
1 2 3 4 5 6 6 5 4 3 2 1
1 2 3 4 5 6 7 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1

import java.util.Scanner;
class Pattern1
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("::Enter Number to Dispaly the Pattern::");
int N=s.nextInt();
System.out.println("::Pattern for "+N+" is::");
for(int i=1;i<=N;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(j+" ");
}
for(int k=0;k<N-i;k++)
{
System.out.print("");
}
for(int j=i;j>=1;j--)
{
if(j==N)
{
System.out.print("");
}
else
{
System.out.print(j+" ");
}
}
System.out.println("");
}
}
}

Posted On : 2017-12-20 00:04:26
Satisfied : 1 Yes  0 No
profile sudheerkumar Javvadi - anyforum.in sudheerkumar Javvadi
0970
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in