|
|
back to boardDiscussion of Problem 1068. Sumwhere is wrong !! already it is working in codeblocks #include<stdio.h> int main(){ int n,sum=0,i; scanf("%d",&n);- if(n>0){ sum=(n*(n+1))/2; } else if(n<0){ sum=(n*(-1*n+1))/2; } printf("%d",sum); } Re: where is wrong !! already it is working in codeblocks Maybe for N < 0 the formula is SUM = ((2-N)*(N+1))/2 Edited by author 06.11.2017 19:47 Re: where is wrong !! already it is working in codeblocks The first term is N The last term is 1 So the average is (1+N)/2 There are (-N) terms below zero And there are two more terms They are 0 and one So there are (2-N) total terms So the sum is average by total number of terms Re: where is wrong !! already it is working in codeblocks Case "N == 0" isn't processed Re: where is wrong !! already it is working in codeblocks Posted by jim 22 Feb 2018 07:10 thank you for your hint! Re: where is wrong !! already it is working in codeblocks Posted by WeXoB 20 May 2020 08:51 Edited by author 20.05.2020 08:52 Edited by author 20.05.2020 08:52 Re: where is wrong !! already it is working in codeblocks Posted by Juhi 15 Jun 2020 11:20 But that should not make a difference |
|
|