Exp 3 Final
Exp 3 Final
Grade: AA / AB / BB / BC / CC / CD /DD
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.
______________________________________________________________________
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)
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
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
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)
if is_armstrong(num):
else:
output
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:
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)
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
9 PP/SEM I/2023-24