Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Alt Sum up all the even numbers in a given range and calculate the average a) Re

ID: 3728329 • Letter: A

Question

Alt Sum up all the even numbers in a given range and calculate the average a) Request a number range (min/max values) separated by a space. b) validate the input data. c) output all the even numbers between the min and max. d) Output total amount of the displayed number e) Output the sum of these numbers. Example Output Specify a range of values (min/max) separated by a space: 11 1 Invalid input. Maximum must be greater than minimum specify a range of values (min/max) separated by a space: 1 11 All the even numbers in the range: 246810 Average 6

Explanation / Answer

Hello, I have an answer for you. But since you have not specified the required programming language, I have defined the answer in both C++ and C. Whichever the language is, the logic is similar. If you are looking for the solution in a different language, kindly let me know. Comments are included, If you have any doubts, feel free to ask, Thanks

//Code in C++

#include<iostream>

using namespace std;

int main(){

                int min,max;

                cout<<"Specify a range of values (min/max) seperated by a space: ";

                //reading minimum and maximum values

                cin>>min;

                cin>>max;

                //checking if input data is valid

                if(min>=max){

                                cout<<"Maximum must be greater than minimum";

                }else{

                                cout<<"All the even numbers in the range: ";

                                int count=0;//for storing the number of even numbers

                                int sum=0; //for storing the total sum

                                //looping through all the numbers in the range

                                for(int i=min;i<=max;i++){

                                                if(i%2==0){

                                                                /* if there is no remainder when divided by 2,

                                                                it means that the number is even */

                                                                cout<<i<<" ";//displaying it

                                                                count++;//incrementing the count

                                                                sum+=i;//adding to the total

                                                }

                                }

                                //finding average

                                float average=(float) sum/count;

                                //displaying sum and average

                                cout<<" Sum="<<sum<<endl;

                                cout<<"Average="<<average;

                }

}

/*OUTPUT*/

Specify a range of values (min/max) seperated by a space: 7 29

All the even numbers in the range:

8 10 12 14 16 18 20 22 24 26 28

Sum=198

Average=18

//Code in C

#include<stdio.h>

int main(){

                int min,max;

                printf("Specify a range of values (min/max) seperated by a space: ");

                scanf("%d %d",&min,&max);

               

                if(min>=max){

                                printf("Maximum must be greater than minimum");

                }else{

                                printf("All the even numbers in the range: ");

                                int count=0;

                                int sum=0;

                                for(int i=min;i<=max;i++){

                                                if(i%2==0){

                                                                printf("%d ",i);

                                                                count++;

                                                                sum+=i;

                                                }

                                }

                                float average=(float) sum/count;

                                printf(" Sum=%d Average=%f",sum,average);

                }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote