





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A comprehensive set of practice exercises for c++ programming, covering fundamental concepts such as variable declaration, data types, operators, expressions, loops, and debugging techniques. Each exercise includes detailed solutions and explanations, making it an ideal resource for beginners to solidify their understanding of c++ programming.
Typology: Study notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!
In C++, variables are declared using their data type followed by their name. Solution: int count; // Declares an integer variable named count char letter; // Declares a character variable named letter
a) Assign the value 10 to the int variable x. int x = 10 ; b) Assign the character โGโ to the char variable y. char y = 'G';
The given code: double 5 annualReceiptsNY; is incorrect because variable names cannot start with a number. Corrected version: double annualReceiptsNY5;
Variable names must start with a letter or underscore and cannot contain special characters except underscores.
To print three separate lines, we use cout and endl. Solution: cout << "The moon" << endl; cout << "Is" << endl; cout << "blue." << endl;
Given: int result; a) result = 7 / 3 + 2 ;
Given: x = 9, y = 4
The given program has multiple errors:
a) Given input: 25 13 7 Executed as: cin >> int1 >> int2 >> int3; โ Solution: int1 = 25, int2 = 13, int3 = 7 b) Given input: 24.5 A 32 Executed as: cin >> var1 >> ch1 >> var2;
cout << endl; // Prints newline
Solution: char ch1, ch2, ch3; cin >> ch1; // Reads 'A' cin.get(ch2); // Reads the first blank space cin.get(ch3); // Reads the second blank space
Given Algebraic Expressions โConvert to C++:
Solution: y = pow(x, 5 );
Solution: c = pow(m, 4 ) / (pow(p, 7 ) * pow(q, 5 ));
Solution: x = (a + 15 ) / ( 4 * b);
Given Expressions:
Given input: 999 111 444 666 555 777 333 888 222 Given code: cin >> X; cin.ignore( 500 , '\n'); cin >> Y >> Z; โ Step-by-step execution:
Given code: cout << "Enter n: "; cin >> n; If (n < 0 ) cout << "That is negative. Try again." << endl; cin >> n; else if cout << "Value of n = " << n << endl; โ Fixed version: cout << "Enter n: "; cin >> n; if (n < 0 ) { cout << "That is negative. Try again." << endl; cin >> n; } else {
Given code: int a = 1 ; while (a > 0 ) { int num = 5 ; cout << num << endl; } cout << a << endl; Errors:
Given code: int n = 2 ; while (n != 15 ) { n = n + 2 ; cout << n << " "; } Errors:
Given input: 25 10 6 - 1 Given code: int number, sum = 0 ; cin >> number; while (number != -1) {
cin >> number; sum = sum + number; } cout << sum << endl; Error:
Given for-loop: for (int i = 20 ; i > 10 ; i--) { cout << i * i; } Equivalent while-loop: int i = 20 ; while (i > 10 ) { cout << i * i; i--; }
Given while-loop: int x = 1 ; while (x > 0 ) { cout << "Enter a number: "; cin >> x; } Equivalent do-while loop: int x; do { cout << "Enter a number: "; cin >> x; } while (x > 0 ); โ Why?
Solution: for (int i = 10 ; i >= 1 ; i--) { cout << i; if (i > 1 ) cout << ", "; } cout << endl; โ Output: 10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1