[go: up one dir, main page]

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

PF Q and Solutions

C++ LANGUAGE
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)
20 views8 pages

PF Q and Solutions

C++ LANGUAGE
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

QUESTIONS

1. OUTPUT OF:
a.
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cout << i << j << endl;

b.
int x = 15;
int y = 3;
if (x + y > 17 || y - x < 20)
{
y = x - y;
x = y + x;
cout << x << " " << y << " " << x + y << " " << y - x << endl;
}
else
{
x = y - x + y % 5;
cout << x << " " << y << " " << x - y << " " << x + y << endl;
}

c.
int first = 16;
int second = 8;
if ((first / second == 2) || (second / first == 3))
{
second = 10;
first = 20;
}
else if ((first % second == 2 || second % first == 1))
{
second = 15;
first = 5;
}
else
{
first = -10;
second = -20;
}
cout << first << " " << second << endl;
d.
int count = 0;
int sum = 0;
while (count <= 5)
{
sum = sum + count * (count - 1);
count++;
}
cout << sum << endl;
2. Correct syntax error :
a.
#include <iostream>
using namespace std;
int main()
{
int choice;
switch (choice)
case 1:
cout < "Option 1" << endl;
break;
case 2:
cout << "Option 2" << endl
break;
default
cout << "Default" << endl;
return 0;
}
return 0;
}

b.
#include <iostream>
using namespace std;

int main() {
for (int i = 0; i < 5; i++
cout << i << endl;
}return 0;

3. Questions :
a. Write a code to generate random numbers from 55 to 65.
b. Create a C++ program that generates a random password. The program should ask the user for
the desired length of the password and then generate a random combination of letters (both
uppercase and lowercase), numbers, and special characters.
c.
Write a program in C++ to find the perfect numbers between 1 and 500.
The perfect numbers between 1 to 500 are:
6
28
496
d.
Write a program in C++ to display the multiplication table vertically from 1 to n.
Sample Output:
Input the number up to: 5
Multiplication table from 1 to 5
1x1=1 2x1=2 3x1=3 4x1=4 5x1=5
1x2=2 2x2=4 3x2=6 4x2=8 5x2=10
1x3=3 2x3=6 3x3=9 4x3=12 5x3=15
1x4=4 2x4=8 3x4=12 4x4=16 5x4=20
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45
1x10=10 2x10=20 3x10=30 4x10=40 5x10=50

e.
If the number of items bought is less than 5, then the shipping charges
are $7.00 for each item bought; if the number of items bought is at
least 5, but less than 10, then the shipping charges are $3.00 for each
item bought; if the number of items bought is at least 10, there are no
shipping charges. Correct the following code so that it computes the
correct shipping charges.

if (numOfItemsBought > 10)


shippingCharges = 0.0;

else if (5 <= numOfItemsBought || numOfItemsBought <= 10);


shippingCharges = 3.00 * numOfItemsBought;

else if (0 < numOfItemsBought || numOfItemsBought < 5)


shippingCharges = 7.00 * numOfItemsBought;

f.
Declare two variables, NUM and COUNTER. COUNTER is by default set to 1 and NUM is set to 3.
Implement a loop that runs NUM times(the start value of num). During each iteration, multiply
NUM by one more than COUNTER, and store the result back in NUM. After the loop ends, print the
final value of NUM.

Write two programs, one using a while loop and the other using a for loop. Ensure that the logic
is correct and provide the expected output for each program.

g.
Write a program that prompts the user to input three numbers. The program should then output
the numbers in ascending order.

h.
Write a C++ program to check whether a triangle is Equilateral, Isosceles or Scalene. (If none
of the sides of a triangle are equal (of equal length), the triangle is scalene. If two triangles’ sides
are equal, the triangle is isosceles. If all three of the sides of a triangle are equal, it is equilateral)
SOLUTION
Question 1:
Part OUTPUT
A. 00
01
02
10
11
12
20
21
22
B. 18 12 30 -6
C. 20 10
D. 20
Question 2:
WRONG CORRECT
#include <iostream> #include <iostream>
using namespace std; using namespace std;

int main() int main()


{ {
int choice; int choice = 2;
switch (choice) // Initialize 'choice' with a value
case 1: here...
cout < "Option 1" << endl;
break; // Use a 'switch' statement to handle
case 2: different cases
cout << "Option 2" << endl switch (choice)
break; { // Add bracket of switch
default case 1:
cout << "Default" << endl; cout << "Option 1" << endl; //added
return 0; <<
} break;
return 0; case 2:
} cout << "Option 2" << endl; //added
semicolon
break;
default:
// Corrected the output statement
cout << "Default" << endl;
}
return 0;
}
#include <iostream> int main()
using namespace std; {
int main() {
for (int i = 0; i < 5; i++ for (int i = 0; i < 5; i++) //added
cout << i << endl; bracket of loop
}return 0; cout << i << endl;

//return 0 will come before bracket


return 0; }
Question 3:
PART ANSWER
A. #include <iostream>
#include <cstdlib> // Required for rand() and srand() functions
#include <ctime> // Required for time() function to seed the random number
generator

using namespace std;

int main()
{
// Seed the random number generator with the current time
srand(time(0));

// Generate a random number between 55 and 65


int randomNumber = 55 + rand() % 11;

cout << "Random number between 55 and 65: " << randomNumber << endl;

return 0;
}
B. #include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
srand((time(0)));

int passwordLength;
cout << "Enter the desired password length: ";
cin >> passwordLength;

string characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*";

cout << "Random password: ";


for (int i = 0; i < passwordLength; i++)
{
int index = rand() % characters.length();
cout << characters[index];
}

cout << endl;

return 0;
}
C. #include <iostream>
using namespace std;

int main() {
cout << "The perfect numbers between 1 and 500 are:" << endl;

for (int num = 1; num <= 500; num++) {


int sum = 0;
for (int i = 1; i < num; i++) {
if (num % i == 0) {
sum += i;
}
}

if (sum == num) {
cout << num << endl;
}
}
return 0;
}
D. #include <iostream>
using namespace std;

int main() {
int n;
cout << "Input the number up to: ";
cin >> n;

cout << "\nMultiplication table from 1 to " << n << endl << endl;

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


for (int j = 1; j <= n; j++) {
cout << j << "x" << i << "=" << j * i << " ";
}
cout << endl;
}

return 0;
}
E. if (numOfItemsBought >= 10) {
shippingCharges = 0.0;
}
else if (numOfItemsBought >= 5 && numOfItemsBought < 10) {
shippingCharges = 3.00 * numOfItemsBought;
}
else if (numOfItemsBought < 5 && numOfItemsBought > 0) {
shippingCharges = 7.00 * numOfItemsBought;
}
else {
// Handle the case where numOfItemsBought is not a valid positive number
cout << "Invalid number of items bought." << endl;
}
F. Using a While Loop:
#include <iostream>
using namespace std;

int main() {
int NUM = 3;
int COUNTER = 1;

while (COUNTER <= 3)


{
NUM = NUM * (COUNTER + 1);
COUNTER++;
}

cout << "Final value of NUM (using while loop): " << NUM << endl;

return 0;
}

Output:
Final value of NUM (using while loop): 72

Using a For Loop:

#include <iostream>
using namespace std;

int main() {
int NUM = 3;

for (int COUNTER = 1; COUNTER <= 3; COUNTER++)


{
NUM = NUM * (COUNTER + 1);
}

cout << "Final value of NUM (using for loop): " << NUM << endl;

return 0;
}

Output:
Final value of NUM (using for loop): 72

G. //There are more than one way of doing this problem


#include <iostream>
using namespace std;
int main()
{
int num1, num2, num3;

cout << "Enter the first number: ";


cin >> num1;

cout << "Enter the second number: ";


cin >> num2;

cout << "Enter the third number: ";


cin >> num3;

if (num1 <= num2 && num1 <= num3)


{
cout << num1 << ", ";
if (num2 <= num3)
{
cout << num2 << ", " << num3 << endl;
}
else
{
cout << num3 << ", " << num2 << endl;
}
}
else if (num2 <= num1 && num2 <= num3)
{
cout << num2 << ", ";
if (num1 <= num3)
{
cout << num1 << ", " << num3 << endl;
}
else
{
cout << num3 << ", " << num1 << endl;
}
}
else
{
cout << num3 << ", ";
if (num1 <= num2)
{
cout << num1 << ", " << num2 << endl;
}
else
{
cout << num2 << ", " << num1 << endl;
}
}
return 0;
}
H. #include <iostream>
using namespace std;

int main()
{
int side1, side2, side3;

cout << "Enter the length of side 1: ";


cin >> side1;

cout << "Enter the length of side 2: ";


cin >> side2;

cout << "Enter the length of side 3: ";


cin >> side3;

if (side1 == side2 && side2 == side3)


{
cout << "The triangle is Equilateral." << endl;
}
else if (side1 == side2 || side1 == side3 || side2 == side3)
{
cout << "The triangle is Isosceles." << endl;
}
else
{
cout << "The triangle is Scalene." << endl;
}

return 0;
}

You might also like