Solution - Timus Problem 1068. Sum

1068. Sum

Time limit: 2.0 second
Memory limit: 64 MB
Your task is to find the sum of all integer numbers lying between 1 and N inclusive.

Input

The input consists of a single integer N that is not greater than 10000 by it's absolute value.

Output

Write a single integer number that is the sum of all integer numbers lying between 1 and N inclusive.

Sample

inputoutput
-3
-5


সল্যুশন্সঃ
#include<iostream>
#include <cstdlib>
using namespace std;
int main()
{
    int n;
    cin >> n;
    if(n==0 || n==1)
        cout << 1 << endl;
    else
    {
        if(n>1)
        {
            int t=0;
            for(int i=1; i<=n; i++)
            {
                t+=i;
            }
            cout << t << endl;
        }else
        {
            int t=-1;
            n=abs(n);
            for(int i=1; i<=n; i++)
            {
                t+=i;
            }
            cout << -(t) << endl;
        }
    }
    return 0;
}

আলোচনাঃ
ছোট বেলায় কত চক পেন্সিল ফুরিয়েছি এই ১, ২ যোগ করে। আজ সেটা প্রোগ্রামে করতে হলো। :)

Comments

Popular posts from this blog

Solution - Codeforces Problem 327B - Hungry Sequence

Solution - Timus Problem 1293. Eniya

What is Data Mining? ডাটা মাইনিং কি?