Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Here are the instructions:
Write a program that accepts an integer input from the keyboard and computes the sum of all the integers from 1 to that integer. Example, if 7 were input, the sum: 1 + 2 + 3 + 4 + 5 + 6 + 7 would be computed. Use a while or for loop to perform the calculations. Print out the result after the sum has been calculated. NOTE: if you input a large integer, you will not get the correct results.
What I can't figure out is how to ADD all of the integers together. Any help would be greatly appreciated.
//preprocessor directives
int main ()
{
//declare and initialize variables
int n, i;
int total;
//user input
cout << "Enter an integer: ";
cin >> n;
//compute sum of all integers from 1 to n
total=0;
for (i = 1; i <= n; i++)
cout << i;
return 0;
}
You add by using the += operator:
for (i = 1; i <= n; i++)
total += i;
cout << total;
Note this is short for total = total + i.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 days ago.
Improve this question
I have an exercise to do to implement the Gregory-Leibniz series approximation of Pi. This is the code I have:
#include <iostream>
#include <iomanip>
#include <cmath>
int main() {
int k = 0;
int d = 0;
std::cin >> k;
std::cin >> d;
double sum = 0;
for(int i;i<=k;i++){
sum = sum + (pow(-1, i) / (2*i + 1));
}
sum = 4 * sum;
std::cout << std::fixed << std::setprecision(d) << sum << "\n";
return 0;
}
On my computer, for example, the input "1 3" outputs result "2.667", but on the school coderunner website, the output is always 0s, so in this it's "0.000". Can someone please help me?
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
char x;
cout << "\n/something?(y/n): ";
cin >> x;
if(x=='y'){
int n2 = 0;
cout << "number: ";
cin >> n2;
int i = n2;
for(int i; 0<i; i--){
cout << i;
}
}
else{
system("pause");
}
How come when I run the code it doesn't count down from the number the user gave?
What is happening here is that you declare local variable i in the block which contains for loop, and then declare another local variable i in the loop itself, and the one in the loop is not initialized. It gets some "trash" value, from each it counts down. I assume you wanted to use i declated above the loop in the loop, but it is done differently. To fix this I suggest 2 most reasonable variants:
variant 1 - use i declared above the loop
int i = n2;
for(; i>0; i--) {
...
variant 2 - use loop-local i:
// just this, do not declare i above the loop
for(int i = n2; i>0; i--) {
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to write a program using a while loop that gives an output based on range of input variable declared.
#include <iostream>
using namespace std;
int main() {
float fahren = 0;
float celsius;
while (fahren < 400) {
// convert fahreneheit to celsius
// Subtract 32, then multiply it by 5, then divide by 9
celsius = 5 * (fahren - 32) / 9;
cout << celsius;
fahren+= 100;
}
return 0;
}
I expect 4 values to be returned, however I only get 1, which is the value at 0.
You print 4 values but without separator, you might add std::cout << std::endl;
while (fahren < 400) {
// convert fahreneheit to celsius
// Subtract 32, then multiply it by 5, then divide by 9
celsius = 5 * (fahren - 32) / 9;
std::cout << celsius << std::endl; // added new line here
fahren += 100;
}
Demo
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Exercise text:
Write a program using array that asks user to input 10 numbers, then calculates the average of those numbers and finds out the total number that is greater or equal to the average. For example, you have input 10 numbers as: 1,2,3,4,5,-1,-2,-3,-4,-5. The average is 0. The total number that is greater or equal to the average is 5 which are 1,2,3,4,5.
You have to use the loop to do this problem.
My code so far:
using namespace std;
#include <iostream>
int main()
{
double nums[10];
double sum = 10;
double averageNums;
int numsGreaterThan = 0;
for ( int i = 0; i < 10; i++ )
{
cout << "Enter number " << i +1 << ": ";
cin >> nums;
sum = sum + nums;
}
averageNums = sum / 10;
for ( int j = 0; j < 10; j++ )
{
if ( nums[j] >= averageNums )
numsGreaterThan++;
}
cout << numsGreaterThan << " numbers are greater than the average.";
system ("pause");
}
The errors I'm struggling with:
Error 1 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'double [10]' (or there is no acceptable conversion)
Error 2 error C2111: '+' : pointer addition requires integral operand
IntelliSense: no operator ">>" matches these operands
operand types are: std::istream >> double [10]
IntelliSense: expression must have arithmetic or unscoped enum type
cin >> nums;
should be
cin >> nums[i];
So
sum = sum + nums;
should be
sum = sum + nums[i];
Because nums is an array and it should be indexed. You are trying to pass an array to cin and + operator with double. Hence, you are getting error.
You need to index the array:
cin >> nums[i];
sum = sum + nums[i];
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
#include <iostream>
#include <cmath>
using namespace std;
bool prime(int n);
int main()
{
double i;
while (true)
{
cout << "Enter a number that isn't 0: ";
cin >> i;
if ( i == 0)
break;
if(prime(i))
cout << i << " is prime" << endl;
else
cout << i << " is not prime." << endl;
}
system ("Pause");
return 0;
}
bool prime (int n)
{
int i;
double sqrt_of_n = sqrt(double (n));
for (i = 2; i <= sqrt_of_n; i++)
{
if (int(n) % 1 == 0)
return false;
}
return true;
}
Everytime I run the program, if I input 7, I get that 7 isn't prime. Can someone help me figure out where I messed up?
I have tried changing between double and int for i and n.
If I input 3, it shows prime.
The problem is that it's showing some prime numbers as not prime.
The body of your for loop doesn't use i at all.
In particular, n % 1 is always zero, for any integral n.
Presumably you want to know whether n is divisible by i, but accidentally checked if n is divisible by 1.
You could easily have discovered this mistake yourself by single-stepping in a debugger, and making the various subexpressions into "watch expressions".