Answer to Question #217939 in C++ for Rosie

Question #217939
9: write a program uses for loop that add up the even numbers between 1 and 20, and add up odd numbers between 1 and 20 the
1
Expert's answer
2021-07-18T05:54:22-0400
#include<iostream>
using namespace std;
int main(){
	float even_sum= 0.0;
	float odd_sum = 0.0;
	
	for(int i=0; i<=20; i++){
		if(i % 2== 0){
			even_sum += i;
		}
		else{
			odd_sum += i;
		}
	}
	cout<<"The sum of odd numbers between 1 and 20 is\t"<<odd_sum<<endl;
	cout<<"The sum of even numbers between 1 and 20 is\t"<<even_sum<<endl;
}

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
APPROVED BY CLIENTS