# Write a C program to separate even and odd numbers of an array and put them in two separate arrays.

Write in C language sum of odd and even number a program that:

1º) Ask for a number (whole data) by keyboard.

2nd) Repeat step 1, while the number entered is different from zero.

3rd) Show on the screen the sum of the odd and even numbers entered by the user.

On the screen you will see, for example:

On-screen display of the program Sum of even and odd numbers, solved in C language
Solution: Nesting an if inside a while loop
/ * Program: Sum of even and odd numbers * /

#include
#include

int main ()
{
int number, pairs, odd;

printf (“\ n Enter a n% integer number (0 = End):”, 163);
scanf (“% d”, & number);

pairs = 0;
odd = 0;

while (number! = 0)
{
if (number% 2 == 0)
pairs + = number;
else
odd + = number;

printf (“\ n Enter a n% integer number (0 = End):”, 163);
scanf (“% d”, & number);
}

printf (“\ n The sum of the pairs is:% d”, pairs);
printf (“\ n \ n The sum of the odd is:% d”, odd);

getch (); /* Pause */

return 0;
}
Note that, only in the case that the first number entered by the user is a zero, the while loop will not iterate any time.

### 2 Responses

1. 2017

[…] Previous story Write a C program to separate even and odd numbers of an array and put them in two se… […]

2. 2017

[…] Write a C program to determine a given matrix is a sparse matrix. says: […] Previous story Write a C program to separate even and… […]

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!