Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

312
Visualizações
Reverse asterisks right triangle, java program

I am very close to this, however instead of the asterisks being horizontal, in a row, they are vertical, one in top of the other.

Question: Write a program that displays the output below. Use a variable to store the value for the number of asterisks in the first row. You must use multiple loops in your solution.

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

I currently have:

public class JavaAssignment6b {
    public static void main( String[] args ) {
        for(int r = 1; r <= 10; r++) {
            for(int j = 10; j >= r; j--) {
                System.out.println("*");
            }
            System.out.println(" ");
        }
    }
}               
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Simple answer to this question. All you have to do is add a simple if statement. I have posted two solutions below, one prints the number of asterisks on the first line, the other posts the number of asterisks on every line.

Solution 1

To print the number of asterisks on the first line:

int numberOfAsterisks = 0;
    for(int r = 1; r <= 10; r++) {
        for(int j = 10; j >= r; j--) {
            System.out.print("*");
            numberOfAsterisks++;
         }    
         if (r==1) {
            System.out.print(numberOfAsterisks);
         }
            System.out.println();
    }               
}

And the output

**********10
*********
********
*******
******
*****
****
***
**
*

Solution 2

To print the number of asterisks on every line:

int numberOfAsterisks = 0;
                for(int r = 1; r <= 10; r++) {
                    for(int j = 10; j >= r; j--) {
                        System.out.print("*");
                        numberOfAsterisks++;
                    }
                    System.out.print(numberOfAsterisks);
                    numberOfAsterisks = 0;
                    System.out.println();
        }               
    }

and the output for this answer:

**********10
*********9
********8
*******7
******6
*****5
****4
***3
**2
*1
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda