PDF 25:12:2023 15:00:57-1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

COMPUTER SCIENCE (NEW) - 083

INDEX
S. Program Date Signature
No.
1. Sum & Product of a number 12-04-2023
2. Square & Cube of number 15-04-2023
3. Arithmetic operations 17-04-2023
4. Print Table of a number 27-04-2023
5. Maximum of numbers 28-04-2023
6. Prime Number 28-04-2023
7. Swapping of two number 04-07-2022
8. Operations on Circle 07-07-2023
9. Compound Interest 11-07-2023
10. Palindrome Number 12-07-2023
11. Operation on Circle using function 14-07-2023
12. Integer and Character Value 20-07-2023
13. Number, Alphabet or Special Character 22-07-2023
14. Checking divisibility 24-07-2023
15. Factorial of a number 24-07-2023
16. Armstrong number 28-07-2023
17. Fibonacci Series 28-07-2023
18. Printing patterns 01-08-2023
19. Sorting of list elements 01-08-2023
20. Working on Dictionaries 02-08-2023
21. String palindrome 11-08-2023
22. Binary Search 12-08-2023
23. Linear search 14-08-2023
24. Random numbers 17-08-2023
25. Writing data into file 18-08-2023
26. Displaying file contents 19-08-2023
27. Counting characters in data file 21-08-2023
28. Counting words in text file 26-08-2023
29. Most common word 28-08-2023
30. Longest word in text file 28-08-2023
31. Text File Operations 29-08-2023
32. Transferring text from files 29-08-2023
33. Counting words in text file - split function 01-09-2023
34. CSV file Operations 01-09-2023
35. Vowels in text file 02-09-2023
36. CSV file 04-09-2023
37. Read write data from CSV file 08-09-2023
38. Vowels and consonants in text file 09-09-2023
39. Read and display CSV file 13-09-2023
40. Stack in Python 17-10-2023
41. Stack (Push-Pop) 20-10-2023
42. Stack Operations 26-10-2023
43. Python-MySQL 28-10-2023
44. Python-MySQL 02-11-2023
45. Python-MySQL 03-11-2023
46. Python-MySQL 04-11-2023
47. Python-MySQL 22-11-2023
48. MySQL 23-11-2023
PROGRAM : 1

# Write a python program to take input for 2 numbers, calculate and print their sum,
product and difference?

a=int(input("Enter 1st no "))

b=int(input("Enter 2nd no "))

s=a+b

p=a*b

if(a>b):

d=a-b

else:

d=b-a

print("Sum = ",s)

print("Product = ",p)

print("Difference = ",d)
PROGRAM : 2

#Write a python Program to take input for a number, calculate and print its square
and cube?

a=int(input("Enter any no "))

b=a*a

c=a*a*a

print("Square = ",b)

print("cube = ",c)
PROGRAM : 3

# Menu based program to find sum, subtraction, multiplication and division of values.

print("1. Sum of two numbers")

print("2. Subtraction of two numbers")

print("3. Multiplication of two numbers")

print("4. Division of two numbers")

choice = int(input('Enter your choice : '))

if choice==1 :

a=int(input('Enter first number : '))

b=int(input('Enter second number : '))

c=a+b

print("Sum of two numbers =",c)

elif choice==2 :

a=int(input('Enter first number : '))

b=int(input('Enter second number : '))

c=a-b

print("Subtraction of two numbers =",c)


elif choice==3 :

a=int(input('Enter first number : '))

b=int(input('Enter second number : '))

c=a*b

print("Multiplication of two numbers =",c)

elif choice==4 :

a=int(input('Enter first number : '))

b=int(input('Enter second number : '))

c=a/b

print("Division of two numbers =",c)

else :

print("Wrong choice")
PROGRAM : 4

# Python Program to Print Multiplication Table of a Number.

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

for i in range(1, 11):

print(num, 'x', i, '=', num*i)


PROGRAM : 5

#Write a python program to take input for 3 numbers, check and print the largest
number?

a=int(input("Enter 1st no :"))

b=int(input("Enter 2nd no :"))

c=int(input("Enter 3rd no :"))

if(a>b and a>c):

m=a

else:

if(b>c):

m=b

else:

m=c

print("Max no = ",m)
PROGRAM : 6

# Program in python to check a number whether it is prime or not.

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

for i in range(2,num):

if num % I == 0:

print(num, "is not prime number.")

break;

else:

print(num,"is prime number.")


PROGRAM : 7

# Python Program to exchange the values of two numbers using a temporary


variable.

def swap(x, y):

temp = x

x=y

y = temp

print("\n\nAfter swapping")

print("Value of first number :", x)

print("Value of second number :", y)

num1 = int(input("Enter first number :"))

num2 = int(input("Enter second number :"))

print("\n\nBefore swapping")

print("Value of first number :",num1)

print("Value of second number :",num2)

swap(num1,num2)
PROGRAM : 8

# Python Program to find Diameter, Circumference and Area Of a Circle using


function

import math

def cal_Diameter(radius):

return 2 * radius

def cal_Circum(radius):

