This is a companion discussion topic for the original entry at https://toph.co/p/odd-or-even
This is a companion discussion topic for the original entry at https://toph.co/p/odd-or-even
#include<stdio.h>
int main()
{
int N, i, sum1=0, sum2=0;
scanf("%d", &N);
for(i=1;i<=N;i+=2){
sum1+=i;
}
for(i=2;i<=N;i+=2){
sum2+=i;
}
printf("%d", (sum1-sum2));
return 0;
}
where is my mistake??
N can be as large as 10^18. You cannot store such a large number in an int
.
#include < iostream >
int main()
{
long long N; // i also tried with int type
std::cin >> N;
if(N % 2 == 0) {
std::cout << -N / 2 << std::endl;
}
else {
std::cout << (N + 1) / 2 << std::endl;
}
return 0;
}
// I can’t find the wrong. what’s the mistake. Is my algorithm wrong? Need help.
Why is there a minus sign?
if N is even , the result would be negative…result = (summation of odd numbers till N) - (summation of even number till N)
okk…my code is accepted . There shouldn’t be the minus sign . Thank you very much for this suggestion. I couldn’t understand the problem clearly. Thank you again.
Glad to help
whats wrong here?
#include<stdio.h>
int main()
{
int N,i,sum1=0,sum2=0;
scanf("%d",&N);
for(i=1;i<=N;i++)
{
if(i%2==1)
sum1=i+sum1;
else
sum2=i+sum2;
}
if(N%2==1)
{printf("%d",sum1-sum2);}
else
{printf("%d",sum2-sum1);}
return 0;
}
Try testing your code with the input 10000000000
.
a=int(input("Enter the value of N: ")
if N%2==0:
print("Even Number")
else:
print("Odd Number")
input N
if N%3==0:
print(“This is a Odd Number”)
else:
print(“This is a Even Number”)
Why I am getting compilation error?
Please share your submission ID.
#include <stdio.h>
void main()
{
int i, N, odd= 0, even= 0,res=0;
0<=N<=10^18;
scanf("%d", &N);
for (i = 1; i <= N; i++)
{
if (i % 2 == 0)
even = even + i;
else
odd = odd + i;
}
res=odd-even;
printf("%d\n", res);
}
whats wrong with the code?
So basically my problem is that the answer it gives seems to be correct, but after submitting it I get a message saying wrong answer. My code is:
#include <stdio.h>
int main()
{
long long N, sum1=0, sum2=0;
scanf("%I64d", &N);
while(N>=0)
{
sum1=sum1+N;
N--;
sum2=sum2+N;
N--;
}
if(sum1>sum2)
{
printf("%I64d\n", sum1-sum2);
}
else
{
printf("%I64d\n", sum2-sum1);
}
return 0;
}
#include<stdio.h>
int main()
{
long long int a,sum,sum1,sum2,sum3,even;
scanf("%lld",&a);
if(a%2==0)
{
sum1=(a*(a+1))/2;
even=a/2;
sum2=even*(even+1);
sum3=sum1-sum2;
sum=sum2-sum3;
}
else
{
sum1=(a*(a+1))/2;
even=(a-1)/2;
sum2=even*(even+1);
sum3=sum1-sum2;
sum=sum3-sum2;
}
printf("%lld",sum);
return 0;
}
Where is the problem in this code!!!
#include<stdio.h>
int main()
{
long long i,sum1=0,sum2=0,n,n1,n2;
scanf("%lld",&n);
if(n%2==1){
n1=(n-1)/2+1;
sum1=(n1*(2+(n1-1)*2))/2;
sum2=((n1-1)*(4+(n1-2)*2))/2;
printf("%lld",sum1-sum2);
}
else{
n2=(n-2)/2+1;
sum1=((n2-1)*(2+(n2-2)*2))/2;
sum2=(n2*(4+(n2-1)*2))/2;
printf("%lld",sum2-sum1);
}
return 0;
}
#include<stdio.h>
#include<conio.h>
main()
{
int A;
printf("Enter the value");
scanf("%d",&A);
if(A%2==0)
printf("%d is the even",A);
else
printf("%d is the odd",A);
getch();
}
why wrong answer?
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int n,sumOfEven,sumOfOdd,result=0;
while(1==scanf("%d",&n))
{
sumOfEven=0;
sumOfOdd=0;
for(int i=0; i<=n; i++)
{
if(i%2==0)
{
sumOfEven+=i;
}
else
{
sumOfOdd+=i;
}
}
result=abs(sumOfEven-sumOfOdd);
cout<<result<<endl;
}
return 0;
}