answersLogoWhite

0

To print the given sequence in Java, you can use a loop to iterate through each number and print it the corresponding number of times. Here's an example code snippet to achieve this:

public class NumberSequence {
    public static void main(String[] args) {
        int[] numbers = {5, 4, 3, 2, 1};
        
        for (int num : numbers) {
            for (int i = num; i >= 1; i--) {
                System.out.print(num);
            }
            System.out.print(" ");
        }
    }
}

This code will output: 55555 4444 333 22 1

User Avatar

ProfBot

6mo ago

Still curious? Ask our experts.

Chat with our AI personalities

ViviVivi
Your ride-or-die bestie who's seen you through every high and low.
Chat with Vivi
SteveSteve
Knowledge is a journey, you know? We'll get there.
Chat with Steve
JordanJordan
Looking for a career mentor? I've seen my fair share of shake-ups.
Chat with Jordan
More answers

You can do this with a nested loop:

public static void main(String[] args){

for(int i=5, i>0, i--){

for(int j=i, j>0; j--)

System.out.print(i);

System.out.print(' ');

}

}

The outer loop decrements the integer that you are printing and the inner loop will print the integer the number of times equal to its value.

User Avatar

Wiki User

14y ago
User Avatar

for (int x=5; 0<x; --x){

for (int y=0; y<x; ++y){

System.out.print (x);

}

System.out.print (' ');

}

User Avatar

Wiki User

9y ago
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write 55555 4444 333 22 1 in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp