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

ict assignment # 4

The document contains a programming assignment with multiple questions requiring code implementations. It includes flowcharts and Python code for calculating electricity bills, computing factorials, checking for prime numbers, managing student records, counting word occurrences in a sentence, and reversing a sentence. Each question is followed by its respective code and output sections.

Uploaded by

farhanjaffar12
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 views7 pages

ict assignment # 4

The document contains a programming assignment with multiple questions requiring code implementations. It includes flowcharts and Python code for calculating electricity bills, computing factorials, checking for prime numbers, managing student records, counting word occurrences in a sentence, and reversing a sentence. Each question is followed by its respective code and output sections.

Uploaded by

farhanjaffar12
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/ 7

Assignment#4

Name: Talal Nasir reg no: SP24-BAI-052

Question#1
(A)FLOWCHART:

OUTPUT:
(B) CODE:
n=int(input("Enter the number of units
consumed : ")) if n<=100: print("bill = 0 ")
elif n<=300:
print("Bill = ",(n-100)*25)
else:
print("Bill = ",7500+(n-300)*35) #7500 for first 300 units

OUTPUT:
QUESTION # 2
CODE:

while True:
choice = input("Enter your choice (1-
5): ") number=int(input("Enter a
number : "))
if choice == '1':
if number == 0 or number == 1:
factorial = 1
else:
factorial = 1 for i in
range(2, number + 1):
factorial *= i
print(f"The factorial of {number} is
{factorial}")

elif choice == '2':


components = [int(digit) for digit in
str(number)] print(f"The components of {number}
are {components}")
elif choice == '3':
if str(number) == str(number)[::-1]:
print(f"{number} is a palindrome.")
else:
print(f"{number} is not a palindrome.")

elif choice == '4':


n =int(number)
flag=0 for i in
range(2,n):
if n%i==0:
flag=1
if(flag==0):
print(f"{number} is prime")
else:
print (f"{number} is not prime")

elif choice == '5':


print("Exiting the program.")
exit()
else:
print("Invalid choice. Please enter a valid
choice.")
OUTPUT:
QUESTION 3
CODE:
# 1. Empty list of students.
students =
[]
# 2. Implement a function named dd_student.
def add_student(name, age, grades):
student = {
'name': name,
'age': age,
'grades': grades
}
students.append(student)

# 3. Implement a function named calculate_average.


def calculate_average(grades):
if not grades:
return 0
return sum(grades) / len(grades)

# 4. Implement a function named print_student_info.


def print_student_info(student):
name = student['name'] age =
student['age'] grades =
student['grades'] average_grade =
calculate_average(grades)
print(f"Name: {name}, Age: {age}, Average Grade:
{average_grade:.2f}")

# 5. Add five students to the students list.


add_student("Menahil", 20, [85, 90,
78]) add_student("Arika", 22, [75,
80, 88]) add_student("Yousra", 19,
[95, 100, 92])
add_student("Jajwa",33,[79,88,90])
add_student("Aadi",19,[96,83,80])
for student in
students:
print_student_info(student)
OUTPUT:

QUESTION 4
CODE:
sentence = input("Enter a sentence: ")
words = sentence.split(" ")
words_count =
{}
for word in words: if
word in words_count:
words_count[word] += 1

else:
words_count[word] = 1
for word in
words_count:
print(f"{word} : {words_count[word]}")

OUTPUT
QUESTION 5

CODE:
sentence = input("Enter a sentence: ")
words = sentence.split() reversed_words
= words[::-1] reversed_sentence = '
'.join(reversed_words)
print("Reversed sentence:",
reversed_sentence)

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy