0% found this document useful (0 votes)
5 views

Basic Programs (4)

The document contains various Python programming exercises covering basic concepts such as conditional statements (if, elif), loops (for, while), nested loops, and string manipulation. Each section includes specific programs like a grade calculator, electricity bill calculation, BMI calculator, and more, along with their objectives and corresponding Python code. The exercises are designed to help users practice and understand fundamental programming concepts.

Uploaded by

jayanth9b.vhs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Basic Programs (4)

The document contains various Python programming exercises covering basic concepts such as conditional statements (if, elif), loops (for, while), nested loops, and string manipulation. Each section includes specific programs like a grade calculator, electricity bill calculation, BMI calculator, and more, along with their objectives and corresponding Python code. The exercises are designed to help users practice and understand fundamental programming concepts.

Uploaded by

jayanth9b.vhs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

Basic Programs
2. If and elif:
3. For loop and while loop
4. Nested loops
5. Strings

2. If and Elif Statements

Program 1: Grade Calculator

• Objective: Write a program that takes a score and outputs a grade based on the
following conditions: 90+ = A, 80-89 = B, 70-79 = C, 60-69 = D, below 60 = F.

Python code:

score = float(input("Enter the score: "))

if score >= 90:

grade = 'A'

elif score >= 80:

grade = 'B'

elif score >= 70:

grade = 'C'

elif score >= 60:

grade = 'D'

else:

grade = 'F'

print("Grade:", grade)

Program 2: Electricity Bill Calculation


• Objective: Calculate the electricity bill based on the number of units consumed
with variable rates: first 100 units at ₹5, next 100 units at ₹7, and above 200 units
at ₹10 per unit.

Python code:

units = int(input("Enter units consumed: "))

bill = 0

if units <= 100:

bill = units * 5

elif units <= 200:

bill = (100 * 5) + (units - 100) * 7

else:

bill = (100 * 5) + (100 * 7) + (units - 200) * 10

print("Total Electricity Bill: ₹", bill)

Program 3: BMI Calculator

• Objective: Calculate the BMI and classify it based on the value: Underweight
(<18.5), Normal (18.5-24.9), Overweight (25-29.9), Obesity (30+).

Python:

weight = float(input("Enter weight in kg: "))

height = float(input("Enter height in meters: "))

bmi = weight / (height ** 2)

print("BMI:", round(bmi, 2))


if bmi < 18.5:

print("Category: Underweight")

elif bmi < 24.9:

print("Category: Normal")

elif bmi < 29.9:

print("Category: Overweight")

else:

print("Category: Obesity")

Program 4: Triangle Type Checker

• Objective: Given the lengths of three sides, determine if they form an


equilateral, isosceles, or scalene triangle.

Python code:

a = float(input("Enter side a: "))

b = float(input("Enter side b: "))

c = float(input("Enter side c: "))

if a == b == c:

print("The triangle is equilateral.")

elif a == b or b == c or a == c:

print("The triangle is isosceles.")

else:

print("The triangle is scalene.")


3. For and While Loops

Program 1: Prime Numbers in a Range

• Objective: Print all prime numbers within a specified range.

Python code: s

tart = int(input("Enter the starting number: "))

end = int(input("Enter the ending number: "))

print("Prime numbers between", start, "and", end, "are:")

for num in range(start, end + 1):

if num > 1:

for i in range(2, int(num ** 0.5) + 1):

if num % i == 0:

break

else:

print(num, end=" ")

Program 2: Sum of Odd and Even Numbers Separately

• Objective: Calculate the sum of odd and even numbers in a range separately.

Python code;

start = int(input("Enter the starting number: "))

end = int(input("Enter the ending number: "))

sum_even = 0
sum_odd = 0

for num in range(start, end + 1):

if num % 2 == 0:

sum_even += num

else:

sum_odd += num

print("Sum of even numbers:", sum_even)

print("Sum of odd numbers:", sum_odd)

Program 3: Fibonacci Series

• Objective: Generate the Fibonacci series up to n terms.

n = int(input("Enter the number of terms: "))

a, b = 0, 1

print("Fibonacci Series:", a, b, end=" ")

for i in range(2, n):

c=a+b

print(c, end=" ")

a, b = b, c

Program 4: Armstrong Numbers in a Range

• Objective: Print all Armstrong numbers within a specified range.

start = int(input("Enter the starting number: "))

end = int(input("Enter the ending number: "))


for num in range(start, end + 1):

power = len(str(num))

sum_of_digits = sum(int(digit) ** power for digit in str(num))

if num == sum_of_digits:

print(num, "is an Armstrong number")

4. Nested Loops

Program 1: Multiplication Table

• Objective: Print the multiplication table up to a specified range.

n = int(input("Enter the range for the multiplication table: "))

for i in range(1, n + 1):

for j in range(1, 11):

print(f"{i} x {j} = {i * j}")

print()

Program 2: Pyramid Pattern

• Objective: Display a pyramid pattern of stars.

rows = int(input("Enter number of rows: "))

for i in range(rows):

print(" " * (rows - i - 1) + "*" * (2 * i + 1))

Program 3: Pascal's Triangle

• Objective: Generate Pascal's Triangle up to a specified number of rows.

rows = int(input("Enter number of rows for Pascal's Triangle: "))

for i in range(rows):
num = 1

print(" " * (rows - i), end="")

for j in range(i + 1):

print(num, end=" ")

num = num * (i - j) // (j + 1)

print()

Program 4: Diamond Pattern

• Objective: Print a diamond shape pattern.

n = int(input("Enter the number of rows for the diamond: "))

for i in range(n):

print(" " * (n - i - 1) + "*" * (2 * i + 1))

for i in range(n - 2, -1, -1):

print(" " * (n - i - 1) + "*" * (2 * i + 1))

5. Strings

Program 1: Count Vowels and Consonants

• Objective: Write a program to count vowels and consonants in a string.

text = input("Enter a string: ")

vowels = "aeiouAEIOU"

vowel_count = sum(1 for char in text if char in vowels)

consonant_count = sum(1 for char in text if char.isalpha() and char not in vowels)

print("Vowels:", vowel_count)

print("Consonants:", consonant_count)
5. Strings

Program 1: Count Vowels and Consonants

• Objective: Write a program to count vowels and consonants in a string.

text = input("Enter a string: ")

vowels = "aeiouAEIOU"

vowel_count = sum(1 for char in text if char in vowels)

consonant_count = sum(1 for char in text if char.isalpha() and char not in vowels)

print("Vowels:", vowel_count)

print("Consonants:", consonant_count)

Program 2: Palindrome String Check

• Objective: Check if a given string is a palindrome.

text = input("Enter a string: ")

if text == text[::-1]:

print("The string is a palindrome.")

else:

print("The string is not a palindrome.")

Program 3: Character Frequency Count

• Objective: Count the frequency of each character in a string.

text = input("Enter a string: ")

frequency = {}
for char in text:

frequency[char] = frequency.get(char, 0) + 1

print("Character frequency:", frequency)

Program 4: Find All Substrings of a String

• Objective: Write a program to find all possible substrings of a given string.

text = input("Enter a string: ")

print("All possible substrings:")

for i in range(len(text)):

for j in range(i + 1, len(text) + 1):

print(text[i:j])

You might also like