0% found this document useful (0 votes)
54 views11 pages

Practical List

The document contains 15 programming problems with their solutions in Python. Each problem asks the user to write a program to perform a specific task such as inputting and comparing numbers, calculating averages, checking palindromes, generating patterns, and counting vowels/consonants. For each problem, the code for the solution is provided along with example input/output.

Uploaded by

Vivek Kumar
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)
54 views11 pages

Practical List

The document contains 15 programming problems with their solutions in Python. Each problem asks the user to write a program to perform a specific task such as inputting and comparing numbers, calculating averages, checking palindromes, generating patterns, and counting vowels/consonants. For each problem, the code for the solution is provided along with example input/output.

Uploaded by

Vivek Kumar
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/ 11

CS (083) HOLY CROSS SCHOOL, HAZARIBAG KHALID IQBAL

CLASS-XIth (ABCD)

List of Lab Programs with solutions:

1. WAP to Input two numbers and display the larger / smaller number.

Ans #WAP to Input two numbers and display the larger / smaller number

num1=int(input("enter your first number"))

num2=int(input("enter your second number"))

if num1>num2:

print("num1 is greater than num2")

elif num2>num1:

print("num2 is greater than num2")

Output

enter your first number4

enter your second number3

num1 is greater than num2

2. WAP to Input three numbers and display the largest / smallest number.

Ans # WAP to Input three numbers and display the largest / smallest number

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

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

num3=int(input("Enter your third number ="))

if num1>num2 and num1>num3:

print("First Number is larger than others")

elif num2>num1 and num2>num3:

print("Second number is larger than others")

else:

print("Third number is larger than others")

output
Enter your first number =4

Enter your second number =6

Enter your third number =8

Third number is larger than others

3. WAP to display average of two numbers.

Ans # wap to display average of two numbers

num1=int(input("enter your first number ="))

num2=int(input("enter your second number ="))

average_number=((num1+num2)/2)

print("average number of your input",average_number)

output

enter your first number =5

enter your second number =4

average number of your input 4.5

4. Write a program to swap two numbers.

Ans # Write a program to swap two numbers.

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

num2=int(input("Enter your Second number"))

num1,num2=num2,num1

print('num1=',num1)

print('num2=',num2)

output

Enter your first number6

Enter your Second number12

num1= 12

num2= 6

5. WAP to check given number is positive or negative or zero.


Ans # wap to check a given number is positive,negative or zero

num=int(input("Enter your number"))

if num>0:

print("Number is positive")

elif num<0:

print("Number is Negative")

else:

print("Number is zero")

output

Enter your number502

Number is positive

6. WAP to display traffic signals(using if… else statements).

Ans # WAP to display traffic signals(using if… else statements).

Colour=str(input("type your colour" ))

if Colour=='Green':

print("Go")

elif Colour=='Red':

print("Stop")

elif Colour=='Yellow':

print("Wait and only Start the Vehicle")

else:

print("plz write (Green,Red or Yellow) any one:- only one")

output

type your colourRed

Stop

7. WAP to input a number & print its square root.


Ans # Wap to input a number and print its square root

import math # import math module.

a = int(input("Enter a number to get the Square root")) # take an input function and pass the
variable a.

res = math. sqrt(a) # Use math. sqrt()

print("Square root of the number is", res) # print the Square Root

output

Enter a number to get the Square root36

Square root of the number is 6.0

8. WAP to print numbers in the range from 1 to 20 (Use for loop sts).

Ans # WAP to print numbers in the range from 1 to 20 (Use for loop sts)

st1=int(input("enter your value"))

st2=int(input("enter your value"))

for i in range(st1,st2):

print(i)

output

enter your value45

enter your value54

45

46

47

48

49

50

51

52

53

9. WAP to accept a number n from the user & print its table.
Ans

number = int(input ("Enter the number of which the user wants to print the multiplication
table: "))

count = 1

# we are using while loop for iterating the multiplication 10 times.

print("The Multiplication Table of: ", number)

for count in range(1, 11):

print (number,'x',count,'=',number * count)

output

Enter the number of which the user wants to print the multiplication table: 3

The Multiplication Table of: 3

3x1=3

3x2=6

3x3=9

3 x 4 = 12

3 x 5 = 15

3 x 6 = 18

3 x 7 = 21

3 x 8 = 24

3 x 9 = 27

3 x 10 = 30

10. Write the while loop to print the series:- 5, 10, 15, 20, ………………………………….100.

Ans

i=5

while i<=100:
print(i)

i=i+5

output

10

15

20

25

30

35

40

45

50

55

60

65

70

75

80

85

90

95

100

11. Generate the following patterns using nested loop

a) *

**

***
****

*****

Ans

Rows=int(input("Enter your Rows"))

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

for j in range(i):

print('*',end=" ")

print()

output

Enter your Rows5

**

***

****

*****

b) A

AB

ABC

ABCD

ABCDE

Ans

for i in range (65,70):

# inner loop

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

print(chr(j),end="")

print()

output
A

AB

ABC

ABCD

ABCDE

12. Write a program to input a string from the user and print it in the reverse order without
creating a new string.

Ans

st=input("Enter the string:-")

for i in range (-1,-len(st)-1,-1):

print(st[i],end=" ")

output

Enter the string:-3456755644

4465576543

13. Write a program using a user defined function to check if a string is a palindrome or not. (A
string is called palindrome if it reads same backwards as forward. For example, Kanak is a
palindrome.).

Ans

def checkpalin(st):

i=0

j=len(st) -1

while(i<=j):

if (st[i] !=st[j]):

return False

i+=1

j-=1

return True

# end of the function


st=input("Enter a string:-")

results = checkpalin(st)

if results==True:

print("The given string",st,"is a palindeome")

else:

print("The given string",st,"is not a palindeome")

output

Enter a string:-kanak

The given string kanak is a palindeome

14. Write a to display the Fibonacci sequence up to n-th term.

Ans #program to display the fibonacci sequence upto nth term

nterm=int(input("How many terms"))

#first two terms

n1,n2=0,1

count=0

#check if the number of terms is valid

if nterm<=0:

print("Please enter a positive integer")

#if there is only one term ,return 01

elif nterm==1:

print("Fibonacci sequence upto",nterms," ")

print(n1)

#generate fibonacci sequence

else:

print("fibonacci sequence:")

while count<nterm:

print(n1)
nth=n1+n2

#update values

n1=n2

n2=nth

count+=1

output

How many terms10

fibonacci sequence:

13

21

34

15. Program to count the total number of vowels and consonants in a string.

Ans # Define all the vowels in a list

vowels=['a','e','i','o','u']

#input a string and transform it to lower case

str=input("Enter a string").lower()

#Define ciunter variable for both vowels and consonants

v_ctr=0

c_ctr=0

#ilerate through the characters of the input string


for x in str:

# if character is in the vowels list.

#update the vowel counter otherwise update consonant

if x in vowels:

v_ctr+=1

else:

c_ctr+=1

#output the values of the counters

print("vowels:",v_ctr)

print("consonants",c_ctr)

output

Enter a stringraj verma

vowels: 3

consonants 6

You might also like