- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C program to find marks of students for boys or girls

Suppose we have an array called marks, where some marks are given, all even indices marks like marks[0], marks[2] and so on are holding marks of boys and all even indexed marks are holding for girls. We have another input called gender. The value of gender is either 'b' or 'g', when it is 'b' we shall have to return the sum of all boys, and when it is 'g' return sum of marks for all girls. (Size of array is N)

So, if the input is like N = 9 marks = [8,5,2,6,7,5,9,9,7] gender = 'g', then the output will be 25 because 5 + 6 + 5 + 9 = 25.

To solve this, we will follow these steps −

- g_sum := 0
- b_sum := 0
- for initialize i := 0, when i < n, update (increase i by 1), do:
- if i mod 2 is 1, then:
- g_sum := g_sum + marks[i]

- Otherwise
- b_sum := b_sum + marks[i]

- if i mod 2 is 1, then:
- if gender is same as 'b', then:
- return b_sum

- return g_sum

## Example

Let us see the following implementation to get better understanding −

#include <stdio.h> #define N 9 int solve(int marks[], int n, char gender){ int g_sum = 0; int b_sum = 0; for(int i = 0; i < n; i++){ if(i % 2 != 0){ g_sum += marks[i]; }else{ b_sum += marks[i]; } } if(gender == 'b') return b_sum; return g_sum; } int main(){ int marks[N] = {8,5,2,6,7,5,9,9,7}; char gender = 'g'; int sum = solve(marks, N, gender); printf("%d", sum); }

## Input

{8,5,2,6,7,5,9,9,7}, 'g'

## Output

25

Advertisements