C++ while loop, p.s im new to c++

I have this question.,i tried it and it worked at first. But i dont know what happened today, it wont work. could someone please find the error.
Write a program that allows the input of an integer value n and displays all multiples of 3 which are less than or equal to n, as well as the sum of the square of these values.

#include <iostream>

using namespace std;
int main()
{
int i,n;
float sum;
i=1;
cout<<"Enter value of n: ";
cin>>n;
while (i<=n){
if (i%3 == 0){
cout<<"i: "<<i<<endl;
sum+=i*i;
}
i++;
}
cout<<"The sum of the square of all multiples of 3 less than or equal to "<<n<< " is: "<<sum<<endl;
return 0;
}
You have forgot to initialize the sum variable.
Also, you don't have to count up by ones. Just count up by 3s instead:
1
2
3
4
    for (i=3; i <= n; i += 3) {
	cout << "i: " << i << endl;
	sum += i * i;
    }
Topic archived. No new replies allowed.