C program to find sum of even and odd numbers using function

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:

Output:

Leave a Reply

%d bloggers like this: