WSQ 04, Sum of numbers

--Originally published at Programming the city

Hello guys, I’ve been programming this whole week and I realized you need to practice to become a master in code programming.

In this code, I´ll show you how I can sum the range of the numbers that are between the numbers the user will type.

This is a simple code, but without an erros, whatever the user types, it has an answer.


#include <iostream>
using namespace std;

int main(){
int upper, lower, sum=0, contador, contador2=0;

cout<<“I will calculate the sum of the range between the numbers you will give me”<<endl;

cout<<“Give me the lower bound “;
cin>>lower;
contador=lower*1;

cout<<“Give me the upper bound “;
cin>>upper;
contador2=upper*1;
if(lower<upper){
do{
lower=lower+1;
sum=sum+lower;
}while(lower<upper);
cout<<“The sum from “<<contador<<” to “<<upper<<” is =”<<sum+contador;
}
else{
if(lower>upper){
cout<<“You gave me the numbers in the incorrect order, but don´t worry, I´ll fix that”<<endl;
do{
upper=upper+1;
sum=sum+upper;
}while(upper<lower);
cout<<“The sum from “<<contador2<<” to “<<lower<<” is =”<<sum+contador2;
}
else{
if(lower==upper){
cout<<“You gave me the same number! What’s happening to you?”;
}
}
}

return 0;


As you can see, I put only one library, but many loops and if’s.

wsq04

 

Now you are able to see a beautiful code.

#TC1017

#WSQ04