I`ve got some problems here with homework and i can`t figure it out.You must input n numbers with no more than 4 digits.Find how many even numbers contains the longest sequence of even numbers.
exemple: n = 8 x = 5 3 4 6 8 2 7 9 seq must be 4
#include <iostream>
int sequ(int);
int main(int argc, charconst *argv[]) {
int n;
std::cout << sequ(n) << '\n';
return 0;
}
int sequ(int n){
int x, seq, seq1; // seq = the sequence of even numbers // seq1 is the variable where i deposit the first seq
std::cin >> n;// the x is the input number
while(n) {
std::cin >> x;
if (x % 2 == 0){
seq++;
if (seq >=2) {
seq1 = seq;
}
}
if (x % 2 == 1)
seq = 0;
n--;
}
if(seq1 >= seq) return seq1;
elsereturn seq;
}
Your code will be a lot easier to work with if you indent it to match the block structure.
I don't see anything in your code that looks at individual digits in x. It looks like you're just checking whether x itself is even.
Try creating a function int longestEvenSequence(int num) The eventual goal is to have it return the longest sequence of even digits in the number num. To start, just write code that extracts and prints the digits in num. Once that's working, modify the code to find the longest sequence of even digits. Once you have this working, use it in your seq() function to get the answer to the problem.