Answer to Question #249856 in C++ for Carla

Question #249856
Implemented using while loop
Enter first number: 5
Enter second number: 10
Sum of all even number: 24
Sum of all odd number: 21
5 square is 25
6 square is 36
7 square is 49
8 square is 64
9 square is 81
10 square is 100
Sum of squares of all the numbers: 355


How to get an output like this Using while loop?
1
Expert's answer
2021-10-12T00:37:39-0400
#include<iostream>
using namespace std;
int main()
{
	    int i,y;
	    int firstNum,secondNum;
	     int sum=0,total=0,add=0;
		cout<<"Enter the first Number: ";
		cin>>firstNum;
		cout<<"Enter the second Number which should be greater than the first number: ";
		cin>>secondNum;
		for(i = firstNum; i <= secondNum; i++){
			cout<<i << " squared is "<< (i*i)<< "\n";
			int m=i*i;
			add+=m;}
		while(firstNum<=secondNum){
		    if(firstNum%2==0){
		        firstNum=firstNum+1;
		        cout<<firstNum<<" ";
		        firstNum=firstNum+2;
		    }
		    else{
		         cout<<firstNum;
		         firstNum=firstNum+2;
		    }
		    sum=firstNum+(firstNum+2);
		    if(firstNum%2!=0){
		    	cout<<firstNum;
		  firstNum=firstNum+2;}
		  else{
		  	firstNum=firstNum+1;
		  	firstNum=firstNum+2;
		  }  
		  total=firstNum+(firstNum+2);
}
		    cout<<"\nSum of even: "<<sum;
		    cout<<"\nSum of odd numbers: "<<total;
		    cout<<"\nSum of squares of all numbers: "<<add;
}

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