This is meant to be done in C programming code. For a number N input by the user
ID: 3800407 • Letter: T
Question
This is meant to be done in C programming code.
For a number N input by the user,
1. Print the average of all numbers from 1 to N,
2. Print the average of all even numbers from 1 to N, and
3. Print the average of all odd numbers from 1 to N. ……
4. Print the average of every third number ( e.g., 1, 4, 7, )
The program should not accept a negative number. If the user enters a negative number, it should ask for a new number. Once the program prints output on the console, it should ask the user if s/he is interested in entering another number. If the user enters ‘y’ or ‘Y’ the program will continue the execution, but for any other input, the program will terminate.
Hint: Use a for loop to solve this problem.
Example: Enter a positive number: 5
The average of all numbers from 1 to 5 is 3
The average of all even numbers from 1 to 5 is 3
The average of all odd numbers from 1 to 5 is 3
The average of every third number from 1 to 5 is 2.5
Would you like to enter another number? N
Explanation / Answer
#include <stdio.h>
int main( )
{
int n,i;
int allSum=0, evenSum = 0, oddSum =0, thirdSum = 0;
double allAverage, evenAverage, oddAverage, thirdAverage;
int thirdCount = 0, evenCount = 0, oddCount = 0;
char ch = 'Y';
while(ch == 'y' || ch == 'Y'){
printf("Enter a positive number: ");
scanf("%d", &n);
if(n <= 0){
printf("Invalid input. Value must be greater than 0 ");
}
else{
for( i=1; i<=n; i++){
allSum = allSum +i;
if(i % 2 == 0){
evenSum = evenSum + i;
evenCount++;
}
else{
oddSum = oddSum + i;
oddCount++;
}
if((i-1) % 3 == 0){
thirdSum = thirdSum+i;
thirdCount++;
}
}
allAverage = allSum/(double)n;
evenAverage = evenSum/(double)evenCount;
oddAverage = oddSum/(double)oddCount;
thirdAverage = thirdSum/(double)thirdCount;
printf("The average of all numbers from 1 to %d is %lf ",n, allAverage);
printf("The average of even numbers from 1 to %d is %lf ",n, evenAverage);
printf("The average of odd numbers from 1 to %d is %lf ",n, oddAverage);
printf("The average of every third number from 1 to %d is %lf ",n, thirdAverage);
printf("Would you like to enter another number?(y or n) ");
scanf(" %c", &ch);
}
}
return 0;
}
Output:
sh-4.2$ gcc -o main *.c
sh-4.2$ main
Enter a positive number: 5
The average of all numbers from 1 to 5 is 3.000000
The average of even numbers from 1 to 5 is 3.000000
The average of odd numbers from 1 to 5 is 3.000000
The average of every third number from 1 to 5 is 2.500000
Would you like to enter another number?(y or n) y
Enter a positive number: 9
The average of all numbers from 1 to 9 is 6.666667
The average of even numbers from 1 to 9 is 4.333333
The average of odd numbers from 1 to 9 is 4.250000
The average of every third number from 1 to 9 is 3.400000
Would you like to enter another number?(y or n) n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.