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
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.
Mistake number 1 in the code
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.
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
Related
While I was writing the code, I had to print out total 25 numbers, so I put the number 25 inside the numberArray, however when I run the program, it dd not print out 25 numbers. Instead the number of the random numbers kept changing and did not stick to the specific value. What should I do in this case to make my code print out 25 numbers?
public static void printArray(int[] ear) {
System.out.println("ODD NUMBERS : ");
for (int e = 0; e<ear.length ; e ++) {
ear[e] = (int)(Math.random()* 100);
if(ear[e]%2!=0)
System.out.print(ear[e] + " ");
}
System.out.println("\n" + "EVEN NUMBERS : ");
for (int e = 0; e<ear.length ; e ++) {
ear[e] = (int)(Math.random()* 100);
if(ear[e]%2==0)
System.out.print(ear[e] + " ");
}
}
public static void main(String[] args) {
int[] numberArray = new int[25];
printArray(numberArray);
}
}
As stated in the code, you are only printing the ODD and EVEN numbers of the array. That's why you get different outcomes on each run.
If you change the code to print the array index instead of the value you can see that you are printing
ODD NUMBERS :
4 10 12 13 14 15 18 19 22 23 24
EVEN NUMBERS :
0 1 3 4 5 6 9 13 16 17 18 19 20 22 23
I have a program which generates rows of 6 numbers (int arrays).
I am passing the output to another program that sorts it with the BubbleSort algorithm and writes it into a textfile.
If the first program is used without passing it works fine, no repeated numbers no zeroes. but when sorted there are repeated numbers and even i have seen zeroes, the case of the zeroes i could not reproduce atm, but the double occuring numbers. Could it have something to do with multithreading/parallel processing or the environment in whicht it is executed and which consist of a amd multicore win 10 host and deb jessie guest.
java LottoArray | java BubbleSort>test2.txt //terminal
test2.txt
2 13 16 20 27 40
9 14 17 21 25 41
6 11 11 19 27 44
4 10 25 34 39 47
11 12 17 36 44 48
1 15 23 31 39 40
3 22 22 23 33 45
1 25 26 26 35 49
11 14 24 25 41 49
6 6 14 17 38 46
4 19 19 28 35 39
As you can see the sixs in the row before the last row are double and the 22s and the 11s.
public class LottoArray{
public static void main (String [] args){
for(int o=0;o<=10;o++){
int Reihe [] = new int [6];
int zahl;
int j=0;
int i= 0;
while(j<Reihe.length){
zahl = (int) (Math.random()*50);
boolean schonda = false;
while ( i<j){
if(Reihe[i]== zahl)
schonda=true;
i++;
}
if(schonda==false && zahl !=0){
Reihe[j]=zahl;
j++;}
}
for(int z=0;z<6;z++){
System.out.print(Reihe[z]+" ");
}
System.out.println();
}
}
}
public class BubbleSort {
public static void main(String args[]) {
int arr[]= new int[6];
while(!StdIn.isEmpty()){
for(int i=0;i<6;i++)
arr[i]= StdIn.readInt();
boolean getauscht;
do {
getauscht= false;
for (int i=0; i<arr.length-1; i++) {
if ( arr[i] > arr[i+1]) {
int tmp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = tmp;
getauscht = true;
}
}
}while(getauscht);
for (int i=0; i<arr.length; i++)
System.out.print(arr[i]+" " );
System.out.println();
}
}
}
if i use the code without the bubbleSort and stream the output into a textfile, there are no repeated numbers and no zeroes, as it should be not possible because i coded the condition if(schonda==false && zahl !=0)
15 2 20 5 26 34
13 6 15 33 12 37
44 17 16 23 40 25
25 47 10 43 40 44
25 29 3 30 10 41
32 1 23 35 43 28
9 34 28 32 33 25
5 46 31 16 25 9
9 13 16 18 40 5
29 15 16 2 16 15
34 33 44 13 43 48
has someone experiences with that kind of occurring numbers that should not ?
You problem is in this block of LottoArray:
int j=0;
int i= 0;
while(j<Reihe.length){
zahl = (int) (Math.random()*50);
boolean schonda = false;
while ( i<j){
if(Reihe[i]== zahl)
schonda=true;
i++;
}
if(schonda==false && zahl !=0){
Reihe[j]=zahl;
j++;
}
}
The first time you enter in the while (i<j){ loop from above (for the first element), both i and j are 0, so the loop is not executed.
The second time (checking the second number), i is 0 and j is 1, so the loop is executed and i is increased.
The third time (checking the third number), i is 1 and j is 2.
Same for the rest, i is always j-1.
This is a bug, as you are not starting the check on the first element. I suppose you are only getting duplicates using the BubbleSort out of luck, as the error is not there.
To fix this, initialize i inside the first while, in the same place as the schonda var, not above with j.
I am trying to shift elements in a 2D array (specifically a square matrix) so that the result is a "staircase" pattern:
Original:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Objective:
1 2 3 4 5
10 6 7 8 9
14 15 11 12 13
18 19 20 16 17
22 23 24 25 21
I've managed to write a program that can perform the task in a 1D array for the first line of the matrix (see below), but I can't seem to translate the code so that it functions in a 2D array.
How do I get my code to function in a 2D array in Java?
And, for later, how would I go about incrementing the change so that, as the row increases (from upper to lower levels of the matrix), the number of shifts also increases?
public class StaircaseMatrix
{
public static void main(String[] args)
{
int[] num = {1,2,3,4,5};
shiftRight(num);
System.out.println("After shifting the array is:");
for (int x = 0; x < num.length; x++)
System.out.print(num[x] + " ");
}
public static void shiftRight(int[] list)
{
int last = list[list.length - 1];
for (int j = list.length - 1; j > 0; j--) {
list[j] = list[j - 1];
}
list[0] = last;
}
}
You call shiftRight() 0 times for array2D[0], 1 time for array2D[1], 2 times for array2D[2], and so on, using a for loop.
I want to read all test cases in UVA Online Judge problem. Test cases are divided by a blank line. Example:
11
20
1
2
3
6
8
12
13
13
15
16
17
18
19
20
20
21
25
26
30
31
12
20
1
2
3
6
8
12
13
13
15
16
17
18
19
20
20
21
25
26
30
31
17
20
1
2
3
6
8
12
13
13
15
16
17
18
19
20
20
21
25
26
30
31
while(sc.hasNextLine())
{
numberOfYears = sc.nextInt();
numberOfPopes = sc.nextInt();
years = new ArrayList<Integer>();
for(int i = 0; i < numberOfPopes; i++)
{
years.add(sc.nextInt());
}
p = new ProblemPope(numberOfYears, numberOfPopes, years);
}
I know, that scanner waits for another line or wants CTRL+Z or CTRL+D. I just want to know if there is a way how to end it after reading all test cases.
Is there a way to do it by Scanner?
I think you are looking for this:
while (true) {
if(!sc.hasNext())
break;
...
}
or simply:
while(sc.hasNext()) {
...
}
you can use sc.close() to close the scanner from reading input
Your code might end in runtime error if you do not check hasNext(),hasNextLine() or hasnextInt() getting all inputs.
But
Normally the first most input will be the number of testcases in most of the online contests. so you can use it to your advantage like the following.
int testCaseCount= sc.nextInt();
for(int testCase=0; testcase < testCaseCount; testCase++){
numberOfYears = sc.nextInt();
numberOfPopes = sc.nextInt();
years = new ArrayList<Integer>();
for(int i = 0; i < numberOfPopes; i++)
{
years.add(sc.nextInt());
}
p = new ProblemPope(numberOfYears, numberOfPopes, years);
}
But if the input does not specify number of test cases, then it is not dynamically entered.
ie it is tested from a file. In such cases the sc.hasNextLine() should work.
if both are not working, please mention the link to the question, so that i can verify what the problem is.
I'm new to Java. I'm trying to make a triangular multiplication table that looks somewhat like this:
Enter # of rows 7
1 2 3 4 5 6 7
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
Each row/column has to be numbered, and I have no idea how to do this. My code is seemingly waaay off, as I get an infinite loop that doesn't contain the correct values. Below you will find my code.
public class Prog166g
{
public static void main(String args[])
{
int userInput, num = 1, c, d;
Scanner in = new Scanner(System.in);
System.out.print("Enter # of rows "); // user will enter number that will define output's parameters
userInput = in.nextInt();
boolean quit = true;
while(quit)
{
if (userInput > 9)
{
break;
}
else
{
for ( c = 1 ; c <= userInput ; c++ )
{ System.out.println(userInput*c);
for (d = 1; d <= c; d++) // nested loop required to format numbers and "triangle" shape
{
System.out.print(EasyFormat.format(num,5,0));
}
}
}
}
quit = false;
}
}
EasyFormat refers to an external file that's supposed to format the chart correctly, so ignore that. If someone could please point me in the right direction as far as fixing my existing code and adding code to number the columns and rows, that would be greatly appreciated!
Two nested for loops will do the trick:
for (int i = 1; i < 8; i++) {
System.out.printf("%d\t", i);
}
System.out.println();
for (int i = 1; i < 8; i++) {
for (int j = 1; j <= i; j++) {
System.out.printf("%d\t", i * j);
}
System.out.println();
}
OUTPUT
1 2 3 4 5 6 7
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49