Java >> Tutoriel Java >  >> Java

Programme de motif de diamant en Java

Programme de motif de diamant en Java | Il existe différents programmes de modèles de diamants en Java. Ici, nous écrirons un modèle de demi-diamant, un modèle de diamant complet, un programme de modèle de diamant creux, différents programmes de modèle de diamant avec des nombres. Maintenant, commençons avec un programme de motif de diamant complet avec des étoiles.

1. Affichez le motif d'étoiles en losange complet ci-dessous à l'aide de Java.

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

Le programme pour le motif d'étoiles en diamant complet ci-dessus peut être écrit comme suit :

import java.util.Scanner;

public class DiamondPattern {

   public static void main(String[] args) {
      // take input
      Scanner scan = new Scanner(System.in);
      int n = 0;
      System.out.print("Enter N value:: ");
      n = scan.nextInt();
      printStartDiamond(n);
   }

   private static void printStartDiamond(int n) {
      if(n <= 0)
      System.out.println("Enter Positive Number");

      // for increasing portion
      for(int i=1; i<=n; i++) {
         // print space
         for(int j = i; j<=n; j++) {
            System.out.print(" ");
         }
         // print star
         for(int k = 1; k <= 2*i-1; k++) {
            System.out.print("*");
         }
         // new line
         System.out.println();
      }

      // for decrement portion
      for(int i=n-1; i >=1; i--) {
         // print space
         for(int j=n; j >= i; j--) {
            System.out.print(" ");
         }
         // print star
         for(int k=1; k <= 2*i-1; k++) {
            System.out.print("*");
         }
         // new line
         System.out.println();
      }
   }

}

2. Ce modèle est juste l'opposé du modèle précédent. Écrivez un programme Java pour afficher le modèle d'espace et d'étoiles ci-dessous sur la console.

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

Le code Java pour le modèle ci-dessus peut être écrit comme,

import java.util.Scanner;

public class DiamondPattern {

   public static void main(String[] args) {
      // take input
      Scanner scan = new Scanner(System.in);
      int n = 0;
      System.out.print("Enter N value:: ");
      n = scan.nextInt();
      printSpaceDiamond(n);
   }

   private static void printSpaceDiamond(int n) {
      if(n <= 0)
      System.out.println("Enter Positive Number");

      // for first half portion (top to bottom)
      for(int i=1; i <= n; i++) {
         // print star
         for(int j = i; j <= n; j++) {
            System.out.print("*");
         }
         // print space
         for(int k = 1; k < 2*i-1; k++) {
            System.out.print(" ");
         }
         // print star 
         for(int l=i; l <= n; l++) {
            System.out.print("*");
         }
         // new line
         System.out.println();
      }

      // for second half portion
      for(int i=n-1; i >= 1; i--) {
         // print star
         for(int j=n; j >= i; j--) {
            System.out.print("*");
         }
         // print space
         for(int k=1; k < 2*i-1; k++) {
            System.out.print(" ");
         }
         // print star 
         for(int l=n; l >= i; l--) {
            System.out.print("*");
         }
         // new line
         System.out.println();
      }
   }

}

3. Écrivez un programme Java pour afficher le diamant complet de nombres ci-dessous.

     1
    123
   12345
  1234567
 123456789
  1234567
   12345
    123
     1

Le programme Java pour le modèle ci-dessus est,

import java.util.Scanner;

public class DiamondPattern {

   public static void main(String[] args) {
      // take input
      Scanner scan = new Scanner(System.in);
      int n = 0;
      System.out.print("Enter N value:: ");
      n = scan.nextInt();
      printNumberDiamond(n);
   }

   private static void printNumberDiamond(int n) {
      if(n <= 0)
      System.out.println("Enter Positive Number");

      // declare temp variable
      int a = 0;
	  
      // for first half portion (top to bottom)
      for(int i=1; i <= n; i++) {
         // in each iteration a will start from 1
         a = 1;
		 
         // print space 
         for(int j = i; j <= n; j++) {
            System.out.print(" ");
         }
         // print digit
         for(int k = 1; k <= 2*i-1; k++) {
            System.out.print(a++);
         }
         // new line
         System.out.println();
      }

      // for second half portion
      for(int i=n-1; i >= 1; i--) {
         // in each iteration a will start from 1
         a = 1;
		 
         // print space
         for(int j=n; j >= i; j--) {
            System.out.print(" ");
         }
         // print digit
         for(int k=1; k <= 2*i-1; k++) {
            System.out.print(a++);
         }
         // new line
         System.out.println();
      }
   }
}

4. Écrivez un programme Java pour afficher le losange complet ci-dessous. Les nombres commencent par 0 et se terminent par 0.

     0
    010
   01210
  0123210
 012343210
01234543210
 012343210
  0123210
   01210
    010
     0

