PF Q and Solutions
PF Q and Solutions
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.
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()
{
// Seed the random number generator with the current time
srand(time(0));
cout << "Random number between 55 and 65: " << randomNumber << endl;
return 0;
}
B. #include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
srand((time(0)));
int passwordLength;
cout << "Enter the desired password length: ";
cin >> passwordLength;
string characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*";
return 0;
}
C. #include <iostream>
using namespace std;
int main() {
cout << "The perfect numbers between 1 and 500 are:" << endl;
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;
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;
cout << "Final value of NUM (using while loop): " << NUM << endl;
return 0;
}
Output:
Final value of NUM (using while loop): 72
#include <iostream>
using namespace std;
int main() {
int NUM = 3;
cout << "Final value of NUM (using for loop): " << NUM << endl;
return 0;
}
Output:
Final value of NUM (using for loop): 72
int main()
{
int side1, side2, side3;
return 0;
}