Please check my code

Hi.
I am solving a very easy question (Problem Code: ADDNATRL)
https://www.codechef.com/CCSTART2/problems/ADDNATRL

I can’t seem to figure what am I doing wrong. The code seems right to me but on submission I am getting Wrong Answer.

#include
using namespace std;

int main() {
// your code goes here
int n;
cin>>n;
int sum=(n*( n + 1 ))/2;
cout<<sum;
return 0;
}

Just Run A Loop From 1 to N And Add All The Numbers Which Come In The Loop
And Also Dont Use Int Use long long
BTW Your Approach Is Also Correct
Code In C++

   int main() {
        // your code goes here
        long long n;
        cin>>n;
        long long sum=(n*( n + 1 ))/2;
        cout<<sum;
        return 0;
    }

Code In Java

import java.util.Scanner;

public class BitShifts {
    public static void main(String[] args) {
        Scanner scin = new Scanner(System.in);
        long n = scin.nextInt();
        System.out.println(n*(n+1)/2);
    }
}


Now Its Working

1 Like

you have to use long int for n and sum.

your logic is right
use long long instead of int as contraints are till 10^9 .it will work.

void sol() {

  ll n;
  cin>>n;
  cout << (n*(n+1)/2) << endl;

 }
1 Like

Thanks @meghnarai For :heart:

Mission Accomplished.

which mission?

@cubefreak777 Are You Going To Attempt May Long??

No I have my final exams in coming week.

@cubefreak777 Best Of Luck For Your Exams :100:

1 Like

I under stand the use of long for c++, but why the same approach is not working in Python 3.
Its integer size unlimited