Print inverted half pyramid as shown:
1111
222
33
4
My code is:
public class HW5 {
//5. Print Inverted Half Pyramid.
public static void main(String[] args) {
int n = 4;
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}
}
}
Above code gives following output:
4444
333
22
1
How do I print the required output as shown above?
You need to swap the for loops. The outer loop is the number to print and the inner loop is the number of times to print it.
public class HW5 {
public static void main(String[] args) {
int n = 4;
for (int j = 1; j <= n ; j++ ) {
for (int i = n - j + 1; i >= 1; i--) {
System.out.print(j);
}
System.out.println();
}
}
}
Above code gives following output:
1111
222
33
4
Related
for a school project, I am trying to make an inverted half pyramid
my code is currently this
public static void main(String[] args) {
int rows = 5;
for(int i = rows; i >= 1; --i) {
for(int j = 1; j <= i; ++j) {
System.out.print(j + " ");
}
System.out.println();
}
}
with this output:
12345
1234
123
12
1
desired output:
54321
=4321
==321
===21
====1
Update (based on the updated requirement):
You need a loop to print the = equal to (rows - row number) times.
public class Main {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; --i) {
for (int j = i; j < rows; j++) {
System.out.print("=");
}
for (int j = i; j >= 1; --j) {
System.out.print(j);
}
System.out.println();
}
}
}
Output:
54321
=4321
==321
===21
====1
Original answer:
Your inner loop should be
for (int j = i; j >= 1; --j)
i.e. for each row, it should start with the row number (i.e. i) and go down up to 1.
It is straight forward you will have to change to things: your inner loop and you will have to move println statement inside the loop
//code
public static void main(String[] args){
int rows = 5;
for (int i = rows; i >= 1; --i){
for(int j = i; j >= 1; --j)
System.out.print(j + " ");
System.out.println();
}
}
I created the code that should print a pattern like
12345
2345
345
45
5
I have the code written below, the logic works fine in python but in java the output is different.
class Testing{
public static void main(String args[])
{
for (int i = 1; i<6;i++)
{
for (int j =0; j<i-1;j++)
{
System.out.print(" ");
}
while (i < 6){
System.out.print(k);
System.out.println();
i++;
}
}
}
}
The output is just 12345. I don't understand why does it iterate over first for loop for only once.
Use another variable for while control.
public class Testing {
public static void main(String args[]) {
int k;
for (int i = 1; i < 6; i++) {
for (int j = 0; j < i - 1; j++) {
System.out.print(" ");
}
k = i;
while (k < 6) {
System.out.print(k);
k++;
}
System.out.println();
}
}
}
You can see this in this link
this will show you :
12345
2345
345
45
5
Note: When 'while loop' increases. That increases i value bigger than 6. So next time it terminates the outer loop. That was your mistake.
package com.appointment.api;
class Testing {
public static void main(String args[]) {
for (int i = 1; i < 6; i++) {
System.out.println();
for (int j = 0; j < i - 1; j++) {
System.out.print(" ");
}
int x = i;
while (x < 5) {
System.out.print(i);
x++;
}
}
}
}
Following is a java-8 implementation of the problem:
IntStream.rangeClosed(1, MAX)
.forEach(i -> IntStream.rangeClosed(1, MAX)
.mapToObj(j -> j == MAX ? j + "\n" : j >= i ? j : " ")
.forEach(System.out::print)
);
Set MAX = 5 and it will print your pattern.
Output:
12345
2345
345
45
5
I am learning Java for myself and have following problem:
Task: Write (explicitly) nested for loops to produce the following output:
5
5 5
5 5 5
5 5
5
My idea:
public class Exercises {
public static void main (String [] args) {
for (int line = 1; line <= 3; line++) {
for (int i = 1; i<= -2*line+6; i++){
System.out.print(" ");
}
System.out.println("5");
}
for ( int line = 4; line <= 5; line ++){
for (int i = 1; i <= 2*line-6; i++){
System.out.print(" ");
}
System.out.println("5");
}
}
}
My output:
5
5
5
5
5
I don't know how to get nested loops there. Can you give me not the answer, but some tips?
Try this solution:
/**
*
* #author Adil
*/
public class Exercises {
public static void main (String [] args) {
for (int i = 1; i <= 3; i++) {
for(int s = 4; s > i; s--) {
// add spacing
System.out.print(" ");
}
for (int j = 1; j < i; j++) {
//display/add star
System.out.print("5");
}
// add new line
System.out.println("");
}
for (int i = 1; i <= 3; i++) {
for (int s = 1; s < i; s++) {
// add spacing
System.out.print(" ");
}
for (int j = 4; j > i; j--) {
//display/add star
System.out.print("5");
}
// add new line
System.out.println("");
}
}
}
It will produce the following output:
5
55
555
55
5
Try this solution give below:
public class Exercises {
public static void main(String[] args) {
int noOfColumns = Integer.parseInt(args[0]); // Enter no of 5's in last
// column
int noOfRows = noOfColumns;
int index = noOfColumns;
int mid = noOfRows / 2;
for (int i = 0; i < noOfRows; i++) {
if (i <= mid) {
index = index - 2;
} else {
index = index + 2;
}
for (int j = 0; j < noOfColumns; j++) {
if (j % 2 == 0 && j >= index) {
System.out.print("5");
} else {
System.out.print(" ");
}
} // End of inner loop
System.out.println("");
} // End of outer loop
}
}
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("");
}
package mypack;
public class Loop {
public static void main(String[] args) {
for(int i=0;i<=8;i++){
for(int j=1;j<=i+1;j++){
System.out.print(j);
}
System.out.println();
}
for(int i=8;i>=1;i--){
for(int j=1;j<=i;j++){
System.out.print(j);
}
System.out.println();
}
}
}
output::
1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678
1234567
123456
12345
1234
123
12
1
i want the mirror image of it on right side.
can someone help me with this.
What you can do is to count the number of spaces before you add the reflection of the numbers to form a butterfly effect in the console, using the String.format
sample:
public static void main(String[] args) {
int j;
for (int i = 0; i <= 8; i++) {
for ( j = 1; j <= i + 1; j++) {
System.out.print(j);
}
System.out.print(String.format("%"+((10 - j)*2 != 0 ? (10 - j)*2 : "")+"s", ""));
for ( j = i + 1; j >= 1; j--) {
System.out.print(j);
}
System.out.println();
}
for (int i = 8; i >= 1; i--) {
for ( j = 1; j <= i ; j++) {
System.out.print(j);
}
System.out.print(String.format("%"+((10 - j)*2 != 0 ? (10 - j)*2 : "")+"s", ""));
for ( j = i; j >= 1; j--) {
System.out.print(j);
}
System.out.println();
}
}
results:
1 1
12 21
123 321
1234 4321
12345 54321
123456 654321
1234567 7654321
12345678 87654321
123456789987654321
12345678 87654321
1234567 7654321
123456 654321
12345 54321
1234 4321
123 321
12 21
1 1
one easy way is to do:
public static void main(String[] args) {
int limit = 10;
String left = "%-9s";
String right = "%9s";
for (int i = 0; i < limit; i++) {
StringBuilder b = new StringBuilder();
for (int j = 1; j < i + 1; j++) {
b.append(j);
}
System.out.println(String.format(left, b.toString())
+ String.format(right, b.reverse().toString()));
}
// skipping the redundant line from last syso in above loop
for (int i = limit - 2; i > 0; i--) {
StringBuilder b = new StringBuilder();
for (int j = 1; j < i + 1; j++) {
b.append(j);
}
System.out.println(String.format(left, b.toString())
+ String.format(right, b.reverse().toString()));
}
}
idea is to print the sequence (left aligned) and then print its reverse(right aligned) in each iteration.
package mypack;
public class Loop {
public static void main(String[] args) {
for(int i=0;i<=6;i++){
for(int j=1 ;j<=i+1;j++ ){
System.out.print(j);
}
System.out.print("\t");
for (int j=0;j<6-i;j++)
System.out.print(" ");
for (int k=1;k<=i+1;k++)
System.out.print(k);
System.out.println();
}
for(int i=6;i>=0;i--){
for(int j=1;j<=i+1;j++){
System.out.print(j);
}
System.out.print("\t");
for (int j=0;j<6-i;j++)
System.out.print(" ");
for (int k=1;k<=i+1;k++)
System.out.print(k);
System.out.println();
}
}
}
got it by myself,thank you all..