Java Nested loop to print numbers in a descending triangle [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have to write a nested loop fragment that will have the following output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Numbers have to be in that exact order, i'm trying to use for loops but I can't really figure out how to remove the repeating numbers.
How would I achieve this using nested loops?
The output i'm currently getting looks like:
1
1 2
1 2 3
1 2 3 4
etc.

Nevermind, I just figured it out!
int x = 1;
for(int i = 0; i < 5; i++){
for(int j = 0; j <= i; j++ ){
System.out.print(x + " ");
x++;
}
System.out.println();
}

You want to have an external count, not base the value you're printing out on j. Thus increase the count in the nested loop every time and print that value.

Write a code something like below. I didn't check the syntax, that is the work for you.
for (int i = 0 ; i < k; i++){
for(int j = 0; j <= i; j++){
System.out.print(j + " ");
}
System.out.println();
}

Related

Write a program to print the following pattern using a nested loop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
The pattern of 2n+1 should be stair case
1
1 4
1 4 9
1 4 9 16
1 4 9 16 25
I'm trying to imply the 2n+1 equation into the for loop. I'm messing around for an hour but still can't think a way to solve this problem. This is what I have tried so far.
public class hw5{
public static void main(String[] args){
for(int i=1 ; i <= 5; i++){
for(int j=1; j <= 25; j = j + 3){
System.out.print(j);
}
System.out.println();
}
}
}
The series printed is just the square of integers starting at one. So just save each of the previous squares in a string.
String s = "";
int max = 5;
for(int i = 1; i <= max; i++) {
System.out.println(s+= (i*i)+" ");
}
prints
1
1 4
1 4 9
1 4 9 16
1 4 9 16 25
However, starting with 1, the sum of the consecutive odd numbers also form perfect squares. And 2n+1 is the representative form of an odd number for any integral value of n.
So you can also do it like this.
compute the sums of all the computed odd numbers in the loop
after each sum is computed, append to a string and print
int sum = 0;
String s = "";
int max = 5;
for (int n = 0; n < max; n++) {
sum += (2*n + 1);
System.out.println(s += sum + " " );
}
prints
1
1 4
1 4 9
1 4 9 16
1 4 9 16 25
for (int i=1; i<6; i++) {
for (int j=1; j<i+1; j++) {
System.out.print(j*j+" ");
}
System.out.println();
}

Trying to output 0 1 2 3 8 5 6 14 8 9 using a for loop in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is what I have so far:
for (int i = 0; i<= 9; i++){
int output = i;
if (i % 4 == 0){
output *= 2;
}
System.out.print(output + " ");
}
and it outputs this:
0 1 2 3 8 5 6 7 16 9
I'm not sure why it's multiplying the 8 and not the 7 like I want it to do.
It's supposed to output this:
0 1 2 3 8 5 6 14 8 9
Thanks for any feedback!
Try it like this.
for (int i = 0; i<= 9; i++){
int output = i;
if (i == 4 || i ==7){ // <-- when to double the value
output *= 2;
}
System.out.print(output + " ");
}

How to set counter with predefined variable in java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need to set counter with variable to start number as variable. e.g my variable "COUNT" could be any value between 1 to 12 and followed by remaining numbers. Consider this
if COUNT = 3 then my series of number should go like below
3 (Assigned run time variable)
4
5
6
7
8
9
10
11
12
1
2
Many thanks for help in advance.
There are a couple of ways to do this - you can play with the modulus operator, % or you can take advantage of the fact that a for-loop can increment more than one variable at a time. So you use one variable to control the loop count, and another for the print value:
public static void main(String[] args) {
int x=3;
for (int i=0; i<12; i++, x++) {
if (x>12) {
x=1;
}
System.out.println(x);
}
}
Here is the output:
...
3
4
5
6
7
8
9
10
11
12
1
2
Process finished with exit code 0
You can do this in single for loop like this.
public static void main(String[] args) {
int COUNT = 3;
for (int i=0; i<12; i++) {
if (i + COUNT > 12) {
System.out.println(i + COUNT - 12);
} else {
System.out.println(i + COUNT);
}
}
}
int countStart = 3;
for(int count = countStart; count < 13; count++){
System.out.println(count);
}
for(int count = 1; count < countStart; count++){
System.out.println(count);
}
The code starts from COUNT which is written here as countStart, and keeps printing until it reaches 12.
From here, a new loop runs from 1 to countStart, printing all the values
With only one loop
int count=3;
for(int i=count;i<count+13;i++){
if(i<13)
System.out.println(i);
else if(i>13){
System.out.println(i-13);
}
}
}
Here your count start from 3
it will iterate the loop through 13+count to print all value
after that it will check if the i vale lessthan 13 then it will print the i value else it print i-13 from 1,2 ... upto cont-1

Java questions using only for-loops [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm really new to Java programming so i was hoping if any of you could help me with this.
This question lets you practice with a single loop.
(a) Write a program OddSequencer.java that prints the following series 1 2 4 7 11 16 using a for-loop. Hint: Identify the pattern in this sequence first. In particular, what is the difference between two consecutive numbers in this sequence?
Sorry, i wasn't trying to just dump it on you guys. I get that for part (a) there's a pattern in the series of numbers in that it goes from 1 (1+1) -> 2 (2+2) -> 4 (4+3) -> 7 (7+4) -> 11 (11+5) -> 16
What i currently have is
public class OddSequencer {
public static void main(String [] args){
for(int i = 1; i <= 16; i) // i'm not sure what the last condition is supposed to be
System.out.println(i);
}
}
(b) Write a program called SumLoops.java with a main method that prints out the sum of multiples of 3 from 3 to 30 (inclusive of both 3 and 30) using a for-loop.
What i currently have is
public class SumLoops {
public static void main(String [] args){
for(int i = 3; i <= 30; i+=3)
System.out.println(i);
}
}
The output of your program should look like this:
D:\is200\lab1>java SumLoops
Sum of multiples of 3 from 3 to 30 = 165
D:\is200\lab1>
For a consider this :
/*Outputs a serious of 5 numbers, starting at 1.
The difference between one number to the next one is
a sequence of numbers, starting at 1 and incremented by 1
i.e: 1,2,3,4,5
*/
//represents the difference between one number and the next one.
//the first difference is 1.
int step = 1;
//represents the numbers to output. Initialized to first number
int outputNumber = 1;
//loop over all steps
for( step=1; step <= 6 ; step++){
//do output
System.out.print(" "+ outputNumber ) ;
outputNumber += step; //add step to get the next output number
}
for b:
//the sum of multiplies
int sumOfMultiples =0;
//loop that runs from i=1 as long as i*30 <=30
for(int i = 1; (3*i) <=30; i++){
sumOfMultiples += 3*i;
}
System.out.println("Sum of multiples of 3 from 3 to 30 = "+sumOfMultiples);
I hope that the comments make it clear.

Create triangle with "1" " 0" by for loop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How can I write a program in Java to make the following triangle?
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
Try:
String s = "";
for(int i = 0 ; i < nLines ; ++i) {
s = (i % 2 == 0 ? "1 " : "0 ") + s;
System.out.println(s);
}
I don't want to give you a solution, but there's some patterns you can see:
The number of digits is the same as the number of the line, assumign it starts in 1. For example, in the first line, you have 1 digit; in the second you have 2 digits.
If it's an odd line, the first digit is a 1; otherwise, it's a 0.
You always switch between 0 and 1, until you've reached the number of the line.
public class CurvedZebraTriangle{
public static void main(String []args){
int n=5;
for(int i = 0; i <= n; ++i)
{
for(int j = 0; j< i; j++)
System.out.print((i+j) % 2 == 0 ? "0 " : "1 ");
System.out.print("\n");
}
}
}

Categories

Resources