return 2 * math.pi * radius

def cal_Area(radius):

return math.pi * radius * radius

r = float(input("Enter the radius of a circle: "))

diameter = cal_Diameter(r)

circumference = cal_Circum(r)

area = cal_Area(r)

print("\nDiameter of a Circle = %.2f" %diameter)

print("\nCircumference of a Circle = %.2f" %circumference)

print("\nArea of a Circle = %.2f" %area)


PROGRAM : 9

# Program to calculate compound interest.

p=float(input("Enter the principal amount : "))

r=float(input("Enter the rate of interest : "))

t=float(input("Enter the time in years : "))

x=(1+r/100)**t

CI= p*x-p

print("Compound interest is : ", round(CI,2))


PROGRAM : 10

# Program to check a number whether it is palindrome or not.

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

n=num

res=0

while num>0:

rem=num%10

res=rem+res*10

num=num//10

if res==n:

print("Number is Palindrome")

else:

print("Number is not Palindrome")


PROGRAM : 11

# Program to find Diameter, Circumference and Area of a Circle using function.

import math

def cal_Diameter(radius):

return 2 * radius

def cal_Circum(radius):

return 2 * math.pi * radius

def cal_Area(radius):

return math.pi * radius * radius

r = float(input("Enter the radius of a circle: "))

diameter = cal_Diameter(r)

circumference = cal_Circum(r)

area = cal_Area(r)

print("\nDiameter of a Circle = %.2f" %diameter)

print("\nCircumference of a Circle = %.2f" %circumference)

print("\nArea of a Circle = %.2f" %area)


PROGRAM : 12

# To find character value and integer value vice versa.

var=True

while var:

