Lab 4 (Year 3 Sem 1)

Lab 4 is the fifth lab in semester 1 of year 3, and the fifth lab in the total of year 3. It is a lab for loops, and is considered to be easy.

Description
No description was provided for this lab.

Unlocked after completion

 * OCD (Score at least 60% for Lab 4: Loops)
 * Infinite Trap (Score at least 80% for Lab 4: Loops)

Question
Write a program named SmallestN to find the smallest integer n such that n2 is greater than x. Show proof that the number found satisfy the condition as shown in the sample output below.

Your program should:
 * prompt the user to enter x.
 * have a method named smallestn which will take in parameter x, finds the smallest n such that n2 is greater than x, return the value of n. You should use a while loop to find n.
 * Finally, print the result (refer to sample output for format).

Sample Output 1:

Enter x: 1000 32 is the lowest number, such that n^2 is greater than 1000 Proof: 31^2 = 961 Proof: 32^2 = 1024

Sample Output 2:

Enter x: 12000 110 is the lowest number, such that n^2 is greater than 12000 Proof: 109^2 = 11881 Proof: 110^2 = 12100

Solution
Use a variable to keep track of the answer, set it to 1 initially and increment while it squared is less than the number given.

Question
A positive integer is called a perfect number if it is equal to the sum of all of its positive divisors, excluding itself. For example, 6 is the first perfect number because 6 = 3 + 2 + 1. The next is 28 = 14 + 7 + 4 + 2 + 1. Write a program named PerfectNumber which will determine if an input integer is a perfect number.

Your program should:
 * prompt the user to enter n.
 * have a method named isPerfect which will take in parameter n, returns true if the number if a perfect number, false otherwise.
 * Finally, print the result (refer to sample output for format).

Sample Output 1:

Enter n: 2 2 is not a perfect number

Sample Output 2:

Enter n: 28 28 is a perfect number

Solution
[[Practice 4 (Year 2 Sem 1)#Question 5: Perfection|Its very evident you have an issue with integrity, being able to lie about your lack of coding ability.

See the picture below. Such submissions are NOT accepted!

I will not accept such rubbish from you and you jolly well do it properly. If you have other of such nonsensical submissions you better own up before I find out and I will make you go through even worse consequences.

GO AND DO PRACTICE 4 Q5 AGAIN! If you are not sure how to do it you jolly well put your ego aside and ask.]]

Question
Write a program named BinaryToDecimal that prompts the user to enter a binary number and displays its corresponding decimal value. You are to use a loop to complete this task.

Your program should:
 * prompt the user to enter a binary number.
 * have a method named toDecimal which will take in parameter binary of int type, converts it to its equivalent decimal, returns decimal.
 * Finally, print the result (refer to sample output for format).

Sample output 1:

Enter a binary number: 10000 10000 equals 16 in decimal

Sample output 2: Enter a binary number: 10110 10110 equals 22 in decimal

Solution
A similar question can be found in Year 1 Sem 2 Lab 4 Question 1, and Year 2 Sem 1 Practice 6 Question 2

Question
Write a program named Pyramid that prompts the user to enter an integer from 1 to 15 and displays a pyramid.

Sample output:

Enter the number of lines: 14

1                                                                            2  1  2                                                                       3  2  1  2  3                                                                 4  3  2  1  2  3  4                                                           5  4  3  2  1  2  3  4  5                                                     6  5  4  3  2  1  2  3  4  5  6                                               7  6  5  4  3  2  1  2  3  4  5  6  7                                         8  7  6  5  4  3  2  1  2  3  4  5  6  7  8                                   9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9                             10 9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9  10                      11 10 9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9  10 11                12 11 10 9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9  10 11 12          13 12 11 10 9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9  10 11 12 13    14 13 12 11 10 9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9  10 11 12 13 14

Solution
A similar question can be found in Year 2 Semester 1 Lab 9.2 Question 5.