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
Chat with our AI personalities
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.
for (int x=5; 0<x; --x){
for (int y=0; y<x; ++y){
System.out.print (x);
}
System.out.print (' ');
}
Implement this method: public static void makeTriangle(int limit) { int count = 0; for(int i = 1; i <= limit; i++) { count = i; while(count > 0) { System.out.print(i); count--; } System.out.println(); } }
int main() { int i,j,sum,k; for(i=1;i<=5;i++) { k=1; sum =0; for(j=1;j<=i;j++) { sum = sum+(i*k); k=k*10; } cout<< sum; cout<< "\n"; } }
#include <stdio.h> int main (void) { puts ("1 22 333 4444 55555"); return 0; }
using System;namespace RightAngleTraingle{class Program{static void Main(string[] args){for (int x = 1; x
The solution is two use a nested for loop: int startingNum = 1; int maxNum = 10; for(int currentNum = startingNum; currentNum <= maxNum; currentNum++) { for(int j = 0; j < currentNum; j++) { printf("%d", currentNum); } printf("\n"); }