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

Python 1st Worksheet

Uploaded by

Vijay Rai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python 1st Worksheet

Uploaded by

Vijay Rai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

SOLVED WORKSHEET ON PYTHON PROGRAMMING

Topic: Looping

1. Write a program to input a number and print all numbers from 1 to n


n=int(input("Enter any number"))
for i in range (1,n+1,1):
print(i)
2. Write a program to input a number and print all its factors.
n=int(input("Enter any number"))
for i in range (1,n+1,1):
if(n%i==0):
print(i)
3. Write a program to input a number and print its factorial.
Factorial of a number is the product of all the natural numbers from 1 to n.
5! =1*2*3*4*5=120
n=int(input("Enter any number"))
p=1
for i in range (1,n+1,1):
p=p*i
print(p)
4. Write a program to input a number and check whether it is a prime number or not.
A number which exactly have 2 factors is called Prime number.
n=int(input("Enter any number"))
c=0 #to count numbers of factors
for i in range (1,n+1,1):
if(n%i==0):
c=c+1
if(c==2):
print(f"{n} is the prime number")
else:
print(f"{n} is not the prime number")
5. Write a program to input a number and print sum of digits.
Example:
Enter any number 168
The sum of digits of 168 is 15

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


s=0
a=n
while(a>0):
d=a%10
s=s+d
a=a//10
print(f"The sum of digits of {n} is {s}")

6. Write a program to input a number and check whether it is a palindrome number or not.
A number which is equal to its reverse is called Palindrome number
n=int(input("Enter any number"))
s=0
a=n
while(a>0):
d=a%10
s=s*10+d
a=a//10
if(s==n):
print(f"The {n} is a Palindrome number")
else:
print(f"The {n} is not a Palindrome number")

You might also like