Need help understanding why im getting it as wrong answer when i submit

why am i getting this error when i submit? sum of natural numbers.

Try

k = (n*(n+2))//2

As in python ‘/’ operator returns a float by default. ‘//’ operator stand for floor division and returns int.

1 Like

thanks for help

Main Method That Calculates It: n*(n+1)/2
My Submission Page
Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner scin = new Scanner(System.in);
        long n = scin.nextInt();
        System.out.println(n*(n+1)/2);
	}
}

C++

#include <iostream>
using namespace std;

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