For Loop

Java Pyramid 5 Example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

126 Comments

    • import java.util.Scanner;
      class prc2
      {
      public static void main(String arg[])
      {
      Scanner a=new Scanner(System.in);
      int n=a.nextInt();
      for(int i=1;i<=n;i++)
      {
      for(int j=1;j<=i;j++)
      {
      System.out.print(” ” +j);
      }
      System.out.print(“\n”);
      }
      }

        • Hi Guys
          Need help thank you.
          1 4 9 16 25 36 49 64 81 100
          4 9 16 25 36 49 64 81 100
          9 16 25 36 49 64 81 100
          16 25 36 49 64 81 100
          25 36 49 64 84 100
          36 49 64 81 100
          49 64 81 100
          64 81 100
          81 100
          100

        • import java.util.*;
          class h
          {
          public static void main(String args[])
          {
          Scanner sc=new Scanner(System.in);
          String s=”1234″;
          int n1=s.length();
          for(int i=0;i<n1+1;i++)
          {
          for(int j=0;j<i;j++)
          {
          String word=s.substring(0,i);
          System.out.print(word);
          break;
          }
          System.out.println();
          }
          }
          }

    • public class MyClass {
      public static void main(String args[]) {
      int i,j,n=6;
      for(i=0;i<n;i++)
      {
      for(j=1;j<i;j++)
      {
      System.out.print(""+j);
      }
      System.out.println("");
      }

      }
      }

    • public class PyramidExample{
      public static void main(String args[]){

      for(int i=1;i<=4;i++){
      for(int j=1;j<=i;j++){
      System.out.print(j);

      }System.out.println();
      }
      }
      }

    • int num = 1;
      int row = 0;
      int j = 5;

      for (; j >= 1; j = j – 2) {
      for (int k = 1; k <= j; k++) {
      System.out.print(num);
      num++;
      }
      System.out.println();
      row++;
      }

      j = row;

      for (; j <= 5; j = j + 2) {
      for (int l = 1; l <= j; l++) {
      System.out.print(num);
      num++;
      }
      System.out.println();
      }

    • int num=1;
      for(i=1;i<=5;i+=2)
      {
      System.out.println();
      for(j=i;j<=5;j++)
      {
      System.out.print(num);
      num++;

      }
      }

      for(i=1;i<=2;i++)
      {System.out.println();
      for(j=2*i+1;j>=1;j–)
      {
      System.out.print(num);
      num++;
      }
      }

    • {
      Scanner input = new Scanner(System.in);

      System.out.print(“Enter the number of lines: “);
      int numberOfLines = input.nextInt();

      if ((numberOfLines < 1) || (numberOfLines > 15))
      {
      System.out.println(“You must enter a number from 1 to 15”);
      System.exit(0);
      }

      for (int row = 1; row <= numberOfLines; row++)
      {
      for (int column = 1; column <= numberOfLines – row; column++)
      {
      System.out.print(” “);
      }
      for (int num = row; num >= 1; num–)
      {
      System.out.print(” ” + num);
      }
      for (int num = 2; num <= row; num++)
      {
      System.out.print(” ” + num);
      }

      System.out.println(“”);
      }
      input.close();
      }

    • public class Helper
      {
      public static void main(String args[]){
      String space;
      for(int i = 1; i <= 5; i++){
      space = “”;
      for(int j = 1; j <=i; j++)space+=j;
      for(int k = i-1; k>0;k–)space+=k;
      System.out.println(space);
      }
      }
      }

      • package pyromid;

        public class prmdmodel6
        {
        public static void main(String args[]){

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

        for(int j = 1; j <=i; j++)
        System.out.print(j);
        for(int k = i-1; k>0;k–)
        System.out.print(k);
        System.out.println();
        }
        }
        }

        • import java.util.Scanner;
          public class JavaPyramid {

          public static void main() {
          int d;
          Scanner sc=new Scanner(System.in);
          System.out.println(“number:”);
          d=sc.nextInt();
          for(int i=d; i>0 ;i–){

          for(int j=0; j < i; j++){
          System.out.print(j+1);
          }

          System.out.println("");
          }

          }
          }

    • public class xyz
      {
      public static void main(String args[]){
      String space;
      for(int i = 0; i <= 4; i++){
      space=””;
      for(int j = 1;j<=5-i;j++){
      space+=j+”0″;
      }
      System.out.println(space);
      }
      }
      }

      • /*
        1020304050
        10203040
        102030
        1020
        10
        */

        package pyromid;

        public class prmdmodel5 {

        public static void main(String[] args) {

        for(int i=5;i>0;i–)
        {
        for(int j=1;j<=i;j++)
        {
        System.out.print(j*10);
        }
        System.out.println();
        }
        }

        }

      • 1
        2 6
        3 7 10
        4 8 11 13
        5 9 12 14 15
        in 1st row there are 4 space
        in 2nd row 3 space
        in 3rd row 2 space
        4th row 1 space
        5th row no space

        • public class Helper
          {
          public static void main(String args[]){
          String space;
          int c = 0;
          for(int i = 1; i <= 5; i++){
          space=””;
          space+=i+” “;
          c=i;
          for(int j = 1; j < i; j++){
          space+=(c+(5-j))+” “;
          c=c+(5-j);
          }
          System.out.println(space);
          }
          }
          }

            • let me write it more clearly
              space = “”;
              space += i + ” “; //not empty. It has space
              /* If you look at the Radhesham’s pattern, the first element of the nth line is n. Take a look,
              for the 1st line
              1
              -> 1st element of 1st line is 1
              next line
              2 6
              -> 1st element of 2nd line is 2.
              and so on.
              In my loop, I use the counter i in the first loop for the number of lines(I hope thats clear).
              Hope this helped!

  • Write a program to draw the following patterns:

    * * * * * * * * * * * * * * * *

    * * * * * * * * * * * * * *

    * * * * * * * * * * * *

    * * * *

    * *

    • public class Patt {
      public static void main(String[] args) {
      for (int i = 8; i>=1; i–) {
      if(i==1||i==2||i==6||i==7||i==8)
      {
      for (int j = 1; j <=(i*2); j++) {
      System.out.print(“*”);

      }

      System.out.println(“\n”);
      }
      }

      }

      }

  • Can i know the sample code of this :

    Sample Output:

    Enter a Name: Atheena

    Hello Atheena!!!

    The first letter of your name is a vowel.

    Ath

    Ath Ath

    Ath Ath Ath

    Ath Ath Ath Ath

    Ath Ath Ath Ath Ath

    Try Again? [Y/N]: yes

    Enter a Name: Raizel

    Hello Raizel!!!

    The first letter of your name is a consonant.

    Rai Rai Rai Rai Rai

    Rai Rai Rai Rai

    Rai Rai Rai

    Rai Rai

    Rai

    Try Again? [Y/N]: Yes

    • package sample;

      import java.util.Scanner;

      public class PattrenPrint {
      public static void main(String[] args) {
      Scanner in=new Scanner(System.in);
      System.out.println(“enter name …”);
      String name=in.next();
      if(name.charAt(0)==’a’||name.charAt(0)==’e’||name.charAt(0)==’i’||name.charAt(0)==’o’||name.charAt(0)==’u’){
      char[] arr=name.toCharArray();

      for (int i = 0; i < arr.length; i++) {
      for (int j = 0; j < i; j++) {
      System.out.print(” “+name.substring(0,3));
      }
      System.out.println();
      }

      }
      else{
      char[] arr=name.toCharArray();

      for (int i = arr.length; i >= 0; i–) {
      for (int j = 0; j < i; j++) {
      System.out.print(” “+name.substring(0,3));
      }
      System.out.println();
      }
      }
      }

      }

    • package sample;

      import java.util.Scanner;

      public class PattrenPrint {
      public static void main(String[] args) {
      Scanner in=new Scanner(System.in);
      System.out.println(“enter name …”);
      String name=in.next();
      System.out.println();
      System.out.println(“Hello….! “+name);

      if(name.charAt(0)==’a’||name.charAt(0)==’e’||name.charAt(0)==’i’||name.charAt(0)==’o’||name.charAt(0)==’u’||name.charAt(0)==’A’|name.charAt(0)==’E’||name.charAt(0)==’I’||name.charAt(0)==’O’||name.charAt(0)==’U’){
      char[] arr=name.toCharArray();

      for (int i = 1; i <= 5; i++) {
      for (int j = 0; j < i; j++) {
      System.out.print(” “+name.substring(0,3));
      }
      System.out.println();
      }

      }
      else{
      char[] arr=name.toCharArray();

      for (int i = 5; i >=1; i–) {
      for (int j = 0; j < i; j++) {
      System.out.print(” “+name.substring(0,3));
      }
      System.out.println();
      }
      }
      in.close();
      }

      }

    • public class Test
      {

      public static void main(String[] args)
      {

      String s=”string”;

      char c[]=s.toCharArray();
      for(int i=s.length();i>=1;i–){
      for(int c1=0;c1<i;c1++){ system.out.print(c[c1]);}}}};=””>

    • public class StringPatt {
      public static void main(String[] args) {
      String patt=”COMPUTER”;

      for (int i = patt.length()-1; i >=0; i–) {
      for (int j = 0; j <=i; j++) {
      char c=patt.charAt(j);
      System.out.print(c);

      }
      System.out.println();

      }
      }

      }

    • /*
      computer
      compute
      comput
      compu
      comp
      com
      co
      c
      */

      package pyromid;

      import java.util.Scanner;

      public class prmdmodel3 {

      public static void main(String[] args) {
      Scanner sc=new Scanner(System.in);
      System.out.println(“enter string”);
      String st=sc.next();
      char ch[]=st.toCharArray();

      for(int i=ch.length;i>=0;i–)
      {
      for(int j=0;j<i;j++) {=”” system.out.print(ch[j]);=”” }=”” system.out.println();=”” }=”” }=”” }=””>

    • class AA
      {
      public static void main(String[] args)
      {
      for(int i=2;i<7;i++){
      for(int j=0;j<i;j++){ system.out.print(“*”);=”” }=”” system.out.println(“”);}}}=””>

    • class Nested_For

      {

      public static void main(String args[])

      {

      for(int i=9;i>=1;i–)

      {

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

      {

      System.out.print(i+” “);

      }

      System.out.println(” “);

      }

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

      {

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

      {

      System.out.print(i+” “);

      }

      System.out.println(“”);

      }

      }

      }

    • public class pyromid {

      public static void main(String[] args) {

      for(int i=9;i>0;i–)

      {

      for(int j=0;j<i;j++) {=”” system.out.print(i);=”” }=”” system.out.println();=”” }=”” for(int=”” i=”1;i&lt;=9;i++)” {=”” for(int=”” j=”0;j&lt;i;j++)” {=”” system.out.print(i);=”” }=”” system.out.println();=”” }=”” }=”” }=””>

    • public class MyPatt {
      public static void main(String[] args) {
      for (int i = 9; i >=1; i–) {
      for (int j = 1; j <=i; j++) {
      System.out.print(i);
      }
      System.out.println();
      }
      for (int i = 1; i <=9; i++) {
      for (int j = 1; j <=i; j++) {
      System.out.print(i);
      }
      System.out.println();
      }

      }
      }

    • class Down
      {
      public static void main(String args[])
      {
      for(int i=9;i>=1;i–)
      {
      System.out.println();
      for(int j=1;j<=i;j++)
      {
      int k=i;
      System.out.print(k);
      }
      }
      for(int i=1;i<=9;i++)
      {
      System.out.println();
      for(int j=1;j<=i;j++)
      {
      int h=i;
      System.out.print(h);
      }
      }
      }
      }

    • import java.util.Scanner;
      public class Pyramid2 {

      public static void main(String args[])
      {
      Scanner s=new Scanner(System.in);
      System.out.println(“Enter number of rows: “);
      int rows=s.nextInt();

      for(int i=rows;i>=0;i–)
      {
      for (int j=1;j<=i;j++)
      {
      System.out.print(j);
      }
      System.out.print(“\n”);
      }

      for(int i=1;i<=rows;i++)
      {
      for(int j=1;j<=i;j++)
      {
      System.out.print(j);
      }
      System.out.println();

      }
      }
      }

    • package pyromid;

      public class pyromidmodel8 {

      public static void main(String[] args) {
      // TODO Auto-generated method stub.

      for(int i=0;i<=5;i++)
      {
      for(int j=0;j<=i;j++)
      {
      System.out.print(” “+j*i);
      }
      System.out.println();
      }

      }

      }

    • package New_JT;

      public class Pyramid1
      {

      public static void main(String[] args)
      {
      for (int j = 5; j >= 0; j–)
      {
      System.out.println(” “);

      for (int i=1; i<=j; i++)
      {
      System.out.print(i);

      }

      System.out.print(” “);
      for (int i=j; i>0; i–)
      {
      System.out.print(i);
      }

      }
      for (int i = 2; i <= 5; i++)
      {

      for (int k=1; k<=i;k++)
      {
      System.out.print(k);
      }
      System.out.print(” “);
      for (int m=i;m>0;m–)
      {

      System.out.print(m);
      }
      System.out.println();

      }

      }

      }

    • public class Pyramid {
      public static void main(String[] args) {
      for (int i = 5; i >=1; i–) {
      for (int j = 1; j <=i; j++) {
      System.out.print(j);

      }
      System.out.print(” “);
      for (int j1 = i; j1 >=1; j1–) {
      System.out.print(j1);

      }
      System.out.println();

      }
      for (int i = 2; i <=5; i++) {
      for (int j = 1; j <=i; j++) {
      System.out.print(j);

      }
      System.out.print(” “);
      for (int j1 = i; j1 >=1; j1–) {
      System.out.print(j1);

      }
      System.out.println(” “);

      }
      }

      }

  • what will be the code for this?

    12345 54321

    1234 4321

    123 321

    12 21

    1 1

    12 21

    123 321

    1234 4321

    12345 54321

    • int h=0;
      for(int i=5; i>0 ;i–){

      for(int j=1; j <= i; j++){
      System.out.print(j);
      h=j;
      }
      System.out.print(” “);
      for (int j = 1; j<=i; j++) {
      System.out.print(h–);
      }
      System.out.println(“”);
      }
      for(int i=2; i<= 5 ;i++){

      for(int j=1; j <= i; j++){
      System.out.print(j);
      h=j;
      }
      System.out.print(” “);
      for (int j = 1; j<=i; j++) {
      System.out.print(h–);
      }

      System.out.println(“”);
      }

    • public class MyPatt {
      public static void main(String[] args) {
      for (int i = 1; i<=5; i++) {
      for (int j = 5; j>=i; j–) {
      System.out.print(j);
      }
      System.out.println();
      }
      for (int i = 1; i <=5; i++) {
      for (int j = 1; j <=i; j++) {
      System.out.print(“*”);
      }
      System.out.println();
      }

      }
      }

    • code–
      class print_star
      {
      public static void main(String args[])
      {
      int i,j;
      for(i=1; i<=4; i++)
      {
      System.out.println();

      {for(j=1; j<=i; j++)
      System.out.print(“*”);
      }
      }
      }

      }

    • public class Loop
      {

      public static void main(String args [])
      {

      int ctr=0;
      int ctr1=0;
      int n=0;
      Scanner scan=new Scanner (System.in);
      System.out.println (“Enter number:”);
      n=scan.nextInt();

      for (ctr1=1;ctr1<=n;ctr1++)
      {
      for (ctr=1;ctr<=ctr1;ctr++)
      {
      System.out.print (“*”);
      }
      System.out.println ();
      }

      }

      }

    • public class numberformat1 {

      public static void main(String[] args) {

      int row, column;
      // Total number of rows it will iterate
      for (row = 1; row <= 5; row++) {
      // total number of columns it will iterate
      for (column = 1; column <= row; column++) {
      System.out.print(“*” + ” “);
      }
      System.out.println(“”);
      }
      } // end method main
      } // end class
      hjj

    • /** write a progarm to print following pattern.

      *
      **
      ***
      ****
      *****
      */
      class PatternDemo2
      {
      public static void main(String args[])
      {

      for(int i=1;i<=5;i++)
      {
      for(int j=1;j<=5;j++)
      {
      if(j<=i)
      {
      System.out.printf(“*”);

      }
      }
      System.out.println();
      }
      }
      }

    • Console.WriteLine(“input a rows”);
      int rows=Convert.Toint32(Console.ReadLine());
      For(int i=1;i<=rows;i++)
      {
      for(int j=1;j<=i;j++)
      {
      Console.WriteLine(“*”);
      }
      }

  • output:
    The pyramid is –
    1
    232
    45654
    7890987
    123454321
    67890109876
    2345678765432
    901234565432109
    78901234543210987
    6789012345432109876

  • public class numberformat1 {

    public static void main(String[] args) {

    int row, column;
    // Total number of rows it will iterate
    for (row = 1; row <= 5; row++) {
    // total number of columns it will iterate
    for (column = 1; column <= row; column++) {
    System.out.print(“*” + ” “);
    }
    System.out.println(“”);
    }
    } // end method main
    } // end class

    • You can try this -:

      public static void main(String[] args) {
      String s1=”JAVA”;
      char[] array=s1.toCharArray();
      for(int i=0;i<=array.length-1;i++){

      for (int j=0;j<=i;j++){
      System.out.print(array[j]);
      }
      System.out.println(“”);

      }
      }

    • class pattern
      {
      public static void main()
      {
      int i,j;
      for(i=1;i<=4;i++)
      {
      for(j=1;j<=i;j++)
      {
      System.out.print(” * “);
      }
      System.out.println();
      }
      }
      }

    • class Pattern{
      public static void main(int n)
      {
      int i,j,p,k=1;
      for(i=1;i<=n;i++){
      if(i%2==0){
      for(j=1;j<=i;j++){
      k++;
      }
      p=k-1;
      for(j=1;j<=i;j++){
      System.out.print(p+”\t”);
      p–;
      }
      }
      else
      {
      for(j=1;j<=i;j++){
      System.out.print(k+”\t”);
      k++;
      }
      }
      System.out.println();
      }

      for(i=n-1;i>=1;i–){
      if(i%2==0){
      for(j=1;j<=i;j++){
      k++;
      }
      p=k-1;
      for(j=1;j<=i;j++){
      System.out.print(p+”\t”);
      p–;
      }
      }
      else
      {
      for(j=1;j<=i;j++){
      System.out.print(k+”\t”);
      k++;
      }
      }
      System.out.println();
      }
      }
      }

    • import java.util.*;
      class SamplePattern
      {
      public static void main(String[] arg)
      {
      int n,m=0,p,j,k;
      System.out.println(“enter no.”);
      Scanner sc=new Scanner(System.in);
      n=sc.nextInt();
      n=(n/2)-1;
      for(int i=1;i<=n;i++)
      {

      if(i<=((n/2)+1))
      {
      m=m+i;
      p=m;
      for(j=1;j<=i;j++)
      {
      System.out.print(p+” “);
      p–;
      }//for j
      }//if
      else
      {
      k=n+1;
      m=m+(k-i);
      p=m;
      for(j=1;j<=k-i;j++)
      {
      System.out.print(p+” “);
      p–;
      }//for j
      }//else
      System.out.println();
      }//for i
      }//main

      }//class

  • enter number of raws: 5
    ””””’1
    ””””212
    ”””32123
    ”’4321234
    543212345

    note: the ( ‘ ) are space, space didnt show in the message

    • for(int i=1; i<= 4 ;i++){

      for(int j=1; j <= i; j++){

      if(i!=j)
      System.out.print(j+”a”);
      else
      System.out.print(j);
      }

      System.out.println(“”);
      }

  • Add the alphabet in the given String in Java
    Eg: String str = “ABCDEF”

    Output : AOBOCODOEOF

    Please update the code fast.

  • What is the code for
    1 1 1 1 1
    2 2 2 2 2
    3 3 3 3 3
    4 4 4 4 4
    5 5 5 5 5
    I need the answer as soon as possible
    Thankyou

    • public class patternPlus {

      public static void main(String[] args) {
      int i, j;
      for (i = 1; i <= 4; i++) {
      for (j = 1; j < (i * 2); j++) {
      System.out.print("+");

      }
      System.out.println(" ");
      }
      }
      }

  • Hi,
    How to write a program for frequency(sot by frequency,not by numeric value)
    input:
    60
    65
    50
    65
    70
    70
    65
    60
    65
    70
    output:
    65 4
    70 3
    60 2
    50 1

  • Hi,
    reads a date in yyyy-mm-dd and prints it in words(use appropriate day suffix “st” ,”nd”, “rd”,”th”)
    input:

    2017-03-04
    output:
    4th March 2017

  • Hi,
    print number of character,words and lines.
    input:
    essay.txt
    output:
    number of character:1365
    number of words:93
    nuber of lines:44

    • public class fileReading
      {
      public static void main(String[] args) throws IOException
      {
      File file = new File(“Your File path on local disk”);
      FileInputStream fileStream = new FileInputStream(file);
      InputStreamReader ip = new InputStreamReader(fileStream);
      BufferedReader reader = new BufferedReader(ip);

      String line;

      // Initializing counters
      int countWord = 0;
      int sentenceCount = 0;
      int characterCount = 0;
      int paragraphCount = 1;

      while((line = reader.readLine()) != null)
      {
      if(line.equals(“”))
      {
      paragraphCount++;
      }
      if(!(line.equals(“”)))
      {

      characterCount += line.length();

      String[] wordList = line.split(“\\s+”);

      countWord += wordList.length;

      String[] sentenceList = line.split(“[!?.:]+”);

      sentenceCount += sentenceList.length;
      }
      }

      System.out.println(“Total word count = ” + countWord);
      System.out.println(“Total number of sentences = ” + sentenceCount);
      System.out.println(“Total number of characters = ” + characterCount);
      System.out.println(“Number of paragraphs = ” + paragraphCount);
      }
      }

  • How do you do this?

    Create a Java class that takes 1 number as input, then programmatically outputs an inverted triangle, with the input as the starting number.

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

    pls answer how to do this
    I know im 4 years late
    but im having hope

  • I need some help pls

    a.) while.html
    b.) do-while.html
    c.) for.html

    2. Implement a loop that will produce this output:

    999999999
    88888888
    7777777
    666666
    55555
    4444
    333
    22
    1

  • 543212345
    4321 1234
    321 123
    21 12
    1 1

    I need a code for this please help
    Remember NOT for this
    5432112345
    4321 1234
    321 123
    21 12
    1 1

Sponsors

Facebook Fans