Can anyone point me in the right direction to help me figure this out more/understand it. I am trying to figure out to do this (the answer is given so I am not asking for homework help or anything).
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int sum=0, i=1;
bool flag=true;
while (i<=5 && flag)
{
sum = sum + i*i;
i++;
if(sum>10)
flag=false;
}
cout << i <<sum;
Answer:414
#include <iostream>
int main(){
int sum = 0, i = 1;
bool flag = true;
while (i <= 5 && flag) //This is saying that while i <= 5 and flag is still true do the following things inside the brackets
{
sum = sum + i*i; // sum is now equal to sum + i^2
i++; //Add one to your 'i' counter
if (sum > 10) //Now check if the sum is greater than 10
flag = false; //if it is, set the flag to false.
}
std::cout << "i is equal to: " << i << "\n";
std::cout << "sum is equal to: " << sum << "\n";
}