|
|
back to boardDiscussion of Problem 1068. Sumi got AC in C.. Posted by Suparna 11 Jan 2019 16:21 int num,n,i,sum=0; // read n;
//case 1 if(n>0){ for(i=1;i<=n;i++){ sum=sum+i; } printf("%d",sum); } //case 2 if(n<0){ for(i=(-1);i>=n;i--){ sum=sum+i; } sum=sum+1; printf("%d",sum); } //case 3 else if(n==0){ printf("1"); } } Re: i got AC in C.. Posted by mdatik 26 Aug 2019 23:16 when n<0,then sum=sum+1; why you using sum=sum+1; Re: i got AC in C.. #include<stdio.h> int main() { int num,n,i,sum=0; scanf("%d", &n); if(n>0){ for(i=1;i<=n;i++){ sum=sum+i; } printf("%d",sum); } if(n<0){ for(i=n;i<=1;i++){ sum=sum+i; }
printf("%d",sum); } else if(n==0){ printf("1"); } } |
|
|