How to find ans for the problem(That's odd...) In c++? Please reply faster if you know. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?????

7th Dec 2020, 8:57 AM
RAVIKIRAN.D
RAVIKIRAN.D - avatar
19 Answers
+ 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
7th Dec 2020, 10:11 AM
Arsenic
Arsenic - avatar
+ 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
7th Dec 2020, 4:03 PM
Arsenic
Arsenic - avatar
+ 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
8th Dec 2020, 4:54 AM
Arsenic
Arsenic - avatar
+ 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”; }
7th Dec 2020, 4:40 PM
Yawar Abbas
Yawar Abbas - avatar
+ 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.
7th Dec 2020, 10:17 AM
Arsenic
Arsenic - avatar
+ 1
RAVIKIRAN.D that's where loops come into picture.
7th Dec 2020, 1:15 PM
Arsenic
Arsenic - avatar
+ 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.
8th Dec 2020, 3:40 AM
RAVIKIRAN.D
RAVIKIRAN.D - avatar
+ 1
Arsenic Thanks alot for your answer☺️☺️☺️
8th Dec 2020, 4:57 AM
RAVIKIRAN.D
RAVIKIRAN.D - avatar
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.
7th Dec 2020, 10:40 AM
Dikshit Rishi Jain
Dikshit Rishi Jain - avatar
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
7th Dec 2020, 1:04 PM
RAVIKIRAN.D
RAVIKIRAN.D - avatar
0
Arsenic can you pls tell me which loops I supposed to use
7th Dec 2020, 1:18 PM
RAVIKIRAN.D
RAVIKIRAN.D - avatar
0
Thanks alot I'll try my best.
7th Dec 2020, 4:14 PM
RAVIKIRAN.D
RAVIKIRAN.D - avatar
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.
8th Dec 2020, 3:46 AM
Yawar Abbas
Yawar Abbas - avatar
0
‎یاور بلتستانی‎ K I accept your second error but I'm fine in 1st one and but in output of (int all) I'm strucking.
8th Dec 2020, 3:52 AM
RAVIKIRAN.D
RAVIKIRAN.D - avatar
8th Dec 2020, 4:27 AM
Yawar Abbas
Yawar Abbas - avatar
0
‎یاور بلتستانی‎ output is not getting.
8th Dec 2020, 4:49 AM
RAVIKIRAN.D
RAVIKIRAN.D - avatar
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.
8th Dec 2020, 4:53 AM
RAVIKIRAN.D
RAVIKIRAN.D - avatar
0
RAVIKIRAN.D Finally you find solution In my program You have to input all 9 values then you will find correct answer
8th Dec 2020, 6:50 AM
Yawar Abbas
Yawar Abbas - avatar
- 2
فى تةيذسؤ
7th Dec 2020, 7:52 PM
حبيبي الغالي 32 Mmm
حبيبي الغالي 32 Mmm - avatar