#include <stdio.h>
int main(void){
float num1,num2,avg;
printf("Enter the first number ");
scanf("%f",&num1);
printf("Enter the second number ");
scanf("%f",&num2);
avg=(num1+num2)/2;
printf("Average of two numbers = %4.2f", avg);
return 0;
}
This code can be modified to include more integers for average value calculation. #include <stdio.h>
int main() {
int n, sum = 0, num;
printf("Enter the number of elements: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter number %d: ", i + 1);
scanf("%d", &num);
sum += num;
}
float average = sum / (float)n;
printf("Average: %.2f\n", average);
return 0;
}
Output
Enter the number of elements: 4
Enter number 1: 5
Enter number 2: 6
Enter number 3: 7
Enter number 4: 8
Average: 6.50
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
float average = sum / (float)n;
printf("Average: %.2f\n", average);
return 0;
}
Output
Average: 30.00
#include <stdio.h>
int main() {
int values[] = {70, 80, 90};
int weights[] = {1, 2, 3};
int n = sizeof(values) / sizeof(values[0]);
int weighted_sum = 0, total_weight = 0;
for (int i = 0; i < n; i++) {
weighted_sum += values[i] * weights[i];
total_weight += weights[i];
}
float weighted_average = weighted_sum / (float)total_weight;
printf("Weighted Average: %.2f\n", weighted_average);
return 0;
}
Output
Weighted Average: 83.33
md raihan | 31-07-2016 |
Thank u so much.....for given us a simple code that complete my assignment..so thank u....so much..sir.. |
g v divyaklakshmi | 30-08-2016 |
thank u very much . its is very use full for us in right time |
that guy | 31-12-2016 |
Thank you so much. it helps so much for my project. But there are still errors, can u help me with that? |
riddhi | 03-10-2017 |
how to find avg in simple method |