0% found this document useful (0 votes)
7 views9 pages

Exp 3 Final

The document outlines an experiment for K. J. Somaiya College of Engineering students focused on decision-making statements in Python. It includes objectives to count prime and composite numbers, check for Armstrong numbers, and provides theoretical explanations of control statements like if, if-else, and loops. Additionally, it contains example codes, expected outcomes, and post-lab questions for further understanding.

Uploaded by

parth.palse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

Exp 3 Final

The document outlines an experiment for K. J. Somaiya College of Engineering students focused on decision-making statements in Python. It includes objectives to count prime and composite numbers, check for Armstrong numbers, and provides theoretical explanations of control statements like if, if-else, and loops. Additionally, it contains example codes, expected outcomes, and post-lab questions for further understanding.

Uploaded by

parth.palse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

K. J.

Somaiya College of Engineering, Mumbai-77


(A Constituent College of Somaiya Vidyavihar University)
Department of Science and Humanities

Batch: EXCP Roll No.: 61

Experiment / assignment / tutorial No. 3

Grade: AA / AB / BB / BC / CC / CD /DD

Signature of the Staff In-charge with date

TITLE: Decision Making Statements

AIM: 1) Write a program to count the number of prime numbers and composite
numbers entered by the user.
2) Write a program to check whether a given number is Armstrong or not.
______________________________________________________________________

Expected OUTCOME of Experiment: Use different Decision Making statements in


Python.
______________________________________________________________________
Resource Needed: Python IDE
______________________________________________________________________
Theory:

Decision Control Statements


1) Selection/Conditional branching statements
a) if statement
b) if-else statement
c) if-elif-else statement
2)Basic loop Structures/Iterative statement
a) while loop
b) for loop

If statement:
In Python if statement is used for decision-making operations. It contains a body of
code which runs only when the condition given in the if statement is true.

Syntax:
if condition:
statement(s)

1 PP/SEM I/2023-24
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Science and Humanities

If flowchart:

If-else Statement:
An else statement can be combined with an if statement. An else statement contains the
block of code that executes if the conditional expression in the if statement resolves to
0 or a FALSE value.
The else statement is an optional statement and there could be at most only
one else statement following if.

Syntax:
if expression:
statement(s)
else:
statement(s)

2 PP/SEM I/2023-24
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Science and Humanities

If-else flowchart:

If-elif-else Statement:
The elif statement allows you to check multiple expressions for TRUE and execute a
block of code as soon as one of the conditions evaluates to TRUE.
Similar to the else, the elif statement is optional. However, unlike else, for which there
can be at most one statement, there can be an arbitrary number of elif statements
following an if.

Syntax:
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)

3 PP/SEM I/2023-24
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Science and Humanities

While loop:
A while loop statement in Python programming language repeatedly executes a target
statement as long as a given condition is true.

Syntax:
while expression:
statement(s)

While loop flowchart:

For Loop:
The for statement in Python differs a bit from what you may be used to in C. Rather
than giving the user the ability to define both the iteration step and halting condition (as
C), Python’s for statement iterates over the items of any sequence (a list or a string), in
the order that they appear in the sequence.

Syntax:

4 PP/SEM I/2023-24
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Science and Humanities

for iterating_var in sequence:


statements(s)

For loop flowchart:

Problem Definition:
1) Write a program to read the numbers until -1 is encountered. Also, count the
number of prime numbers and composite numbers entered by the user

2) Write a program to check whether a number is Armstrong or not.

5 PP/SEM I/2023-24
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Science and Humanities

(Armstrong number is a number that is equal to the sum of cubes of its digits for
example: 153 = 1^3 + 5^3 + 3^3.)

Code

def is_armstrong(number):

digits = str(number)

sum_of_cubes = sum(int(digit) ** 3 for digit in digits)

return sum_of_cubes == number

num = int(input("Enter a number: "))

if is_armstrong(num):

print(f"{num} is an Armstrong number.")

else:

print(f"{num} is not an Armstrong number.")

output

Enter a number: 153

153 is an Armstrong number.

Books/ Journals/ Websites referred:

1. Reema Thareja, Python Programming: Using Problem Solving Approach,


Oxford University Press, First Edition 2017, India
2. Sheetal Taneja and Naveen Kumar, Python Programming: A modular
Approach, Pearson India, Second Edition 2018,India
3. https://docs.python.org/3/tutorial/controlflow.html#for-statements

6 PP/SEM I/2023-24
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Science and Humanities

Implementation details:

7 PP/SEM I/2023-24
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Science and Humanities

Output(s):

Conclusion:

Post Lab Questions:


1) When should we use nested if statements? Illustrate your answer with the help
of an example.
2) age = 22
3) if age < 13:
4) print("You are a child.")
5) else:
6) if age < 20:
7) print("You are a teenager.")
8) else:
9) print("You are an adult.")
10)

8 PP/SEM I/2023-24
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Science and Humanities

2) Explain the utility of break and continue statements with the help of an
example.
3) for number in range(10):
4) if number == 6:
5) break
6) print(number)
7)

for number in range(20):


if number % 3 == 0:
continue
print(number)

3) Write a program that accepts a string from user and calculate the number of
digits and letters in string.
def count_letters_and_digits(input_string):
letters_count = 0
digits_count = 0
for char in input_string:
if char.isalpha():
letters_count += 1
elif char.isdigit():
digits_count += 1

return letters_count, digits_count


user_input = input("Enter a string: ")
letters, digits = count_letters_and_digits(user_input)
print(f"Number of letters: {letters}")
print(f"Number of digits: {digits}")
Enter a string: abcdefghijklmnopqrstuvwxyz1234567890
Number of letters: 26
Number of digits: 10

Date: _____________ Signature of faculty in-charge

9 PP/SEM I/2023-24

You might also like