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

What is the explanation for modular programming?

0 Answers  


how to copy a string without using c function

5 Answers  


If fflush wont work, what can I use to flush input?

0 Answers  


how to calculate the time complexity of a given algorithm? pls give exaples..mainly for the coplexities such as O(log n),O(n log n)...

1 Answers   Infosys,


What does a run-time "null pointer assignment" error mean?

2 Answers  






What is a char in c?

0 Answers  


what is the advantage of software development

1 Answers  


why Language C is plateform dependent

3 Answers   Siemens, Wipro,


how does the C compiler interpret the following two statements p=p+x; q=q+y; a. p=p+x; q=q+y b. p=p+xq=q+y c. p=p+xq; q=q+y d. p=p+x/q=q+y

2 Answers   TCS, Tech Synergy,


How do you write a program which produces its own source code as its output?

2 Answers  


O,T,T,F,F,S,S,E,N,?,?,?,T,F,F,S,S,E,N

9 Answers   ADP,


main() { int i=5; printf("%d",++i + i); } output is 10 ------------------------ main() { int i=5; printf("%d",i++ + i); }output is 12 why it is so? give appropiate reason....

2 Answers  


Categories