WSQ04 – On to functions – Sum of numbers.

--Originally published at Blogging through my thoughts

¡Hello , programmers and curious friends! This is my fourth program , created using Ubuntu bash and Atom. For this task , my classmates and I were instructed to create a program that asks for a range of integers and then prints the sum of the numbers in that range (inclusive).

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

I decided to use a loop to calculate the sum, because my programming teacher Ken Bauer, wanted us to practice repetitive work.

_______________________________________________________________

The resources that helped me were:
-How to think like a computer scientist.

-cpluplus.com

-Programming hub, mobile app.

I added some extra steps, because I wanted to show more information to the users , like:

-Both numbers are the same. Give me a correct range.

– The sum is also executed if the user writes the upper and lower bound in the wrong order.

WSQ04ATOM1

WSQ04ATOM2
Atom code.
WSQ04UBUNTU
Ubuntu commands.

WSQ04MASTERY

Until next time!


Homework 4 sum of numbers

--Originally published at Solving problems with programming

In this homework we are going to make the sum of numbers for a range that the user gives. To do this we are going to use a loop. In the loop has to a counter an a varaible were al the numbers are added. The counter will help the loop to stablish until when the loop is going to be repeated. The loop is going to be repeated until the counter is equal to the highest number from the range of the sum.

Here is my code any question of the structure or suggestion to do it in the comments.

The code here

#include <iostream>
#include <cmath>
using namespace std;
int sumanumeros(int bajo, int alto){
int suma,contador;
suma=0;
contador=bajo;
if(bajo>alto){
cout<<“El número menor es más grande que el mayor”<<endl;
return 0;
} else{
if(bajo==alto){
cout<<“El número menor es igual al mayor”<<endl;
return 0;
} else{
do{
suma=suma+contador;
contador++;
}while(contador<=alto);
return suma;
}
}
}
int main(){
int min,max,resultado;
char op;
cout <<“Este programa sirve para hacer una suma de un rango de números”<<endl;
cout <<“Necesitas dar el número menor y mayor del rango “<<endl;
do{
cout <<“Número menor:”;
cin >> min;
cout<<endl<<“Número mayor:”;
cin>> max;
cout<<endl;
resultado= sumanumeros(min,max);
if(resultado>0);{
cout<<“Resultado: “<<resultado<<endl;
}
cout<<“¿Quiere hacer otra suma?(s/n)”<<endl;
cin>>op;
}while(op==’S’||op==’s’);
return 0;
}


WSQ 04 Sum of Numbers

--Originally published at Franco TC1017

I’m so sorry to upload this program like this. I know it looks like it was written by before fire was discovered. Well, this program asks the user for a

Well, this program asks the user for a lower bound and an upper bound. Then it sums every number between them including both bounds. If the user sets a lower bound greater than the upper bound the program returns an ERROR message.

I had to split the las cout in six lines instead of one because it just wouldn’t let me write it in a single line. If I wrote it in a single line my result would change to an evil six digit number.

sum-of-numbers-codesum-of-numbers-terminal


WSQ04-between we win

--Originally published at The Talking Chalk

I am sure most of us know about the story of a Young Carl Gauss giving the sum of all numbers within 1 and 100 to his teacher in a class after he asked for the value of the sum. Since I was a kid, I liked to think that one day I could do something as astonishing as what the Young Gauss did… I did not until now, but hey! I made a code that can do what gauss did and more!

It was not a big deal, only had to create a do-while loop so the lowest number would be added a 1 until it reached the biggest number, as well as the count would sum 1.

Finally, the result would be half of the biggest multiplied by itself plus one minus the original lowest number plus 1 multplied by the loest number minus 1, all divided by 2.

I should have used just a for loop…

#include <iostream>
using namespace std;
int main()
{
int lowest, biggest, count, sum;
cout<<“Provide us a number.”<<endl;
cin>>lowest;
do
{
cout<<“Now provide a number bigger than the first you gave”<<endl;
cin>>biggest;
}
while(biggest<=lowest);
do
{
lowest=lowest+1;
count=count+1;
}
while(lowest!=biggest);
sum=biggest*(biggest+1)/2-((lowest-count-1)*((lowest-count-1)+1))/2;
cout<<“The sum of all numbers between the range is “<<sum<<endl;
return 0;
}


WSQ04

--Originally published at TC1017

Programming is life, programming is love. WSQ04 is about a person who needed desperately a good grade and a program who didn’t want to run. But in love, if you put effort and confidence, eventually it will work. If you want no sum from ‘n’ number to ‘z’ number, just use this code.

#BAPL

P.S Want to hear cool music to study or program? I know a Youtube channel who is streaming cool music, if you want to hear it I’ll leave the link below.

P.S I do not own the channel, all the credits to Chilled Cow (Name of the channel)


For sigma like Σ

--Originally published at prgrm.co

This assignment was focused on a different kind of loop, previously I used a “do while” loop, check it out here,  but this time I’ll be using a “for” loop.

