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 gender is same as 'b', then:
- 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
Related Articles
- In a college, out of 4320 students, 2300 are girls. Find the ratio of(a) Number of girls to the total number of students.(b) Number of boys to the number of girls.(c) Number of boys to the total number of students.
- In a school $\frac{4}{11}$ of the students are girls. If there are 490 boys. Find the number of girls.
- In a class of 52 students, the number of boys is $\frac{ 6}{7}$ of the number of girls find out the number of boys.
- What is the total number of students in a class with 28 boys and \( x \) girls?
- The number of boys and girls in the class are in the ratio of 7:5. The number of boys is 8 more than the number of girls. Find the number of students in the class.
- The marks obtained by 10 students in a test are $60,72,70,64,56,52,71,54,62,59$. Find the mean marks of the students. Find the mean marks of the students if 6 extra marks are given to each student.
- In school there are 1200 students. If the percentage of boys in a school is \( 60 \% \), then find the of girls.
- There are 20 girls and 15 boys in a class.(a) What is the ratio of number of girls to the number of boys?(b) What is the ratio of number of girls to the total number of students in the class?
- 10 students of Class X took part in a Mathematics quiz. If the number of girls is 4 more than the number of boys, find the number of boys and girls who took part in the quiz.
- In a class of 30 students, the number of girls is one-fifth of the number of boys. How many boys are there in the class?
- If the ratio of boys to girls is 12 : 5, and the total number of girls is 840. How many boys are there?
- In a Mathematics test, the following marks were obtained by 40 students. Arrange these marks in a table using tally marks.(a) Find how many students obtained marks equal to or more than \( 7 . \)(b) How many students obtained marks below 4?"
- The ratio of boys to girls in the sixth grade is $2:3$. If there are 24 boys, how many girls are there?
- In a class there are 48 boys and 60 girls. The class teacher wants to divide the students into smaller groups such that each group contains the same number of students and each group contains only boys or only girls. What is the minimum possible number of group that can be created ?
- In a class A of 25 students, 20 passed with \( 60 \% \) or more marks; in another class B of 30 students, 24 passed with \( 60 \% \) or more marks. In which class was a greater fraction of students getting with \( 60 \% \) or more marks?
Kickstart Your Career
Get certified by completing the course
Get Started