Hello guys!
I’m trouble with this question.
CodeChef: Practical coding for everyone .
Could you please help me to figure out the problem?
import java.util.Scanner;
public class Main {
public static int evenSum(int n) {
int sum = 0;
int i = 1;
int cursor = i;
for( i = 1; i <= n ; i++) {
cursor = i * 2;
sum = sum+cursor ;
}
return sum;
}
public static int oddSum(int n) {
int sum = 0;
int i = 1;
int cursor = i;
for(i = 1; i <=n ; i++) {
cursor = (i * 2) -1;
if( cursor %2 ==1){
sum = sum + cursor;
}
}
return sum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println();
int n = sc.nextInt();
System.out.println( oddSum(n) + " " +evenSum(n));
}
}
You should provide the link to your problem and your submission .
I wrote this code, output is coming it is still not accepting
#include
using namespace std;
int main() {
int n;
cin>>n;
int evenSum = 0;
int evenCurr = 2;
int oddCurr = 1;
int oddSum = 0;
for(int i=1; i<=n; i++){
evenSum += evenCurr;
evenCurr +=2;
}
for(int i=0; i<n; i++){
oddSum += oddCurr;
oddCurr +=2;
}
cout<<oddSum<<" "<<evenSum;
return 0;
}
@anushka5653 change all the Datatypes from int to long(see constraints)…
#include <bits/stdc++.h>
using namespace std;
int main()
{
long n;
cin >> n;
long evenSum = 0;
long evenCurr = 2;
long oddCurr = 1;
long oddSum = 0;
for (long i = 1; i <= n; i++)
{
evenSum += evenCurr;
evenCurr += 2;
}
for (long i = 0; i < n; i++)
{
oddSum += oddCurr;
oddCurr += 2;
}
cout << oddSum << " " << evenSum;
return 0;
}
2 Likes
Can’t we do like this???
int main() {
// your code goes here
int N, even_sum, odd_sum;
cin >> N;
odd_sum=N*N;
even_sum=odd_sum+N;
cout << odd_sum << " " << even_sum;
return 0;
}
Yeah it worked, Thanks : )
I was trying to solve this question but couldn’t solve but now after changing the datatypes, I am able to solve this
Thanks
plz write the data type long instead of long.