STD 8 Python New

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Std 8: New python programs added to the final term practical exam

Program 1: Write a program to count total number of digits in a given


number.

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

count=0

while(n>0):

count=count+1

n=n//10

print("The number of digits in the number are:",count)

Output:

Enter number:1234

The numbers of digits in the number are: 4

Program 2: Write a program to find factorial of a given number

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

factorial = 1

if n>=1:

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

factorial= factorial * i

print(" factorial of a number is :", factorial)

Output:

Enter a number:5

factorial of a number is : 120

Program 3: Write a program to print Fibonacci series

N = int(input("Enter Number = " ))


a=0

b=1

c=0

if N < 0:

print("Invalid input")

elif N == 0:

print("0")

elif N == 1:

print("0\n1")

else:

print("0\n1 ")

for i in range(N-1):

c=a+b

print(c)

a=b

b=c

Output:

Enter Number = 5

3
5

Program 4 Write a Python program to swap two variables

# To take inputs from the user

x = input('Enter value of x: ')

y = input('Enter value of y: ')

#create a temporary variable and swap the values

temp = x

x=y

y = temp

print('The value of x after swapping: {}'.format(x))

print('The value of y after swapping: {}'.format(y))

Output:

Enter value of x: 12

Enter value of y: 34

The value of x after swapping: 34

The value of y after swapping: 12

Program 5 Write a Python program to reverse the digits in a given number

n = int(input("Enter the integer number: "))


rn = 0
while (n > 0):
rem = n % 10
rn = (rn * 10) + rem
n = n // 10
print("The reverse number is :",rn)
Output:

Enter the integer number: 45632


The reverse number is : 23654

You might also like