Hi I am very very new to C++ i am trying to create a loop that reads 2 integers and addsall the intergers between. I am very very lost!
#include <iostream>
using namespace std;
int main()
{
int num1=10;
int num2=0;
int total;
while (num2<10)
{ total= num2+ 1;
total++;
}
system ("pause");
return 0;
}
9 2436
Hi I am very very new to C++ i am trying to create a loop that reads 2 integers and addsall the intergers between. I am very very lost!
#include <iostream>
using namespace std;
int main()
{
int num1=10;
int num2=0;
int total;
while (num2<10)
{ total= num2+ 1;
total++;
}
system ("pause");
return 0;
}
Hi,this will start looping "when hell freezes over",because you are not increasing num2.
So you want to sum m + (m+1) + (m+2) + ... + (n-2) + (n-1) + n?
Say for m=2 and n=6 you want to find 2+3+4+5+6?
There's a centuries old nice closed form formula available:
0+1+2 + ... + n= n*(n+1)/2
You don't want to count starting from 0, you want to count starting at m,
so the values 0, 1, 2, .. m-1 should be substracted again: that last sum,
using that same old formula again equals:
0+1+2+ ... + m-1 = (m-1)*m/2
So your sum for any m <= n (both nonnegative) equals:
n*(n+1)/2-(m-1)*m/2
We don't need no steenkin' loops ;-)
kind regards,
Jos
OK so I suck! I have only been doing this for 3 weeks.
ps thanks jos
You're welcome of course. As you saw: there's no need for loops here. If your
assignment text tells you to use loops, simply add the numbers
m, (m+1), (m+2) ... (n-2), (n-1), n together; use a loop counter from m to n and
initialize a sum variable to zero before you start the loop.
kind regards,
Jos
ok this is exactly what my text says,...Write a C++ program with a loop structure that reads in 2 intergers, calculates the sum of all the intergers between thee 2 intergers (not including these 2 intergers) if any, and displays the final result. For example, if the 2 intergers ou read are -2 and 5, then your program should output 9 (=-1+0+1+2+3+4).
Sakinah :-)
OK, you can use a for...loop here if you'd like. The initial condition will set your index equal to num1 + 1, the end condition will be while the index is less than num2, and you will increment the index in each update. Inside the loop, add the index to the total.
ok this is exactly what my text says,...Write a C++ program with a loop structure that reads in 2 intergers, calculates the sum of all the intergers between thee 2 intergers (not including these 2 intergers) if any, and displays the final result. For example, if the 2 intergers ou read are -2 and 5, then your program should output 9 (=-1+0+1+2+3+4).
Sakinah :-)
First and first did you have to input numbers or are the numbers declared?
Find the diferance between those numbers:
5-(-2)=7,and lower it by two becasue you are not including limits of interval.
This will be 5,
so this is it:
<Code has been removed. Please review our FAQ Posting Guidelines>
Regards,Savage!!!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: guyzdancin |
last post by:
I have developed a parsing program to handle large csv files and
compute sums. The program was
developed and successfully tested using only String objects. I want
replace String objects with
...
|
by: rinmanb70 |
last post by:
I have a table of transactions, some with past dates, some dated the
current date, and some dated in the near future. On a report, I'm
looking for a way to get four different sums using the...
|
by: DrewKull |
last post by:
Hello, I have a query which sums check boxes (so 0 or -1) then Abs the
sum so i can get the number of check box 'checked' per column... based
on start and end date.
SELECT Abs(Sum(CommCon)) AS ,...
|
by: tpdinh |
last post by:
I have to evaluate the sum in the for-loop. How do i do that?
Also, I need to print the partial sums using %e. Please help!
|
by: chunk1978 |
last post by:
hey... can anyone show me how to calculate sums to the 2nd decimal only? like:
5.50 + 4.00 = 9.50
i seriously have zero idea and could defo use some help...thanks!
<script...
|
by: RJMac |
last post by:
I’m using ADO 2.6 in VB6 to read in data from a Union query (qryJoinBalanceData) in Access 2000. The Union query contains 3 sub-queries, each of which joins several tables, but they all generate...
|
by: wisemen |
last post by:
I have a table with 2 columns, and .I want to run a query that will give me a cumulative sum of the no. of entries that have an <= and on the second column give me a cumulative sum of the no. of...
|
by: Ifoel |
last post by:
Hi all,
Sorry im beginer in vb.
I want making programm looping character or number.
Just say i have numbers from 100 to 10000.
just sample:
Private Sub Timer1_Timer()
if check1.value= 1...
|
by: someusernamehere |
last post by:
Hey people, I need to do some sistem in PHP and MySQL, I have do it a
part but the other is the problem, basically is mathemetical
operations, te user register something and after insert some...
|
by: theinsider2012 |
last post by:
JAVA for Windows XP
the question is:
Write an application that shows the sum of 1 to n for every n from 1 to 50. That is, the program prints 1 (the sum of 1 alone), 3 (the sum of 1 and 2), 6...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |