How to print strings in multiple lines in a decreasing order? - java

I'm using Java 8 and I need to get this output:
XXXXX
XXXX
XXX
XX
X
where "X" is a string.
I wrote a simple code:
String s = new String ("X");
int j = 5;
for (int i = 0; i<5; i--)
{
System.out.println(s);
j--;
if (j < 1)
break;
Naturally, get this:
X
X
X
X
X
I understand that I need to somehow make Java repeat printing the string i times (in a loop I assigned for i), but don't know how (neither repeat nor \i didn't work).
What's the best way to do it?
Thanks!

All you need to do is:
final String s = "X";
for (int i = 5; i > 0; i--) {
for (int j = 0; j < i; j++) {
System.out.print(s);
}
System.out.println();
}
which outputs:
XXXXX
XXXX
XXX
XX
X

You can use a recursive print operation to achieve this without loops.
import java.util.Collections;
public class Test {
public static void main(final String[] args) {
recursiveTriangle(10);
}
public static void recursiveTriangle(final int length) {
if (length <= 0 ) {
return;
}
System.out.println(String.join("", Collections.nCopies(length, "X")));
recursiveTriangle(length-1);
}
}

You can use while loop inside for
String s = "X";
for (int i = 5; i > 0; i--) {
int j=0;
while(i>j++)
System.out.print(s);
System.out.println();
}

concat here is another option:
String s = "";
for (int i = 5; i>0; i--) {
String s1 = "";
for (int j = i; j > 0; j--){
s1 += s.concat("X");
}
System.out.println(s1);
}

Related

Different variations for using loops to a pattern in java

I have been trying different variations of for loops and have no clue how to make these patterns:
Pattern
1
121
12321
1234321
My code is the following but doesn't work like the example above.
for (int i = 1 ; i <= rows ; i++) {
for (int j = (rows + 1 - i) ; j > 0 ; j-- ) {
System.out.print(j);
}
System.out.print("\n");
}
Your code prints only the suffix of each line, you are missing to write 12....i for each line.
In addition, the loop should start from i, not from rows-i+1.
for (int i = 1 ; i <= rows ; i++) {
//add an inner loop that prints the numbers 12..i
for (int j = 1 ; j < i ; j++ ) {
System.out.print(j);
}
//change where j starts from
for (int j = i ; j > 0 ; j-- ) {
System.out.print(j);
}
System.out.println(""); //to avoid inconsistency between different OS
}
First note that 11*11 = 121, 111*111=12321, etcetera.
Then that 10n - 1 is a number that consists of n 9's, so (10n - 1)/9 consists of n 1's.
So we get:
int powerOfTen = 1;
for (int len = 0; len < 5; len++)
{
powerOfTen = powerOfTen*10;
int ones = (powerOfTen-1)/9;
System.out.println(ones*ones);
}
Code explains everything!
public static void main(String[] args) {
String front = "";
String back = "";
int rows = 5;
for (int i = 1; i <= rows; i++) {
System.out.println(front+i+back);
front += i;
back = i + back;
}
}
Try this one: it may seems too much looping, but yet easy to understand and effective.
public static void main(String[] args) {
int rows=5;
int i,j;
for(i=1;i<=rows;i++)
{
/*print left side numbers form 1 to ...*/
for(j=1;j<i;j++)
{
System.out.printf("%d", j);
}
/*Print the middle number*/
System.out.printf("%d", i);
/*print right numbers form ... to 1*/
for(j=i-1;j>0;j--)
{
System.out.printf("%d", j);
}
System.out.println("");
}
}
int n=0;
for(int m =0; m<=5; m++){
for(n= 1;n<=m;n++){
System.out.print(n);
}
for(int u=n;u>=1;u--){
System.out.print(u);
}
System.out.print("");
}

How do I print this pattern in java?

