CSC 120 Projec1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

CSC 120 PROJECT: COMPUTER AS A

PROBLEM-SOLVING TOOL
Name: Ademola George
Matric Number: 210805147
Department: Science
Question 1:

Analysis
1. Problem Statement: The problem involves printing the string "Ademola George" multiple
times.
2. Inputs: There are no explicit inputs required for the code snippet. The string "Ademola
George" is hardcoded within the code.
3. Outputs: The output is the repeated printing of the string "Ademola George" 100 times.
4. Approach: The code uses a loop to iterate 100 times and prints the string "Ademola George"
on each iteration.
5. Complexity: The time complexity of the code is constant since it prints the same string 100
times, regardless of the input size.

Analysis
6. Problem Statement: The problem involves printing the string "Ademola George" multiple
times.

7. Inputs: There are no explicit inputs required for the code snippet. The string "Ademola
George" is hardcoded within the code.

8. Outputs: The output is the repeated printing of the string "Ademola George" 100 times.

9. Approach: The code uses a loop to iterate 100 times and prints the string "Ademola George"
on each iteration.

10. Complexity: The time complexity of the code is constant since it prints the same string 100
times, regardless of the input size.

Design
1. Algorithm:
a. Step1: Set name as "Ademola George"
b. Step2: Print name concatenated 100 times
2. Flowchart

Start

Define a variable

Set value of name variable to


"Ademola George"

Multiply name variable by 100

Print the result

End

Implementation
name = "Ademola George"
print (name * 100)

Question 2:

Analysis
1. Problem Statement: Fill the terminal screen with the user's name both horizontally and
vertically.

2. Inputs: User's name (obtained through input ()).

3. Outputs: The user's name printed repeatedly to fill the screen horizontally and vertically.

4. Approach:

 Obtain the terminal screen size using os.get_terminal_size () if the standard output is
connected to a terminal; otherwise, use a default size of (80, 24).

 Iterate screen_size [0] times to print the user's name horizontally, avoiding line
breaks.

 Iterate screen_size [1] - 1 times to print the user's name vertically, with a line break
before each repetition.

 Wait for user input using input () to pause the program.

5. Complexity: The time complexity depends on the terminal screen size (W and H). The loops
iterate W and H - 1 times, respectively. Overall Complexity: O (W + H).
Design
1. Algorithm:
a. Step 1: Import os module
b. Step 2: Import sys module
c. Step 3: Prompt the user to enter their name and store it in the variable name
d. Step 4: Get the terminal size using os.get_terminal_size () if sys.stdout is a terminal,
otherwise set screen_size to (80, 24)
e. Step 5: Iterate over the range of screen_size [0] using a loop variable _
f. Step 5.1: Print the value of name without a newline character
g. Step 6: Iterate over the range of screen_size [1] - 1 using a loop variable _
h. Step 6.1: Print a newline character
i. Step 6.2: Print the value of name without a newline character
j. Step 7: Wait for user input
2. Flowchart
Start

Import os and sys modules

Get user's name

Print user's name horizontally

Print a new line

Print user’s name


alphabetically

Print user’s name


alphabetically

End

Implementation
import os
import sys
name = input ("What is your name? ")
screen_size = os.get_terminal_size () if sys.stdout.isatty () else
(80, 24)
for_in range(screen _size[0]):
print(name, end="")
for _ in range(screen_size [1] - 1):
print()
print(name, end="")
input()

Question 3:

Analysis
1. Problem Statement: The problem involves printing the numbers from 1 to 100 along with
the string "Ademola George" for each number.

2. Inputs: There are no explicit inputs required for the code snippet. The string "Ademola
George" is hardcoded within the code.

3. Outputs: The output is the printing of numbers from 1 to 100, each followed by the string
"Ademola George".

4. Approach: The code uses a loop that iterates from 1 to 100 using the range function. On
each iteration, it prints the current number and the string "Ademola George".

5. Complexity: The time complexity of the code is constant since it always prints 100 lines
regardless of the input size.

Design
Algorithm:

a. Step 1: Set name as "Ademola George"


b. Step 2: Iterate over the range from 1 to 101 (exclusive) using a loop variable i
c. Step 2.1: Print the value of i followed by a space and the value of name
Flowchart:

Start

Define name variable

Set value of name variable to


"Ademola George"

Iterate over range (1, 101)

Print current iteration of loop,


followed by name variable

End
Implementation
name = "Ademola George"
for i in range(1, 101):
print(i, name)

Analysis
1. Problem Statement: The problem involves computing the result of the expression (a - b) / (c
+ d), where a, b, c, and d are given numeric values.

2. Inputs: The code snippet uses the following numeric values:

 a = 512

 b = 282

 c = 47.48

 d=5

3. Outputs: The output is the result of the expression (a - b) / (c + d).

4. Approach: The code assigns the given numeric values to variables a, b, c, and d. It then
calculates the result of the expression (a - b) / (c + d) and stores it in the variable named
"result". Finally, it prints the value of "result".

5. Complexity: The code has a constant time complexity since it performs a fixed number of
arithmetic operations.

Implementation
name = "Ademola George"
for i in range(1, 101):
print(i, name)

Question 4:

Analysis
1. Problem Statement: The problem involves computing the result of the expression (a - b) / (c
+ d), where a, b, c, and d are given numeric values.

2. Inputs: The code snippet uses the following numeric values:

 a = 512

 b = 282

 c = 47.48

 d=5

3. Outputs: The output is the result of the expression (a - b) / (c + d).

4. Approach: The code assigns the given numeric values to variables a, b, c, and d. It then
calculates the result of the expression (a - b) / (c + d) and stores it in the variable named
"result". Finally, it prints the value of "result".