Ok, so… the “for” loop consist of 4 parts:

  • Initialization: here is where you will declare how the loop starts.
  • Condition: in this part, the program will evaluate if the loop is true or false.
  • Increment: the initialization that you started at first will either increase or decrease.
  • Statement: What happens every time a loop is completed.

If you need further information check this out.

Ok check the code:

#include <iostream>

using namespace std;

int main(){

int i, low, high, sum = 0;

cout << "Give me the lower number: ";
cin >> low;
cout << "Give me the higher number: ";
cin >> high;

for (i = low; i <= high; i++){
sum = sum + i;
}

cout << "The sum of numbers between " << low << " & " << high << " is: " << sum << endl;

return 0;
}

Featured image.


Contador.cpp // cout<< “The + of the # in the range is : “<< endl; Error … !!

--Originally published at Alvaro_246

SUM OF NUMBERS

En este programa utilice un contador “do” para poder lograr el objetivo, que es sumar los números que se encuentren en un determinado rango, independientemente de los números que ingrese el usuario.

2017-02-13-6

2017-02-13-3 2017-02-13-1 2017-02-13-2

 

CODIGO:

#include <iostream>
using namespace std;

int main()
{
string Respuesta;
int num1, num2, con=0;
cout <<“This program calculate de sum of all numbers in specific range”<< endl;
cout <<“You want to try ….?” <<endl;
cin>> Respuesta;
if (Respuesta==”Yes”){

cout <<“Give me the lower number”<< endl;
cin >> num1;
cout <<“Give me the upper number”<<endl;
cin >> num2;
int con2=num1;
do{
con=con+con2;
con2 ++;
}while (con2<=num2);
cout << ” Sum is:” << con <<endl;}
return 0;
} //Alvaro_246


WSQ04 Suma de numeros

--Originally published at Hensel

Este programa realiza la suma de números que se encuentran dentro de un rango, por ejemplo : se ingresa el numero 1 y el numero 10, después realiza la suma de esta forma 1+2+3+4+5+6+7+8+9+10, y el resultado entregado sería 55. Utilice un “for” para que mi variable llamada cont aumentara cada vez que se realizara una suma, y un do-while, para que el usuario decidiera si seguir en el juego o salir de el, también agregue otro do-while para desplegar un mensaje en caso de que el usuario, ingresara los datos en un orden incorrecto, es decir, si ingresara 10, y después el numero 4. comparto mi código para que lo puedan ver.

sum-of-numbres2sum-of-numbres1


#WSQ04

--Originally published at OlafGC / Class #TC1017

WSQ04, the most challenging task so far (just kidding, they are all pretty easy. Even more if you have a friend’s help). Anyway, I’ll upload the video later, so you can see the whole process.

Code:

#include <iostream>
using namespace std;

int main()
{
int n1, n2, dif, sum, sum2, count=0, cicle;
cout<<endl;
cout<<“Give me two numbers and I’ll calculate the sum of the integer in that range.”<<endl;
cout<<endl;
cout<<“Please introduce the first number (the lower bound):”<<endl;
cin>>n1;
cout<<“Please introduce the second number (the upper bound):”<<endl;
cin>>n2;
dif=n2-n1;
cicle=dif;
sum=n1;
sum2=sum+1;
do
{
sum=sum+sum2;
sum2++;
count++;
}
while(cicle>count);
cout<<“The sum of the integers in that range is “<<sum<<“.”<<endl;
return 0;
}

Here’s the code working:wsq04


WSQ 04- Sum of numbers.

--Originally published at |Blogging through my thoughts|

¡Hello , programmer and curious friends! This is my fourth program , created using Cygwin and Atom. For this task , my classmates and I were instructed to create a program that asks for a range of integers and then prints the sum of the numbers in that range (inclusive).

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

I decided to use a loop to calculate the sum, because my programming teacher Ken Bauer, wanted us to practice repetitive work.

The resources that helped me were:
-How to think like a computer scientist.

-cpluplus.com

-Programming hub, mobile app.

I added some extra steps, because I wanted to show more information to the users , like:

-Both numbers are the same. Give me a correct range.

– The sum is also executed if the user writes the upper and lower bound in the wrong order.

Here you will find my code and some captures:

wsq04-cygwin
Running on Cygwin.
wsq04-code
Atom code.

#include
using namespace std;

int sum(int min,int max);

int main(){
cout << “This program prints the sum of numbers in a range(inclusive):” << endl;
int a,b;
cout << “Enter the lower bound:” << endl; cin >> a;
cout << “Enter the upper bound:” << endl; cin >> b;

if (a<b){
cout << “The sum from ” << a << ” to ” << b << ” is: ” << sum (a,b) << endl; } else if (a>b){
cout << “The sum from ” << a << ” to ” << b << ” is: ” << sum (b,a) << endl;
}
else {
cout << “Both numbers are the same. Give me a correct range.” << endl;
}
return 0;
}

int sum (int base , int limit)

wsq03-topics
wsq04-topic
masterytopics1
Continue reading "WSQ 04- Sum of numbers."