Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print a message to the screen.
C++
#include <iostream> int main() { std::cout [1] "Hello, world!" << std::endl; return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using >> instead of << for output.
Using + or -- which are not output operators.
✗ Incorrect
In C++, << is used to send output to the screen using std::cout.
2fill in blank
mediumComplete the code to read an integer from the user.
C++
#include <iostream> int main() { int number; std::cout << "Enter a number: "; std::cin [1] number; std::cout << "You entered: " << number << std::endl; return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using << instead of >> for input.
Using + or -- which are not input operators.
✗ Incorrect
In C++, >> is used to get input from the user using std::cin.
3fill in blank
hardFix the error in the code to correctly read and print a number.
C++
#include <iostream> int main() { int value; std::cout << "Enter value: "; std::cin [1] value; std::cout << "Value is " << value << std::endl; return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using << instead of >> with std::cin.
Trying to use + or -- operators for input.
✗ Incorrect
The input operator for std::cin is >>. Using << causes an error.
4fill in blank
hardFill both blanks to create a program that reads a number and prints its double.
C++
#include <iostream> int main() { int num; std::cout << "Enter number: "; std::cin [1] num; std::cout << "Double is " << num [2] 2 << std::endl; return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using << instead of >> for input.
Using + instead of * for doubling.
✗ Incorrect
Use >> to read input and * to multiply the number by 2.
5fill in blank
hardFill all three blanks to read two numbers and print their sum.
C++
#include <iostream> int main() { int a, b; std::cout << "Enter two numbers: "; std::cin [1] a >> b; int sum = a [2] b; std::cout [3] "Sum is: " << sum << std::endl; return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using << instead of >> for input.
Using - instead of + for addition.
Using >> instead of << for output.
✗ Incorrect
Use >> to read both numbers, + to add them, and << to print the result.