0% found this document useful (0 votes)
26 views18 pages

CS File

Cs answers

Uploaded by

Siddharth Rana
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)
26 views18 pages

CS File

Cs answers

Uploaded by

Siddharth Rana
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/ 18

Computer

Science
Project File
Session 2023-
24
Class 11 Sci
Th
By Siddharth Rana 28

Q1. Input a welcome message and


display it
Code : Output:

print("Welcome")

Q2. Input two numbers and display


the sum of the numbers
Code : Output:

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


b=int(input("Enter a second number:"))
c=a+b
print(c)
Q3. Input three numbers and display
the largest / smallest number.
Code : Output:

num1 = float(input("Enter the first number: "))


num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
largest_number = max(num1, num2, num3)
smallest_number = min(num1, num2, num3)
print(f"Largest number: {largest_number}")
print(f"Smallest number: {smallest_number}")

Q4 Generate the following patterns


using nested loops:
Pattern 1 :

Code : Output:

for i in range(1, 6):


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

Pattern 2 :
Code : Output:

for i in range(5, 0, -1):


for j in range(1, i + 1):
print(j, end="")
print()

Pattern 3 :

Code : Output:

for i in range(5, 0, -1):


for j in range(1, i + 1):
print(j, end="")
print()

Q5. Determine whether a number is a


perfect number, an Armstrong
number or a palindrome.
Code :
Output:

def is_perfect_number(num):
divisors_sum = sum([i for i in range(1, num) if num % i == 0])
return divisors_sum == num

def is_armstrong_number(num):
num_str = str(num)
n = len(num_str)
armstrong_sum = sum(int(digit) ** n for digit in num_str)
return armstrong_sum == num

def is_palindrome(num):
num_str = str(num)
return num_str == num_str[::-1]

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

