0% found this document useful (0 votes)
24 views8 pages

Output

The document contains 6 programming questions and their solutions in C++. Question 1 checks the character class of a user-input character. Question 2 calculates the number of boxes and containers needed to pack a given number of cookies. Question 3 is a true/false quiz on C++ expressions. Question 4 generates random math problems to test the user. Question 5 checks if two numbers are rotations of each other. Question 6 calculates Fibonacci numbers up to a given position.

Uploaded by

Amber Lily
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views8 pages

Output

The document contains 6 programming questions and their solutions in C++. Question 1 checks the character class of a user-input character. Question 2 calculates the number of boxes and containers needed to pack a given number of cookies. Question 3 is a true/false quiz on C++ expressions. Question 4 generates random math problems to test the user. Question 5 checks if two numbers are rotations of each other. Question 6 calculates Fibonacci numbers up to a given position.

Uploaded by

Amber Lily
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ASSIGNMENT 2_23F-0805_BS(CS)1D

Q1
#include <iostream>

using namespace std;

int main()
{
int ch;
cout << "Enter a character" << endl;
cin >> ch;

int classcode;

if (ch >= 'a' && ch <= 'z') {


classcode = 301;
}
else if (ch >= 'A' && ch <= 'Z') {
classcode = 302;
}
else if (ch >= '0' && ch <= '9') {
classcode = 303;
}
else if (ch == '.' || ch == ',' || ch == ';' || ch == '\'' || ch == '"' || ch ==
':') {
classcode = 304;
}
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^' || ch ==
'&' || ch == '>' || ch == '<' || ch == '=') {
classcode = 305;
}
else {
classcode = 306;
}

cout << "Character class: ";


switch (classcode) {
case 301:
cout << "Small alphabet";
break;
case 302:
cout << "Capital alphabet";
break;
case 303:
cout << "Digit";
break;
case 304:
cout << "Punctuation";
break;
case 305:
cout << "Operator";
break;
case 306:
cout << "Special";
break;
}
cout << endl;
cout << "Class code: " << classcode << endl;
return 0;
}

Q2
#include <iostream>

using namespace std;

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;

int boxesneeded = 0, containersneeded = 0;


leftovercookies = no_of_cookies % cookies_in_box;

while (no_of_cookies >= cookies_in_box) {


int i = 0;

while (i < boxes_in_container && no_of_cookies >= cookies_in_box) {


i++;
no_of_cookies -= cookies_in_box;
}
containersneeded++;
boxesneeded += i;
}
cout << "Number of boxes needed: " << boxesneeded << endl;
cout << "Number of containers needed: " << containersneeded << endl;

if (leftovercookies > 0) {
cout << "Leftover cookies: " << leftovercookies << endl;
}
if (leftoverboxes > 0) {
cout << "Leftover boxes: " << leftoverboxes << endl;
}

return 0;
}

Q3
#include <iostream>

using namespace std;

int main()
{
int score = 0;
cout << "Let's start the quiz!" << endl;

for (int i = 1; i <= 11; ++i) {


bool answer;
if (i == 1) {
cout << "I. (!0) is true or false? : ";
cin >> answer;
if (answer == bool(1))
score++;
}
else if (i == 2) {
cout << "II. (!1 || !0) && !(1 && 0) is true or false? : ";
cin >> answer;
if (answer == bool(1))
score++;
}
else if (i == 3) {
cout << "III. (5 + 4 < 3 && 7 + 3 <= 20) is true or false? : ";
cin >> answer;
if (answer == bool(0))
score++;
}
else if (i == 4) {
cout << "IV. 'a' != 'b' - 1 is true or false? : ";
cin >> answer;
if (answer == bool(1))
score++;
}
else if (i == 5) {
cout << "V. ((3 % 2) * 1 == 1 && 5 * (3 % 3) == 0) is true or false?
: ";
cin >> answer;
if (answer == bool(1))
score++;
}
else if (i == 6) {
cout << "VI. \"Ali\" >= \"Noor\" is true or false? : ";
cin >> answer;
if (answer == bool(0))
score++;
}
else if (i == 7) {
cout << "VII. \"123\" >= \"456\" is true or false? : ";
cin >> answer;
if (answer == bool(0))
score++;
}
else if (i == 8) {
int answer;
cout << "VIII. (9 / 3 % 3) + (3 * 4 / 4) is: ";
cin >> answer;
if (answer == int(3))
score++;
}
else if (i == 9) {
int answer;
cout << "IX. ((3 - 2 / 2) * 7 % 7) is: ";
cin >> answer;
if (answer == int(2))
score++;
}
else if (i == 10) {
char answer;
cout << "X. 'a' + (5 % 5) * 4 is: ";
cin >> answer;
if (answer == char('a'))
score++;
}
else if (i == 11) {
char answer;
cout << "XI. 'Z' - 5 + (30 / 6) is: ";
cin >> answer;
if (answer == char('Y'))
score++;
}

}
cout << "Quiz completed! Your final score out of 11 is: " << score;

return 0;
}
Q4
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

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>

using namespace std;

int count_digits(int n)
{
int count = 0;
while (n > 0)
{
count++;
n /= 10;
}
return count;
}

bool is_rotation(int x, int y)


{
int n = count_digits(x);
if (n != count_digits(y))
return false;
for (int i = 0; i < n; i++)
{
int q = y % 10;
y /= 10;
y += q * pow(10, n - 1);
if (x == y)
return true;
}
return false;
}

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>

using namespace std;

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;
}

You might also like