added weekly exercises of isp-ws25
This commit is contained in:
58
c/isp-ws25/weeklies/week02/w02ex04.c
Executable file
58
c/isp-ws25/weeklies/week02/w02ex04.c
Executable file
@@ -0,0 +1,58 @@
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
- find mistakes in the given source code
|
||||
|
||||
[x] ask for lower limit
|
||||
[x] ask for upper limit
|
||||
[x] calculate sum of all even numbers contained
|
||||
[x] calculate average of all even numbers contained
|
||||
[x] include lower, exclude upper
|
||||
[x] error if not lower < upper: "Upper limit must be higher than lower limit\n"
|
||||
[x] terminate on error
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
//int lower_limit = 0
|
||||
int lower_limit = 0;
|
||||
int upper_limit = 0;
|
||||
|
||||
printf("Enter lower limit: ");
|
||||
scanf("%d", &lower_limit);
|
||||
|
||||
printf("Enter upper limit: ");
|
||||
//scanf("%d", upper_limit);
|
||||
scanf("%d", &upper_limit);
|
||||
|
||||
//if (lower_limit <= upper_limit)
|
||||
if (upper_limit <= lower_limit)
|
||||
{
|
||||
printf("Upper limit must be higher than lower limit\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
//int sum;
|
||||
int sum = 0;
|
||||
//int count;
|
||||
int count = 0;
|
||||
|
||||
// Loop to find the sum of even numbers
|
||||
for (int i = lower_limit; i < upper_limit; i++)
|
||||
{
|
||||
//if ((i % 2) = 0)
|
||||
if ((i % 2) == 0)
|
||||
{
|
||||
sum = sum + i;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate the average of the even numbers
|
||||
//float average = count / sum;
|
||||
float average = sum / count;
|
||||
printf("Sum of even numbers: %d\n", sum);
|
||||
printf("Average of even numbers: %f\n", average);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user