
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Finding even and odd numbers in a set of elements dynamically using C language
Problem
To compute sum of even numbers and odd numbers in a set of elements using dynamic memory allocation functions.
Solution
In this program, we are trying to find even and odd numbers in a set of numbers.
The logic used to find even numbers in a set elements is given below −
for(i=0;i<n;i++){ if(*(p+i)%2==0) {//checking whether no is even or not even=even+*(p+i); //calculating sum of even all even numbers in a list } }
The logic used to find odd numbers in a set elements is given below −
for(i=0;i<n;i++){ if(*(p+i)%2==0) {//checking number is even or odd even=even+*(p+i); } Else {//if number s odd enter into block odd=odd+*(p+i); //calculating sum of all odd numbers in a list } }
Example
#include<stdio.h> #include<stdlib.h> void main(){ //Declaring variables, pointers// int i,n; int *p; int even=0,odd=0; //Declaring base address p using malloc// p=(int *)malloc(n*sizeof(int)); //Reading number of elements// printf("Enter the number of elements : "); scanf("%d",&n); /*Printing O/p - We have to use if statement because we have to check if memory has been successfully allocated/reserved or not*/ if (p==NULL){ printf("Memory not available"); exit(0); } //Storing elements into location using for loop// printf("The elements are :
"); for(i=0;i<n;i++){ scanf("%d",p+i); } for(i=0;i<n;i++){ if(*(p+i)%2==0){ even=even+*(p+i); } else{ odd=odd+*(p+i); } } printf("The sum of even numbers is : %d
",even); printf("The sum of odd numbers is : %d
",odd); }
Output
Enter the number of elements : 5 The elements are : 34 23 12 11 45 The sum of even numbers is : 46 The sum of odd numbers is : 79
- Related Articles
- Even numbers at even index and odd numbers at odd index in C++
- How to separate even and odd numbers in an array by using for loop in C language?
- Find elements of an Array which are Odd and Even using STL in C++
- Largest Even and Odd N-digit numbers in C++
- Count number of even and odd elements in an array in C++
- Count subarrays with same even and odd elements in C++
- What are even and odd numbers?
- Absolute Difference of even and odd indexed elements in an Array (C++)?
- The sum of two prime numbers $(>2)$ is(A) odd (B) even (C) prime (D) even or odd
- Absolute Difference of even and odd indexed elements in an Array in C++?
- Swap Even Index Elements And Odd Index Elements in Python
- Missing even and odd elements from the given arrays in C++
- Finding the only even or the only odd number in a string of space separated numbers in JavaScript
- Sorting odd and even elements separately JavaScript
- Finding all the n digit numbers that have sum of even and odd positioned digits divisible by given numbers - JavaScript

Advertisements