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.
Related
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();
}
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
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 4 years ago.
Improve this question
if you have 2 arrays 1 to m and 1 to n in which both m and n are >= 1 and <= 1000, find how many ways you can get a sum of 10 with the two array lists
Ex:
Output: "What is m"
User Input: 7
Output: "What is n"
User Input: 5
Output: "There are 3 ways to get 10"
I'll probably get down voted for providing an answer, because you are providing no clear question, and you aren't showing any effort to solve your problem yourself before the community helps you.
What I'm able to decipher from your sample result is there is no need for ArrayList if you're given two inputs m and n.
From your inputs, take the largest number and run a loop from 1 to that number. Subtract that number from your sum and check if that difference is greater than 0 and less than the other number. If that's true you have a way to compute the sum of 10.
Something like:
public class StackOverflow {
public static void main(String args[]) {
int m = 7;
int n = 5;
int sum = 10;
// Take the larger of the two inputs
int limit = m > n ? m : n;
int count = 0;
for (int i = 1; i <= limit; i++) {
int possibleN = sum - i;
// Check that the possibleN is greater than 0 and less than n
if (0 < possibleN && possibleN <= n) {
System.out.printf("%d + %d = %d\r\n", i, possibleN, sum);
count++;
}
}
System.out.printf("There are %d ways to get %d\r\n", count, sum);
}
}
Results:
5 + 5 = 10
6 + 4 = 10
7 + 3 = 10
There are 3 ways to get 10
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();
}
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");
}
}
}