Why Add natural no. solution is not being accepted ?

Hey, i tried the math formula as well as for loop but when i submit it shows wrong answer can someone help me with this ? CODE:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    int n = input.nextInt();
    int sum = (n*(n+1))/2;
    System.out.println(sum);
}

}

Try this : declare sum variable as long (instead of int) because integer overflow may occur while multiplying n with (n+1)

Can you share the link to the problem ?

Nope. Tried that also not working not taking submission says wrong answer. same issue with sum is everywhere. Triangle with angle.
https://www.codechef.com/viewsolution/54591123

If you are talking about this question (CodeChef: Practical coding for everyone), Triangle with angle solution code(C++):

    int a,b,c;
	cin>>a>>b>>c;
	if(a+b+c==180 && (a!=0 && b!=0 && c!=0)){
	    cout<<"YES";
	}
	else cout<<"NO";

Sum is Everywhere question (CodeChef: Practical coding for everyone ), you are using wrong formula in your code. (see below code in C++)

    long long int n ; cin>>n;
    long long int odd= n*n;
    long long int even = n*(n+1);
    
    cout<<odd<<" "<<even<<endl;

And whenever you post the doubt, share the problem link as well :slight_smile:

1 Like

Hey it worked the problem indeed was of data type as you said before but i was taking only the result variable in long. All three of the questions had the same issue.

Thank you. :smiley:
P.S. New to this platform did not know how to share links. Will keep it in mind.

1 Like