Unit-3
Unit-3
Unit-3
and Programming
Unit-3
Algorithms
• does it terminate?
• is it correct?
• is the result of the algorithm
determined?
• how much resources will it use in
terms of
• memory? (and memory bandwidth?)
• operations?
• run-time
Swapping Two Numbers Using Variable
• In many case, programmers are required to swap values of two variables. Here, we
shall learn how to swap values of two integer variables, that may lead to swapping of
values of any type. Values between variables can be swapped in two ways −
Step 1: Start
Step 7: Stop
Pseudocode
Step 1: Start
Step 3: Add the values of the a and b and assign it to the variable a.
Step 4: Subtract the values of a and b and assign it to the variable b. (Here the
value of a is changed due to the step 3).
Step 7: Stop
Pseudocode
Step 1: Start
Step 7: Stop
Python Program
Program to count digits in an integer
• The integer entered by the user is stored in variable n. Then the while
loop is iterated until the test expression n != 0 is evaluated to 0 (false).
• After first iteration, the value of n will be 345 and the count is
incremented to 1.
• After second iteration, the value of n will be 34 and the count is
incremented to 2.
• After third iteration, the value of n will be 3 and the count is
incremented to 3.
• At the start of fourth iteration, the value of n will be 0 and the loop is
terminated.
Program to count digits in an integer
• Method 1
Program to count digits in an integer
• Method 2
Program to count digits in an integer
• Method 3
Program to count digits in an integer
• Method 4
Algorithm: calculate the sum and average of first n
natural numbers
Python Program to calculate the Sum
Python Program to calculate average
Calculate the sum and average of first n natural
numbers using formula
Calculate the sum and average of first n natural
numbers using formula
Calculate the sum and average of multiple user-
entered numbers
Calculate the sum and average of multiple user-
entered numbers
Calculate the sum and average of multiple user-
entered numbers
Calculate sum using the built-in sum function in
Python
Sum of numbers in the list is required everywhere. Python provide an inbuilt function
sum() which sums up the numbers in the list
sum() function in Python
sum() function in Python
Practical Application: Problems where we require sum to
be calculated to do further operations such as finding out the
average of numbers.
Factorial Computation
Algorithm to calculate factorial value of a number
• step 1. Start
step 2. Read the number n
step 3. [Initialize]
i=1, fact=1
step 4. Repeat step 4 through 6 until i=n
step 5. fact=fact*i
step 6. i=i+1
step 7. Print fact
step 8. Stop
Fibonacci Sequence
Algorithm to print Fibonacci series up to given
number N
•
Step 1: Start
• Step 2: Declare variables N, N1, N2, N3, i.
• Step 3: Read value of N from user.
• Step 4: if N < 2, display message “Enter number > 2” and go to step 9.
• Step 5: Initialize variables N1 = 0, N2 = 1, i = 0
• Step 6: Display N1, N2
• Step 7: Compute N3 = N1 + N2
• Step 8: Repeat following statements until i < N - 2
Display N3
N1 = N2
N2 = N3
N3 = N1 + N2
i =i+1
• Step 9: Stop
Reverse a Number
Algorithm to Reverse a Number
• Step 1:Start
• Step 2:Intilize reverse=0
• Step 3:Read digit
• Step 4:check whether digit>0 then go to step 5 else go to step 9
• Step 5:reverse=reverse*10
• Step 6:reverse=reverse+digit%10
• Step 7:digit=digit/10
• Step 8: Go to step 4
• Step 9:Print reverse
• Step 10: Stop
Base Conversion
• Decimal to Any Base
• Decimal to Octal
• Decimal to Binary
• Decimal to Hexadecimal
• Binary to Any Base
• Octal to any base
• Hexadecimal to any base
The Basic Idea
• The basic algorithm for this conversion is to repeatedly divide
(integer division) the given decimal number by the target
base value until the decimal number becomes 0.
• The remainder in each case forms the digits of the number in
the new base system, however, in the reverse order.
An Upper Limit
• The algorithm, in general, will work for any base, but we need
digits/characters to represent that many numbers of a base system.
• For simplicity, we limit ourselves to base 36 i.e 10 numbers + 26
alphabets.
• (We can differentiate lower and upper case, but we intend to focus on the
algorithm here!).
• It will be clear after the implementation that the method has to work
for any base.
Decimal to Basex
1.Divide the decimal number by the radix of the target
base
2.The remainder from step 1 becomes the value for the
current column.
3.Use the quotient (answer) from step 1 as the decimal
value to calculate the next column.
4.Return to step 1 and repeat until the quotient is zero.
Let's return to the number 237 and convert it to a binary number: