There is an array of n students( stu[n]).If gender is boy then my code adds
- for boy
b
, 2nd,4th,6th,........even position elements of array and - for girl
g
, 1st,3rd,5th....odd position elements of array.
1> Gender of boys denoted by b. 2> Gender of girls denoted by g.
Input info>>
- The 1st line contains n, denoting the number of students in the class, hence the number of elements in the array.
- Each of the subsequent lines contains the marks of n students .
- The last line contains gender b/g;
Output info>>
The output should contain the sum of all the alternate elements of marks as explained above.
#include <stdio.h>
int main() {
int n,i;
scanf("%d",&n);//n denotes number of students.
int stu[n],sum=0;
for(i=1;i<=n;++i)
scanf("%d",&stu[i]);//for accepting input in array.
char gen;
scanf("%s",&gen);//for accepting input gender b/g.
for(i=1;i<=n;i++){
if(gen=='g' && i%2!=0){ //girl g adds alternate odd position elements.
sum=sum+stu[i];
printf("%d",sum);
}
else if(gen=='b' && i%2==0){ //boy b adds alternate even position elements.
sum=sum+stu[i];
printf("%d",sum);
}
}
//code
return 0;
}
Sample Input
3 3 2 5 b
Sample Output 8
explanation>> marks=[3,2,5] and gender = b so it will add 3+5(even position 0,2 alternate elements). If gender in place of b is g then it will produce output = 2.
My code is shows output of 0 in all test cases.