java print a reverse triagle s with integers - java

I am trying to implement a java code to print a triangle in reversed order.This is my code.
public void drawPatternC(int num) {
//Loop through the lines from 1 to n
System.out.println("Pattern .C:" + "\u21B5");
for (int i = 1; i <= num; i++) {
// printing spaces, 2 at a time from j=1 to j= num-i
for (int j = 1; j <= (num - i); j++) {
System.out.print(" . ");
}
//Printing number increamentally from 1 to line number j
for (int j = 1; j <= i; j++) {
System.out.print(j + ". ");
}
System.out.println("\u21B5");
}
}
I want the result to look like
....1
...21
..321
.4321
but am getting a different i need help

You weren't looping the right way with your second loop
Replace
for (int j = 1; j <= i; j++)
with
for (int j = i; j > 0; j--)
Correction
public void drawPatternC(int num) {
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= (num - i); j++) {
System.out.print(" . ");
}
//Printing number increamentally from 1 to line number j
for (int j = i; j > 0; j--) {
System.out.print(j+". ");
}
System.out.println();
}
}
Output
. . . . 1.
. . . 2. 1.
. . 3. 2. 1.
. 4. 3. 2. 1.
5. 4. 3. 2. 1.

This code should fix your problem. Its a little more simplified.
public void drawPatternC(int num)
{
for(int i=1;i<num;i++)
{
for(int j=num;j>i;j--)
{
System.out.print(".");
}
for(int k=i;k>0;k--)
{
System.out.print(k);
}
System.out.println();
}
}
OUTPUT(when num = 5):
....1
...21
..321
.4321

I have made some changes in you code
public static void drawPatternC(int num) {
//Loop through the lines from 1 to n
System.out.println("Reverse triangle:");
for (int i = 1; i <= num; i++) {
// printing spaces, 2 at a time from j=1 to j= num-i
for (int j = 1; j <= (num - i + 1); j++) {
System.out.print(".");
}
//Printing number increamentally from 1 to line number j
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println("");
}
}

Related

Java: Formatting nested loops?

I am pretty new to Java and I wanted to make a staircase effect using loops, but adding an increasing amount of spaces to the string as you go down each row.
Heres my code-
for(int i = size; i>0; i--) {
for(int k = 1; k>size-1; k++) {
output+=" ";
}
for(int j = i; j>0; j--) {
output+=let;
}
output+="\n";
}
return output;
}
Ultimately the goal is to have it print this:
22222
2222
222
22
2
But mine prints this:
22222
2222
222
22
2
I'm sorry, I know this is beginner stuff but I don't know where else to go. Any help is appreciated. Thanks.
You need size - i spaces each time, so change for(int k = 1; k>size-1; k++) { to for(int k = 0; k<size-i; k++) {.
for(int i = size; i>0; i--) {
for(int k = 0; k<size-i; k++) {
output+=" ";
}
for(int j = i; j>0; j--) {
output+=let;
}
output+="\n";
}
Try this code:
int size=5;
for(int i = size; i>0; i--) {
for(int k = size-i; k>0; k--) {
System.out.print(" ");
}
for(int j = i; j>0; j--) {
System.out.print("2");
}
System.out.println("");
}
your second for loop condition is wrong.
convert for(int k = 1; k>size-1; k++)into for(int k = size-i; k>0; k--)
Try this:
for(int i=0; i<n; i++)
{
for(int j = 2 * (n-i); j >= 0; j--)
{
System.out.print(" ");
}
for(int j = 0; j<=i; j++)
{
System.out.print("2 ");
}
System.out.println();
}
Note that the int n is the number of lines you want to print
n = 5 -> 5 lines

how to print reverse number pattern triangle in java

This is my code:
for (int i = 4; i >= 1; i--) {
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int k = i; k <= 4; k++) {
System.out.print(k+"");
}
System.out.println();
}
Current output:
4
34
234
1234
Desired output:
1
21
321
4321
What changes are necessary in order for me to get the desired output as shown above?
Let the first loop (i) run from 1 to 4 and the second (j) from 4 to i.
This reverses your output.
You did every thing right, just the last for should have a very minor change:
for (int k = 5-i; k >= 1; k--){
Here you go:
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
for (int j = 4; j > i; j--) {
System.out.print(" ");
}
for (int k = i; k >= 1; k--){
System.out.print(k + "");
}
System.out.println();
}
}
Your loops are incorrect, you can refer the below code with inline comments:
for (int i = 1; i <= 4; i++) { //iterate from 1 to 4
//Loop from i+1 to insert spaces first
for (int j = i+1; j <= 4; j++) {
System.out.print(" ");
}
//Loop from i to insert the number next to each other
for (int j = i; j >= 1; j--) {
System.out.print(j);
}
System.out.println(); //insert a new line
}
for (int i = 1; i <= 4; i++)
{
for (int k = i; k <= 4; k++)
{
System.out.print(" ");
}
for (int j = 1; j < i; j++)
{
System.out.print(j);
}
System.out.println();
}

