Programme pyramidal en Java
Programme pyramidal en Java | Il existe de nombreux programmes de modèles écrits par des programmeurs à des fins de pratique. Le motif en étoile pyramidale de Java en fait partie. Dans cet article, nous afficherons un motif en étoile demi-pyramide, un motif en étoile demi-pyramide inversée, un motif en étoile pyramide complète, un motif en étoile pyramide complète au centre de l'écran et un motif en étoile pyramide complète inversée.
Modèle d'étoile demi-pyramide
*
* *
* * *
* * * *
* * * * *
Le programme Java pour le motif en étoile en demi-pyramide ci-dessus est,
public class Pyramid {
public static void main(String[] args) {
// outer loop for row
for (int i=1; i <= 5; i++) {
// inner loop for column
for(int j=1; j <= i; j++) {
// print star
System.out.print("* ");
}
// new line
System.out.println();
}
}
}
Le programme précédent utilise l'opérateur d'incrémentation pour afficher le motif en étoile demi-pyramide en Java. Mais on peut aussi utiliser l'opérateur de décrémentation.
Motif en étoile demi-pyramide utilisant à la fois les opérateurs d'incrémentation et de décrémentation,
Ici, nous utiliserons l'opérateur de décrémentation pour la boucle externe et l'opérateur d'incrémentation pour la boucle interne pour imprimer au-dessus de l'impression.
for (int i=5; i >= 1; i--) {
for(int j=i; j <= 5; j++) {
// print star
System.out.print("* ");
}
// new line
System.out.println();
}
Maintenant, nous allons utiliser l'opérateur d'incrémentation pour la boucle externe et l'opérateur de décrémentation pour la boucle interne pour imprimer le même motif (ci-dessus).
for (int i=1; i <= 5; i++) {
for(int j=i; j >= 1; j--) {
// print star
System.out.print("* ");
}
// new line
System.out.println();
}
Modèle d'étoile demi-pyramide inversée
* * * * *
* * * *
* * *
* *
*
Le code pour le motif en étoile en demi-pyramide inversée est,
public class InvertedPyramid {
public static void main(String[] args) {
for (int i=5; i >= 1; i--) {
for(int j=i; j >= 1; j--) {
// print star
System.out.print("* ");
}
// new line
System.out.println();
}
}
}
*
**
***
****
*****
Le code du modèle ci-dessus est donné ci-dessous,
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = 0;
// take input
System.out.print("Enter number of rows:: ");
n = scan.nextInt();
// loop
for (int i=1; i <= n; i++) {
// space
for(int j=1; j <= n-i; j++)
System.out.print(" ");
// star
for(int k=1; k <= i; k++)
System.out.print("*");
// new line
System.out.println();
}
}
}
*****
****
***
**
*
Le code Java pour le motif en étoile ci-dessus,
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = 0;
// take input
System.out.print("Enter number of rows:: ");
n = scan.nextInt();
// loop
for (int i=1; i <= n; i++) {
for(int j=1; j <= n; j++)
if(j < i)
System.out.print(" "); // space
else
System.out.print("*"); // star
// new line
System.out.println();
}
}
}
Modèle d'étoile pyramidale complète en Java
*
***
*****
*******
*********
Le programme complet de modèle d'étoile pyramidale en Java peut être écrit comme,
import java.util.Scanner;
public class FullPyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = 0;
// take input
System.out.print("Enter number of rows:: ");
n = scan.nextInt();
// loop
for (int i=1; i <= n; i++) {
// space
for(int j=1; j <= n-i; j++)
System.out.print(" ");
// star
for(int k=1; k <= (2*i-1); k++)
System.out.print("*");
// new line
System.out.println();
}
}
}
Il existe de nombreuses autres façons d'afficher le même motif,
// loop
for (int i=1; i <= n; i++) {
for(int j=1; j <= 2*n; j++)
if(j <= n-i)
System.out.print(" "); // space
else if(j < n+i)
System.out.print("*"); // star
// new line
System.out.println();
}
Le même modèle peut également être affiché en utilisant la technique de récursivité,
import java.util.Scanner;
public class FullPyramid {
// static variable
static int stars = -1;
// main method
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = 0;
// take input
System.out.print("Enter number of rows:: ");
n = scan.nextInt();
displayFullPyramid(n);
}
private static void displayFullPyramid(int n) {
if(n <= 0) return;
// variables
int space = n-1;
stars += 2;
// space
for(int i = 0; i < space; i++)
System.out.print(" ");
// stars
for(int j = 0; j < stars; j++)
System.out.print("*");
// new line
System.out.println();
// recursive call
displayFullPyramid(--n);
}
}
Généralement, sur un écran d'ordinateur, on peut imprimer au maximum 80 caractères horizontalement. Ici, nous allons imprimer la pyramide complète pour n lignes.
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = 0, c=80;
// take input
System.out.print("Enter number of rows:: ");
n = scan.nextInt();
// loop
for (int i=1; i <= n; i++) {
for(int j=1; j <= (c/2-i); j++)
System.out.print(" "); // space
for(int k=1; k <= (2*i-1); k++)
System.out.print("*"); // star
// new line
System.out.println();
}
}
}
Modèle d'étoile pyramidale pleine inversée
*********
*******
*****
***
*
Solution1 : – Utiliser uniquement des opérateurs d'incrémentation,
import java.util.Scanner;
public class InvertedPyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = 0;
// take input
System.out.print("Enter number of rows:: ");
n = scan.nextInt();
// loop
for (int i=1; i <= n; i++) {
for(int j=1; j < 2*n; j++)
if(j < i)
System.out.print(" "); // spaces
else if(j <= 2*n-i)
System.out.print("*"); // stars
// new line
System.out.println();
}
}
}
Solution2 :- Utiliser à la fois les opérateurs d'incrémentation et de décrémentation,
for (int i=n; i >= 1; i--) {
for(int j=1; j <= n-i; j++)
System.out.print(" "); // spaces
for(int k=1; k <= 2*i-1; k++)
System.out.print("*"); // stars
// new line
System.out.println();
}
Solution 3 :-
for (int i=n; i >= 1; i--) {
for(int j=n; j > i; j--)
System.out.print(" "); // spaces
for(int k=1; k <= 2*i-1; k++)
System.out.print("*"); // stars
// new line
System.out.println();
}
*
* *
* * *
* * * *
* * * * *
Le code du modèle ci-dessus est,
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = 0;
// take input
System.out.print("Enter number of rows:: ");
n = scan.nextInt();
// loop
for (int i=1; i <= n; i++) {
for(int j=1; j <= n-i; j++)
System.out.print(" "); // spaces
for(int k=1; k <= i; k++)
System.out.print("* "); // stars
// new line
System.out.println();
}
}
}
* *
** **
*** ***
**** ****
**********
Le code du modèle ci-dessus est,
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = 0;
// take input
System.out.print("Enter number of rows:: ");
n = scan.nextInt();
// loop
for (int i=1; i <= n; i++) {
for(int j=1; j <= 2*n; j++)
if(j <= i || j > (2*n-i))
System.out.print("*"); // star
else
System.out.print(" "); // space
// new line
System.out.println();
}
}
}