'Java For Loop Repeating Sequence Triangle Program
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
Solution 1:[1]
The triangle is simply a triangle where the next number is greater by +5 than the previous number. So we need to increment 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.
Now the output also has one error. In the 7th line of output you posted the first value the inner loop will print is 16. This 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. Here is the solution with just minor changes that I've 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();
}
}
The output is also here:
CORRECT OUTPUT IN ACCORDANCE WITH THE CORERCTLOGIC
Hope you can are able to solve this now. I took the same inputs given at the start to avoid any problems. Anyways, All the best.
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | marc_s |
| Solution 2 |