Using for loops to create an upside-down triangle [duplicate]

This question already has answers here:
Creating a triangle with for loops
(13 answers)
Closed 8 years ago.
Im a java student, and i'm have quite the hard time, trying to use for loops to create an upside down triangle.
This is what my code looks like now, a straight forward triangle. How can i make another one just like it, but upside down?
for (int i=1; i<20; i += 2)
{
for (int k=10; k < (0 - i / 2); k++)
{
System.out.print(" ");
}
for (int j=0; j<i; j++)
{
System.out.print("*");
}
System.out.println("");
}
Is that tricky?
Just change
for (int i=1; i<20; i += 2)
To
for (int i = 19; i >0; i -= 2)
Code.
for (int i = 19; i > 0; i -= 2) {
for (int k = 10; k < (0 - i / 2); k++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println("");
}
Out put:
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
int c = 2*lines;
for (int i = lines-1; i>=0; i--)
{
for (int j = i; j < lines; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= c; k++)
{
if (k % 2 == 0)
{
System.out.print(" ");
}
else
{
System.out.print(symbol);
}
}
System.out.print("\n");
c -= 2;
}
All you have to do is to change your 3rd for loop
for (int j=0; j<i; j++)
{
System.out.print("*");
}
In your code, you print 1 star, 3 stars, 5 stars and so on... (i stars actually)
To make it upside down, start the j at the max value and decrement it so you print n - i stars
for (int j = 20 - i; j > 0; j--)
{
System.out.print("*");
}
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
for (int i = 1; i < 20; i += 2) {
for (int k = 10; k < (0 - i / 2); k++) {
sb.append(" ");
}
for (int j = 0; j < i; j++) {
sb.append("*");
}
sb.append("\n");
}
System.out.println(sb.reverse());
}
However you are probably supposed to learn something about loops and the algorithm.

Number Patterns using loops in Java

