# Skenz - How To Wiki

### Site Tools

Action disabled: revisions
cs:c_language:while_cycle_1

# while cycle (Example 1)

Concepts:
While cycle, if instruction, module operator (%)

Text:
Implement a C program that:

• acquires 5 number from the keybord
• computes how many even and odd numbers are inserted
• prints the results of this computation

Solution:

while_cycle_1.c
```/* Insert 5 numbers using the keyboard and count how many even and odd numbers have been inserted */

#include <stdio.h>
#define NUMBERS 5

int main(){
int n;
int x;
int n_even, n_odd;

n=0;
n_even=0;
n_odd=0;

while(n<NUMBERS){
printf("Insert number %d: ", n);
scanf("%d", &x);

if (x%2 == 0) {
printf("Even!\n");
n_even = n_even+1;
}else{
printf("Odd!\n");
n_odd = n_odd+1;
}

n = n+1;
}

printf("EVEN: %d - ODD: %d\n", n_even, n_odd);

return 0;
}```

The program executes 5 times the block of code inside the while cycle that:

• asks the user to insert a number
```  scanf("%d", &x);
```
• checks if the number is even if (x%2 == 0) or odd (branch else of the if instruction)
```  if (x%2 == 0) {
/* Code executed if x is even */

}else{
/* Code executed if x is odd */
}
```
• in the case of an even, it increments the variable n_even, otherwise, in the case of an odd number, it increments the variable n_odd
• at the ends of the while cycle the program prints the results
```  printf("EVEN: %d - ODD: %d\n", n_even, n_odd);
```