In this program, we will see even or odd number solution with use of function. We will write separate function which will be called from main method to find out even odd numbers.

## Pre-requisites:

Input output statements, main method, function, if else statement

## Approach:

First a function is declared outside main method after including header files in program. We will define this function after closing main method or can do declaration and definition at same time on top of main method.

Inside main method we will take user input and set it as limit and then pass this value to function as argument. The function definition will have logic to find even odd numbers. In our function for even odd numbers we will declare variables to store sum of even numbers and odd numbers respectively and counter variable i to be used in loop. We are finding out evens n odds below user input number so while loop will be used where we will iterate loop less than input number times and find out evens n odds sum.

While loop inside function will have condition that counter variable i to be less than equal to limit(user input). If else statement will be used inside while loop and if statement will work based on result of modulo division operation result. When numbers are modulo divided by 2 then if output returns no remainder then we will consider number as even else consider as odd. So when if condition is successful then number is even otherwise odd. inside if statement body, value of i will be added to variable sum_of_even whenever i hold even number. Inside else statement body, value of I will be added to sum_of_odd. At end of loop we will increment counter variable i and after while loop closes print sum of even and odd numbers.

## 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 |
#include<stdio.h> void evn_odd_f(int); int main() { int N; printf("\n Provide maximum limit:"); scanf("%d",&N); evn_odd_f(N); return 0; } void evn_odd_f(int v) { int i=0, sum_of_even=0,sum_of_odd=0; while(i<=v) { if(i%2==0) { sum_of_even += i; } else { sum_of_odd += i; } i++; } printf("\n sum of even numbers: %d",sum_of_even); printf("\n sum of odd numbers: %d",sum_of_odd); } |

### Output:

1 2 3 4 |
Provide maximum limit:8 sum of even numbers: 20 sum of odd numbers: 16 |