Answer to Question #294260 in C++ for shanze

Question #294260

Write a c++ program that asks user to input starting and ending number of range. if the ending number is greater than starting number show the range input by user and display the following 1)sum of the even number between the range, 2)sum of all odd number between the range, 3)count how many numbers are present between the range, 4)how many numbers between the range are either divisible by 05 or 10, 5) if there is number "1000" present between the range, the ending number should change to 1000 and then should break, 6)average of all numbers between the range?

if the ending number is not greater than starting number show you entered an invalid range.


1
Expert's answer
2022-02-05T14:56:33-0500
#include <iostream>
using namespace std;


int main(){
	int startingNumber;
	int endingNumber;
	cout<<"Input starting number: ";
	cin>>startingNumber;
	cout<<"Input ending number: ";
	cin>>endingNumber;
	if(startingNumber<endingNumber){
		int sumEvenNumber=0;
		int sumOddNumber=0;
		int count=0;
		int divisible5010=0;
		float average;
		for(int i=startingNumber;i<=endingNumber;i++){
			if(i%2==0){
				sumEvenNumber+=i;
			}else{
				sumOddNumber+=i;
			}
			//5) if there is number "1000" present between the range, 
			//the ending number should change to 1000 and then should break, 
			count++;
			if(i%5==0 || i%10==0){
				divisible5010++;
			}
			if(i==1000){
				break;
			}
		}
		int sum=sumEvenNumber+sumOddNumber;
		average=(float)sum/(float)count;
		cout<<"Sum of the even number between the range: "<<sumEvenNumber<<"\n";
		cout<<"Sum of all odd number between the range: "<<sumOddNumber<<"\n";
		cout<<"Count how many numbers are present between the range: "<<count<<"\n";
		cout<<"How many numbers between the range are either divisible by 05 or 10: "<<divisible5010<<"\n";
		cout<<"Average of all numbers between the range: "<<average<<"\n";
	}else{
		cout<<"\nYou entered an invalid range. The starting number is greater than ending number\n";
	}


	cin>>endingNumber;
	return 0;


}



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog