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

Exp 4 and 5

The document contains exercises demonstrating various programming concepts in Python, including checking if a number is even or odd, finding absolute values, determining the largest number among three inputs, and checking for leap years using conditional statements. It also covers loops for printing patterns, calculating sums, generating Fibonacci series, and checking for palindromes. Additionally, it explains comparison and logical operators, as well as the differences between if-else and nested-if statements.
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)
12 views9 pages

Exp 4 and 5

The document contains exercises demonstrating various programming concepts in Python, including checking if a number is even or odd, finding absolute values, determining the largest number among three inputs, and checking for leap years using conditional statements. It also covers loops for printing patterns, calculating sums, generating Fibonacci series, and checking for palindromes. Additionally, it explains comparison and logical operators, as well as the differences between if-else and nested-if statements.
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/ 9

Exp 4

Exercise:

1. Check Whether a Number is Even or Odd (if ... else)


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

if num % 2 == 0:
print("Even")
else:
print("Odd")

2. Find the Absolute Value of an Input Number (if statement)


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

if num < 0:
num = -num

print("Absolute value:", num)

3. Find the Largest Number Among Three Numbers (Nested if)


a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))

if a > b:
if a > c:
print("Largest number is:", a)
else:
print("Largest number is:", c)
else:
if b > c:
print("Largest number is:", b)
else:
print("Largest number is:", c)

●​ Concept Used: Nested if statements


4. Check if a Year is a Leap Year or Not (if ... else and Nested if)
year = int(input("Enter a year: "))

if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print("Leap Year")
else:
print("Not a Leap Year")
else:
print("Leap Year")
else:
print("Not a Leap Year")

●​ Concept Used: Nested if ... else

5. Check if a Number is Positive, Negative, or Zero (if ... elif ... else)
num = float(input("Enter a number: "))

if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")

●​ Concept Used: if ... elif ... else

6. Calculate Grade Based on Marks of 5 Subjects (if ... elif ... else)
marks = []
for i in range(5):
marks.append(float(input(f"Enter marks for subject {i+1}: ")))

average = sum(marks) / 5

if average >= 90:


grade = "A"
elif average >= 80:
grade = "B"
elif average >= 70:
grade = "C"
elif average >= 60:
grade = "D"
else:
grade = "F"

print("Grade:", grade)

●​ Concept Used: if ... elif ... else

PRQ:

1. Operators Used in if Conditional Statements in Python

In Python, if statements use comparison operators and logical operators to evaluate


conditions.

A. Comparison Operators

These operators compare values and return True or False.

Operator Description Example (a=5, b=10)

== Equal to a == b → False

!= Not equal to a != b → True

> Greater than a > b → False

< Less than a < b → True

>= Greater than or equal to a >= b → False

<= Less than or equal to a <= b → True

B. Logical Operators

These operators combine multiple conditions.


Operator Description Example (x=5, y=10)

and Returns True if both conditions are True (x > 0 and y > 0) →
True

or Returns True if at least one condition is (x > 0 or y < 0) →


True True

not Reverses the condition's result not(x > y) → True

2. Difference Between if-else and nested-if Statements


Feature if-else Statement Nested if Statement

Definition Used when there are only two Used when an if statement is placed
possible outcomes (True or inside another if statement for more
False). complex conditions.

Structure Has a single level of condition Checks conditions at multiple levels.


checking.

Readability Easier to read and understand. Can be harder to read if too many levels of
nesting are used.

Use Case Best for simple decision-making. Best for situations where one condition
depends on another condition.

Exp 5

Exercise:

1. Print the Patterns Using Loops

(a) Right-Angled Triangle (Using Nested for Loop)


for i in range(1, 5):
for j in range(i):
print("*", end=" ")
print()
Output:

*
**
***
****

(b) Diamond-Like Pattern (Using Nested for Loop)


for i in range(1, 5):
for j in range (i):
print("*", end=" ")
print()
for i in range(3, 0, -1):
for j in range (i):
print("*", end=" ")
print()

Output:

*
**
***
****
***
**
*

(c) Number Pattern (Using Nested for Loop)


rows = 4
for i in range(rows, 0, -1):
for j in range(i):
print("1" if j % 2 == 0 else "0", end="")
print()

Output:

1010101
10101
101
1

2. Print Even Numbers Between 1 to 100 (Using while Loop)


num = 2
while num <= 100:
print(num, end=" ")
num += 2

Output:

2 4 6 8 10 ... 100

3. Sum of First 10 Natural Numbers (Using for Loop)


sum_natural = 0
for num in range(1, 11):
sum_natural += num
print("Sum of first 10 natural numbers:", sum_natural)

Output:

Sum of first 10 natural numbers: 55

4. Print Fibonacci Series (Using for Loop)


n = 10 # Number of terms
a, b = 0, 1
print("Fibonacci Series:", end=" ")
for _ in range(n):
print(a, end=" ")
a, b = b, a + b

Output:

Fibonacci Series: 0 1 1 2 3 5 8 13 21 34
5. Calculate Factorial of a Number (Using while Loop)
num = int(input("Enter a number: "))
factorial = 1
i=1
while i <= num:
factorial *= i
i += 1
print(f"Factorial of {num} is {factorial}")

Input: 5​
Output: Factorial of 5 is 120

6. Reverse a Given Number (Using while Loop)


num = int(input("Enter a number: "))
rev = 0
while num > 0:
digit = num % 10
rev = rev * 10 + digit
num //= 10
print("Reversed Number:", rev)

Input: 1234​
Output: 4321

7. Sum of Digits of a Number (Using while Loop)


num = int(input("Enter a number: "))
sum_digits = 0
while num > 0:
sum_digits += num % 10
num //= 10
print("Sum of digits:", sum_digits)

Input: 1234​
Output: 10
8. Check if a Number is a Palindrome (Using while Loop)
num = int(input("Enter a number: "))
original = num
rev = 0
while num > 0:
rev = rev * 10 + num % 10
num //= 10

if original == rev:
print("Palindrome")
else:
print("Not a Palindrome")

Input: 121​
Output: Palindrome

Input: 123​
Output: Not a Palindrome

PRQ:

1. Output of the Given while Loop Code

Code:
x = 10
while x > 5:
print(x, end=" ")
x -= 1

Expected Output:
10 9 8 7 6

Explanation:

●​ The loop starts with x = 10 and runs as long as x > 5.


●​ Inside the loop, x is printed, followed by x -= 1 (decrementing x by 1).
●​ When x becomes 5, the condition x > 5 fails, and the loop stops.
2. Convert while Loop to for Loop

Given while Loop:


x=1
while x < 10:
print(x, end=" ")
x += 1

Equivalent for Loop:


for x in range(1, 10):
print(x, end=" ")

Expected Output:
123456789

Explanation:

●​ The while loop increments x from 1 to 9 before stopping.


●​ The for loop uses range(1, 10), which automatically increments x on each iteration.
●​ Both versions produce the same output.

You might also like