Le programme Java pour le modèle ci-dessus est,

import java.util.Scanner;

public class DiamondPattern {

   public static void main(String[] args) {
      // take input
      Scanner scan = new Scanner(System.in);
      int n = 0;
      System.out.print("Enter N value:: ");
      n = scan.nextInt();
      printNumberDiamond(n);
   }

   private static void printNumberDiamond(int n) {
      if(n <= 0)
      System.out.println("Enter Positive Number");

      // declare temp variable
      int a = 0;
	  
      // for first half portion (top to bottom)
      for(int i=1; i <= n+1; i++) {
		 
         // print space 
         for(int j = i; j <= n; j++) {
            System.out.print(" ");
         }

         // print digit
         for(int k = 1; k <= 2*i-1; k++) {
            if(k < i)
            System.out.print(a++);
            else if(k == i)
            System.out.print(a);
            else
            System.out.print(--a);
         }
		 
         // new line
         System.out.println();
      }

      // for second half portion
      for(int i=n; i >= 1; i--) {
		 
         // print space
         for(int j=n; j >= i; j--) {
            System.out.print(" ");
         }
		 
         // print digit
         for(int k=1; k <= 2*i-1; k++) {
            if(k < i)
            System.out.print(a++);
            else if(k == i)
            System.out.print(a);
            else
            System.out.print(--a);
         }
		 
         // new line
         System.out.println();
      }
   }

}

Programme de modèle de diamant creux en Java

5. Écrivez un programme Java pour afficher le diamant creux d'étoiles ci-dessous.

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

Le code du modèle ci-dessus est,

import java.util.Scanner;

public class HollowDiamond {

   public static void main(String[] args) {
      // take input
      Scanner scan = new Scanner(System.in);
      int n = 0;
      System.out.print("Enter N value:: ");
      n = scan.nextInt();
      printStarHollowDiamond(n);
   }

   private static void printStarHollowDiamond(int n) {
      if(n <= 0)
      System.out.println("Enter Positive Number");
	  
      // for first half portion (top to bottom)
      for(int i=1; i <= n+1; i++) {
		 
         // print space 
         for(int j = i; j <= n; j++) {
            System.out.print(" ");
         }

         // print digit or space
         for(int k = 1; k <= 2*i-1; k++) {
            if(k==1 || k==(2*i-1) )
            System.out.print("*");
            else
            System.out.print(" ");
         }
		 
         // new line
         System.out.println();
      }

      // for second half portion
      for(int i=n; i >= 1; i--) {
		 
         // print space
         for(int j=n; j >= i; j--) {
            System.out.print(" ");
         }
		 
         // print digit or space
         for(int k=1; k <= 2*i-1; k++) {
            if(k==1 || k==(2*i-1) )
            System.out.print("*");
            else
            System.out.print(" ");
         }
		 
         // new line
         System.out.println();
      }
   }

}

6. Écrivez un programme Java pour afficher le losange creux de nombres ci-dessous en utilisant Java.

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

Le code du modèle ci-dessus est,

import java.util.Scanner;

public class HollowDiamond {

   public static void main(String[] args) {
      // take input
      Scanner scan = new Scanner(System.in);
      int n = 0;
      System.out.print("Enter N value:: ");
      n = scan.nextInt();
      printNumberHollowDiamond(n);
   }

   private static void printNumberHollowDiamond(int n) {
      if(n <= 0)
      System.out.println("Enter Positive Number");

      // declare temp variable
      int a = 1;

      // for first half portion (top to bottom)
      for(int i=1; i <= n; i++) {

         // print space 
         for(int j = i; j <= n; j++) {
            System.out.print(" ");
         }

         // print digit and space 
         for(int k = 1; k <= 2*i-1; k++) {
            if(k==1 || k==(2*i-1) )
            System.out.print(a);
            else
            System.out.print(" ");
         }

         // increase temp variable
         a++;

         // new line
         System.out.println();
      }

      // update temp variable
      a = n-1;

      // for second half portion
      for(int i=n-1; i >= 1; i--) {

         // print space
         for(int j=n; j >= i; j--) {
            System.out.print(" ");
         }

         // print digit and space 
         for(int k=1; k <= 2*i-1; k++) {
            if(k==1 || k==(2*i-1) )
            System.out.print(a);
            else
            System.out.print(" ");
         }

         // decrease temp variable
          a--;

         // new line
         System.out.println();
      }
   }

}

Motif demi-losange

7. Écrivez un programme Java pour afficher le motif d'étoiles ci-dessous en demi-losange.

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

La variable étoile contient le nombre d'étoiles dans la nième ligne. Dans chaque ligne, la variable étoile est mise à jour. Quand i>n alors (2*n-i) sera négatif donc, la fonction abs() est utilisée pour trouver la valeur absolue de (2*n-i).