I'm not able to print this pattern:-
90
890
7890
67890
567890
4567890
34567890
234567890
1234567890
I,ve already tried this:
class loop1
{
public static void main(String args[])
{
for(int i=1;i<10;i++)
{
for(int j=9;j<10;j++)
{ System.out.print(j);
j--;
}
System.out.println("");
}
}
}
but get an output of unlimited 9, a never ending loop.
try this
public static void main(String args[]) {
for (int i = 1; i < 10; i++) {
for (int j = i; j > 0; j--) {
System.out.print(10 - j);
}
System.out.println(0);
}
}
Try this its very simple
working example is here
StringBuffer s = new StringBuffer("0");
for(int i = 9; i > 0; i--) {
s.insert(0, i);
System.out.println(s);
Result
90
890
7890
67890
567890
4567890
34567890
234567890
1234567890
for(int i=1;i<10;i++) {
for(int j=10-i;j<10;)
System.out.print(j++);
System.out.println("0");
}
Try this:
StringBuffer s = new StringBuffer("0");
for(int i = 9; i > 0; i--) {
s.append(i);
System.out.println(s);
}
Your outer loop is looping the wrong way; for a working solution, it should be going from 9 down to 1. Then, your inner for loop would be looping up to 9:
for(int i = 9; i > 0; i--) {
for(int j = i; j < 10; j++) {
System.out.print(j);
}
System.out.println(0);
}
The 0 can't be part of the loop because it doesn't fit with the 1-9 pattern. However, you could print the last digit of each number and cut the 0 part of the println statement:
for(int i = 9; i > 0; i--) {
for(int j = i; j < 10; j++) {
String number = j + "";
System.out.print(number.substring(number.length() - 1));
}
System.out.println();
}
Maybe i am late but i have to post this because of my love for loops
public class SeriesLoop {
public static void main(String a[]){
for(int i=9;i>=1;i--){
for(int j=i;j<=10;j++){
System.out.print(j%10);
}
System.out.println();
}
}
}
DEMO
Cheers

Drawing a grid using nested loops

I am trying to draw a grid that looks like this:
1
12
123
1234
12345
123456
1234567
12345678
123456789
Here is my code:
public class shape {
public static void main(String[] args){
int number = 1, newNumber, zMax = 1;
String numString = "1";
for (int i = 1; i <= 9; i++){
for (int z = 0; z < zMax; z++){
System.out.print(numString);
number = number + 1;
numString += Integer.toString(number);
}
System.out.println("");
if (zMax <= 9)
zMax++;
}
}
}
It prints out something like this:
1
12123
121231234
12123123412345
etc
It is on the right track but I can't figure out what is going wrong... please help!
EDIT: Miss-understood the question, corrected it.
The reason your numbers repeat is your second loop. you either need to reinitialize numstring or reuse the old one and only add the new number.
public class shape { public static void main(String[] args){
String numString = "";
for (int i = 1; i <= 9; i++){
numstring = numstring + i;
System.out.println(numstring);
}
}
int start = 1;
int max = 10;
for(int i = 1; i < max; i++){
for(int j = 1; j <= i; j++){
System.out.print(j);
}
System.out.println("");
}

for loop to generate "1,4,9,16,25,36,49,64,81,100"

how can I write a for loop to give this output? I was thinking a nested loop?
for (int i = 0; i < 100; i++){
for (int j = 0; j < i; j++) {
but i don't know how to go from there?
thanks
I won't give out the answer but I'll give you a hint.
That is a list of the first 10 perfect squares. So you just need one loop to go through 10 values and get their square.
try this... ;)
for (int i = 1; i <= 10; i++){
int result = i * i;
System.out.printLn(result);
}
for loop to generate “1,4,9,16,25,36,49,64,81,100”
class series
{
public static void main(String[]args)
{
int i,j;
for(i=1; i<=10; i++) {
j=i*i;
System.out.println(j);
}
}
}
You have to iterate the numbers, and then append the list:
for (int i =1; i <= 10; i++) {
System.out.print(i*i + " ");
}
class forloops
{
public static void main ()
{
for (int i = 1; i <= 20; i = i + 1)
{
System.out.println (i * i + "ans");
}
}
}

java for loop pyramid

I want to create a loop with a string so that with each successive loop, the last and first characters from the previous line will not be displayed. I'm also trying to print the length of each loop next to each line. It will look like an upside-down pyramid.
I had this:
for(int scounter = fullName.length(); scounter > 0; scounter--)
for (String name : fullName)
for(int counter = 0; counter < fullName.length(); counter++)
System.out.println(scounter + "[ " + fullName.substring(0, fullName.length() counter)+ " ]");
It prints something like this:
24******
24****
24**
24*
Yet I'm looking for something similar to this:
7*******
5*****
4***
1*
String str = "*******";
for (int i = 0, l = str.length(); i <= l/2; i++) {
String line = str.substring(i, l - i);
System.out.printf("%" + (i + 1) + "d%s\n", line.length(), line);
}
This will print:
7*******
5*****
3***
1*
I'm assuming you meant 3 instead of 4 in your example, that is, that you want to decrement by 2.
I started working on this problem and found my solution to be slightly different from Joao's. Hope this helps!
public class pyramid
{
public static void main(String[] args)
{
for(int i=0, sz=args[0].length();i<sz; ++i,--sz)
{
System.out.printf("%"+(i+1)+"d%s\n", sz-i, args[0].substring(i,sz));
}
}
}
Invocation as per request:
java pyramid abcdefg
7abcdefg
5bcdef
3cde
1d
java pyramid abcdef
6abcdef
4bcde
2cd
Your example does not match the words of your question, so here's a method that behaves according to your words as I understand them:
public void pyramid(String text) {
int len = text.length();
int start = 0;
while (start < len) {
for (int i = 0; i < start; ++i) {
System.out.print(" ");
}
System.out.println(text.substring(start, len));
++start;
--len;
}
}
for(int i = 1; i<=4; i++) {
for(int k = 3;k>=i; k--){
System.out.print(" ");
}
for(int j = 1; j<=i; j++){
System.out.print("*");
}
for(int n = 2; n<=i;n++){
System.out.print("*");
}
System.out.println(" ");
}
for(int m = 1 ;m<=3; m++){
for(int o = 1;o<=m; o++){
System.out.print(" ");
}
for(int p = 3;p>=m;p--){
System.out.print("*");
}
for(int q = 2;q>=m;q--){
System.out.print("*");
}
System.out.println();
}
}
}

Categories

Resources