Pattern Program in java Part-3


 Program #1:  java program to print pyramid of stars using for loop in below format


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




  1. package com.learnJavaOnline;
  2. public class PrintStarsFormat {
  3.  
  4. public static void main(String[] args) {
  5.   
  6. int  numberOfStars=10;
  7.  
  8. for (int row = 1; row <= 10; row++) {
  9.  
  10.     for(numberOfStars = 1; numberOfStars <= row; numberOfStars++) {
  11.       System.out.print("*");
  12.     }
  13.  
  14.     System.out.println(); 
  15.  }
  16.  
  17. }
  18.  
  19. }
Output:
  1. *
  2. **
  3. ***
  4. ****
  5. *****
  6. ******
  7. *******
  8. ********
  9. *********
  10. **********

            

Program #2: java program to print pyramid of stars using for loop in below format



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



  1. package com.learnJavaOnline;
  2. public class PrintStarsPyramid {
  3.  
  4. public static void main(String[] args) {
  5.   
  6. int  numberOfStars=10;
  7.  
  8. for(int i=numberOfStars; i>0 ;i--){
  9.  
  10.     for(int j=0; j < i; j++){
  11.  
  12.           System.out.print("*");
  13.     }
  14.  
  15.     System.out.println("");
  16. }
  17.  
  18. }
  19.  
  20. }
Output:
  1. **********
  2. *********
  3. ********
  4. *******
  5. ******
  6. *****
  7. ****
  8. ***
  9. **
  10. *



Program #3 java program to print pyramid of stars using for loop in below format


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




  1. package com.learnJavaOnline;
  2. public class PrintStarsPyramid {
  3.  
  4. public static void main(String[] args) {
  5.   
  6. int  numberOfStars=10;
  7.  
  8.  for(int i=1; i<= numberOfStars ;i++){
  9.  
  10. for(int j=0; j < i; j++){
  11.  
  12.   System.out.print("*");
  13.  
  14. }
  15.  
  16. System.out.println("");
  17.  
  18. }
  19.  
  20. for(int i=numberOfStars; i>0 ;i--){
  21.  
  22. for(int j=0; j < i; j++){
  23.   System.out.print("*");
  24. }
  25.  
  26. System.out.println("")}
  27.  
  28. }
  29.  
  30. }
Output:
  1. *
  2. **
  3. ***
  4. ****
  5. *****
  6. ******
  7. *******
  8. ********
  9. *********
  10. **********
  11. **********
  12. *********
  13. ********
  14. *******
  15. ******
  16. *****
  17. ****
  18. ***
  19. **
  20. *




Print Pascals triangle using java program


Pattern Program in java Part-2

#4 Java Program to print Numbers in Below pattern

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




  1. package com.learnJavaOnline;
  2. public class NumbersFormat {
  3. public static void main(String[] args) {
  4.  
  5. int r, c1,c2;
  6.  
  7. for (r = 1; r <= 10; r++) {
  8.  
  9. for (c1 = 1; c1 <r; c1++) {
  10.   System.out.print(" ");
  11. }
  12.  
  13. for (c2 = 1; c2 <= 10 - r; c2++) {
  14.     System.out.print(c2 + " ");
  15. }
  16.  
  17. System.out.println("");
  18. }
  19.  
  20. }
  21.  
  22. }


Output:
  1. 1 2 3 4 5 6 7 8 9  
  2. 1 2 3 4 5 6 7 8 
  3.   1 2 3 4 5 6 7 
  4.    1 2 3 4 5 6 
  5.     1 2 3 4 5 
  6.      1 2 3 4 
  7.       1 2 3 
  8.        1 2 
  9.         1
            
#5 Java Program to Print Numbers in Below pattern:

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




  1. package com.learnJavaOnline;
  2. public class NumbersFormat {
  3. public static void main(String[] args) {
  4.  
  5. int r, c1,c2;
  6.  
  7. for (r = 1; r <= 10; r++) {
  8.  
  9. for (c1 = 1; c1 <10-r; c1++) {
  10.   System.out.print(" ");
  11. }
  12.  
  13. for (c2 = 1; c2 <= r; c2++) {
  14.     System.out.print(c2 + " ");
  15. }
  16.  
  17. System.out.println("");
  18. }
  19.  
  20. }
  21.  
  22. }

