-
Integer problem
i had this question today on my introductory C programming course midterm.
Take 4 integers from user and display the number of odd or even integers present.
for example, if u enter 3,6,7,88 -- then the output should be 2 odd numbers and 2 even numbers....like this..
can sum1 help me with this??
i just cannot get the algorithm right, let alone the CODE!! im so frstrated...please help me
i can do until scanf, where the user inputs. then from the conditional statements,,,im stuCK!!!@~!
help me
hobilla:mad:
-
Start by writing something (with scanf, if you want) that will get all the required input and print it out without doing anything. Once you know that it is working, post it to make sure your potential cboard gurus agree, and we can proceed from there.
The key to distinguishing odd and even numbers is modulus (%) so this is actually a very easy task, algorithmically speaking. What you need to do is learn to use the appropriate commands, appropriately.
-
do it the same way you do it in your head. when you see an even number, add to the count of even numbers. when you see an odd number, add to the count of odd numbers.
Code:
int odd = 0, even = 0;
while (more numbers)
if (even) even++;
else odd++;
now just fill in the underlined parts. :)
-
In order to get rid of the unnecessary if-statement, one may as well want to use:
Code:
static int eo[2];
foreach(n in numbers)
eo[n%2]++;
But you can safely ignore this, hobilla.
Greets,
Philip
-
we didnt do while
so we only did if else and switch statement..
so how can i do it?
-
By writing it out on paper how you would do it if you were handed some numbers in real life.