[go: up one dir, main page]

0% found this document useful (0 votes)
65 views34 pages

Computer Journal Programs

The document contains 8 programs written in Python. Program 1 prints the student's information. Program 2 performs arithmetic operations on two input numbers. Program 3 demonstrates random number generation. Program 4 shows the working of math and statistics modules. Program 5 calculates marks, percentage, grade and result of a student. Program 6 displays a menu to calculate area of different shapes. Program 7 is a number guessing game using a while loop. Program 8 stores student details in a list and allows searching by roll number.

Uploaded by

Jawaan Santh
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)
65 views34 pages

Computer Journal Programs

The document contains 8 programs written in Python. Program 1 prints the student's information. Program 2 performs arithmetic operations on two input numbers. Program 3 demonstrates random number generation. Program 4 shows the working of math and statistics modules. Program 5 calculates marks, percentage, grade and result of a student. Program 6 displays a menu to calculate area of different shapes. Program 7 is a number guessing game using a while loop. Program 8 stores student details in a list and allows searching by roll number.

Uploaded by

Jawaan Santh
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/ 34

# 1.

Program to print my information


print("My name is Jawaan Santh.")
print("My roll number is 16.")
print("I am in 'XI A' Science class.")
print("I am in red house.")
print("I have chosen handball as my activity).")
print("THANK YOU.")
My name is Jawaan Santh.
My roll number is 16.
I am in 'XI A' Science class.
I am in red house.
I have chosen handball as my activity.
THANK YOU.
# 2. Program to enter any two numbers and perform arithmetic
operation.
num1 = int(input("Enter first number:"))
num2 = int(input("Enter second number:"))
sum = num1 + num2
diff = num1 - num2
product = num1 * num2
division = num1 / num2
floor_division = num1 // num2
remainder = num1 % num2
print("Sum of ", num1, "+", num2, "=", sum)
print("Difference of ", num1, "-", num2, "=", diff)
print("Product of ", num1, "*", num2, "=", product)
print("Division(Quotient with fractional value) of ", num1, "/",
num2, "=", division)
print("Floor Division(Quotient without fractional value) of ",
num1, "//", num2, "=", floor_division)
print("Remainder of ", num1, "%", num2, "=", remainder)
Enter first number:20
Enter second number:20
Sum of 20 + 20 = 40
Difference of 20 - 20 = 0
Product of 20 * 20 = 400
Division(Quotient with fractional value) of 20 / 20 = 1.0
Floor Division(Quotient without fractional value) of 20 // 20 = 1
Remainder of 20 % 20 = 0
# 3. Program to generate random numbers using random
module
import random
print("*WORKING OF 'random' MODULE*")
num1 = random.random()
print("This random() function generates a random floating
number(N) 0 <= N < 1.0:", num1)
num2 = random.randint(5, 15)
print("This randint(x, y) function generates a random integer
number(N) from x <= N <= y:", num2)
num3 = random.randrange(5, 15)
print("This randrange(x, y) function generates a random integer
number(N) from x <= N < y:", num3)
num4 = random.randrange(5, 15, 2)
print("This randrange(x, y, z) function generates a random
integer number(N) from x <= N < y with step value z:", num4)
print("Generating a floating number in a range:")
num5 = random.random() * (15 - 5) + 5
print("Printing a float number(N) 5.0 <= N < 15.0:", num5)
*WORKING OF 'random' MODULE*
This random() function generates a random floating number(N)
0 <= N < 1.0: 0.2830718174557768
This randint(x, y) function generates a random integer
number(N) from x <= N <= y: 10
This randrange(x, y) function generates a random integer
number(N) from x <= N < y: 8
This randrange(x, y, z) function generates a random integer
number(N) from x <= N < y with step value z: 13
Generating a floating number in a range:
Printing a float number(N) 5.0 <= N < 15.0: 10.54982798359567
# 4. Working of math and statistics modules
import math
import statistics
print("*WORKING OF 'math' MODULE*")
print("Use of pow() function:")
x = int(input("Enter base value x = "))
y = int(input("Enter exponent value y = "))
z = math.pow(x, y)
print("x to the power y:", x, "^", y, "=",z)
print("\nUse of sqrt() function:")
a = int(input("Enter base value a = "))
x = int(input("Enter exponent value x = "))
b = int(input("Enter base value b = "))
y = int(input("Enter exponent value y = "))
c = int(input("Enter base value c = "))
z = int(input("Enter exponent value z = "))
sqrt = math.sqrt(pow(a, x) + pow(b, y) + pow(c, z))
print("Square root of a^x + b^y + c^z:", sqrt)

print() #This print() function will print an empty line.


print("*WORKING OF 'statistics' MODULE*")
nums = [7, 10, 15, 6, 20, 18, 25, 3, 7]
mean_value = statistics.mean(nums)
median_value = statistics.median(nums)
mode_value = statistics.mode(nums)
print("Mean(average) value of", nums, "is:",mean_value)
print("Median(middle) value of", nums, "is:",median_value)
print("Mode(most often repeated) value in", nums,
"is:",mode_value)
*WORKING OF 'math' MODULE*
Use of pow() function:
Enter base value x = 20
Enter exponent value y = 2
x to the power y: 20 ^ 2 = 400.0

Use of sqrt() function:


Enter base value a = 100
Enter exponent value x = 2
Enter base value b = 65
Enter exponent value y = 5
Enter base value c = 300
Enter exponent value z = 10
Square root of a^x + b^y + c^z: 2430000000000.0

*WORKING OF 'statistics' MODULE*


Mean(average) value of [7, 10, 15, 6, 20, 18, 25, 3, 7] is:
12.333333333333334
Median(middle) value of [7, 10, 15, 6, 20, 18, 25, 3, 7] is: 10
Mode(most often repeated) value in [7, 10, 15, 6, 20, 18, 25, 3,
7] is: 7
# 5. Program to enter 11th std. marks and calculate total,
percentage, grade and result
roll = int(input("Enter your roll number:"))
name = input("Enter your name:")
print("Enter marks out of 100 for all subjects:")
phy = float(input("Enter marks of Physics:"))
chem = float(input("Enter marks of Chemistry:"))
maths = float(input("Enter marks of Maths:"))
eng = float(input("Enter marks of English:"))
cs = float(input("Enter marks of Computer Science:"))
total_marks_scored = phy + chem + maths + eng + cs
percent = (total_marks_scored * 100) / 500
grade = None
if percent >= 75:
grade = "A+"
elif percent >= 60:
grade = "A"
elif percent >= 45:
grade = "B"
elif percent >= 35:
grade = "C"
else:
grade = "Failed."
print("\n*Now printing the result*")
print("Your Total marks:", total_marks_scored)
print("Your Percentage:", percent)
print("Your Grade:", grade)
Enter your roll number:16
Enter your name: Jawaan
Enter marks out of 100 for all subjects:
Enter marks of Physics: 90
Enter marks of Chemistry: 95
Enter marks of Maths: 96
Enter marks of English: 98
Enter marks of Computer Science: 99

*Now printing the result*


Your Total marks: 478.0
Your Percentage: 95.6
Your Grade: A+
# 6. Write a program to display a menu for calculating area of
circle, rectangle and triangle
print("Enter 1 to calculate area of Circle:")
print("Enter 2 to calculate area of Rectangle:")
print("Enter 3 to calculate area of Triangle:")
print("Enter 4 to Exit:")
choice = int(input("Enter your choice:"))
if choice == 1:
radius = float(input("Enter radius of circle:"))
area = 3.141 * radius * radius
print("Area of radius",radius,"is = ",area.__round__(3))
elif choice == 2:
l = float(input("Enter length of rectangle:"))
b = float(input("Enter breadth of rectangle:"))
area = l * b
print("Area of rectangle of ",l,"x",b,"is = ",area)
elif choice == 3:
base = int(input("Enter the base of triangle:"))
height = float(input("Enter height of triangle:"))
area = 0.5 * base * height
print("Area of triangle of base",base,"and height",height,"is =
",area)
else:
print("Thank You.")
exit()
Enter 1 to calculate area of Circle:
Enter 2 to calculate area of Rectangle:
Enter 3 to calculate area of Triangle:
Enter 4 to Exit:
Enter your choice: 1
Enter radius of circle: 5.98
Area of radius 5.98 is = 112.323

Enter 1 to calculate area of Circle:


Enter 2 to calculate area of Rectangle:
Enter 3 to calculate area of Triangle:
Enter 4 to Exit:
Enter your choice: 2
Enter length of rectangle: 50
Enter breadth of rectangle: 20
Area of rectangle of 50.0 x 20.0 is = 1000.0

Enter 1 to calculate area of Circle:


Enter 2 to calculate area of Rectangle:
Enter 3 to calculate area of Triangle:
Enter 4 to Exit:
Enter your choice: 3
Enter the base of triangle: 20
Enter height of triangle: 20.5
Area of triangle of base 20 and height 20.5 is = 205.0

Enter 1 to calculate area of Circle:


Enter 2 to calculate area of Rectangle:
Enter 3 to calculate area of Triangle:
Enter 4 to Exit:
Enter your choice: 4
Thank You.
# 7. Number guessing game using while loop
n = 27
number_of_guesses = 1
print("Number of guesses is limited to only 5 times: ")
while (number_of_guesses <= 5):
guess_number = int(input("Guess the number :"))
if guess_number < 27:
print("You entered smaller number. Please input greater
number.\n")
elif guess_number > 27:
print("You entered greater number. Please input smaller
number.\n ")
else:
if number_of_guesses == 1:
print("GREAT YOU GUESSED THE NUMBER IN FIRST
ATTEMPT.")
break
else:
print("Contratulations! You guessed in",
number_of_guesses, "attempts.")
break
print(5 - number_of_guesses, "number of guesses left.")
number_of_guesses = number_of_guesses + 1

if(number_of_guesses > 9):


print("Game Over, You lost")
Number of guesses is limited to only 5 times:
Guess the number :89
You entered greater number. Please input smaller number.

4 number of guesses left.


Guess the number :20
You entered smaller number. Please input greater number.

3 number of guesses left.


Guess the number :26
You entered smaller number. Please input greater number.

2 number of guesses left.


Guess the number :27
Contratulations! You guessed in 4 attempts.
#8 Program to enter details of students in a list and then search a roll
number and display the details
addmission = []
choice = "y"
while choice == "y" or choice == "Y":
rollno = int(input("Enter roll number of the student:"))
name = input("Enter name of the student:")
Class = input("Enter class of the student:")
division = input("Enter division of the student:")
percentage = float(input("Enter percentage scored by the student:"))
s_details = [rollno, name, Class, division, percentage]
addmission.append(s_details)
choice = input("Do you want to add more students details? (Y/N):")
print()
print()
print("*DISPLAYING ALL STUDENTS DETAILS*")
for i in range(len(addmission)):
print("Roll Number:", addmission[i][0])
print("Name:", addmission[i][1])
print("Class:", addmission[i][2])
print("Division:", addmission[i][3])
print("Percentage:", addmission[i][4])
print()
rollno = int(input("To search any students details, enter roll number:"))
for i in range(len(addmission)):
if rollno == addmission[i][0]:
print("Name:", addmission[i][1])
print("Class:", addmission[i][2])
print("Division:", addmission[i][3])
print("Percentage:", addmission[i][4])
break
else:
print("Roll Number does not exist.")
Enter roll number of the student: 16
Enter name of the student: Jawaan
Enter class of the student: XI Science
Enter division of the student: A
Enter percentage scored by the student: 95
Do you want to add more students details? (Y/N): N

*DISPLAYING ALL STUDENTS DETAILS*


Roll Number: 16
Name: Jawaan
Class: XI Science
Division: A
Percentage: 95.0

To search any students details, enter roll number: 16


Name: Jawaan
Class: XI Science
Division: A
Percentage: 95.0
# 9. Program to print all odd & even nos. and calculate sum of
all natural, even and odd numbers in a range
print("*Program to print all odd & even nos. and calculate sum
of all natural, even and odd numbers in a range*")
num1 = int(input("Enter starting number:"))
num2 = int(input("Enter ending number:"))
print(f"Even numbers from {num1} to {num2} are:",end="")
for even in range(num1, num2 + 1):
if even % 2 == 0:
print(even,end=" ")
print()
print(f"Odd numbers from {num1} to {num2} are:",end="")
for even in range(num1, num2 + 1):
if even % 2 == 1:
print(even,end=" ")
print()
sum_even = 0
sum_odd = 0
sum_naturals = 0
for n in range(num1, num2 + 1):
sum_naturals += n
if n % 2 == 0:
sum_even += n
else:
sum_odd += n
print(f"The sum of all natural numbers from {num1} to {num2}
is: {sum_naturals}")
print(f"The sum of all even number from {num1} to {num2} is:
{sum_even}")
print(f"The sum of all odd number from {num1} to {num2} is:
{sum_odd}")
*Program to print all odd & even nos. and calculate sum of all
natural, even and odd numbers in a range*
Enter starting number: 1
Enter ending number: 20
Even numbers from 1 to 20 are:2 4 6 8 10 12 14 16 18 20
Odd numbers from 1 to 20 are:1 3 5 7 9 11 13 15 17 19
The sum of all natural numbers from 1 to 20 is: 210
The sum of all even number from 1 to 20 is: 110
The sum of all odd number from 1 to 20 is: 100
# 10. Program to check whether a number or string is a palindrome
or not
print("Program to check whether a number or string is palindrome
or not.")
while True:
num = int(input("Enter an integer to check:"))
temp_num = num
reverse = 0
while(temp_num > 0):
digit = temp_num % 10
reverse = reverse * 10 + digit
temp_num = temp_num // 10
if(num == reverse):
print(num, "number is a palindrome number.")
else:
print(num, "number is not a palindrome number.")
print()
string = input(("Enter a string to check:"))
length = len(string)
mid = length // 2
reverse = -1
for a in range(mid):
if string[a] == string[reverse]:
a += 1
reverse -= 1
else:
print(string, "is not a palindrome string.")
break
else:
print(string, "is a palindrome string.")
choice = input("Do you want to check again? (Y/N):")
if choice == "n" or choice == "N":
break
print()
Enter an integer to check: 4004
4004 number is a palindrome number.

Enter a string to check: jos


jos is not a palindrome string.
Do you want to check again? (Y/N): N
# 11. Working of nested loops
print("Working of 'for' loop inside another 'for' loop.")
no_of_student = int(input("How many students total marks and
percentage do you want to calculate?:"))
for count in range(no_of_student):
print("Enter Marks of Student:", (count + 1))
num_subjects = int(input("How many subjects:"))
sum = 0
percentage = 0
for sub in range(num_subjects):
marks = int(input("Enter marks out of 100 for subject " + str(sub +
1) + " : "))
sum = sum + marks
percentage = (sum * 100) / (num_subjects * 100)
print("Total marks:", sum)
print("Percentage:", percentage)
print()
print("Now Working of 'for' loop inside 'while' loop.")
wish = int(input("To continue press any key else press 0:"))
if wish == 0:
exit()
count = 1
ch = 'y'
while ch == 'y' or ch == "Y":
print("Enter Marks of Student:",count)
num_subjects = int(input("How many subjects:"))
sum = 0
percentage = 0
for sub in range(num_subjects):
marks = int(input("Enter marks out of 100 for subject " + str(sub +
1) + " : "))
sum = sum + marks
percentage = (sum * 100) / (num_subjects * 100)
print("Total marks:", sum)
print("Percentage:", percentage)
print()
count += 1
ch = input("Do you want to calculate for next student? (Y/N):")
Working of 'for' loop inside another 'for' loop.
How many students total marks and percentage do you want to
calculate?: 1
Enter Marks of Student: 1
How many subjects: 3
Enter marks out of 100 for subject 1 : 96
Enter marks out of 100 for subject 2 : 96
Enter marks out of 100 for subject 3 : 96
Total marks: 288
Percentage: 96.0

Now Working of 'for' loop inside 'while' loop.


To continue press any key else press 0: 0
# 12. Program to find LCM & HCF of two numbers
print("Program to find LCM & HCF of two numbers:")
try:
nOne = int(input("Enter First Number:"))
try:
nTwo = int(input("Enter Second Number:"))
if nOne > nTwo:
smaller = nOne
else:
smaller = nTwo
hcf = 0
for i in range(1, smaller + 1):
if nOne % i == 0 and nTwo % i == 0:
hcf = i

lcm = (nOne * nTwo) / hcf


print(f"The LCM of {nOne} and {nTwo} is: {lcm}")
print(f"The HCF of {nOne} and {nTwo} is: {hcf}")
except ValueError:
print("\nInvalid Input!")
except ValueError:
print("\nInvalid Input!")
Program to find LCM & HCF of two numbers:
Enter First Number: 20
Enter Second Number: 100
The LCM of 20 and 100 is: 100.0
The HCF of 20 and 100 is: 20
# 13. Program to count the number of words, lines, characters,
alphabets, digits, capital & small letters, spaces and special characters
sentence = input("Enter a sentence:")
Total_Words = len(sentence.split())
Total_Lines = len(sentence.split(".")) - 1
Total_Char = 0
Total_Alpha = 0
Digits = 0
Capitals = 0
Smalls = 0
Spaces = 0
Specials = 0
for char in sentence:
if char.isalpha():
Total_Alpha += 1
if char.isupper():
Capitals += 1
else:
Smalls += 1
elif char.isdigit():
Digits += 1
elif char.isspace():
Spaces += 1
else:
Specials += 1
print("Total number of words:",Total_Words)
print("Total number of lines:",Total_Lines)
print("Total Characters:",len(sentence))
print("Total Alphabets:",Total_Alpha)
print("Total Digits:",Digits)
print("Total Capital letters:",Capitals)
print("Total Small letters:",Smalls)
print("Total Space:",Spaces)
print("Total Special Characters:",Specials)
Enter a sentence: count() method returns the number of occurences of
the substring in string.
Total number of words: 12
Total number of lines: 1
Total Characters: 76
Total Alphabets: 61
Total Digits: 0
Total Capital letters: 0
Total Small letters: 61
Total Space: 12
Total Special Characters: 3
# 14. Program to enter a sentence and then make a Title Case sentence
print("*Program to enter a sentence and then make a Title Case
sentence*")
sentence = input("Enter a Sentence: ")
print("1st Method: Converting in Title Case using 'while' loop.")
length = len(sentence)
letter = 0
end = length
Title_Case = ""
while letter < length:
if letter == 0:
Title_Case += sentence[0].upper()
letter += 1
elif(sentence[letter] == " " and sentence[letter + 1] != " "):
Title_Case += sentence[letter]
Title_Case += sentence[letter + 1].upper()
letter += 2
else:
Title_Case += sentence[letter]
letter += 1
print("Original Sentence: ", sentence)
print("New Sentence: ", Title_Case)

print("\n2nd Method: Converting in Title Case using 'for' loop.")


words = sentence.split()
Title_Case = ""
last_word = words[len(words) - 1]
for w in words:
if last_word == w:
Title_Case += w.capitalize()
else:
Title_Case += w.capitalize() + " "
print("Original Sentence: ", sentence)
print("New Sentence: ", Title_Case)
*Program to enter a sentence and then make a Title Case sentence*
Enter a Sentence: len()function counts the number of elements in a
String.
1st Method: Converting in Title Case using 'while' loop.
Original Sentence: len()function counts the number of elements in a
String.
New Sentence: len()function Counts The Number Of Elements In A
String.

2nd Method: Converting in Title Case using 'for' loop.


Original Sentence: len()function counts the number of elements in a
String.
New Sentence: Len()function Counts The Number Of Elements In A
String.
# 15. Program to register students and store in a list and then
display the name and registration number
list_of_Add = []
gr_no = []
count = 1
while count <= 5:
ans1 = input("Q.1: Have you Registered yourself? Yes/No?:")
if ans1 in ["NO", "no", "No", "nO"]:
ans2 = input("Q.2: Do you want to Register? Yes/No:")
if ans2 in ["YES", "yes", "Yes"]:
Name = input("Q.3: What is your name?\nName:")
list_of_Add.append(Name)
reg_no = Name[0 : 5 : 2] + str(count)
gr_no.append(reg_no)
print("You have been registered\nThank You.")
print("Your Registration Number is:", reg_no)
print("Total admission done till now:", len(list_of_Add))
if count == 5:
print("SEAT FULL. ADMISSION OVER.")
count += 1
else:
print("Thank You. Have nice day.")
else:
print("Please visit next counter for further information.")
print()
print("Now printing the registered names:")
print("Name\t\t\tGr.No")
for i in range(len(list_of_Add)):
print(list_of_Add[i],"\t\t\t",gr_no[i])
Q.1: Have you Registered yourself? Yes/No?:no
Q.2: Do you want to Register? Yes/No:yes
Q.3: What is your name?
Name: Jawaan
You have been registered
Thank You.
Your Registration Number is: aa1
Total admission done till now: 1
Q.1: Have you Registered yourself? Yes/No?:no
Q.2: Do you want to Register? Yes/No:yes
Q.3: What is your name?
Name: Ashish
You have been registered
Thank You.
Your Registration Number is: si2
Total admission done till now: 2
Q.1: Have you Registered yourself? Yes/No?:no
Q.2: Do you want to Register? Yes/No:yes
Q.3: What is your name?
Name:Vedant
You have been registered
Thank You.
Your Registration Number is: Vdn3
Total admission done till now: 3
Q.1: Have you Registered yourself? Yes/No?:no
Q.2: Do you want to Register? Yes/No:yes
Q.3: What is your name?
Name:Yash
You have been registered
Thank You.
Your Registration Number is: Ys4
Total admission done till now: 4
Q.1: Have you Registered yourself? Yes/No?:no
Q.2: Do you want to Register? Yes/No:yes
Q.3: What is your name?
Name: Poornaya
You have been registered
Thank You.
Your Registration Number is: or5
Total admission done till now: 5
SEAT FULL. ADMISSION OVER.

Now printing the registered names:


Name Gr.No
Jawaan aa1
Ashish si2
Vedant Vdn3
Yash Ys4
Poornaya or5
# 16. Program to enter a list of numbers and then count unique,
duplicate and frequency of each numbers
l = eval(input("Enter a list of numbers:"))
length = len(l)
unique = []
duplicate = []
count = i = 0
while i < length:
element = l[i]
count = 1
if element not in unique and element not in duplicate:
i += 1
for j in range(i,length):
if element == l[j]:
count += 1
else:
print("Element", element,"Frequency:",count)
if count == 1:
unique.append(element)
else:
duplicate.append(element)
else:
i += 1
print("Original List:",l)
print("Unique Elements are:",unique)
print("Duplicate Elements are:",duplicate)
Enter a list of numbers: 2,50,50,40,16,90,0,3,1,2
Element 2 Frequency: 2
Element 50 Frequency: 2
Element 40 Frequency: 1
Element 16 Frequency: 1
Element 90 Frequency: 1
Element 0 Frequency: 1
Element 3 Frequency: 1
Element 1 Frequency: 1
Original List: (2, 50, 50, 40, 16, 90, 0, 3, 1, 2)
Unique Elements are: [40, 16, 90, 0, 3, 1]
Duplicate Elements are: [2, 50]
# 17. Write a program to reverse all two digit numbers in a list
and store at the same place.
# LIST = [25, 9, 5, 565, 65, 98, 456, 35]
# Expected Output: [52, 9, 5, 565, 56, 89, 456, 53]
LIST = [25, 9, 5, 565, 65, 98, 456, 35]
for ele in range(len(LIST)):
if LIST[ele] >= 10 and LIST[ele] <= 99:
quo = LIST[ele] // 10
rem = LIST[ele] % 10
reverse = (10 * rem) + quo
LIST[ele] = reverse
print(LIST)
[52, 9, 5, 565, 56, 89, 456, 53]

You might also like