import java.util.Scanner;

public class HalfDiamond {

   public static void main(String[] args) {
      // take input
      Scanner scan = new Scanner(System.in);
      int n = 0;
      System.out.print("Enter N value:: ");
      n = scan.nextInt();
      printStarHalfDiamond(n);
   }

   private static void printStarHalfDiamond(int n) {
      if(n <= 0)
      System.out.println("Enter Positive Number");

      // declare temp variable
      int star = 0;

      for(int i=1; i < 2*n; i++) {
         if(i < n) star = i;
         else star = Math.abs(2*n - i);

         // print star 
         for(int j = 1; j <= star; j++) {
            System.out.print("*");
         }

         // new line
         System.out.println();
      }
   }
}

8. Écrivez un programme Java pour afficher le diamant creux d'étoiles en miroir ci-dessous. C'est tout le contraire du précédent.

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

Le programme Java pour le diamant creux d'étoiles en miroir ci-dessus,

import java.util.Scanner;

public class HalfDiamond {

   public static void main(String[] args) {
      // take input
      Scanner scan = new Scanner(System.in);
      int n = 0;
      System.out.print("Enter N value:: ");
      n = scan.nextInt();
      printStarHalfDiamond(n);
   }

   private static void printStarHalfDiamond(int n) {
      if(n <= 0)
      System.out.println("Enter Positive Number");

      // declare temp variable
      int star = 0;

      for(int i=1; i < 2*n; i++) {
         
         // print star 
         for(int j = 1; j <= n; j++) {
            if(j <= Math.abs(n-i))
            System.out.print(" ");
            else 
            System.out.print("*");
         }

         // new line
         System.out.println();
      }
   }
}

9. Écrivez un programme Java pour afficher le modèle de losange creux ci-dessous composé de nombres et d'étoiles.

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

Le code Java pour le modèle ci-dessus peut être écrit comme,

import java.util.Scanner;

public class HalfDiamond {

   public static void main(String[] args) {
      // take input
      Scanner scan = new Scanner(System.in);
      int n = 0;
      System.out.print("Enter N value:: ");
      n = scan.nextInt();
      printStarNumberHalfDiamond(n);
   }

   private static void printStarNumberHalfDiamond(int n) {
      if(n <= 0)
      System.out.println("Enter Positive Number");

      // declare temp variable
      int place = 0;

      for(int i=1; i < 2*n; i++) {
         if(i < n) place = i;
         else place = Math.abs(2*n-i);
         
         // print star or number
         for(int j = 1; j < 2*place; j++) {
            if(j % 2 == 0)
            System.out.print("*"); // star
            else 
            System.out.print(place); // number
         }

         // new line
         System.out.println();
      }
   }
}

10. Écrivez un programme Java pour afficher le modèle de nombres en losange creux ci-dessous.

1
12
123
1234
12345
1234
123
12
1

Le code Java pour le modèle ci-dessus peut être écrit comme,

import java.util.Scanner;

public class HalfDiamond {

   public static void main(String[] args) {
      // take input
      Scanner scan = new Scanner(System.in);
      int n = 0;
      System.out.print("Enter N value:: ");
      n = scan.nextInt();
      printNumberHalfDiamond(n);
   }

   private static void printNumberHalfDiamond(int n) {
      if(n <= 0)
      System.out.println("Enter Positive Number");

      // declare temp variable
      int place = 0, a = 0;

      for(int i=1; i < 2*n; i++) {
         // after every iteration 
         // initialize a with 1
         a = 1;

         if(i < n) place = i;
         else place = Math.abs(2*n-i);

         // print star or number
         for(int j = 1; j <= place; j++) {
            System.out.print(a++); // number
         }

         // new line
         System.out.println();
      }
   }
}

11. Écrivez un programme pour afficher le modèle de nombres en losange creux ci-dessous.

1
123
12345
1234567
123456789
1234567
12345
123
1

Le code Java pour le modèle ci-dessus peut être écrit comme,

import java.util.Scanner;

public class HalfDiamond {

   public static void main(String[] args) {
      // take input
      Scanner scan = new Scanner(System.in);
      int n = 0;
      System.out.print("Enter N value:: ");
      n = scan.nextInt();
      printNumberHalfDiamond(n);
   }

   private static void printNumberHalfDiamond(int n) {
      if(n <= 0)
      System.out.println("Enter Positive Number");

      // declare temp variable
      int place = 0, a = 0;

      for(int i=1; i < 2*n; i++) {
         // after every iteration 
         // initialize a with 1
         a = 1;

         if(i < n) place = i;
         else place = Math.abs(2*n-i);

         // print star or number
         for(int j = 1; j <= 2*place-1; j++) {
            System.out.print(a++); // number
         }

         // new line
         System.out.println();
      }
   }
}

Balise Java