+ 3

Write a program that prints the first 100 members of the sequence 2, - 3, 4, -5, 6, -7, 8.

Can we work on this? What could be the possible approach? The problem is, the c# program should print the first 100 members of the sequence. Implying that it is an infinite sequence to be continuous with the '...' sign after the 100th element. Good luck!

18th Apr 2018, 9:21 AM
Samuel DANISON
Samuel DANISON - avatar
7 Respuestas
+ 3
Which language, Javascript? var i; for (i = 2; i <= 100; i++) { if (i % 2 == 0) document.write(i+"<br>"); else document.write (i*-1+"<br>"); }
18th Apr 2018, 9:36 AM
Luigi
Luigi - avatar
+ 3
#Python oneliner print(*[i*(1,-1)[i%2] for i in range(2,103)])
18th Apr 2018, 12:37 PM
Louis
Louis - avatar
+ 1
Hi ThiemTD In your code i = b, but "b" what does it do? Missing data entry? Or "b" is equivalent to any value you want? =)
18th Apr 2018, 12:44 PM
Luciano Ariel Caputi
Luciano Ariel Caputi - avatar
+ 1
@Luciano Ariel Caputi, Yes, It is my mistake. Thanks for your opinion.
19th Apr 2018, 2:32 AM
ThiemTD
ThiemTD - avatar
18th Apr 2018, 9:27 AM
ThiemTD
ThiemTD - avatar
0
Hi, I would solve this by incrementing x=x+2 and using an additional variable (y) and either ad the character “-“ as prefix or, better, make y negative by formula *(-1) My code suggestion for Java: (apologies for bad formatting :) public class Program { public static void main(String[] args) { int x=2; int y; while (x<=100){ y=(x+1)*(-1); System.out.println(x); System.out.println(y); x=x+2; } } }
18th Apr 2018, 6:02 PM
Michael Hof
Michael Hof - avatar
0
This is my solution in C#. static void Main(string[] args) { // This is about the best take. for (int i = 2; i < 102; i++) { if(i % 2 == 0) Console.Write("{0}, {1}, ", i, (i + 1)*(-1)); } }
20th Apr 2018, 5:25 AM
Samuel DANISON
Samuel DANISON - avatar