Hello, I\'m having a query regarding C languate coding. I would be verythankful
ID: 3613257 • Letter: H
Question
Hello,
I'm having a query regarding C languate coding. I would be verythankful if someone could help me with it.
The program is as follows:
//This program calculates the sum of even mentioned for the upperlimit of integers entered by the user
#include<iostream.h>
#include<stdlib.h>
#include<iomanip.h>
using namespace std;
main()
{
int upperlimit=0,sum=0,number=1;
cout<<"Please enter the upperlimit for the numbers you wantto calculate the sum of: ";
cin>>upperlimit;
while(number<=upperlimit)
{
if(2*(number/2)==number) //using division method(story revolvesaround datatype, decimal get tranculated due to integer datatype)
{
sum = sum + number;
}
number = number+1;
}
cout<<"The sum of the even integers having upperlimit"<<upperlimit<<" is:"<<sum<<setp(100000)<<endl;
system("pause");
}
This program is running fine in Dev C++. What I want to know isthat when I put a integer value equal to or less than 92681 I getpositive value in answer. But when I put a value above the statedvalue suppose 92682 onwards I get result in negative value. How doI know till wat limit my answer is being calculated correctly andhow do I calculate the sum of even integer values above the digit92682?Hint: What I suppose the problem seems to be some thingrelated to the way the data types are stored in memory, like intstores 4 byte in memory. But still there is got to be a way bywhich we can find sum of integer values above the range of 92681.I'll be very thankful if someone could answer it.
Thanks.
Explanation / Answer
please rate - thanks The problem is your answer is too large to fit inan integer. It's even too big to fit in a long int. Thesolution is to make sum a double. //This program calculates the sum of even mentioned for the upperlimit of integers entered by the user #include #include #include using namespace std; main() { int upperlimit=0,number=1; double sum=0; coutupperlimit; while(numberRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.