1001---Sum Problem

http://acm.hdu.edu.cn/showproblem.php?pid=1001
 
Problem Description
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).

In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
 

Input
The input will consist of a series of integers n, one integer per line.
 

Output
For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.
 

Sample Input
1
100
 

Sample Output
1
 
5050
 

Author
DOOM III
 

Recommend
We have carefully selected several similar problems for you:  1090 1003 1091 1004 1092 
 
两种方法:
 1 #include<stdio.h>
 2 int main()
 3 {
 4     int a,i,sum;
 5     while(scanf("%d",&a)!=EOF)
 6     {
 7         sum=0;
 8         for(i=1;i<=a;i++)
 9         {
10             sum=sum+i;
11         }
12         printf("%d\n\n",sum);
13     }
14     return 0;
15 } 
View Code

 

 1 #include <stdio.h>
 2 int sum(int);
 3 
 4 int main(){
 5     int input,output;
 6     while (scanf("%d",&input)==1)
 7     {
 8         output = sum(input);
 9         printf("%d\n\n",output);
10     }
11     return 0;
12 }
13 int sum(int n){
14     int sum,i;
15     sum=0;
16     for (i=1;i<=n;i++)
17     {
18         sum+=i;
19     }
20     return sum;
21 }
View Code

 

posted @ 2016-07-24 09:13  W-ning  阅读(109)  评论(0)    收藏  举报