I have been trying different variations of for loops and have no clue how to make these patterns:
Pattern 1
54321
5432
543
54
5
Pattern 2
1
12
123
1234
12345
Pattern 3
12345
2345
345
45
5
Pattern 4
1
123
12345
123
1
My code that almost matched pattern 1 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");
}
public class PrintPattern {
public static void main(String[] args){
printPattern1();
printPattern2();
printPattern3();
printPattern4();
}
public static void printPattern1(){
for (int i = 0; i<5; i++){
for(int j = 5; j>i; j--)
System.out.print(j);
System.out.println();
}
}
public static void printPattern2(){
for (int i = 0; i<5; i++){
for(int k = 0; k<4-i; k++)
System.out.print(" ");
for(int j = 1; j<=i+1; j++)
System.out.print(j);
System.out.println();
}
}
public static void printPattern3(){
for (int i = 0; i<5; i++){
for(int k = 0; k<i; k++)
System.out.print(" ");
for(int j = i+1; j<=5; j++)
System.out.print(j);
System.out.println();
}
}
public static void printPattern4(){
for (int i = 0; i<5; i++){
for(int k = 0; k<Math.abs(2-i); k++)
System.out.print(" ");
for(int j = 1; j<=5-2*Math.abs(2-i); j++)
System.out.print(j);
for (int p = 0; p<Math.abs(2-i); p++)
System.out.print(" ");
System.out.println();
}
}
}
Your inner for loop
for (int j = (rows + 1 - i) ; j > 0 ; j-- ){
System.out.print(j);
}
will always count down to 1, because it keeps going until j is zero. Also, the number that the current row starts on will depend on the current row, because you used i in your assignment to j. To get pattern 1 both of those things will have to change.
Since you posted an attempt for pattern one, I'll tell you a solution for pattern one -
int rows = 5; // <- Start at 5.
for (int i = rows; i > 0; i--) { // <- Use decrementing loop(s).
for (int j = rows; j > rows - i; j--) { // <- Start at 5 (again)
System.out.print(j);
}
System.out.println();
}
Output is pattern 1 in your question,
54321
5432
543
54
5
Try this:
public class NumberPattern {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int k = 2; k >= i; k--) {
System.out.print(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print(j);
}
System.out.println();
}
for (int i = 1; i <= 2; i++) {
for (int k = i; k > 0; k--) {
System.out.print(" ");
}
if (i % 2 != 0) {
for (int j = 1; j <= (2 * i + 1); j++) {
System.out.print(j);
}
} else {
for (int j = 1; j <= (i / 2); j++) {
System.out.print(j);
}
}
System.out.println();
}
}
}
public static void main(String[] args) {
int rows = 5;
System.out.println("------ PATTERN 1 ------");
for (int i = 1 ; i <= rows ; i++){
for (int j = rows; j >= i ; j--){
System.out.print(j);
}
System.out.println();
}
System.out.println("\n------ PATTERN 2 ------");
for (int i = 1 ; i <= rows ; i++){
int k;
for (k = rows ; k > i; k--){
System.out.print(" ");
}
for (int j = 1; j <= k ; j++){
System.out.print(j);
}
System.out.println();
}
System.out.println("\n------ PATTERN 3 ------");
for (int i = rows ; i >= 1 ; i--){
int k;
for (k = rows ; k > i; k--){
System.out.print(" ");
}
for (int j = 1; j <= k ; j++){
System.out.print(j);
}
System.out.println();
}
System.out.println("\n------ PATTERN 4 ------");
int whitespaces = rows/2;
for (int i = 1 ; i <= rows; i++){
// absolute value of whitespaces
int abs_whitespaces =
(whitespaces < 0 ? -whitespaces : whitespaces);
for (int j = 0 ; j < abs_whitespaces ; j++){
System.out.print(" ");
}
for (int j = 1 ; j <= rows - 2 * abs_whitespaces ; j++){
System.out.print(j);
}
whitespaces-=1;
System.out.println();
}
}
The first program is:
class timepass {
public static void main() {
for (int a = -1;a<=5;a++) {
for(int b = 5; b >= a;b--) {
System.out.print("*");
}
System.out.println();
// enter code here
}
}
}
The second program is:
class timepass {
public static void main() {
for(int i = 1;i<= 6;i++) {
for(int j = 1;j<= i ;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Triangle {
public static void main(String[ ] args){
Scanner scan = new Scanner(System.in);
System.out.println("enter no.of line you need");
int n = scan.nextInt();
for(int i=1;i<=n;i++){
for(int j=5;j>=i;j--){
System.out.print(j);
}
System.out.println(" ");
}}}

Java to print number triangle with nested loop

I am trying to print the following using a nested loop in Java:
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
but it's coming out like the following:
1 2 3 4 5 6
2 3 4 5 6
3 4 5 6
4 5 6
5 6
6
Here is my code:
for (int i = 1; i <= 6; i++) {
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int j = i; j <= 6; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
Any help would be appreciated. Thanks
int n = 7;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
System.out.println(" ");
}
for (int j = i; j <= 6; j++) {
System.out.println(j +" ");
}
}
Your Program should be -
for (int i = 1; i <= 6; i++) {
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int j = 1; j <= (6-i+1); j++)
{
System.out.print(j + " ");
}
System.out.println();
}
Thanks
Set this condition in inner second loop.
for (int j = 1; j <= 7 - i ; j++)
Edit :
Complete code is
for (int i = 1; i <= 6; i++) {
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int j = 1; j <= 7 - i ; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
for (int i = 2; i <= 7; i++) {
for (int j = 2; j < i; j++) {
System.out.print(" ");
}
for (int j = i; j <= 7; j++) {
System.out.print(j-1 + " ");
}
System.out.println();
}
This is giving the same output ... Please check
public static void main(String[] args)
{ int c=0;
for(int i=6;i>0;i--)
{
for(int k=0;k<c;k++)
{
System.out.print(" ");
}
for (int j=1;j<=i;j++)
{
System.out.print(j +" ");
}
c++;
System.out.println(" ");
}
}
}
Try this :
for (int i = 1; i <= 7; i++) {
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int j = 1; j <= 7-i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}

Categories