+ 3
How to find ans for the problem(That's odd...) In c++? Please reply faster if you know.
I'm very confused please help me. I clearly don't know how to do. We can use by modulo'%' . But how?????
20 ответов
+ 4
You need to add your attempt in question description if you want the answer.
as for the hint, it is already provided to you by Jay Matthews
+ 4
RAVIKIRAN.D if you really want to be spoon fed by others then here is the step by step instructions of have to do. But still you have to implement it on your own as I will not provide you with code till the time you don't post your attempt:-
Here's what you have to :-
1) read the first number to find out how many time you have to further take input
2) read that many time using loop(any will work {for/while}
3) as soon as you read one, check wether it is even or odd(using modulo or bitwise and operator)
4) if the number read is "even" then add it's value in grand total
5) display the grand total
+ 4
RAVIKIRAN.D nice to see that you attempted that question on your own.🙂
As for the problem in your code, it's just because you are printing the sum as soon as you are alterning it(means inside the loop).
According to the question, you have to display the sum at the end
Here is the fix👇
https://code.sololearn.com/c9V1m8sqMVVo/?ref=app
+ 3
RAVIKIRAN.D
You can use loops
int a[10];
cin>>a;
for(int i=0;i<10;i++)
{
if(a[i]%2==0)
cout<<”even”;
else
cout<<”odd”;
}
+ 2
Martin Taylor yes using bitwise operations are interesting and help a lot in places like competitive programming where decreasing your execution time is one of the key requirements.
+ 1
RAVIKIRAN.D that's where loops come into picture.
+ 1
#include <iostream>
using namespace std;
int N;
int all=0,sum=0;
int main(){
int a[9];
cin >>N;
for (int i=0;i<N;++i){
cin >>a[i];
}
for (int x=0;x<N;++x){
if(a[x]%2==0){
all+=a[x];
cout <<all;
}
}
return 0;
}
Arsenic Is my answer correct .cz I'm getting some differently but if output a[x],it is showing all even numbers.but in sum im lagging .help me.correct this one.
+ 1
Arsenic
Thanks alot for your answer☺️☺️☺️
0
The percentage sign (%) is used to find the remainder. For example: 3%2=1. Here, 3÷2=1 and the remainder is 1, so the value returned is 1.
0
Yeah I know these things before
That n%2=0,the ans is even and if it is n%2=1 the ans is odd.
but how to execute these in multiple inputs
0
Arsenic can you pls tell me which loops I supposed to use
0
Thanks alot
I'll try my best.
0
RAVIKIRAN.D
First and second error:
for (int i=0;i<9;i++)
Or N should intialize to 9 eg N=9;
There is no need to show zero in else statement.
0
یاور بلتستانی K I accept your second error but I'm fine in 1st one and but in output of (int all) I'm strucking.
0
RAVIKIRAN.D
Check out this
https://code.sololearn.com/cV510hnxAsGh/?ref=app
0
یاور بلتستانی output is not getting.
0
Arsenic
But when I substitute cout <<all as cout<<a[x],I'm getting all even numbers alone but when I'm executing cout<<all , I cnt get their sum.
0
RAVIKIRAN.D
Finally you find solution
In my program
You have to input all 9 values then you will find correct answer
- 2
فى تةيذسؤ