Powered by Blogger.
Showing posts with label for loop program. Show all posts
Showing posts with label for loop program. Show all posts

Write a Java program that will display the Pyramid | Java

Java Program for Pyramid of Numbers:

public class Pyramid{
 public static void main(String[]args){
   int n=9;
 for(int i=1;i<=n;i++){
 for(int j=n−i;j>=1;j−−)
 System.out.print(” ”);
 for(int k=1;k<=i;k++)
 System.out.print(k+” ”);
 for(int p=i−1;p>=1;p−−)
 System.out.print(p+” ”);
 System.out.println();
   }
  }
 }

Java | For Loop Statement Program in java

For Loop Statement:

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to be executed a specific number of times.

A for loop is useful when you know how many times a task is to be repeated.

for loop syntax:

for(initialization; condition; increment/decrement){
        statements;
}

For Loop Program:

public class Test {

   public static void main(String args[]) {

      for(int x = 20; x < 30; x = x + 1) {
         System.out.print("value of x : " + x );
         System.out.print("\n");
      }
   }
}

Output:
value of x : 20
value of x : 21
value of x : 22
value of x : 23
value of x : 24
value of x : 25
value of x : 26
value of x : 27
value of x : 28
value of x : 29