Output:
  1.         1 
  2.        1 2 
  3.       1 2 3 
  4.      1 2 3 4 
  5.     1 2 3 4 5 
  6.    1 2 3 4 5 6 
  7.   1 2 3 4 5 6 7  
  8. 1 2 3 4 5 6 7 8 
  9. 1 2 3 4 5 6 7 8 9 
  10. 1 2 3 4 5 6 7 8 9 10
            

Print Pascals triangle using java program




Pattern Program in java Part-1

#1 Java Program To print Numbers in Below pattern:

              1
             2 3
            4 5 6
           7 8 9 10
         11 12 13 14 15




  1. package com.javatutorial;
  2. public class NumbersFormat {
  3.  
  4.  public static void main(String[] args) {
  5.  
  6.  int num=15;
  7.  int temp=1;
  8.      
  9.  for (int i = 1; i <= num; i++)
  10.  {
  11.  
  12.   for (int k = i; k <num; k++)
  13.    System.out.print(" ");
  14.    for (int j =1; j <= i; j++){
  15.  
  16.     System.out.print("" +temp+ " ");
  17.      temp++;
  18.  
  19.  if(temp>15){
  20.        break;
  21.  }
  22.  
  23.  }
  24.  
  25.   System.out.println();
  26.  
  27. if(temp>15){
  28.      break;
  29.   }
  30.  
  31. }
  32.  
  33. }

  34. }
Output:

  1.               1
                 2 3
                4 5 6
               7 8 9 10
              11 12 13 14 15





# Java Program to print Numbers in Below Format:

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


  1. package com.learnJavaOnline;
  2. public class NumbersFormat {
  3. public static void main(String[] args) {
  4.  
  5. int r, c;
  6.  
  7. for (r = 1; r <= 10; r++) {
  8.  
  9. for (c = 1; c <= r; c++) {
  10.  
  11. System.out.print(c + " ");
  12.  
  13. }
  14.  
  15. System.out.println("");
  16. }
  17.  
  18. }
  19.  
  20. }
Output:
  1. 1 2 
  2. 1 2 3 
  3. 1 2 3 4 
  4. 1 2 3 4 5 
  5. 1 2 3 4 5 6 
  6. 1 2 3 4 5 6 7 
  7. 1 2 3 4 5 6 7 8 
  8. 1 2 3 4 5 6 7 8 9 
  9. 1 2 3 4 5 6 7 8 9 10


#3 Java Program to print numbers in below format

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


  1. package com.learnJavaOnline;
  2. public class NumbersFormat {
  3. public static void main(String[] args) {
  4.  
  5. int r, c;
  6.  
  7. for (r = 1; r <= 10; r++) {
  8.  
  9. for (c = 1; c <= 10-r; c++) {
  10.  
  11. System.out.print(c + " ");
  12.  
  13. }
  14.  
  15. System.out.println("");
  16. }
  17.  
  18. }
  19.  
  20. }

Output:
  1. 1 2 3 4 5 6 7 8 9 
  2. 1 2 3 4 5 6 7 8 
  3. 1 2 3 4 5 6 7 
  4. 1 2 3 4 5 6 
  5. 1 2 3 4 5 
  6. 1 2 3 4 
  7. 1 2 3 
  8. 1 2 
  9. 1



Print Pascals triangle using java program

Return type / return statement in java example

