0% found this document useful (0 votes)
3 views

loop

The document contains several Python programs that demonstrate various functionalities, including calculating the factorial of a number, checking if a variable is numeric, counting digits in a number, reversing a number, checking for palindromes, printing multiplication tables, and finding the average of a set of numbers. Each program includes user input and basic control structures. The examples illustrate fundamental programming concepts in Python.

Uploaded by

riaz ahamed
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)
3 views

loop

The document contains several Python programs that demonstrate various functionalities, including calculating the factorial of a number, checking if a variable is numeric, counting digits in a number, reversing a number, checking for palindromes, printing multiplication tables, and finding the average of a set of numbers. Each program includes user input and basic control structures. The examples illustrate fundamental programming concepts in Python.

Uploaded by

riaz ahamed
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/ 2

1.

Python Program to check the type


a) def factorial (n):
if not isinstance(n, int):
print 'Factorial is only defined for integers.'
return None
elif n < 0:
print 'Factorial is not defined for negative integers.'
return None
elif n == 0:
return 1
else:
return n * factorial(n-1)

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

b) def check_number(var):
if isinstance(var, (int, float)):
print('variable', var, 'is instance of numeric type')
else:
print('variable', var, 'is not instance of numeric type')

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


check_number(num1)
num2 = float(input("Enter number:"))
check_number(num2)

2.Python Program to count the number of digits in a 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)

3. Python Program to reverse a given number


n=int(input("Enter number: "))
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
print("Reverse of the number:",rev)

4.Python Program to check whether a given number is a palindrome


n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")

5. Python Program to print the table of a given number

n=int(input("Enter the number to print the tables for:"))


for i in range(1,11):
print(n,"x",i,"=",n*i)

6. Python program to find the average of n numbers

n = int(input('How many numbers: '))


total_sum = 0
for i in range (n):
num = float(input('Enter number: '))
total_sum += num

avg = total_sum / n
print('The average value of numbers = %0.2f' %avg)

You might also like