0% found this document useful (0 votes)
9 views4 pages

ITC Lab7

Uploaded by

jobsobserverpk
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)
9 views4 pages

ITC Lab7

Uploaded by

jobsobserverpk
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
You are on page 1/ 4

Experiment # 7

Break/continue statements and String


Indexing
Note: Follow the read-think-plan-code-test rule to build programs. Test your code for all
the possible inputs.
1.
2.
3.
4.
5. Determine the output of the following codes. What will happen if line 87 and 88 of code
1 exchange their positions? What will happen if line 9 of code 2 is written below but
outside the if-statement’s body?

Code 1 Code 2

6. A. The code below is incomplete and contains errors. Complete and eliminate all the
errors so that the code determines whether the number entered by the user is even or odd
until the user enters ‘end’. You are allowed to modify line 13, 15, 17 and 18 only.
a. What will happen if x = ‘end’ is written at line 13?
b. Will line 15 work correctly if the user enters ‘end’. If no, what is the solution?
c. Write one statement at line 17 which skips everything below and ultimately
allows the program to exit the while-loop when ‘end’ is entered.
d. Is it necessary to change line 18 if line 15 is modified? If yes give reason and
suggest the change.

Prepared by: Rehan Naeem Introduction to Computing Lab Manuals Page | 24


Output

B. Complete the program below to get the output in part A. Hint: Use the code in part A
with just two changes to it.

7. Write a program to determine the number of 1s, 2s and 3s in a number. Store the number
in a variable as a string and use string indexing technique to solve this task. The output
should look like

8. A. A palindrome is a number which looks the same if you read it from left to right or
right to left. For example 12321 is a palindrome. Complete the following program to
detect whether a 5 digit number is a palindrome or not. Use string indexing technique to
solve this problem. Hint: a 5 digit number is a palindrome if
a. its 1st and 5th digit are equal.
b. Its 2nd and 4th digits are equal.
Note: Modify lines 52, 53, 54, 57 and 58 only after understanding the incomplete code.

Prepared by: Rehan Naeem Introduction to Computing Lab Manuals Page | 25


Output

B. Write a general code that works well for any n-digit palindrome where n is odd.

9. To display the digits of a number in reverse order, the following algorithm is used.
Arrange the steps of the following algorithm to get the correct output.
a. Perform floor operation of the number ‘x’ with 10.
b. Perform remainder operation of the number ‘x’ with 10.
c. Print the resultant number ‘x’.
d. Repeat steps ‘a’ to ‘c’ till the number ‘x’ is not equal to 0.

10. Write a program that determines whether a positive number has consecutive digits or not.
If it has then the output should look like the one given below. Your program should work
correctly for any positive number. Hint: Use string indexing technique.

11. Write a program to implement Collatz conjecture. The Collatz conjecture is a conjecture
in mathematics that concerns a sequence defined as follows: start with any positive
integer n. Then each term is obtained from the previous term as follows: if the previous
term is even, the next term is one half of the previous term. If the previous term is odd,
the next term is 3 times the previous term plus 1. The conjecture is that no matter what
value of n, the sequence will always reach 1.
For instance, starting with n = 12, one gets the sequence 12, 6, 3, 10, 5, 16, 8, 4, 2, 1.
n = 19, for example, takes longer to reach 1: 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13,
40, 20, 10, 5, 16, 8, 4, 2, 1.

12. Write a program to implement carry-less addition i.e. regular addition, only with the carry
from each column ignored. So, for example, if we carryless-ly add 8+7, we get 5 (ignore
the carry). And if we add 18+27, we get 35 (still ignore the carry). 785+376 displays 51.

Prepared by: Rehan Naeem Introduction to Computing Lab Manuals Page | 26


The output should look like the one given below. Hint: Use string indexing technique for
this purpose.

Prepared by: Rehan Naeem Introduction to Computing Lab Manuals Page | 27

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy