PF Assignment - 3
PF Assignment - 3
Question # 1:
Write a program to simulate a very simple version of Snakes & Ladders. This game contains no
snakes and no ladders. Two players compete in this game, and the first player who reaches 20 points
wins. The gameplay is straightforward: two players keep on rolling the dice in
turn until one of them reaches exactly 20 points. If a player rolls a dice and the total points cross 20,
this turn is discarded, and the total points don’t change.
The user is prompted to press any key to roll the dice (explore getch() to implement this behaviour).
You can use rand() to implement rolling a dice functionality.
Player 1 Won!!!
Question 2
Write a program in C to Check Whether a Number can be Express as Sum of Two Prime Numbers.
Test Data :
Input a positive integer: 16
Expected Output :
16 = 3 + 13
16 = 5 + 11
Question 3
Write a C++ code to print the pattern of a kite. The program takes the height of the upper portion of
the kite as parameter. Some samples are given below:
Question 4
It is known from Babylonian times that the square root of a number can be approximated by a
method now known as the ‘divide and average’ method. Since the Babylonians used it, it is also
called the Babylonian algorithm.
METHOD: Suppose we want to compute the square root of a number N. Let A>0 be a guess for
square root (N), then a better approximation is given by: B=(A+N/A)/2. We can then improve the
approximation B using C=(B+N/B)/2.
For example, let’s compute the square root of N=2. Let our initial guess be 1. The following
better approximation is (1+2/1)/2=1.5
The next better approximation is (1.5+2/1.5)/2=1.416667
The next better approximation is (1.416667+2/1.416667)/2=1.414216
And so on. The more you repeat this, the closer you get to the answer. Just keep in mind that the
square root of 2 is an irrational number, which means that you can keep on improving your
approximation forever.
1. Write a C++ code that takes a number N and implements above-mentioned method of
calculating the square root and prints the calculated final square root. You have to run 10
iterations for approximation.
2. Keep repeating the code until the user enters ‘-1’ to terminate the program
Question 5
3. Write code which will take two inputs and find nCr (number of combinations) nCr
(n, r) = n! / (n-r)! * r!
4. Write a function which will take two inputs and find nCr (number of combinations) where
nCr(n,r) = nPr(n,r) / r!
5. Design a menu function that takes a number from the user as input and then performs a
specific operation based on that value.
Sample Output:
a. On ‘1’ print factorial of number.
b. On ‘2’ print nPr.
c. On ‘3’ print nCr designed in part 3.
d. On ‘4’ print nCr designed in part 4.
e. On ‘5’ Exit Program.
Question 6
The last digit of a credit card number is the check digit, which protects against transcription errors
such as an error in a single digit or switching two digits. The following method is used to verify
actual credit card numbers but, for simplicity, we will describe it for numbers with 8 digits instead
of 16:
· Starting from the rightmost digit, form the sum of every other digit. For example, if the
credit card number is 4358 9795, then you form the sum 5 + 7 + 8 + 3 = 23.
· Double each of the digits that were not included in the preceding step. Add all digits of
the resulting numbers. For example, with the number given above, doubling the digits,
starting with the next-to-last one, yields 18 18 10 8. Adding all digits in these values
yields 1 + 8 + 1 + 8 + 1 + 0 + 8 = 27.
· Add the sums of the two preceding steps. If the last digit of the result is 0, the number
is valid. In our case, 23 + 27 = 50, so the number is valid.
Write a program that implements this algorithm. The user should supply an 8-digit number, and you
should print out whether the number is valid or not. If it is not valid, you should print the value of
the check digit that would make it valid.
Question 7
Write a C++ program that prints a number spiral. A number spiral is an infinite grid of numbers where
the first row and first column are filled with increasing integers starting from 1, and the rest of the grid
is filled with numbers following a spiral pattern.
Given coordinates (x, y), calculate the number located at that position in the spiral. The spiral begins at
(1, 1) and grows outward.
You will take Two integers x and y as Input, which represent the coordinates of the position in the
number spiral.
A single integer that represents the number at position (x, y) in the spiral will be the output.
Spiral Pattern:
Example:
1. Input: x = 2, y = 3
Output: 8
Explanation: The 2nd row, 3rd column contains 8.
2. Input:
x = 4, y = 2
Output: 15
Explanation: The 4th row, 2nd column contains 15.
Question 8
This is a well-known game with several variants. The following variant has an interesting winning
strategy. Two players alternately take marbles from a pile. In each move, a player chooses how many
marbles to take. The player must take at least one but at most half of the marbles. Then, the other player
takes a turn. The player who takes the last marble loses.
Write a program in which the computer plays against a human opponent. Generate a random integer
between 10 and 100 to denote the initial size of the pile. Generate a random integer between 0 and 1
to decide whether the computer or the human takes the first turn. Generate a random integer between
0 and 1 to determine if the computer plays smart or stupid. In stupid mode, the computer simply
takes a random legal value (between 1 and n / 2) from the pile whenever it has a turn. In smart mode
the computer takes off enough marbles to make the pile size a power of two minus 1—that is, 3, 7,
15, 31, or 63. That is always a legal move, except when the pile size is currently one less than a
power of two. In that case, the computer makes a random legal move.
You will note that the computer cannot be beaten in smart mode when it has the first move, unless
the pile size happens to be 15, 31, or 63. Of course, a human player who has the first turn and
knows the winning strategy can win against the computer.
Question 9
The following iterative sequence is defined for the set of positive integers:
Using the rule above and starting with 13, we generate the following sequence:
This sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved
yet (Collatz Problem), all beginning numbers are thought to finish at 1. Which starting number, ≤
N, produces the longest chain? If many possible such numbers are there, print the maximum one.
In simple words, input N from the user and tell in the range from 1 to N, at which n it has the most
significant sequence.
Note: Once the chain starts, the terms are allowed to go above.
Example:
Input=10
Output=9
Explanation:
Question 10
Draw following Patterns for positive values of integers provided by user as input.
Part A
Part B
For rows=10 → this is for example only
Part C
Part D:
For N=5
Part E:
For N=5