A few things to start with
- Make sure your code is neatly formatted and indented, as below, to make it easy to read. If it's easy to read, it's harder to make mistakes, and easier to find and fix them when you do.
- Don't include conio.h. It's very outdated, non-standard, and you aren't using anything it provides anyway.
- You don't actually need math.h for this, don't include that either.
- You don't really need stdlib, but I will suggest you keep it. Then, you can use return EXIT_SUCCESS; at the end of main instead of return 0;. They behave the same, but using EXIT_SUCCESS is much clearer. Clear code is A Good Thing.
- void main is wrong. Read this: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com.
- Don't use magic numbers like 50. Instead, define a constant with a descriptive name, like MAX_ELEMENTS
- Give variables more descriptive names. n should be num_elements. a could be elements. i is fine, it's a basic loop counter/array index, which is one of the few cases where single-letter variable names are a good idea.
- If you have restrictions on input values, such as < 50 elements, please validate the user's input. That is, if they enter 51 or 500, display an error and ask them again. A do-while loop is good here since it always executes once, and you always want to give the user a chance to input first. If you don't validate their input, your program (as currently written) will attempt to store data in array elements that don't exist. This will access memory your program doesn't own, which results in undefined behavior (see this link), meaning anything, or nothing, can happen. Though most likely, your program will crash with a seg fault.
- Small nit, when asking the user for input, use language that suggests they should do something. "Number of elements" sound like you (the program) will tell the user something. "Please enter the number of elements (max %d): " is much better. Note the %d will be filled in by providing the comment.
- Generally it is better to use double, it offers more precision. Only use float if you really, really need to save memory, which does not seem to be the case for you.
Implementing the above (leaving a bit of work for you to do in a few places.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_ELEMENTS 50
int main(void)
{
int num_elements, i;
double elements[MAX_ELEMENTs], sum;
do {
printf("Please enter number of elements (max %d): ", MAX_ELEMENTS);
scanf("%d", &num_elements);
if (num_elements < 1 or > 50)
print error, please input a value between 1 and MAX_ELEMENTS
} while (num_elements is < 0 or > 50);
printf("Enter elements:\n");
for(i=0;i<n;i++)
{ printf("Please enter a[%d]=", i);
scanf("%lf",&a[i]); // <-- note, you must use %lf to read in a double with scanf, though you can print it with just %f (same as float) using printf
}
// And here what ???
return EXIT_SUCCESS;
}
Okay, now we can get to the "And here what ???" part.
When solving a programming problem, you should work stuff out on paper first. Start with the requirements in English (or your native language). Figure out how you, a human, would solve that problem. Pay attention to the process, every little calculation -- it will form your algorithm. Then, implement each little step in your program. Let's look at your requirements:
1. sum of odd numbers
2. from even positions
Even numbers implies there is no remainder when you divide the number by 2, so we must find a "remainder" operator, also known as a "modulus" operator. Do you know which operator that is? Check your class notes or textbook, or Google "C operators" if you're not sure.
1. Sum the odd numbers. So you need to check if the contents of each array spot are odd.
2. Even positions. You need to check if the array index is an even number.
If both of those conditions are true, then you can add that array spot to the sum. Note, you should make sure your sum starts with a sensible value -- recall what you have (probably) learned about initializing variables. Note I emboldened the word "if", probably for a good reason.
EDIT: Note that, if you must sum only odd numbers, it implies that the array must contain integers. A floating point number (i.e. non-integer) does not really have the concept of odd and even.