5. Complexity: The code has a constant time complexity since it performs a fixed number of
arithmetic operations.
Design
Algorithm:

a. Step 1: Set a as 512


b. Step 2: Set b as 282
c. Step 3: Set c as 47.48
d. Step 4: Set d as 5
e. Step 5: Compute the expression (a - b) / (c + d) and assign it to the variable result
f. Step 6: Print the value of result

Flowchart:

Start

Define a variable

Define b variable

Define c variable

Define d variable

Calculate result

Print result

End

Implementation
a = 512
b = 282
c = 47.48
d = 5
result = (a - b) / (c + d)
print(result)
Question 5:

Analysis
1. Problem Statement: The problem involves calculating the square of a user-input number and
printing a sentence describing the result.
2. Inputs: The input is obtained from the user using the input() function. The user is prompted
to enter a number.
3. Outputs: The output is a printed sentence that states the square of the input number.
4. Approach: The code prompts the user to enter a number and stores it in the variable
"number" after converting it to an integer using the int() function.
It then calculates the square of the input number by raising it to the power of 2 using the
exponentiation operator (**), and assigns the result to the variable "square".

Finally, it prints a sentence using the print () function, where the input number and its square are
inserted into the sentence using commas as separators. The sentence is constructed using string
concatenation.

5. Complexity: The time complexity of the code is constant since it performs a fixed number of
operations regardless of the input size.

Design
Algorithm:

a. Step 1: Prompt the user to enter a number using the input () function and convert the input
to an integer using the int () function. Assign the value to the variable number.
b. Step 2: Compute the square of the number by raising it to the power of 2 and assign the
result to the variable square.
c. Step 3: Print a message using the print () function, which includes the original number and its
square. Format the output as a sentence.
Flowchart:

Start

Get user input

Calculate square of user input

Print square of user input

End

Implementation
number = int (input ("Enter a number: "))
square = number ** 2
print ("The square of", number, "is", square, ".")

Question 6:

Analysis
1. Problem Statement: The problem involves obtaining a number from the user, multiplying it
by different factors, and printing the resulting numbers in a specific format.
2. Inputs: The input is obtained from the user using the input () function. The user is prompted
to enter a number.
3. Outputs: The output is a sequence of numbers formed by multiplying the input number by 1,
2, 3, 4, and 5. The numbers are printed without any spaces between them, and three dashes
("-") are used as separators.
4. Approach: The code prompts the user to enter a number and stores it in the variable
"number" after converting it to an integer using the int () function.
It then uses the print () function to display the sequence of numbers. The sep parameter is used to
specify an empty string as the separator between the printed items. The numbers are obtained by
multiplying the input number by 1, 2, 3, 4, and 5, respectively. The * operator is used for
multiplication, and the "-" string is multiplied by 3 to generate the separator.

5. Complexity: The time complexity of the code is constant since it performs a fixed number of
operations regardless of the input size.

Design
Algorithm:

a. Step 1: Prompt the user to enter a number using the input () function. Convert the input to
an integer using the int () function. Assign the value to the variable number.
b. Step 2: Create a string consisting of three dashes ("---") and assign it to the variable dash.
c. Step 3: Print the number, followed by dash, 2 times the number, dash, 3 times the number,
dash, 4 times the number, dash, and 5 times the number. Use the print () function and the
sep argument to separate the values by the dash.
Flowchart:

Start

Get user input

Define b variable

Calculate the multiplication of


the user input by 2, 3, 4, and 5

Get user input and print it,


followed by three dashes,
followed by the multiplication of
the user input by 2, 3, 4, and 5,
with no spaces in between.
End

Implementation
number = int (input ("Enter a number: "))
print (number, "-" * 3, 2 * number, "-" * 3, 3 * number, "-" * 3, 4
* number, "-" * 3, 5 * number, sep="")

Question 7:

Analysis
1. Problem Statement: Convert a weight given in kilograms to pounds and display the result.
2. Inputs: The user is prompted to enter their weight in kilograms.
3. Outputs: The code prints the weight in pounds.
4. Approach:
 Prompt the user to enter their weight in kilograms using the input () function.
 Convert the input weight to a floating-point value using the float () function and store it
in the variable weight_in_kg.
 Multiply the weight in kilograms by the conversion factor 2.2 to obtain the weight in
pounds.
 Round the weight in pounds to 2 decimal places using the round () function and store it
in the variable weight_in_pounds.
 Print the weight in pounds using the print () function. Format the output as a sentence
using an f-string, where the weight in pounds is inserted into the sentence.
5. Complexity: The time complexity of the code is constant, as it performs a fixed number of
operations regardless of the weight value.

Design
Algorithm:
a. Step 1: Prompt the user to enter their weight in kilograms using the input () function.
Convert the input to a floating-point value using the float () function. Assign the value to the
variable weight_in_kg.
b. Step 2: Calculate the weight in pounds by multiplying the weight in kilograms (weight_in_kg)
by the conversion factor 2.2. Assign the result to the variable weight_in_pounds.
c. Step 3: Round the weight in pounds (weight_in_pounds) to 2 decimal places using the round
() function. Assign the rounded value back to the variable weight_in_pounds.
d. Step 4: Print a message using the print () function, which includes the weight in pounds.
Format the output as a sentence using an f-string, where the weight in pounds is inserted
into the sentence.
Flowchart:
Start

Get user input

Calculate weight in pounds by


multiplying weight in kilograms
by 2.2

Print weight in pounds

End

Implementation
weight_in_kg = float (input ("What is your weight in kilograms? "))
weight_in_pounds = round (weight_in_kg * 2.2, 2)
print (f “Your weight in pounds is {weight_in_pounds}."

You might also like