choice=int(input("Press-1 to find the ordinal value of a character \nPress-2 to find


a character of a value\n"))

if choice==1:

ch=input("Enter a character : ")

print(ord(ch))

elif choice==2:

val=int(input("Enter an integer value: "))

print(chr(val))

else:

print("You entered wrong choice.")

print("Do you want to continue? Y/N")

option=input()

if option=='y' or option=='Y':

var=True

else:

var=False
PROGRAM : 13

# Program to input a character and to print whether a given character is an alphabet,


digit or any other character.

ch=input("Enter any key from the keyboard: ")

if ch.isalpha():

print(ch, "is an alphabet.")

elif ch.isdigit():

print(ch, "is a digit.")

elif ch.isalnum():

print(ch, "is alphanumeric.")

else:

print(ch, "is a special symbol.")


PROGRAM : 14

#Program to check whether a number is divisible by 2 or 3 using nested if.

num=float(input('Enter a number'))

if num%2==0:

if num%3==0:

print ("Divisible by both 3 and 2")

else:

print ("Divisible by 2 but not divisible by 3")

else:

if num%3==0:

print ("Divisible by 3 but not divisible by 2")

else:

print ("Neither Divisible by 2 nor divisible by 3")


PROGRAM : 15

# Program to calculate the factorial of an integer using recursion.

def factorial(n):

if n == 1:

return n

else:

return n*factorial(n-1)

num=int(input("enter the number: "))

if num < 0:

print("Sorry, factorial does not exist for negative numbers")

elif num == 0:

print("The factorial of 0 is 1.")

else:

print("The factorial of ",num," is ", factorial(num))


PROGRAM : 16

#Write a python program to take input for a number check if the entered number is
Armstrong or not.

n=int(input("Enter the number to check the number is armstrong or not? : "))

n1=n

s=0

while(n>0):

d=n%10

s=s + (d *d * d)

n=int(n/10)

if s==n1:

print("Armstrong Number")

else:

print("Not an Armstrong Number")


PROGRAM : 17

# Program to print Fibonacci series using recursion.

def fibonacci(n):

if n<=1:

return n

else:

return(fibonacci(n-1)+fibonacci(n-2))

num=int(input("How many terms you want to display: "))

for i in range(num):

print(fibonacci(i)," ", end=" ")


PROGRAM : 18

# Program to print following pattern.

'''*

**

***

****

*****'''

for i in range(1,6):

print()

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

print ('*',end="")
PROGRAM : 19

# Program to sort values in a list.

aList=[10,5,1,3]

print("Original List",aList)

n=len(aList)

for i in range(n-1):

for j in range(0,n-i-1):

if aList[j]>aList[j+1]:

aList[j],aList[j+1]=aList[j+1],aList[j]

print("Sorted List",aList)
PROGRAM : 20

#Countries and their Capital with currency

d1=dict(),i=1

n=int(input("enter number of entries"))

while i<=n:

c=input("enter country:")

cap=input("enter capital:")

curr=input("enter currency of country")

d1[c]=[cap,curr]

i=i+1

l=d1.keys()

print("\ncountry\t\t","capital\t\t","currency")

for i in l:

z=d1[i]

print(i,'\t\t',end=" ")

for j in z:

print(j,'\t\t',end='\t\t')

x=input("\nenter country to be searched:")

for i in l:

if i==x:

print("\ncountry\t\t","capital\t\t","currency\t\t")

z=d1[i]

print(i,'\t\t',end=" ")

for j in z:

print(j,'\t\t',end="\t")

break
PROGRAM : 21

# Recursive python program to test if a string is palindrome or not.

def isStringPalindrome(str):

if len(str)<=1:

return True

else:

if str[0]==str[-1]:

return isStringPalindrome(str[1:-1])

else:

return False

#__main__

s=input("Enter the string : ")

y=isStringPalindrome(s)

if y==True:

print("String is Palindrome")

else:

print("String is Not Palindrome")


PROGRAM : 22

# Program for binary search.

def Binary_Search(sequence, item, LB, UB):

if LB>UB:

return -5 # return any negative value

mid=int((LB+UB)/2)

if item==sequence[mid]:

return mid

elif item<sequence[mid]:

UB=mid-1

return Binary_Search(sequence, item, LB, UB)

else:

LB=mid+1

return Binary_Search(sequence, item, LB, UB)

L=eval(input("Enter the elements in sorted order (like 1,2,3,4): "))

n=len(L)

element=int(input("Enter the element that you want to search :"))

found=Binary_Search(L,element,0,n-1)

if found>=0:

print(element, "Found at the index : ",found)

else:

print("Element not present in the list")


PROGRAM : 23

# Program for linear search.

L=eval(input("Enter the elements: "))

n=len(L)

item=eval(input("Enter the element that you want to search : "))

for i in range(n):

if L[i]==item:

print("Element found at the position :", i+1)

break

else:

print("Element not Found")


PROGRAM : 24

# Program to generate random numbers between 1 to 6 and check whether a user


won a lottery or not.

import random

n=random.randint(1,6)

guess=int(input("Enter a number between 1 to 6 :"))

if n==guess:

print("Congratulations, You won the lottery ")

else:

print("Sorry, Try again, The lucky number was : ", n)


PROGRAM : 25

# Program to write rollno, name and marks of a student in a data file Marks.dat.

count=int(input('How many students are there in the class'))

fileout=open("Marks.dat","a")

for i in range(count):

print("Enter details of student",(i+1),"below")

rollno=int(input("Enter rollno:"))

name=input("name")

marks=float(input('marks'))

rec=str(rollno)+","+name+","+str(marks)+"\n"

fileout.write(rec)

fileout.close()
PROGRAM : 26

# Program to read and display contents of file Marks.dat.

fileinp=open("Marks.dat","r")

while str:

str=fileinp.readline()

print(str)

fileinp.close()
PROGRAM : 27

# Program to count number of characters in data file data.txt.

file1=open("D:\\data.txt","r")

ch=" "

count=0

while ch:

ch=file1.read(1)

count+=1

print("Number of characters=",count)

file1.close()
PROGRAM : 28

# Program to count number of words in data file data.txt.

file1=open("D:\\data.txt","r")

line=" "

count=0

while line:

line=file1.readline()

s=line.split()

for word in s:

count+=1

print("Number of words=",count)

file1.close()
PROGRAM : 29

# Write a program to find the most common words in a file.

import collections

fin = open('D:\\data.txt','r')

a= fin.read()

d={ }

L=a.lower().split()

for word in L:

word = word.replace(".","")

word = word.replace(",","")

word = word.replace(":","")

word = word.replace("\"","")

word = word.replace("!","")

word = word.replace("&","")

word = word.replace("*","")

for k in L:

key=k

if key not in d:

count=L.count(key)

d[key]=count

n_print = int(input("How many most common words to print: "))

print("\nOK. The {} most common words are as follows\n".format(n_print))

word_counter = collections.Counter(d)

for word, count in word_counter.most_common(n_print):

print(word, ": ", count)

fin.close()
PROGRAM : 30

# Program to find longest word from text file

fin = open("name.txt","r")

str = fin.read()

words = str.split()

max_len = len(max(words, key=len))

for word in words:

if len(word)==max_len:

longest_word =word

print(longest_word)
PROGRAM : 31

# Write a python program to read a file named "filedetail.txt", count and print the
following:

"""

(i) Length of the file total characters in file)

(ii) Total alphabets

(iii) Total upper case alphabets

(iv) Total lower case alphabets

(v) Total digits

(vi) Total spaces

(vii) Total special characters

"""

def count():

a=0

ua=0

la=0

d=0

sp=0

spl=0

f= open("D:\filedetail.txt")

while True:

c=f.read(1)

if not c:

break

print(c,end=' ')

if((c>='A' and c<='Z') or (c>='a' and c<='z')):

a=a+1
if((c>='A' and c<='Z') or (c>='a' and c<='z')):

a=a+1

if(c>='A' and c<='Z'):

ua=ua+1

else:

la=la+1

elif(c>='0' and c<= '9'):

d=d+1

elif (c==' ') :

sp=sp+1

else:

spl=spl+1

print ("total alphabets ",a)

print("total upper case alphabets ",ua)

print ("total lower case alphabets ",la)

print("total digits ",d)

print ("total spaces ",sp)

print("total special characters ",spl)

# function calling

count()

You might also like