For Loop

Java Pyramid 1 Example

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

32 Comments

    • Please show me this java code:-

      1
      1 2 1
      1 2 4 2 1
      1 2 4 8 4 2 1
      1 2 4 8 16 8 4 2 1
      1 2 4 8 16 32 16 8 4 2 1
      1 2 4 8 16 32 64 32 16 8 4 2 1
      1 2 4 8 16 32 64 128 64 32 16 8 4 2 1

      • public class GeneratePrimeNumbersExample {
        public static void main(String[] args) {
        int k = 0;
        int temp = 0;
        for(int i=1; i<=5; i++)
        {

        for(int j=1; j<=(2*i)-1; j++)
        {
        if(j==1)
        {
        k=i;
        temp=1;
        }
        if(j<k) {=”” system.out.print(temp+”=”” “);=”” temp=”temp*2;” }=”” else=”” if(j=”=k)” {=”” system.out.print(temp+”=”” “);=”” }=”” else=”” {=”” temp=”temp/2;” system.out.print(temp+”=”” “);=”” }=”” }=”” system.out.println(“=”” “);=”” for=”” next=”” line=”” (line=”” change)=”” }=”” }=”” }=””>

      • public static void value_times_two(int n) {
        int s, p;
        s = 1;
        p = 1;

        for (int i = 1; i <= n; i++) {
        p = 1;
        System.out.print(p);
        while (s <= i) {
        System.out.print(p *= 2);

        s++;
        }
        while (s > 1) {
        System.out.print(p /= 2);
        –s;
        }
        System.out.println();
        }
        }

      • import java.lang.Math;
        class shape
        {

        public static void main(String a[])
        {
        int rows=8,i,j,half,t=1,ttemp;
        double ptemp=0,power=0,temp;
        for(i=1;i<=rows;i++)
        {
        half=t/2;
        power=0;
        for(j=1;j<=t;j++)
        {
        if(half>=power)
        {
        temp=Math.pow(2.0,power);
        ttemp=(int)temp;
        System.out.print(” ” + ttemp);
        ptemp=power;
        power++;
        }
        else
        {
        ptemp–;
        temp=Math.pow(2,ptemp);
        ttemp=(int)temp;
        System.out.print(” ” + ttemp);
        }
        }
        t=t+2;
        System.out.println();
        }

        }
        }

      • /*
        1
        1 2 1
        1 2 4 2 1
        1 2 4 8 4 2 1
        1 2 4 8 16 8 4 2 1
        1 2 4 8 16 32 16 8 4 2 1
        1 2 4 8 16 32 64 32 16 8 4 2 1
        1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
        */

        public class SurePyramid {

        public static void main(String[] args) {
        int k =0;
        int t=0;
        for(int i=1; i<= 7 ;i+=2){
        t=(int)Math.round(i/2);
        for(int j=0; j < i; j++){

        if(j<=t)
        k =(int)Math.pow(2,j);
        else
        k =(int)Math.pow(2,(–t));

        System.out.print(k+””);
        }

        //generate a new line
        System.out.println(“”);
        }
        }
        }

    • please show me coding for

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

      • for(int i=1; i<=6; i++){
        for(int j=6; j>=1; j–){
        System.out.print(“**”);
        }
        System.out,println();
        }
        for(int k=1; k<=5; k++){
        for(int l=0; l<=k; l++>){
        System.out.print(“**”);
        }
        System.out,println();
        }

  • /*
    1
    1 2 1
    1 2 4 2 1
    1 2 4 8 4 2 1
    1 2 4 8 16 8 4 2 1
    1 2 4 8 16 32 16 8 4 2 1
    1 2 4 8 16 32 64 32 16 8 4 2 1
    1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
    */
    import java.lang.Math;
    class shape
    {

    public static void main(String a[])
    {
    int rows=8,i,j,half,t=1,ttemp;
    double ptemp=0,power=0,temp;
    for(i=1;i<=rows;i++)
    {
    half=t/2;
    power=0;
    for(j=1;j<=t;j++)
    {
    if(half>=power)
    {
    temp=Math.pow(2.0,power);
    ttemp=(int)temp;
    System.out.print(” ” + ttemp);
    ptemp=power;
    power++;
    }
    else
    {
    ptemp–;
    temp=Math.pow(2,ptemp);
    ttemp=(int)temp;
    System.out.print(” ” + ttemp);
    }
    }
    t=t+2;
    System.out.println();
    }

    }
    }

    • for (int i=0; i<5;i++){
      for(int x=0; x<=i; x++){
      System.out.print(“*”);
      }
      System.out.printla ();
      }
      for (int r=4; r>=1; r–){
      for(int y=1; y<r; y++){=”” system.out.print(“*”);=”” }=”” system.out.printla=”” ();=”” }=””>

    • BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      int n = Integer.parseInt(br.readLine());

      System.out.println(“Enter n number :: “+n);

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

      for(int j = 0 ;j<i;j++){ (j=”0+1;j&lt;5;j++)” for=”” print=”” the=”” number=”” in=”” next=”” line=”” with=”” 2345,345,45,5=”” system.out.print(“*”+”=”” “);=”” }=”” system.out.println(“”);=”” if(i=”=n){” for(int=”” l=”n-1;l”>0;l–){

      for(int k= 0 ;k<l;k++){ system.out.print(“*”+”=”” “);=”” }=”” system.out.println(“”);=”” }=”” }=”” }=””>

  • how about this code?
    12345
    2345
    345
    45
    5
    the reverse of this instead of staring from the left to right
    it will start from right to left
    thanks

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

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

      public static void main(String[] args) {
      Scanner s=new Scanner(System.in);
      int n=s.nextInt();
      int i=0,j=1;
      for(i=0;i<=n-1;i++)
      {
      if(i==0)
      {
      System.out.print("1");
      }
      else
      {
      System.out.print(i);
      for(j=1;j<i;j++)
      {
      System.out.print("0");
      }
      if(i!=0)
      {
      System.out.print(i);
      }
      }
      System.out.println();

      }
      }
      }

    • public void pyramid(int n) {

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

      for (int j = 1; j = 1; j–) {
      System.out.print(j);
      }

      System.out.println();

      for (int j = 1; j = 1; j–) {
      System.out.print(j);
      }

      System.out.println();

      }
      }

  • HOW TO PROGRAM that the output is looks likes a diamond,,,,

    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5
    1 2 3 4
    1 2 3
    1 2
    1

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

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

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

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

  • I find it unnecessarily confusing
    (1) to have the loop over “i” start from 1 and have the termination condition be “<=” while the loop over “j” starts from 0 and has a termination condition of “<“.
    (2) to use “i++” and “j++” as the update statement. The syntax for post-increment adds nothing to the explanation of for-loops and would be better done as “i = i + 1” and “j = j + 1”.
    (3) to use println(“”) instead of plain println().

Sponsors

Facebook Fans