import java.io.*;
public class twominusfourplussixminuseight
{
public static void main(String args[])
{ System.out.println("2-4+6-8...-20");
int a=1;
int s=0;
for(int i=2;i<=20;i+=2)
{
if(a%2!=0)
s+=i;
else
s-=i;
a++;
System.out.println(a+ " " +i+ " " +s);
}
System.out.println(s);
}
}
Tuesday, March 23, 2010
2 - 4 + 6 - 8......20
Subscribe to:
Post Comments (Atom)
the logic should be if (a%4!=0)then sum+=a
ReplyDeleteNice one...
Delete:)
No
DeleteHoof
DeleteJust use one loop for (i=2; i <=20;i=i+4)
ReplyDeletes=s+(i-(i+2));
This will work.
Thanks it helped me a lot in an easy way!^_^
DeletePlz show output
ReplyDeleteOUTPUT
Delete2-4+6-8...-20
2 2 2
3 4 -2
4 6 4
5 8 -4
6 10 6
7 12 -6
8 14 8
9 16 -8
10 18 10
11 20 -10
-10
the output instead of coming -10 is coming 6
ReplyDeleteWrong
ReplyDeleteTotally wrong
Delete👍👍👍☺👍👍👍👍👍☺
ReplyDelete🤘🤘🤘🤙🤙
ReplyDeleteThis comment has been removed by the author.
ReplyDeletepublic class Sum_Series
ReplyDelete{
public static void main(String[] args) {
int s = 0;
for (int i = 2; i <= 20; i = i + 2) {
if (i % 4 == 0) {
s = s - i;
} else {
s = s + i;
}
}
System.out.println("The sum of the series is: "+s);
}
}
That's how you do it. Hope it helps :)
I think the logic should be if(i%4==0) this will be the right answer.....
Delete