0% found this document useful (0 votes)
6 views18 pages

3

Document

Uploaded by

dhritipal515
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)
6 views18 pages

3

Document

Uploaded by

dhritipal515
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/ 18

Page no.

Q18.Write a program in python to check a number whether


it is Armstrong or not.
Program:

n=eval(input("Enter a number:"))
s=0
p=n
while p!=0:
r=p%10
p=p//10
s=s+(r*r*r)
if n==s:
print("The number is Armstrong")
else:
print("The number is not Armstrong")
Output:
Enter a number:153
The number is Armstrong.

Enter a number:145
The number is not Armstrong.
Page no.:

Q5. .Write a program in python to print this format.


*
**
***
****
*****
Program:

n=eval(input("Enter the number of rows:"))


for i in range(1,n+1):
for j in range(1,i+1):
print('*',end=' ')
print(' ')

Output:
Enter the number of rows:5
*
**
***
****
*****
Page no.:

Q8. Write a program in python to print this format.


*
**
***
****
***
**
*

Program:

r=eval(input("Enter a number:"))
m=r//2+1
for i in range(1,m+1):
for j in range(1,i+1):
print("*",end=" ")
print('')
for i in range(m+1,r+1):
for j in range(1,r-i+2):
print("*",end=" ")
print('')
Output:
Enter a number:7
*
**
***
****
***
**
*
Page no.:

Q22. Write a program in python to create Fibonacci series


using recursion.
Program:

def fib(n):
if n==1:
return 0
elif n==2:
return 1
else:
return fib(n-1)+fib(n-2)
n=eval(input("Enter the term:"))
f=1
for i in range(1,n+1):
f=fib(i)
print(f,end=" ")

Output:
Enter the term:10
0 1 1 2 3 5 8 13 21
Page no.:

Q12.write a program in python to check whether the


number is even or odd using function.

Program:

def even_odd(n):
r=n%2
if r==0:
print("The number is Even")
else:
print("The number is Odd")
n=eval(input("Enter a number:"))
even_odd(n)

Output:

Enter a number:53
The number is Odd

Enter a number:86
The number is Even
Page no.:

Q13.Write a program in python to check whether the


number is even or odd using recursive function.
Program:

def is_even(n):
r=n%2
if r==0:
return True
else:
return False
n=eval(input("Enter a number:"))
e=is_even(n)
if e==True:
print("The number is Even")
else:
print("The number is Odd")

Output:

Enter a number:69
The number is Odd

Enter a number:96
The number is Even
Page no.:

Q11. Write a program in python to reverse a string using


backward loop.
Program:

s='ABCDEF'
l=len(s)
s1=' '
for i in range(5,-1,-1):
s1=s1+s[i]
print("The reversed string is:",s1)

Output:

The reversed string is: FEDCBA


Page no.:

Q10. Write a program in python to reverse a string using


forward loop.

Program:

s='ABCDEF'
l=len(s)
s1=' '
for i in range(l):
s1=s[i]+s1
print(“The reversed string is:”,s1)

Output:

The reversed string is: FEDCBA


Page no.:

Q21.write a program in python to create Fibonacci series.


Program:

i,j=0,1
fib=int(input("Enter the term:"))
while(fib>0):
print(i,end=" ")
i=i+j
j=i-j
fib-=1

Output:

Enter the term:10


0 1 1 2 3 5 8 13 21 34
Page no.:

Q16.Write a program in python to know the factorial of a


number.

Program:

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

if n<0:
print("Invalid syntax:")
else:
f=1
for i in range(1,n+1):
f=f*i
print ("The factorial is:",f)

Output:

Enter a number:7
The factorial is: 5040
Page no.:

Q17 .Write a program in python to know the factorial of a


number using recursion.

Program:

def fact(n):
if n==0:
return 1
else:
return n*fact(n-1)
n=eval(input("Enter the number:"))
print("The factorial of the number is:",fact(n))

Output:

Enter the number:8


The factorial of the number is: 40320
Page no.:

Q20. .Write a program in python to check a string if it is


palindrome or not using recursion.
Program:

def isPalindrome(str):
for i in range(0,int(len(str)/2)):
if str[i]!=str[len(str)-i-1]:
return False
return True
s=str(input("Enter the string: "))
pal=isPalindrome(s)
if pal:
print("The string is Palindrome")
else:
print("The string is Not Palindrome")

Output:

Enter the string: NOON


The string is Palindrome

Enter the string: MADAM


The string is Palindrome

Enter the string: COMPUTER


The string is Not Palindrome
Page no.:

Q14 .Write a program in python to check a number whether


it is prime or not.
Program:

n=eval(input("Enter the number :"))


p=0
for i in range(1,n+1):
if(n%i==0):
p+=1
if p==2:
print("The number is Prime.")
else:
print("The number is not Prime.")

Output:

Enter the number :29


The number is Prime.

Enter the number :39


The number is not Prime.
Page no.:

Q15. .Write a program in python to check a number


whether it is prime or not using recursion(sqrt function).

Program:

import math
def is_prime(y):
p=True
sq=int(math.sqrt(y))
for i in range(2,sq+1):
if y%i==0:
p=False
break
return p
r=eval(input("Enter the number:"))
if is_prime(r)==True:
print(f"{r} is prime.")
else:
print(f"{r} is not prime.")

Output:

Enter the number:59


59 is prime.

Enter the number:76


76 is not prime.
Page no.:

Q9.Write a program in python to add two numbers.

Program:

p=eval(input("Enter the first number:"))


r=eval(input("Enter the second number:"))
x=p+r
print("The sum of two numbers is=",x)

Output:

Enter the first number:56


Enter the second number:87
The sum of two numbers is= 143
Page no.:

Q19.Write a program in python to check a number if it is


palindrome or not.
Program:

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


r=0
n1=n
while n!=0:
rem=n%10
r=(r*10)+rem
n=n//10
print(r)
if n1==r:
print("The number is Palindrome")
else:
print("The number is Non-Palindrome")

Output:
Enter the number:456654
The number is Palindrome

Enter the number:8568


The number is Non-Palindrome
Page no.:

Q6.Write a program in python to print this format.


*****
****
***
**
*
Program:

r=eval(input("Enter the number of rows:"))


for i in range(1,r+1):
for j in range(r-i+1,0,-1):
print('*',end=' ')
print(' ')

Output:
Enter the number of rows:5
*****
****
***
**
*
Page no.:

Q7. Write a program in python to print this format.

1
12
123
1234
12345

Program:

n=eval(input("Enter the number of rows:"))


for i in range(1,n+1):
for j in range(1,i+1):
print(j,end=' ')
print(' ')

Output:
Enter the number of rows:5
1
12
123
1234
12345

You might also like