0% found this document useful (0 votes)
10 views2 pages

DSA1 Control Structures Exercises

..
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)
10 views2 pages

DSA1 Control Structures Exercises

..
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

Data Structure & Algorithms 1 (DSA1)

Tutorial 4 (week 4 & 5) - Control Structures


2024 - 2025

20 October 2024

OBJECTIVES
Control structures:
• Conditional, Selection

• Loops

Exercise 1
Write an algorithm that prompts the user to input the temperature and the pressure value, and then outputs
the message ”Warning” provided if either the temperature is greater than or equal to 100, or the pressure is
greater than or equal to 200, or both. If neither condition is met, the algorithm should display ”OK”.

Exercise 2
A leap year, also known as a bissextile year, is a year that has an extra day, February 29th, added to it to
keep our calendar year synchronized with the astronomical year. Leap years occur every four years, except
for years that are divisible by 100 but not by 400. In such cases, the year is not a leap year. Write an
algorithm to determine if a given year is a leap year or not.

Exercise 3
A perfect number is a number that is equal to the sum of all its divisors except itself. Write an algorithm
to discover and display all perfect numbers between 1 and N . An example of a perfect number is 6.

Exercise 4
It is known that there are only 4 numbers between 100 and 500 for which the sum of the cubes of their digits
equals the number itself. Construct an algorithm to identify and list these four numbers.
Note: You can use the operator ** in the pseudo-code to calculate the cube. For example, to calculate
the cube of a number, use the format number**3. For instance, 153 = 1 ∗ ∗3 + 5 ∗ ∗3 + 3 ∗ ∗3.

1
Exercise 5
Given an integer N , we aim to extract two other integers, N 1 and N 2. The first integer (N 1) will comprise
the even digits of N , while the second integer (N 2) will consist of the odd digits. You are tasked with writing
an algorithm that takes an input integer N and then calculates and displays the values of N 1 and N 2.

Examples

N N1 N2
25461327 2462 5137
42613786 42686 137
240682 240682 0
103 0 13

Exercise 6
Create an algorithm to manipulate an integer (initially set to 0). The algorithm should start by displaying
the current integer value. Then, it presents a menu with the following options:
1. Add 1

2. Multiply by 2
3. Subtract 4
4. Exit

The user is prompted to input an integer between 1 and 4. If the user selects an option from 1 to 3, the
corresponding operation is performed, and the updated integer value is displayed. The menu is then shown
again, allowing the user to continue or exit by choosing option 4. When option 4 is selected, the algorithm
terminates.

Exercise 7 (at home)


”Skeletal multiplication” is a multiplication in which digits are replaced by question marks, and the challenge
is to find the three numbers that make it up. Write an algorithm that allows us to solve the following
particular skeletal multiplication:

? 4 ? ? ?
× 5 ? 9

= 7 ? ? ? 3 9 2

Common questions

Powered by AI

Designing such an algorithm requires backtracking and constraint satisfaction to replace '?' with digits that satisfy the multiplication equation. The method involves iterating over possible digit values, evaluating equations, and systematically searching for combinations that solve the skeletal multiplication (e.g., ? 4 ? ? ? × 5 ? 9 = 7 ? ? ? 3 9 2 2).

An algorithm can determine if a year is a leap year by checking if the year is divisible by 4. However, if the year is divisible by 100, it must also be divisible by 400 to be a leap year. Thus, for a year to be a leap year, it must either be divisible by 4 and not divisible by 100, or divisible by 400 .

The algorithm should iterate through each digit of integer N, check if it is even or odd, and allocate it to one of two new numbers, N1 or N2. N1 consists solely of even digits, while N2 consists solely of odd digits. The algorithm should then output N1 and N2 .

The algorithm begins by displaying the current integer value (initially zero). It presents menu options: add 1, multiply by 2, subtract 4, or exit. Depending on user input, it performs the respective operation and displays the updated integer. The process repeats until the user selects 'Exit' .

The algorithm should prompt the user to input temperature and pressure values. It should display 'Warning' if either the temperature is 100 or higher, or the pressure is 200 or higher. If neither condition is met, the algorithm should display 'OK' .

An algorithm can loop through numbers from 100 to 500, calculate the cube of each digit, and then sum these cubes. If the sum matches the original number, the number is included in the result. For instance, 153 is such a number because 1^3 + 5^3 + 3^3 equals 153 .

To identify perfect numbers between 1 and N, the algorithm should iterate through each number, calculate the sum of its divisors (excluding the number itself), and compare the sum to the number. If they match, the number is perfect. This process is repeated for all numbers up to N .

You might also like