Home > Software design >  Java For Loop Repeating Sequence Triangle Program
Java For Loop Repeating Sequence Triangle Program

Time:02-27

Can someone help me write the program in Java that prints the following triangle? I'm having trouble coming up with a solution.

Example User input:

Enter increment amount: 5 Enter number of terms in sequence: 7 The number of rows is always set at 10.

What I have written so far:

import java.util.Scanner;
public class Drawtriangle {

    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        System.out.print("Enter increment amount:");
        int incAmt=scanner.nextInt();
        System.out.print("Enter number of terms in the sequence:");
        int numOfTerms=scanner.nextInt();
        int number=1;
        for(int row=1; row<=10;row  ) {
            
            for(int col=1; col<=row; col  ) {
                
                System.out.print(number);
            }
            System.out.println();
        }
        scanner.close();
    }

}

Output:

1
6 11
16 21 26
31  1   6   11
16 21 26 31 1
6   11  16 21 26 31
16 11  16  21 26 31 1
6   11  16 21 26 31 1 6
11 16 21 26 31 1 6  11 16
21 26 31  1  6  11 16 21  26 31 

CodePudding user response:

Simple logic to print the pattern using for loop

import java.util.Scanner;
public class DrawTriangle {

    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        var out = System.out;
        System.out.print("Enter increment amount:");
        int incAmt=scanner.nextInt();
        System.out.print("Enter number of terms in the sequence:");
        int numOfTerms=scanner.nextInt();
        int number=1;
        int count = 0;
        for(int row=0; row<10;row  ) {
            for(int col = 0;col<=row;col  ){
                out.print(number " ");
                number  = incAmt;
                count   ;
                if(count>=numOfTerms){
                    number = 1;//initialising number with 1 if number of terms it has reached
                        count = 0;//resetting counter
                }
            }
            out.println();
        }
        scanner.close();
    }

}

Output:

$ javac DrawTriangle.java && java DrawTriangle 
Enter increment amount:5
Enter number of terms in the sequence:7
1 
6 11 
16 21 26 
31 1 6 11 
16 21 26 31 1 
6 11 16 21 26 31 
1 6 11 16 21 26 31 
1 6 11 16 21 26 31 1 
6 11 16 21 26 31 1 6 11 
16 21 26 31 1 6 11 16 21 26 

CodePudding user response:

ok so I ma new to Stack Overflow so I might not know the professional language of giving answers but here goes nothing :)

The triangle is simply a triangle where the next number is greater by 5 then the previous number , so we need to increament number by 5 each time something is printed out of the loop.

In your code you took number = 1 before entering the loop but then didn't mention any changes it must go through in order to print this triangle.

Mistake number 1 in the code Now the output also has one error, in the 7th line of output u posted the first value the inner loop will print is 16, which is not true as after printing 31, the loop will start again from 1 and increment by 5 again, i think there was a space between 1 and 6 of 16 in the 7th line..so here is the solution with just minor changes i come up with.

import java.util.Scanner; public class Drawtriangle {

public static void main(String[] args) {
    Scanner scanner=new Scanner(System.in);
    System.out.print("Enter increment amount:");
    int incAmt=scanner.nextInt();
    System.out.print("Enter number of terms in the sequence:");
    int numOfTerms=scanner.nextInt();
    int number=1;
    for(int row=1; row<=10;row  ) {       
        for(int col=1; col<=row; col  ) {     
            System.out.print(number " ");
            number = number   5 ;
            if(number>31){
                number = 1;
            }
        }
        System.out.println();
    }
    scanner.close();
}

}

and the output is also here

CORRECT OUTPUT IN ACCORDANCE WITH THE CORERCTLOGIC

sorry if i made it long, i am not aware about how to answer on stackoverlow,to be honest, this is my first ever answer on Stackoverflow, still learning, anyways , hope u can are able to solve this now, i took the same inputs given at the start to avoid any problems. Anyways, All the best.

  • Related