if is_perfect_number(number):
print(f"{number} is a Perfect Number.")
elif is_armstrong_number(number):
print(f"{number} is an Armstrong Number.")
elif is_palindrome(number):
print(f"{number} is a Palindrome.")
else:
print(f"{number} is neither a Perfect Number nor an Armstrong Number nor a
Palindrome.")

Q6. Input a number and check if the


number is a prime or composite
number
Code : Output:

def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True

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

if is_prime(number):
print(f"{number} is a Prime Number.")
else:
print(f"{number} is a Composite Number.")

Q7 Display the terms of a Fibonacci


series.
Code :
Output:

def fibonacci_series(n):
fib_series = [0, 1]

while len(fib_series) < n:


next_term = fib_series[-1] + fib_series[-2]
fib_series.append(next_term)
return fib_series

num_terms = int(input("Enter the number of terms for the Fibonacci series: "))

result = fibonacci_series(num_terms)
print(f"Fibonacci series up to {num_terms} terms: {result}")

Q8 Compute the greatest common


divisor and least common multiple of
two integers

Code :
Output:

def find_gcd(x, y):


while y:
x, y = y, x % y
return abs(x)

def find_lcm(x, y):


return abs(x * y) // find_gcd(x, y)

num1 = int(input("Enter the first integer: "))


num2 = int(input("Enter the second integer: "))
gcd_result = find_gcd(num1, num2)
lcm_result = find_lcm(num1, num2)

print(f"Greatest Common Divisor (GCD) of {num1} and {num2}: {gcd_result}")


print(f"Least Common Multiple (LCM) of {num1} and {num2}: {lcm_result}")

Q9 Count and display the number of


vowels, consonants, uppercase,
lowercase characters in string

Code :
Output:

def count_characters(string):
vowels = "AEIOUaeiou"
consonants = "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz"

vowel_count = 0
consonant_count = 0
uppercase_count = 0
lowercase_count = 0

for char in string:


if char.isalpha():
if char in vowels:
vowel_count += 1
elif char in consonants:
consonant_count += 1

if char.isupper():
uppercase_count += 1
elif char.islower():
lowercase_count += 1

return vowel_count, consonant_count, uppercase_count, lowercase_count

input_string = input("Enter a string: ")

vowels, consonants, uppercase, lowercase = count_characters(input_string)


print(f"Number of vowels: {vowels}")
print(f"Number of consonants: {consonants}")
print(f"Number of uppercase characters: {uppercase}")
print(f"Number of lowercase characters: {lowercase}")

Q10 Input a string and determine


whether it is a palindrome or not;
convert the case of characters in a
string
Code :
Output:

def is_palindrome(string):
clean_string = ''.join(string.split()).lower()
return clean_string == clean_string[::-1]
def convert_case(string):
return string.swapcase()

input_string = input("Enter a string: ")

if is_palindrome(input_string):
print("The entered string is a palindrome.")
else:
print("The entered string is not a palindrome.")

converted_string = convert_case(input_string)
print(f"String after case conversion: {converted_string}")

Q11 Find the largest/smallest number


in a list/tuple
List :

Code :
Output:

numbers_list = [int(x) for x in input("Enter a list of numbers separated by space: ").split()]

largest_number = max(numbers_list)
smallest_number = min(numbers_list)

print(f"Largest number in the list: {largest_number}")


print(f"Smallest number in the list: {smallest_number}")

Tuple :

Code :
Output:

numbers_tuple = tuple(int(x) for x in input("Enter a


tuple of numbers separated by space: ").split())

largest_number = max(numbers_tuple)
smallest_number = min(numbers_tuple)

print(f"Largest number in the tuple: {largest_number}")


print(f"Smallest number in the tuple: {smallest_number}")

Q12 Input a list of numbers and swap


elements at the even location with
the elements at the odd location

Code :

numbers_list = [int(x) for x in input("Enter a list of numbers


separated by space: ").split()]

for i in range(0, len(numbers_list)-1, 2):


numbers_list[i], numbers_list[i+1] = numbers_list[i+1],
numbers_list[i]
print("List after swapping elements at even and odd locations:",
numbers_list)

Output:

Q13 Input a list/tuple of elements,


search for a given element in the
list/tuple.

List :

Code :
Output:

elements_list = [int(x) for x in input("Enter a list of elements


separated by space: ").split()]

search_element = int(input("Enter the element to search: "))

if search_element in elements_list:
print(f"{search_element} found in the list.")
else:
print(f"{search_element} not found in the list.")

Tuple :

Code :
Output:

elements_tuple = tuple(int(x) for x in input("Enter


a tuple of elements separated by space: ").split())

search_element = int(input("Enter the element to search: "))

if search_element in elements_tuple:
print(f"{search_element} found in the tuple.")
else:
print(f"{search_element} not found in the tuple.")

Q14 Create a dictionary for :


Create a dictionary with the roll number, name, and marks of n
students in a class and display the names of students who have
marks above 75:

Code :

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


student_dict = {}
for i in range(n):
roll_number = input("Enter roll number: ")
name = input("Enter name: ")
marks = float(input("Enter marks: "))
student_dict[roll_number] = {'name': name, 'marks': marks}

print("Students with marks above 75:")


for roll_number, details in student_dict.items():
if details['marks'] > 75:
print(details['name'])

Output:

Calculate profit-loss for given Cost and Sell Price:

Code :
Output:

cost_price = float(input("Enter cost price: "))


sell_price = float(input("Enter sell price: "))

profit_loss = sell_price - cost_price


print("Profit/Loss:", profit_loss)
Calculate EMI for Amount, Period, and Interest :

Code:

amount = float(input("Enter loan amount: "))


period = int(input("Enter loan period in months: "))
interest_rate = float(input("Enter annual interest rate: "))

monthly_interest_rate = interest_rate / 12 / 100


emi = (amount * monthly_interest_rate * (1 + monthly_interest_rate) ** period) / ((1 +
monthly_interest_rate) ** period - 1)

print("Monthly EMI: ", emi)

Output :

Calculate tax - GST / Income Tax :

Code:

def calculate_gst(amount, gst_rate):


gst_amount = amount * gst_rate / 100
return gst_amount

def calculate_income_tax(income):
tax_rate = 20
income_tax = income * tax_rate / 100
return income_tax

amount = float(input("Enter amount for GST calculation: "))


gst_rate = float(input("Enter GST rate: "))
print("GST Amount:", calculate_gst(amount, gst_rate))

income = float(input("Enter income for Income Tax calculation: "))


print("Income Tax:", calculate_income_tax(income))

Output:

Print the number of occurrences of a given alphabet in each


string:

Code:
Output:

string_list = ["apple", "banana", "cherry"]


alphabet = input("Enter alphabet to count: ")

for string in string_list:


count = string.lower().count(alphabet.lower())
print(f"Occurrences of '{alphabet}' in '{string}': {count}")

Create a dictionary to store names of states and their capitals :


Code: Output
:

states_capitals = {
"California": "Sacramento",
"Texas": "Austin",
"New York": "Albany",
}

print("States and Capitals:")


for state, capital in states_capitals.items():
print(f"{state}: {capital}")

Create a dictionary of students to store names and marks


obtained in 5 subjects. Print the highest and lowest values in the
dictionary :

Code :

students_dict = {
"John": [90, 85, 92, 88, 95],
"Emma": [78, 89, 92, 80, 85],
"Alex": [95, 92, 88, 78, 86],
}

all_marks = [mark for marks in students_dict.values() for mark in marks]


highest_mark = max(all_marks)
lowest_mark = min(all_marks)
print(students_dict)
print("Highest Mark:", highest_mark)
print("Lowest Mark:", lowest_mark)

Output :

You might also like