please can some one guide me, to the answer

Write a C program to enter 15 numbers as an input from the
keyboard and program will find and print odd numbers and
their average.

i have studied
while and do while loop
for loop
if and else if
switch

Answers were Sorted based on User's Feedback



please can some one guide me, to the answer Write a C program to enter 15 numbers as an input fro..

Answer / daniel

Here is my (hopefully) not so very complicated piece of code:

#include <stdio.h>
#define NUMBERS 15

int main(){
int numbers[NUMBERS]; // array containing the numbers introduce on the keboard
int count = 0, sum = 0; // variables used to calculate the average
int i;
float avg;

printf("Insert numbers, one number by line:\n");
for (i=0;i<NUMBERS;i++){
scanf("%d", &numbers[i]);
}

//calculate avg
for (i=0;i<NUMBERS;i++){
if(numbers[i] % 2 == 1){ // if it's an odd number print it on the stdout
printf("Odd number: %d\n", numbers[i]);
sum += numbers[i]; // sum the numbers

count++; // count the odd numbers
}
}
printf("\n");

//just one last step
avg = (float)sum / count;
printf("Average is %.2f\n", avg);

return 0;
}

Is This Answer Correct ?    2 Yes 0 No

please can some one guide me, to the answer Write a C program to enter 15 numbers as an input fro..

Answer / lalabs

// more simple and faster
if( numbers[i] & 1)
{
printf("Odd number: %d\n", numbers[i]);
sum += numbers[i]; // sum the numbers
count++; // count the odd numbers
}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

Write a program in c to print 1 121 12321 1234321 123454321

11 Answers   ANR, College School Exams Tests, Mu Sigma, Wipro,


What is 2c dna?

0 Answers  


What is a const pointer in c?

0 Answers  


what is the difference between north western polytechnique university and your applied colleges?? please give ur answers for this. :)

0 Answers  


program to find a smallest number in an array

15 Answers   Microsoft, Sony,






how can i get output the following... 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 and 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 plz plz...

3 Answers  


What is the difference between single charater constant and string constant?

0 Answers  


How can I change their mode to binary?

0 Answers  


How do you declare a variable that will hold string values?

0 Answers  


write a program in C to swap two variables

7 Answers   Attrabyte, Marlabs,


What's the difference between constant char *p and char * constant p?

0 Answers   Celstream,


explain about storage of union elements.

2 Answers   ABC, Bosch,


Categories