Sum of numbers

--Originally published at my programming blog

This week the activity I needed to do was create a program that would ask the user for two numbers, the lower bound and the upper bound, and the program would give the user the sum of all the numbers from the lower bound to the upper bound.

I had some trouble with this because I was a little confused because every program from my classmates or on the internet were different so when Ken explained it in class I understood.

So what I did was:

  1. Named 3 integers, x,y and sum.
  2. I equaled sum to 0.
  3. Then I asked the user for the lower and upper bound.
  4. I used a while loop so if the user gave me a higher number in the lower bound the program would ask the user to enter the numbers in the right order.
  5. After that I used a condition where if the integers where in the right order there would be a for loop.
  6. In the for loop is like instructions, it is stated that while the int i that is equaled to x is less or equal to y the second number , i will increase.
  7. And then I equaled sum to i.
  8. Finally I printed the sum of the range.

The code:

#include <iostream>
using namespace std;
int main ()
{

int x, y, i, sum=0;
cout<< “Let’s to the sum of all the numbers in a range.”<<endl;
cout<< “Please give me the lower bound: “<< endl;
cin>> x;
cout<< “Please give me the upper bound: “<<endl;
cin>> y;

while (x>y){
cout<<“Please enter the numbers in the right order (from small to big).”<<endl;
cout<< “Please give me the lower bound: “<< endl;
cin>> x;
cout<< “Please give me the upper bound: “<<endl;
cin>> y;
}

if(x<=y){
for(int i=x; i<=y;

screen-shot-2017-02-08-at-11-24-21-am
screen-shot-2017-02-08-at-11-25-12-am
Continue reading "Sum of numbers"

Sum of numbers… #WSQ04

--Originally published at It&#039;s me

Honestly, this task was a little bit harder than I thought… Everything was great as usual until I had to validate that the numbers were lower and higher and that  the numbers weren’t the same… But I did it! I think the code isn’t to good but it really worked and I’m glad.

Basically for the sum I just needed a loop with do while and for the counter variable instead of writing (cont= cont+1;) I just used the “trick” Ken gave us in class (cont++;). For the validates I used IF and ELSE, the order of this was the difficult part. Finally my code is:

wsq04

And here it is, running with two same numbers, one bigger and one smaller in disorder and in order:

codewsq04

That’s all! Have a nice day.

Original gif at the begginig (because of my confusion) recovered from: http://giphy.com/gifs/gngO1gmBhS9na


Homework 4: Sum of Numbers

--Originally published at Let&#039;s CODE

This program sums a range of consecutive numbers, between a given range by the user. It’s very simple to make, I will show you step by step how to make it. As usual, you need to call the necessary libraries and open the main function. We have to declare two variables for the bounds, one … Continue reading Homework 4: Sum of Numbers

(WSQ04) Please, don’t joke around

--Originally published at Programming Path

The activity is to run a program where it gives you the sum of a range of numbers, which that range is asked to the user. We needed to be careful if the user inserts the numbers in the wrong order. If that happens then we should put something like “your wrong, please enter it again.

Here is the code:

sum2

Again the code:

#include <iostream>
using namespace std;

int main () {
int l, h, i, r = 0;
cout << “I will sum the numbers in the range you give.” << endl;
cout << endl << “Please enter the lowest number: “;
cin >> l;
cout << “Please enter the highest number: “;
cin >> h;
if (h < l) {
cout << “That number is lower than the first one, please don’t joke around.” << endl;
cout << “Enter the highest number again, please: “;
cin >> h;
}
if (l == 0) {
for (i = 0 ; i <= h; i++) {
r = r + i;
}
}
else {
for (i = l; i <= h; i++) {
r = r + i;
}
}
cout << “The sum from ” << l << ” to ” << h << ” is: ” << r << endl;
return 0;
}

This are the result, with two options:

Wrong order:

I will sum the numbers in the range you give.

Please enter the lowest number: 3
Please enter the highest number: 2
That number is lower than the first one, please don’t joke around.
Enter the highest number again, please: 7
The sum from 3 to 7 is: 25

Right order:

I will sum the numbers in the range you give.

Please enter the lowest number: 3
Please enter the highest number: 7

Continue reading "(WSQ04) Please, don’t joke around"

Suman y suman y vuelven a sumar ♪♫

--Originally published at Adal´s Blog

La actividad de esta semana es:


La actividad consiste en crear un código el cual nos ayude a sumar un rango de valores dado


Este problema lo resolví con un ciclo un while para así obtener la sumatoria de los números  

Personas de ayuda:
  • Sandoval A.

Numbers… again?

--Originally published at Loading…

Yes, but this activity was different, the #WSQ04 said:

Write a program that asks for a range of integers and then prints the sum of the numbers in that range (inclusive).

You can use a formula to calculate this of course but what we want you to do here is practice using a loop to do repetitive work.

This one was easy, don’t even need to add an extra library. And because I don’t start the chapter 3 of the book yet (#sorry ☹️), I decided to do it with the functions and loops that I already know. First I read the hole activity very well, and checked my other classmate’s works to understand how to do it, and in my head I made a flowchart.

As usually first I established my variables as int, put the “instructions” using cout, and wrote the operations. Then I added an if for the condition that both numbers were equal, and in the else part I put the do/while loop for make the addition. So, this is the code in the easy form, with nothing extra:

num

After proving that the program runs correctly, I decided to do my “extra” 💪🏼, so, I added an else if  for the condition that the “higher” number was lower than the “lower” number. Everything that I did was, literally, copy/paste what I already had and insert it inside the function. And voilà ? this is my code at the final:

num3num2

?

num4


NUMB3R5

--Originally published at Ken&#039;s Disciple 01

Picture by energepic.com energepic.com

For WSQ04 you may want to get prepared and to know several things first, things like:

  • Reading chapter 4 of our book to learn how to do conditionals.
  • This other page may work too.
  • Other help to actually do this WSQ.
  • Optional: if you want to know the actual formula or maybe get dome extra information this may help.

Captura de pantalla 2017-01-31 a la(s) 13.31.53.png

Al right so let’s get started, as you can see from the picture above first we have to declare variables and give the initial structure, which is asking for the values, then you will need to do an if else condition, if n1 (which is the lower number), is bigger than n2 (upper number) then we do what the program is expected to do, to sum the values between the two integer given. For this process we have to use the for condition (well you don’t have to, there are other ways, but I’ll show you to do it this way), what you put in the parenthesis:

  1. Declare the variable for the for condition, I used x and I said that it’s the same as the lower value.
  2. The next thing is that while x (lower number) is less or the same as our higher value, we will continue to do this process.
  3. The process which is adding one to our lower value, that’s what it means by x++, its the same if you put x=x+1.
  4. Finally we print the result as ans, that why I put ans+=x; that means the same as ans=ans+x.

At this point we finished the hardest part of our program, but now comes the other part of the if, the else, here is the option that the user may give us the lower value as the highest one and viceversa.

Captura de pantalla 2017-01-31 a la(s) 14.24.25.png
Captura de pantalla 2017-01-31 a la(s) 14.25.02.png
Continue reading "NUMB3R5"

#WSQ04 Post Sum of Numbers 23/01/17 and WSQ04.cpp

--Originally published at Solving Problems with Programming

Link of the picture

So in this seventh class that I had on last Monday I started how to make a program with loops in C++.#Mastery14

What I did for this numeric program is solving the problem to the user by writing a program that asks for a range of integers and then prints the sum of the numbers in that range (inclusive).

I could use a formula to calcute this but what I want to do here is practice using a loop to do repetitive work.

For example, the sum from 6 to 10 would be 0 + 6 + 7 + 8 + 9 + 10.

Notice our sum starts with zero, why?. Because it completes the property of sums that are:

{\displaystyle m>n\;,\quad \sum _{i=m}^{n}a_{i}=0}

I talked this part in my video tutorial: #WSQ04 Fifth Tutorial SUM OF NUMBERS 23/01/17

The number of the lower limit 6 is greater than the numbers from 0 to 5, so that is why the result of the sum is zero.

And when we add each number in the range provided by the user. The mathematical formula to do this calculation is?:

sum

sum2

Pictures of author

After that I change my program to handle the user giving the upper and lower bound in the wrong order with error message.

The resources I need it to solve it are here:

sum link 1

sum link 2

The following photograph shows the solution to this problem:

sum3

Picture of author

So at first I wrote the same structure of the program just did the same as what i did in Hello World: Second Class, Second Blog (Blog of the second class 12/01/17) and Hello World.cpp,  #WSQ01 Post Fun with Numbers 16/01/17 and WSQ1.cpp#WSQ02 Post Temperature 23/01/17 and WSQ02.cpp and #WSQ03 Post Pick a Number 23/01/17

forloopoverview
1
2
3
4
Continue reading "#WSQ04 Post Sum of Numbers 23/01/17 and WSQ04.cpp"

WSQ-04, sum of numbers

--Originally published at The Clueless Programmer

In this blog post I will talk about the fourth assignment (fifth if you count the quiz) which I just finished, the one assigned for week four. In this assignment we were asked to make a program that would ask you to write two numbers, and then it would output the integer sum (the sum of every number in between those two numbers) including the numbers you wrote. So up with the program:

imagen1 #include <iostream>
using namespace std;
int main()
{
int n1, n2, sum=0, acum=0;
cout<<“We will calculate the sum of the integers in the range you provide”<<endl;
cout<<“Please give us the lower bound: “;
cin>>n1;
cout<<“Please give us the upper bound: “;
cin>>n2;
acum=n1;
if(n1==n2)
{
cout<<“Both your numbers are the same”<<endl;
}
else if(n1<n2)
{
do
{
sum=sum+acum;
acum=acum+1;
} while(acum<=n2);
}
else
{
acum=n1;
do
{
sum=sum+acum;
acum=acum-1;
} while(acum>=n2);
}
cout<<“The sum from “<<n1<<” to “<<n2<<” (inclusive) is “<<sum<<endl;
}

So okay as you can see we don´t need any special libraries to run the programs, we just need to claim our variables and then ask them to give us both of the numbers. The way I did the program makes it so that it doesn´t matter if you write fist the big number or the low number, the result will be the same.

Next thing you wanna do is start with the ifs so that those little things won´t mess with your program. The first one avoids the numbers that are the same. Then the other are basically the same. You state a new variable with a value of zero and sum to it the first number, written as another variable, and then you sum one to this variable while the variable is lower or the same as the second one. What this does

Continue reading "WSQ-04, sum of numbers"