You have to use the modulo function (
%
). For example is 13 mod 5 (
13%5
) = 3, because 3 is the offcut of the division from 13 by 5. In your case, you have to use this function in an if-clause to check whether the number is even or odd. So you have to use input_no mod 2 (
input_no%2
). Then you get this if-clause:
1 2 3 4 5 6
|
if((input_no%2)!=0)
{
//Do whatever you want to do with the ODD NUMBERS
}else{
//Do whatever you want to do with the EVEN NUMBERS
}
|
This is the solution for the first aspect (the even and odd numbers). For the second (negative integers) you only have to use another if-clause. This one checks whether the number is bigger or equals zero or if it's lower. The code is really easy:
1 2 3 4 5 6
|
if(input_no>=0)
{
//Do something with this POSITIVE NUMBER
}else{
//Do something with this NEGATIVE NUMBER
}
|
But how I see, you have already managed to do this. But I have found a critical error: What - do you think - does the program if you enter more than 100 numbers? (I know it's unrealistic, but possible.) The program will crash. So I'd make sure that you don't allocate more numbers than you have space in the array.
(at lines 15 and 20)
1 2 3 4
|
for(int i=0;i<n&&i<100;i++)
{
...
}
|
This little code above should manage this. See the difference? I added a
&&i<100
in the conditional part of the for-loop.
A second error first appeared to me when I entered this to my DEV-C++ compiler. It says:
Line 4: 'main' must return 'int' |
As you see, the return value of your
main()
is
void
, not
int
. So change this, please.
What I also missed was that you use
cin
/
cout
/etc instead of
std::cin
/
std::cout
/etc. You either have to use
using namespace std;
at the beginning of your code or - everytime when you call cin or cout or... - use
std::
.
And, to your other problem...
priyanshm wrote: |
---|
Then the screen shows if you want to exit press 0
But if i press any other number the program continues and again asks to input the no. of numbers |
...in my version (already corrected), the program doesn't continue if I press 0 and enter. Might be that it was in connection with these errors.
By the way, I see what you mean with that the program outputs the wrong results. As I entered
Enter the no. of numbers you want to input:
5
Number 1: -1
Number 2: 6
Number 3: 5
Number 4: 4
Number 5: 2 |
the results were these:
positive even: 6225932
positive odd: 5
negative: -1 |
By the way, the last one is correct, but only this one. I guess it might be that you did not initialize your array. You know, something like this:
1 2
|
int array[5];
for(int i=0;i<5;i++) array[i]=0;
|
If you don't initialize your variables, you don't know what's in there.
After all these explanations, I'll give you the corrected version of your program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int num[100],i,sum1,sum2,sum,n,m;
m=1;
while(m!=0)
{
sum=0;
sum1=0;
sum2=0;
cout<<"Enter the no. of numbers you want to input: "<<endl;
cin>>n;
for(i=0;i<n&&i<100;i++)
{
cout<<"Number "<<i+1<<" : ";
cin>>num[i];
}
for(i=0;i<n&&i<100;i++)
{
if(num[i]<0)
{
sum1=sum1+num[i];
}
else if(num[i]>=0)
{
if(num[i]%2==0)
{
sum=sum+num[i];
}
else if(num[i]%2==1)
{
sum2=sum2+num[i];
}
}
}
cout<<"Sum of positive even integers is: "<<sum<<endl;
cout<<"Sum of positive odd integers is: "<<sum2<<endl;
cout<<"Sum of negative integers is: "<<sum1<<endl;
cout<<"If you want to terminate the program press 0: ";
cin>>m;
}
system("PAUSE");
return 0;
}
|
With this code you get the right results. =)
And by the way, this forum is not for homework. So it would be far easier to get an answer if you don't write that it's for a homework. ;)