sum of numbers!

What this program does, is that it asks you for two integers and it gives you the sum of all the numbers in that range, as simple as that, yeah sure…..

Hey I’m just kidding! it is not that hard but you have to keep in mind how you want your code to be, for example what I did, is that I put a loop whenever the user gave me a lower bound that was bigger than the upper bound, and it would be in that loop until he or she gave me a lower bound that was smaller than the upper bound.

Also I thought that maybe the user could enter the same two numbers as the lower bound and as the upper bound, and that was what it took me more time to do it, and I don´t know why, but anyways I was able to fix it.

Here’s my code:

#include <iostream>
#include <math.h>
using namespace std;
int main(){
int lowerb, upperb, sum=0, cont;
cout<<«Hey! please give me the lower bound»<<endl;
cin>>lowerb;
cout<<«Great! now please give me the upper bound»<<endl;
cin>>upperb;
while(lowerb>upperb){
cout<<«hey! you gave me a lower bound that is bigger than the upper bound, it is supposed to go the other way around. Try again»<<endl;
cout<<«Hey! please give me the lower bound»<<endl;
cin>>lowerb;
cout<<«Great! now please give me the upper bound»<<endl;
cin>>upperb;
}
cont=lowerb;
if(cont<upperb){
while(cont<=upperb){
sum=sum+cont;
cont=cont+1;
}
}
else{}
cout<<«The sum from «<<lowerb<<» to «<<upperb<<» (inclusive) is: «<<sum<<endl;
return 0;
}

and here’s how it works:

img_1484

 

Un comentario en “sum of numbers!

Deja un comentario