The following code is printing a wrong output for some of the values while right for the others. For example the output for 699838110 is 2938640281442109880 while it should be 2938640272852175288
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int n;
cin>>n;
for(int i=0; i<n; i++)
{
int d;
cin>>d;
if(d==1)
{
cout<<1<<endl;
}
else
{
long long int y=pow(d,2);
long long int x=8+6*y-12*d;
cout<<x<<endl;
}
}
return 0;
}
Is this happening because of the limit of the data type long long int?? If rather than defining x, I straight away write 8+6*d*d-12*d in my cout statement then generates a garbage value. Does this mean that there's a limit to the cout statement. If yes, then what's that limit or is it decided by the data type of the variable which are involved in the computations?