Return type in java: return statement in java

  • Basically return type is used in java methods.
  • Method signature includes this return type.
  • public int show(){ // }
  • we will use methods to do a particular task after completion of task if we want to return something to the calling place these return types will be used.
  • Based on the type of data to be returned will mention it as int , char , float double etc as return type in method signature and return statement should be the last statement of the method body.
  • In Java, the return statement is used to exit a method and return a value to the calling method. The value returned can be of any data type that is specified in the method's return type. 
  • For example, if a method has a return type of int, it can return an integer value. Here's an example of a simple method that uses the return statement to return an int value:


public int add(int a, int b) {
    int sum = a + b;
    return sum;
}

  • In this example, the add method takes two int parameters, a and b, and returns the sum of the two values. The method uses the return statement to return the value of the sum variable back to the calling method.
  • You can also use return statement without returning any value, in this case method should have void return type.


public void printHelloWorld() {
    System.out.println("Hello, World!");
    return;
}

  • In this case the method printHelloWorld doesn't return any value and the calling method doesn't need to assign the return to any variable, just invoking the method will do the job.
  • It is also worth noting that when a return statement is executed, it immediately exits the current method, regardless of where it is in the method's execution flow. So, any code after a return statement will not be executed.

Type of declaration of methods based on return type and arguments:

1.Method with out return type  and without arguments: return statement in java


  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. public void add(){
  5.  
  6. int a=40;
  7. int b=50;
  8. int c=a+b;
  9. System.out.println(c);
  10.  
  11. }

  12. public static void main(String args[]) // ->method prototype.
  13.  
  14. sample obj= new sample();
  15. obj.add();
  16.  
  17.  
  18.  }
  19. }

2.Method with out return type and with arguments.

  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. public void add(int a, int b){
  5.  
  6. int c=a+b;
  7. System.out.println(c);
  8.  
  9. }

  10. public static void main(String args[]) // ->method prototype.
  11.  
  12. sample obj= new sample();
  13. obj.add(13,24);
  14.  
  15.  
  16.  }
  17. }

3.Method with return type  and without arguments.


  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. public int add(){
  5. int a=40;
  6. int b=50;
  7. int c=a+b;
  8. return c;
  9. }

  10. public static void main(String args[]) // ->method prototype.
  11.  
  12. sample obj= new sample();
  13. int x=obj.add(); 
  14. System.out.println(x);
  15.  
  16.  }
  17. }

4.Method with return type and with arguments.

 

  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. public int add(int a, int b){
  5.  
  6. int c=a+b;
  7. return c;
  8. }

  9. public static void main(String args[]) // ->method prototype.
  10.  
  11. sample obj= new sample();
  12.  
  13. int x=obj.add(1,2);
  14. System.out.println(x);
  15.  
  16.  }
  17. }


If Else Statment in Java

If statement :
  • if statement in java is decision making statement in java.
  • if statement will have a condition and if that condition is true then the corresponding block will be executed.
  • if(condition){ //  }
If condition syntax:



  1. if(condition){
  2. //  statements
  3. }
 Sample program on if statement:


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class ifExample { 
  5.  
  6. public static void main(String[] args) {
  7.  
  8. if(true){
  9.  
  10.  System.out.println("if condition executed");
  11.  
  12. }
  13.  
  14. }

Output:
  1. if condition executed

Java program to compare two numbers in java


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class simpleIfExample { 
  5.   
  6. public static void main(String[] args) {
  7.  
  8.   int x= 37;
  9.   int y= 37;
  10.                
  11. if(x==y)
  12.     System.out.println(x+ " is equal to " + y);
  13. }
  14.  
  15. if(x>y){
  16.     System.out.println(x+ " is greater than " + y);
  17. }
  18.  
  19. if(x<y){
  20.     System.out.println(x+ " is less than " + y);
  21. }
  22. }

Output:
  1. 37 is equal to 37



 Else Statement:

  •  if(condition){ //} else { // }

Java program to compare two numbers in java using if else


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class ifElseExample { 
  5.   
  6. public static void main(String[] args) {
  7.  
  8.   int x= 37;
  9.   int y= 37;
  10.                
  11. if(x==y){
  12.     System.out.println(x+ " is equal to " + y);
  13. }else if(x>y){
  14.     System.out.println(x+ " is greater than " + y);
  15. }else{
  16.     System.out.println(x+ " is less than " + y);
  17. }
  18. }



Output:
  1. 37 is equal to 37


Java program to find leap year or not in java using if else


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class ifElseExample { 
  5.   
  6. public static void main(String[] args) {
  7.  
  8.     int year = 2015;                      
  9.   //if year is divisible by 4, it is a leap year

  10.  if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
  11.  System.out.println("Year " + year + " is a leap year");
  12.  else
  13.       System.out.println( year + " is not a leap year");
  14.   
  15.  
  16. }

Output:
  1. 2015 is not a leap year


Java program to find even or odd using if else statment


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class ifElseEvenOddExample { 
  5.  
  6. public static void main(String[] args) {
  7.   
  8. int a=10;
  9. if((a%2)==0){
  10.  
  11.  System.out.println(a+"is even number");
  12.  
  13. }else{ 

  14.     System.out.println(a+"is odd number");
  15.  
  16. }
  17.  
  18. }

Output:
  1. 10 is even number

Static block in Java

  • Static variables are class level variables and without instantiating class we can access these variables.

    1. Static int a;


  • Class loading time itself these variables gets memory
  • Static methods are the methods with static keyword are class level. without creating the object of the class we can call these static methods.

    1. public static void show(){ 
    2.  
    3. }

  • Now its time to discuss about static blocks.
  • Static block also known as static initializer
  • Static blocks are the blocks with static keyword.
  • Static blocks wont have any name in its prototype.
  • Static blocks are class level.
  • Static block will be executed only once.
  • No return statements.
  • No arguments.
  • No this or super keywords supported.
  •  
    1. static{ 
    2.  
    3.  }

     

What is the need of static block?

  • Static blocks will be executed at the time of class loading.
  • So if you want any logic that needs to be executed at the time of class loading that logic need to place inside the static block so that it will be executed at the time of class loading. 
  • To initialize the static variables while class loading itself we need static block.
  • If we have multiple classes then if you want to see the order of those classes loading then static block needed.

When to use static blocks?

  • If you want to access static variables before executing the constructor.
  • If you want to execute your code only once even any number of objects created.
  • If you want to initialize static constants.

When and where static blocks will be executed?

  • Static blocks will be executed at the time of class loading by the JVM  by creating separate stack frames in java stacks area (Please refer JVM Architecture  to know about stacks area).
  • Static blocks will be executed in the order they defined from top to bottom.



  1. package com.instanceofjava;
  2.  
  3. class StaticBlockDemo 
  4. {
  5.  
  6. static{
  7.       System.out.println("First static block executed");
  8. }
  9.  
  10. static{
  11.      System.out.println("Second static block executed");
  12. }
  13.  
  14. static{
  15.      System.out.println("Third static block executed");
  16. }
  17.  
  18. }
     
Output:
  1. First static block executed
  2. Second static block executed
  3. Third static block executed

Order of execution of static block and main method:

  1. package com.instanceofjava;
  2.  
  3. class StaticBlockDemo 
  4. {
  5.  
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8.         System.out.println("Main method executed");
  9. }
  10.  
  11. static{
  12.       System.out.println("First static block executed");
  13. }
  14.  
  15. static{
  16.      System.out.println("Second static block executed");
  17. }
  18.  
  19. static{
  20.      System.out.println("Third static block executed");
  21. }
  22.  
  23. }
     
Output:
  1. First static block executed
  2. Second static block executed
  3. Third static block executed
  4. Main method executed


Order of execution of Static block and constructor:

 

  1. package com.instanceofjava;
  2.  
  3. class StaticBlockDemo 
  4. {
  5.  
  6. StaticBlockDemo(){
  7.  
  8.   System.out.println("Constructor executed");
  9.  
  10. }
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13.  
  14.     System.out.println("Main method executed");
  15.     StaticBlockDemo obj = new StaticBlockDemo();
  16.  
  17. }
  18.  
  19. static{
  20.       System.out.println("First static block executed");
  21. }
  22.  
  23. static{
  24.      System.out.println("Second static block executed");
  25. }
  26.  
  27. static{
  28.      System.out.println("Third static block executed");
  29. }
  30.  
  31. }
     


Output:
  1. First static block executed
  2. Second static block executed
  3. Third static block executed
  4. Main method executed
  5. Constructor executed

What is the output of following program:


  1. package com.instanceofjava;
  2.  
  3. class StaticBlockDemo 
  4. {
  5.  
  6.   static int a=getNum();

  7. public static int getNum(){
  8.  
  9. System.out.println("In method m1()");
  10. return 10;
  11.  
  12. }

  13. public static void main (String[] args) throws java.lang.Exception
  14. {
  15.  
  16.     System.out.println("Main method executed");
  17.   
  18.  
  19. }
  20.  
  21. static{
  22.       System.out.println("First static block executed");
  23. }
  24.  
  25. static{
  26.      System.out.println("Second static block executed");
  27. }
  28.  
  29. static{
  30.      System.out.println("Third static block executed");
  31. }
  32.  
  33. }
     
Output:
  1. In method m1()
  2. First static block executed
  3. Second static block executed
  4. Third static block executed
  5. Main method executed


Static methods in java example

  • A method which has static keyword in its definition is called static method.
  1. static void print(){
  2.  
  3. }

  • JVM will not execute static methods by default. They are executed only if they are called explicitly by developer either from main method or from static variable as its assignment statement or from static block.

Static method called from main method:


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  
  5. static void show(){
  6. System.out.println("static method"):
  7. }
  8.  
  9. public static void main(String[] args)
  10. {
  11.  
  12. show();
  13.  
  14. }
  15. }
Output:

  1. static method

Static method called from variable assignment:


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  static int x=show();
  5. static int show(){
  6. System.out.println("static method called"):
  7. return 10;
  8. }
  9.  
  10. public static void main(String[] args)
  11. {
  12.  
  13. System.our.println(x);
  14.  
  15. }
  16. }
Output:

  1. static method called
  2. 10

Static method called from static block:


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  static{
  5.  show();
  6. }

  7. static void show(){
  8. System.out.println("static method called"):
  9. }
  10.  
  11. }
Output:

  1. static method called

Order of Execution:

  •  Static methods are executed in the order of they are called, not in the order of they are defined.
  • All static methods are executed in java stack area by creating separate stack frame.
  • When a method is called from main method, JVM creates stack frame in main thread for that method execution.
  • The stack frame is destroyed immediately after method execution is completed.

Example program on order of execution of static methods:


  1. package com.instanceofjava;
  2. class StaticMehodDemo
  3. {
  4.  
  5. static void show(){
  6.  
  7. System.out.println("show method called");
  8.  
  9. }
  10.  
  11. static void print(){
  12.  
  13. System.out.println("print method called");
  14.  
  15. }
  16.  
  17. static void display(){
  18.  
  19. System.out.println("display method called");
  20.  
  21. }
  22.  
  23.  
  24. public static void main(String[] args)
  25. {
  26.  
  27. System.our.println("main method called");
  28.  
  29. show();
  30. print();
  31. display();
  32.  
  33.  
  34. }
  35. }


Output:

  1. main method called
  2. print method called
  3. show method called
  4. display method called

Variable initialization with same variable:

  •  We can initialize variable with same variable name. this assignment is valid. In this case the variable value is replaced with same value.
  1. int x=20;
  2. x=x; // valid statement
Example program:



  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  static int a=10;

  5. public static void main(String[] args){
  6.   
  7. int a=20;
  8. a=a;
  9. System.out.println("a="+a);
  10.  System.out.println("StaticDemo.a="+StaticDemo.a);
  11.  
  12. }
  13.  
  14. }
Output:

  1. 20
  2. 10

 

Local preference with parameters:

  • Parameters are also treated as local variables.
  • Hence if parameter declared with same static variable name and if we want to access static variable in presence of parameter or if we want to initialize static variable with parameter name we must refer variable with class name.

Example program:


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  static int x=10;
  5.  
  6. static void m1(int x){ 
  7.  
  8.  System.out.println(x);
  9.  System.out.println(StaticDemo.x);

  10. }

  11. public static void main(String[] args){
  12.   
  13.  m1(37);
  14.  System.out.println(x);
  15.  
  16. }
  17.  
  18. }
Output:

  1. 37
  2. 10

 

You might Like:

 

 

 

 


Select Menu