Output
Output
Q1
#include <iostream>
int main()
{
int ch;
cout << "Enter a character" << endl;
cin >> ch;
int classcode;
Q2
#include <iostream>
int main()
{
const int box = 24, container = 75;
int no_of_cookies, cookies_in_box, boxes_in_container, leftovercookies=0,
leftoverboxes=0;
cout << "Enter the number of cookies" << endl;
cin >> no_of_cookies;
cout << "Enter the cookies in the boxes" << endl;
cin >> cookies_in_box;
cout << "Enter the boxes in containers" << endl;
cin >> boxes_in_container;
if (leftovercookies > 0) {
cout << "Leftover cookies: " << leftovercookies << endl;
}
if (leftoverboxes > 0) {
cout << "Leftover boxes: " << leftoverboxes << endl;
}
return 0;
}
Q3
#include <iostream>
int main()
{
int score = 0;
cout << "Let's start the quiz!" << endl;
}
cout << "Quiz completed! Your final score out of 11 is: " << score;
return 0;
}
Q4
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
int num_questions, correct_answers = 0;
cout << "How many math questions do you want to generate? ";
cin >> num_questions;
srand(time(NULL));
for (int i = 1; i <= num_questions; i++)
{
int x = rand() % 10 + 1;
int y = rand() % 10 + 1;
char op = "+-*/"[rand() % 4];
int answer, correct_answer;
switch (op)
{
case '+':
correct_answer = x + y;
break;
case '-':
correct_answer = x - y;
break;
case '*':
correct_answer = x * y;
break;
case '/':
correct_answer = x / y;
break;
}
cout << "Question " << i << ": " << x << " " << op << " " << y << " = ? ";
cin >> answer;
if (answer == correct_answer)
{
cout << "Correct!" << endl;
correct_answers++;
}
else
{
cout << "Incorrect. The correct answer is " << correct_answer << "." << endl;
}
}
cout << endl << "Your score: " << correct_answers << " out of " << num_questions <<
endl;
return 0;
}
Q5
#include <iostream>
int count_digits(int n)
{
int count = 0;
while (n > 0)
{
count++;
n /= 10;
}
return count;
}
int main()
{
int x, y;
cout << "Enter two numbers: ";
cin >> x >> y;
if (is_rotation(x, y))
cout << x << " is a rotation of " << y << endl;
else
cout << x << " is not a rotation of " << y << endl;
return 0;
}
Q6
#include <iostream>
int main()
{
int a, b, n, c;
do
{
cout << "Enter the first number: ";
cin >> a;
cout << "Enter the second number: ";
cin >> b;
if (a > b)
cout << "The first number must be less than or equal to the second number."
<< endl;
} while (a > b);
do
{
cout << "Enter the position you're interested in: ";
cin >> n;
if (n <= 0)
cout << "The position must be a positive number greater than zero." << endl;
else if (n > 2 && n <= 92)
{
c = 0;
int i = 3;
do
{
c = a + b;
a = b;
b = c;
i++;
} while (i <= n);
cout << "The Fibonacci sequence at position " << n << " is " << c << "." <<
endl;
}
else if (n == 1)
cout << "The Fibonacci sequence at position 1 is " << a << "." << endl;
else if (n == 2)
cout << "The Fibonacci sequence at position 2 is " << b << "." << endl;
else
cout << "The position you've chosen is too large to calculate." << endl;
} while (n <= 0 || (n > 2 && n > 92));
return 0;
}