Complete the code to print numbers from 1 to 5 using a while loop.
#include <iostream> int main() { int i = 1; while ([1]) { std::cout << i << std::endl; i++; } return 0; }
The condition i <= 5 ensures the loop runs while i is from 1 to 5.
Complete the code to sum numbers from 1 to 10 using a while loop.
#include <iostream> int main() { int sum = 0; int num = 1; while ([1]) { sum += num; num++; } std::cout << sum << std::endl; return 0; }
num < 10 which excludes 10 from the sum.The condition num <= 10 makes sure numbers 1 through 10 are added.
Fix the error in the while loop condition to count down from 5 to 1.
#include <iostream> int main() { int count = 5; while ([1]) { std::cout << count << std::endl; count--; } return 0; }
The condition count >= 1 keeps the loop running while counting down from 5 to 1.
Fill both blanks to create a while loop that prints even numbers from 2 to 10.
#include <iostream> int main() { int num = [1]; while (num [2] 10) { std::cout << num << std::endl; num += 2; } return 0; }
< which excludes 10.Start at 2 and continue while num is less than or equal to 10 to print even numbers.
Fill all three blanks to create a while loop that builds a dictionary mapping numbers 1 to 5 to their squares.
#include <iostream> #include <map> int main() { std::map<int, int> squares; int i = [1]; while (i [2] 5) { squares[[3]] = i * i; i++; } for (const auto& [key, value] : squares) { std::cout << key << ": " << value << std::endl; } return 0; }
< which excludes 5.Start i at 1, loop while i is less than or equal to 5, and use i as the key in the map.