You are on page 1of 6

Name: Pranali Govande Class: CO6I

Roll No.:15[fs]
1.Print the following patterns using loop:
for i in range (1,5):
for i in range(1,i+1):
print("*",end="")
print()
Output:

2.Write a Python program to print all even numbers between 1 to 100 using
while loop.
number = 1
while number <= 100:
if(number % 2 == 0):
print("{0}".format(number))
number = number + 1
Output:
3.Write a Python Program to find sum of all natural numbers using for
loop.
num = 10
if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate until zero
while(num > 0):
sum += num
num -= 1
print("The sum of first 10 natural number is:", sum)
Output:

4.Write a Python Program to print Fibonacci series.


nterms = int(input("How many terms? "))
n1, n2 = 0, 1
count = 0
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci series upto",nterms,":")
print(n1)
else:
print("Fibonacci series upto:",nterms)
while count < nterms:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
Output:

5.Write a Python program to Reverse a Given number.


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

6.Write a Python Program takes in a number and finds the sum of digits in
a number.
n=int(input("Enter a number:"))
sum=0
while(n>0):
dig=n%10
sum=sum+dig
n=n//10
print("The total sum of digits is:",sum)
Output:
8.Write a Python Program that takes in a number and checks whether it is
a palindrome or not.
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!")
Output:

You might also like