joe
joe

Reputation: 39

Why do I keep getting the wrong output in c++

int main()
{
int n, i, sum;

cout << "Enter a value for n: ";
cin >> n;
sum = 0;

    i = 1;
    do
{
    sum += i * i;
    i++;
}while (i <= n);
cout << "The sum of the first " << n
    << " numbers is " << sum << endl;

return 0;
}

whenever I run this code and use 4 as an example the output comes up as 30. When the factorial of 4 is 24

Upvotes: 0

Views: 106

Answers (1)

Arpan Das
Arpan Das

Reputation: 103

sum+=i*i 

--> sum for first n squares.

factorial is

prod=prod*i

Upvotes: 4

Related Questions