- 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...
10 odpowiedzi
+ 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;
}
+ 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 ?!!
+ 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 😅
+ 2
Thanks you so much !!!
+ 2
But it is mandatory to use a function in it 😅
+ 2
Ohk!
+ 1
Show me what you tried ...
+ 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;
}
}
+ 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
+ 1
As I showed you in my earlier comment; use % not / in conditional `if` inside `check` function.
if( arr[ i ] % 2 == 0 )