1 of 19

Computational Thinking

By

Dept of CSE

PVPSIT, Kanuru.

PRASAD V. POTLURI SIDDHARTHA INSTITUTE OF TECHNOLOGY

2 of 19

  • Problem:
  • Design an algorithm that accepts a positive integer and reverses the order of its digits.
  • Algorithm development
  • Digit reversals is a techniques that is sometimes used in computing to remove bias from a set of numbers.
  • It is important in some fast information-retrieval algorithms.

Dept of CSE

2025-26

REVERSING THE DIGITS OF AN INTEGER

PVPSIT (Autonomous)

Problem Solving Techniques

3 of 19

  • To solve this we need to access individual digits of the input number.
  • The number 27953 is actually

  • To access the individual digits it is probably going to be easier to start at one end of the number and work through to the other end.
  • The question is at which end should we start? Because other than visually it is not easy to tell how many digits there in the input number.
  • Try to establish the identity of the least significant digit (i.e. the rightmost digit).
  • To do this we need to effectively “chop off” the least significant digit in the number.

Dept of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

4 of 19

  • In other words we want to end up with 2795 with the 3 removed and identified.
  • We can get the number 2795 by integer division of the original number by 10
  • This chops off the 3 but does not allow us to save it.
  • However, 3 is the remainder that results from dividing 27953 by 10.
  • To get this remainder we can use the mod function.
  • That is,
  • Therefore if we apply the following two steps

Dept of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

5 of 19

  • We get the digit 3, and the new number 2795.
  • Applying the same two steps to the new value of n we can obtain the 5 digit.
  • We now have a mechanism for iteratively accessing the individual digits of the input number.
  • Our major concern is to carry out the digit reversal.
  • When we apply our digit extraction procedure to the first two digits we acquire first the 3 and then 5.

Dept of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

6 of 19

Dept of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

7 of 19

Dept of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

8 of 19

Dept of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

9 of 19

Dept of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

10 of 19

  • Pseudo-code
  • begin

read (n)

reverse := 0;

while n > 0 do

begin

reverse := reverse * 10 + n mod 10;

n := n div 10

end;

dreverse := reverse

write out (dreverse)

  • end

Dept of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

11 of 19

Dept of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

12 of 19

  • Notes on design
  • The number of steps to reverse the digits in an integer is directly proportional to the number of digits in the integer.
  • In this design we see once again how a complete solution to a problem is built iteratively from a succession of partial solutions. This is the fundamental design framework for many algorithms.
  • Applications
  • Hashing and information retrieval, database applications.

Dept of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

13 of 19

  • 2.7.1 Design an algorithm that counts the number of digits in an integer.
  • 2.7.2 Design an algorithm to sum the digits in an integer.
  • 2.7.3 Design an algorithm that reads in a set of n single digits and converts them into a single decimal integer. For example, the algorithm should convert the set of 5 digits {2, 7, 4, 9, 3} to the integer 27493.

Dept of CSE

2025-26

Supplementary problems

PVPSIT (Autonomous)

Problem Solving Techniques

14 of 19

  • 2.7.1 Design an algorithm that counts the number of digits in an integer.
  • Algorithm description
  • Establish n, the integer to count the number of digits in it.
  • Set the initial count as zero.
  • While the integer n greater than zero do

(a) Use integer division to reduce the n value by a factor of 10.

(b) Increase the previous count by one.

4. Write out the number of digits in n i.e. stored in count.

Dept of CSE

2025-26

Solutions

PVPSIT (Autonomous)

Problem Solving Techniques

15 of 19

  • begin

read (n)

count:= 0;

while n > 0 do

begin

n := floor (n div 10);

count := count + 1

end;

dcount:= count;

write out (dcount)

  • end

Dept of CSE

2025-26

Pseudo-code

PVPSIT (Autonomous)

Problem Solving Techniques

16 of 19

2.7.2 Design an algorithm to sum the digits in an integer.

  • Algorithm description
  • Establish n, the integer to sum the digits in it.
  • Set the initial value of sum to zero.
  • While the integer n is greater than zero do

(a) Use the remainder function to extract the rightmost digit of the number being summed.

(b) add the recently extracted rightmost digit to sum.

(c) use integer division by 10 to remove the rightmost digit from the number being summed.

4. Write out the sum.

Dept of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

17 of 19

  • Pseudo-code
  • begin

read (n)

sum := 0;

while n > 0 do

begin

sum := sum + n mod 10;

n := n div 10

end;

dsum := sum;

write out (dsum)

  • end

Dept of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

18 of 19

2.7.3 Design an algorithm that reads in a set of n single digits and converts them into a single decimal integer. For example, the algorithm should convert the set of 5 digits {2, 7, 4, 9, 3} to the integer 27493.

  • Algorithm description
  • Establish n, the number of digits to be converted as single decimal integer.
  • Initialize decimal integer to zero.
  • While the number of numbers, n is greater than zero do

(a) read next digit;

(b) increase the previous decimal integer value by multiplying with 10 and add the recently read digit;

(c) decrease number of numbers, n by one;

4. Write out the decimal integer.

Dept of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

19 of 19

  • Pseudo-code
  • begin

read (n)

dinteger := 0;

while n > 0 do

begin

read (digit);

dinteger := dinteger * 10 + digit;

n := n - 1

end;

write out (dinteger)

  • end

Dept of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques