Friday, March 4, 2016

JAVA PROGRAM(ALPHABET PATTERN)

 JAVA  PROGRAM(ALPHABET PATTERN)
A
AB
ABC
ABCD


 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication23;

/**
 *
 * @author Prakash
 */

public class JavaApplication23 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
     
  for(int i=1;i<5;i++)
        {
            int a=65;                          //ASCII value of A
            for(int j=1;j<=i;j++)
              {
                System.out.print((char)a);
                a++;
              }
          System.out.println();
        }
    }   
}

2.   A
    ABA
   ABCBA
  ABCDCBA
 ABCDEDCBA
ABCDEFEDCBA
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication23;

/**
 *
 * @author Prakash
 */
//enum week{
 //   sun,mon,tue,wed,thurs,fri,sat
//};
import java.util.*;  // import java util 
public class JavaApplication23 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
  Scanner scr=new Scanner(System.in); // create scanner obj
int n;
System.out.println("Enter the number of rows. ");
n=scr.nextInt();  // accept a number


char c;
for(int i=1;i<=n;++i)        // FOR LOOP FOR NUMBER OF ROWS
{
c='A';
for(int j=i;j<n;++j)        // FOR LOOP FOR PRINTING SPACES
{
System.out.print(" ");
}
for(int k=1;k<=i;++k)      // FOR LOOP FOR PRINTING ALPHABETS IN DESCENDING ORDER  
{
System.out.print(c);
++c;
}
c-=2;
for(int l=1;l<i;++l)      // FOR LOOP FOR PRINTING ALPHABETS IN ASCENDING ORDER
{
System.out.print(c);
--c;
}
System.out.println();    // INTRODUCING NEW LINE

}

    }   

}


output
Enter the number of rows. 
6
     A
    ABA
   ABCBA
  ABCDCBA
 ABCDEDCBA
ABCDEFEDCBA



      A
    BAB
   CBABC
  DCBABCD
 EDCBABCDE
FEDCBABCDEF


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication23;

/**
 *
 * @author Prakash
 */
//enum week{
 //   sun,mon,tue,wed,thurs,fri,sat
//};
import java.util.*;  // import java util 
public class JavaApplication23 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
  Scanner scr=new Scanner(System.in);
int n;
System.out.println("Enter the number of rows. ");
n=scr.nextInt();

char ch='A';
char c;
for(int i=1;i<=n;++i)        // FOR LOOP FOR NUMBER OF ROWS
{
c=ch;
for(int j=i;j<n;++j)        // FOR LOOP FOR PRINTING SPACES
{
System.out.print(" ");
}
for(int k=1;k<=i;++k)      // FOR LOOP FOR PRINTING ALPHABETS IN DESCENDING ORDER  
{
System.out.print(c);
--c;
}
c+=2;
for(int l=1;l<i;++l)      // FOR LOOP FOR PRINTING ALPHABETS IN ASCENDING ORDER
{
System.out.print(c);
++c;
}
System.out.println();    // INTRODUCING NEW LINE
++ch;                   //  INCREMENTING VALUE OF CH FOR NEXT ITERATION
}

} // end of main method
} // end of main class

/* OUTPUT

    }   
}


run:
Enter the number of rows. 
6
     A
    BAB
   CBABC
  DCBABCD
 EDCBABCDE
FEDCBABCDEF
BUILD SUCCESSFUL (total time: 4 seconds)


18 comments:

  1. how to print like that
    A B C D E
    B C D E A
    C D E A B
    D E A B C
    E A B C D

    ReplyDelete
    Replies
    1. hey, here is code:

      package javaapplication121;



      public class JavaApplication121 {

      /**
      * @gurmeet
      */
      public static void main(String[] args) {
      char alpha[]={'A','B','C','D','E'};
      for(int i=0;i<5;i++)
      {
      for(char a=alpha[i];a<='E';a++)
      {
      System.out.print(a+" ");
      }
      if(i>0)
      {
      for(int j=0;j<i;j++)

      System.out.print(alpha[j]+" ");

      }
      System.out.println();

      }
      }

      }

      Delete
  2. How to print this..
    ABCDEFGFEDCBA
    ABCDEF FEDCBA
    ABCDE EDCBA
    A A

    ReplyDelete
  3. How to print like this
    Great
    reat
    eat
    at
    t

    ReplyDelete
  4. how to write
    a
    bc
    cde
    defg
    efghi
    fghijk by using user input.Please help me.

    ReplyDelete
    Replies
    1. nice PROB since o/p depends on user input
      /*
      Gurmeet Coder
      in this USER INPUT is no. of lines to be printed.
      if any problem, please reply
      */
      import java.util.*;
      public class JavaApplication13 {
      public static void main(String[] args) {
      Scanner sc=new Scanner(System.in);
      System.out.println("Enter the no. of lines:");
      int n,i,j,c=96;
      n=sc.nextInt();
      for(i=1;i<=n;i++)
      {
      if(i>26)
      {
      c=(i%26)+97;
      }
      else
      {
      c+=i;
      }
      for(j=1;j<=i;j++)
      {
      System.out.print((char)c);
      if(c==122)
      {
      c=96;
      }
      ++c;
      }
      System.out.println();
      c=96;
      }
      }

      }

      Delete
  5. Replies
    1. /*
      Gurmeet
      */
      public class JavaApplication11 {

      public static void main(String[] args) {
      int k=3,j,i,h=1;char c1='a',c2='b';
      for(i=1;i<=2;i++)
      {
      for(j=h;j<=k;j++)
      {
      if(j%2!=0)
      {
      System.out.print(c1);
      }
      else
      {
      System.out.print(c2);
      }
      }
      System.out.println(); k=2;h=2;
      }
      }

      }

      Delete
  6. how to write codding of
    abcba
    bcb
    b

    ReplyDelete
  7. how can i print
    A
    aa
    BBB
    bbbb
    CCCCC
    cccccc

    ReplyDelete
    Replies
    1. /*
      Gurmeet Coder
      */
      public class JavaApplication11 {

      public static void main(String[] args) {
      int i,j,k,capital=65,small=97;
      for(i=1;i<=6;i++)
      {
      if(i%2!=0)
      {
      for(k=1;k<=i;k++)
      {
      System.out.print((char)capital);
      }
      ++capital;
      }
      else
      {
      for(k=1;k<=i;k++)
      {
      System.out.print((char)small);
      }
      ++small;
      }
      System.out.println();
      }
      }
      }

      Delete
  8. How to. Print. The code
    A
    BC
    DEA
    BCDE
    ABCDE

    ReplyDelete
    Replies
    1. /*
      Gurmeet Coder
      */
      public class JavaApplication11 {

      public static void main(String[] args) {
      char alphabet[]={'A','B','C','D','E'};
      int i,j,k=0;

      for(i=1;i<=5;i++)
      {
      for(j=1;j<=i;j++)
      {
      System.out.print(alphabet[k]);++k;
      if(k==5)
      {
      k=0;
      }
      }
      System.out.println();

      }
      }
      }

      Delete
  9. abcba
    bcb
    c pattern program with dynamic input enter 1 only a, enter 2 ABA B and 3 abcba bcb c
    Please give me the solution

    ReplyDelete