Simple Adding

版权声明:本文为博主原创文章,采用“署名-非商业性使用-禁止演绎 2.5 中国大陆”授权。欢迎转载,但请注明作者姓名和文章出处。 https://blog.csdn.net/njit_77/article/details/79734867

Challenge

Using the C# language, have the function  SimpleAdding(num) add up all the numbers from 1 to  num. For example: if the input is 4 then your program should return 10 because 1 + 2 + 3 + 4 = 10. For the test cases, the parameter  num will be any number from 1 to 1000. 
Sample Test Cases

Input:12

Output:78


Input:140

Output:9870

Simple Adding算法求数的和(等差数列)

        public static int SimpleAdding(int num)
        {
            int sum = 0;
            while (num > 0)
            {
                sum += num--;
            }
            num = sum;
            return num;
        }





猜你喜欢

转载自blog.csdn.net/njit_77/article/details/79734867