Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
94 views

Python Program

Uploaded by

shrutithearmy403
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Python Program

Uploaded by

shrutithearmy403
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Q1.Input a welcome message and display.

message=input("Enter welcome message : ")


print("Hello, ",message)

Q2. Input two numbers and display the larger/smaller number.

#input first number


num1=int(input("Enter First Number"))
#input Second number
num2=int(input("Enter Second Number"))
#Check if first number is greater than second
if (num1>num2):
print("The Larger number is", num1)
else:
print ("The Larger number is", num2)

Q3. Input three numbers and display the larger/smaller number.

#input first,Second and third number


num1=int(input("Enter First Number"))
num2=int(input("Enter Second Number"))
num3=int(input("Enter Third Number"))
#Check if first number is lesser than rest of the two numbers.
if (num1< num2 and num1< num3):
print("The Smallest number is", num1)
#Check if Second number is greater than rest of the two numbers.
elif (num2 < num1 and num2< num3):
print ("The Smallest number is", num2)
else:
print ("The Smallest number is", num3)

Q4.*
**
***
****
*****
# Function to demonstrate printing pattern
def pypart(n):

# outer loop to handle number of rows


# n in this case
for i in range(0, n):

# inner loop to handle number of columns


# values changing acc. to outer loop
for j in range(0, i+1):
# printing stars
print("* ",end="")

# ending line after each row


print("\r")

# Driver Code
n = 5
pypart(n)

Q5. 12345
1234
123
12
1
def num(a):

# initialising starting number


num = 1
# outer loop to handle number of rows
for i in range(0, a):
# re assigning num
num = 1
# inner loop to handle number of columns
# values changing acc. to outer loop
for k in range(0, i+1):
# printing number
print(num, end=" ")
# incrementing number at each column
num = num + 1
# ending line after each row
print("\r")
# Driver code
a=5
num(a)

Q6. A
AB
ABC
ABCD
ABCDE
n = int(input("Enter number of rows: "))

for i in range(1,n+1):
a = 97
for j in range(1, i+1):
print("%c" %(a), end="")
a += 1
print()

Q11Write a python program to determine given number is perfect,an armstrong or palindrome number.
n = int(input("Enter any number to check whether it is perfect ,armstrong or palindrome : "))
sum = 0
# Check for perfect number
for i in range(1,n):
if n%i==0:
sum = sum + i

if sum == n :
print( n,"is perfect number")
else :
print( n, "is not perfect number")
#check for armstrong number
temp = n
total = 0
while temp > 0 :
digit = temp %10
total = total + (digit**3)
temp = temp//10
if n == total:
print( n,"is an armstrong number")
else :
print( n, "is not armstrong number")
#check for palindrome number
temp = n
rev = 0
while n > 0:
d = n % 10
rev = rev *10 + d
n = n//10
if temp == rev :
print( temp,"is palindrome number")
else :
print( temp, "is not palindrome number")
n = int(input("Enter any number to check whether it is perfect number or not : "))
sum = 0
# Check for perfect number
for i in range(1,n):
if n%i==0:
sum = sum + i

if sum == n :
print( n,"is perfect number")
else :
print( n, "is not perfect number")
#check for armstrong number
n = int(input("Enter any number to check whether it is an armstrong : "))
temp = n
total = 0
while temp > 0 :
digit = temp %10
total = total + (digit**3)
temp = temp//10
if n == total:
print( n,"is an armstrong number")
else :
print( n, "is not armstrong number")

#check for palindrome number


n = int(input("Enter any number to check whether it is palindrome : "))
temp = n
rev = 0
while n > 0:
d = n % 10
rev = rev *10 + d
n = n//10
if temp == rev :
print( temp,"is palindrome number")
else :
print( temp, "is not palindrome number")

Q12 write a program in python to check if the input number is prime or composite number

num = int(input("Enter any number : "))


if num > 1:
for i in range(2, num):
if (num % i) == 0:
print(num, "is NOT a prime number")
break
else:
print(num, "is a PRIME number")
elif num == 0 or 1:
print(num, "is a neither prime NOR composite number")
else:
print(num, "is NOT a prime number it is a COMPOSITE number")
Q13. write a program in python to display fibonacci series
# Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many terms? "))

# first two terms


n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
q14. write a python program to compute the greatest common divisor and least common
multiple of two integers
python program to find LCM of two number using GCD

#input two numbers


n1 = int(input("Enter First number :"))
n2 = int(input("Enter Second number :"))
x = n1
y = n2
while(n2!=0):
t = n2
n2 = n1 % n2
n1 = t
gcd = n1
print("GCD of {0} and {1} = {2}".format(x,y,gcd))
lcm = (x*y)/gcd
print("LCM of {0} and {1} = {2}".format(x,y,lcm))
Q15. write a python program to count and display the number of uppercase lowercase
characters in string
s = input("Enter a sentence: ")
w, d, u, l = 0, 0, 0, 0
l_w = s.split()
w = len(l_w)
for c in s:
if c.isdigit():
d=d+1
elif c.isupper():
u=u+1
elif c.islower():
l=l+1

print ("No of Words: ", w)


print ("No of Digits: ", d)
print ("No of Uppercase letters: ", u)
print ("No of Lowercase letters: ", l)

Q16. write a python program to input a string and determine whether it is a palindrome or
not, convert the case of characters in a string
# Program to check if a string is palindrome or not

my_str = 'aIbohPhoBiA'

# make it suitable for caseless comparison


my_str = my_str.casefold()

# reverse the string


rev_str = reversed(my_str)

# check if the string is equal to its reverse


if list(my_str) == list(rev_str):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
Q17. write a python program to find the largest and smallest number in a list
lst = []
num = int(input('How many numbers: '))for n in range(num):
numbers = int(input('Enter number '))
lst.append(numbers)print("Maximum element in the list is :", max(lst),
"\nMinimum element in the list is :", min(lst))
Q18. write a python program to input a list of numbers and swap elements at the even
location with the elements at the odd location
val=eval(input("Enter a list "))

print("Original List is:",val)

s=len(val)

if s%2!=0:

s=s-1

for i in range(0,s,2):

val[i],val[i+1]=val[i+1],val[i]
print("List after swapping :",val)

Q19. input a list/tuple of elements, search for a given element in the list/tuple.

mylist = []
print("Enter 5 elements for the list: ")
for i in range(5):
value = int(input())
mylist.append(value)
print("Enter an element to be search: ")
element = int(input())
for i in range(5):
if element == mylist[i]:
print("\nElement found at Index:", i)
print("Element found at Position:", i+1)

Q20. write a program in python to create a dictionary with the roll number,name and marks of n students in a class
and display the name of students who have marks above 75.

no_of_std = int(input("Enter number of students: "))


result = {}
for i in range(no_of_std):
print("Enter Details of student No.", i+1)
roll_no = int(input("Roll No: "))
std_name = input("Student Name: ")
marks = int(input("Marks: "))
result[roll_no] = [std_name, marks]
print(result)
# Display names of students who have got marks more than 75
for student in result:
if result[student][1] > 75:
print("Student's name who get more than 75 marks is/are",(result[student][0]))

You might also like