sum of all integer numbers

来源:互联网 发布:三菱系统攻丝编程实例 编辑:程序博客网 时间:2024/05/22 22:37

sum of all integer numbers

时间限制:1000 ms  |  内存限制:65535 KB
难度:0
描述
Your task is to find the sum of all integer numbers lying between 1 and N inclusive.
输入
There are multiple test cases.
The input consists of a single integer N that is not greater than 10000 by it's absolute value.
输出
Write a single integer number that is the sum of all integer numbers lying between 1 and N inclusive.
样例输入
3
样例输出
6

代码实现:(注意要区分n大于0和小于0的情况)

#include<stdio.h>
int main()
{
    int n;
    long i,sum;
    while(scanf("%d",&n)!=EOF)
    {
        sum=0;
        if(n>0)
        {
            for(i=1;i<=n;i++)
                sum+=i;
        }
        else
        {
            for(i=1;i>=n;i--)
                sum+=i;    
        }
        printf("%d\n",sum);
    }
    return 0;
}


原创粉丝点击