Hello, tell me which of the two codes is more correct, or what can be redone or added in the code.
one)
The program should display the first 55 elements of the sequence.
1 3 5 7 9 11 13 15 17 ...
First code:
public class Test { public static void main (String args[]) { int sum = 1; int count = 0; for (int i = sum; i <= 333; i += 2) { sum = i; count++; if (sum == 111) break; System.out.println (count + " элемент - " + sum); } System.out.println ("Первые " + (count - 1) + " элементов"); } }
Second code:
public class Test { public static void main(String args []) { int sum = 0; int count = 0; for (int i = sum; i <= 54; i++) { sum = 2 * i + 1; count++; System.out.println(count + " элемент - " + sum); } System.out.println("Первые " + count + " элементов"); } }
2) Prompt and by this code that can be added or altered.
The program should display all non-negative elements of the sequence.
90 85 80 75 70 65 60 ...
public class Test { public static void main(String args[]) { final int first = 90; final int second = -90; int count = 0; for(int i = first; i > second; i-=5) { count++; if(i == 0)break; System.out.println(count + " элемент - " + i); } System.out.println("Все неотрицательные элементы 90 85 80 75 70 65 60 …."); } }
3) Prompt and by this code that can be added or altered.
The program should display the first 20 elements of the sequence.
2 4 8 16 32 64 128 ...
public class Test { public static void main(String args[]) { final int a = 1; int sum; int count = 0; sum = (int) Math.pow(a,2); for(int i = 1; i <= 20; i++) { sum*=2; count++; System.out.println(count + " элемент - " + sum); } System.out.println("Первые " + count + " элементов"); } }
PS I compiled everything and started the code, asking for advice on what can be completed or redone, as it will be more correct.
Tell me more on the code.
Display all the terms in the sequence 2an-1–1, where a1 = 2, which are less than 10,000.
Here I wrote the code with a for loop, it seems to work. First code
public class Test { public static void main(String args []) { for(int a = 2, i = 1; i < a; a = 2 * a - 1,i++) { if(a <=10000) { System.out.print(a + " "); } } } }
With a for loop, it outputs like this: 3 5 9 17 33 65 129 257 513 1025 2049 4097 8193
The second code with a while loop.
public class Test { public static void main(String args []) { int a = 2; while(true) { if(a <= 10000) { a = 2 * a - 1; System.out.print(a); } } } }
With a while loop, it outputs like this: 3 5 9 17 33 65 129 257 513 1025 2049 4097 8193 16385
Tell me how to write with a while loop.
Tell me by code that I can not figure it out correctly! everything seems to be logically correct
public class Test { public static void main(String args []){ for(int a = -166, i = 1; a > 9 && a < 100; a = 2 * a + 200, i++){ System.out.println(a + " "); } } }
Must output, but does not display the result on the screen all the two-digit terms of the sequence!