Please check my code

Hi.
I am solving a very easy question (Problem Code: 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;
}

1 Like

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

4 Likes

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;

 }
2 Likes

Thanks @meghnarai For :heart:

Mission Accomplished.

1 Like

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

Could u please tell why we have used long long rather than int?

One of the comments already explains the reason.

1 Like

can u provide python sol

1 Like