Complete the code to print a formatted integer using cout.
#include <iostream> int main() { int number = 42; std::cout << [1] << std::endl; return 0; }
The variable number holds the integer value to print. Using std::cout << number outputs its value.
Complete the code to print a floating-point number with two decimal places using std::fixed and std::setprecision.
#include <iostream> #include <iomanip> int main() { double pi = 3.14159; std::cout << std::fixed << std::setprecision([1]) << pi << std::endl; return 0; }
Using std::setprecision(2) with std::fixed formats the floating-point number to show two digits after the decimal point.
Fix the error in the code to print a string and an integer separated by a space.
#include <iostream> #include <string> int main() { std::string name = "Alice"; int age = 30; std::cout << name [1] " " << age << std::endl; return 0; }
In C++, to print multiple items with std::cout, use the insertion operator << between each item.
Fill both blanks to create a formatted output that prints a number with a width of 5 and fills empty spaces with zeros.
#include <iostream> #include <iomanip> int main() { int num = 7; std::cout << std::setw([1]) << std::setfill([2]) << num << std::endl; return 0; }
std::setw(5) sets the width to 5 characters, and std::setfill('0') fills empty spaces with zeros.
Fill all three blanks to print a floating-point number with width 8, precision 3, and left alignment.
#include <iostream> #include <iomanip> int main() { double value = 12.34567; std::cout << std::[1] << std::setw([2]) << std::setprecision([3]) << value << std::endl; return 0; }
std::left aligns output to the left, std::setw(8) sets width to 8, and std::setprecision(3) sets decimal precision to 3.