Seris of numbers as input and output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Seris of numbers as input and output

How to write a program that takes series of numbers as input and display 1 if it is even and 0 If odd using functions. Plz provide code if possible...

3rd Jul 2021, 3:58 AM
Viraj
Viraj - avatar
10 Answers
+ 1
In that case #include <iostream> int main() { int numbers = 0; cin >> numbers; // how many numbers? for( int i = 0, n; i < numbers; i++ ) { cin >> n; // read <i>-th number if( n % 2 == 0 ) { cout << 1 << endl; // even } else { cout << 0 << endl; // odd } } return 0; }
4th Jul 2021, 5:14 AM
Ipang
+ 2
#include <iostream> using namespace std; void show (int[],int); int main() { int numbers[] = {166,27,367,466,56}; int length =5; show (numbers,length); return 0; } void show (int numbers [],int length) { for(int counter =0;counter <length;counter ++) if (numbers[counter] / 2!=0) { cout<<bool (numbers[counter] / 2!=0)<<endl; } else { cout<<bool (numbers[counter] / 2==0)<<endl; } } Bro this is what I have tried. I have written it but it's giving 1 for all values and it's also not taking input from user. Please anyone tell me what should I do ?!!
4th Jul 2021, 2:39 AM
Viraj
Viraj - avatar
+ 2
Wow! thanks dude! But I need a program that takes input from user and outputs 1 if it is even and 0 if odd and user decides how many numbers to enter 😅
4th Jul 2021, 5:03 AM
Viraj
Viraj - avatar
+ 2
Thanks you so much !!!
4th Jul 2021, 5:20 AM
Viraj
Viraj - avatar
+ 2
But it is mandatory to use a function in it 😅
4th Jul 2021, 5:21 AM
Viraj
Viraj - avatar
+ 2
Ohk!
4th Jul 2021, 6:06 AM
Viraj
Viraj - avatar
+ 1
Show me what you tried ...
4th Jul 2021, 1:44 AM
Ipang
+ 1
Ok bro, I appreciate your try 👍 We use modulo operator % to check even/odd numbers, so in the for...loop in `show` function check it like this if( numbers[ counter ] % 2 == 0 ) { cout << 1 << endl; // even } else { cout << 0 << endl; // odd } If you want, we can print 1 or zero as numbers are entered. So we may not really need an array. int n; while( cin >> n ) { if( n % 2 == 0 ) { cout << 1 << endl; } else { cout << 0 << endl; } }
4th Jul 2021, 4:10 AM
Ipang
+ 1
#include <iostream> using namespace std; void check (int[],int ); int main() { int size; cout<<" how many numbers you want to enter ? "; cin>>size ; int arr[size]; for(int i=0;i++;i<size) { cin>>arr[i]; } check (arr,size); } void check (int arr[],int size) { for(int i =0;i <size; i++) if (arr[i] / 2==0) { cout<<" 1 "<<endl; } else { cout<<" 0 "<< endl; } } I did like this but it didn't work
4th Jul 2021, 5:39 AM
Viraj
Viraj - avatar
+ 1
As I showed you in my earlier comment; use % not / in conditional `if` inside `check` function. if( arr[ i ] % 2 == 0 )
4th Jul 2021, 6:01 AM
Ipang