+ 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!
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>");
}
+ 3
#Python oneliner
print(*[i*(1,-1)[i%2] for i in range(2,103)])
+ 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? =)
+ 1
@Luciano Ariel Caputi,
Yes, It is my mistake. Thanks for your opinion.
0
Please see more in: https://code.sololearn.com/cqHjVUyD58Zd/#cpp
Thanks
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;
}
}
}
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));
}
}