1.
Positive negative
2. num=int(input('enter your number'))
3. if(num>0):
4. print(num,' is pssitive')
5. elif(num<0):
6. print(num,'is negative')
7. else:
8. print('num is ',num)
2 .voting eligibility
name=input('enter your name ')
age=int(input('enter your age '))
if(age>=18):
print(name,'you are eligible for voting')
elif(age<18):
print(name,'you are not eligible for voting')
3. sum of two no
a=5
b=6
print("the value of a+b is : ", a+b)
print("the value of a - b is : ", a - b )
print("the value of a ^ * b is : ", a ^ b )
print("the value of a/b is : ", a / b ) # a to the power b
print ('power: ', a ** b)
# # floor division
print ('Floor Division: ', a // b)
4. leap year
#leap year
year=int(input("Enter a year >> "))
def check_leap(year):
if ( (year%400==0) or (year%100!=0) and (year%4==0)):
print("Given year is a leap year")
else:
print("Given year is NOT a leap year")
check_leap(year)
5. even odd
a=int(input('enter your number'))
if(a%2==0):
print(a, 'number is even')
else:
print(a,'num is odd')
6.vowel or consonant
character = input("Enter a character: ")
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
if character in vowels:
print(f"The character '{character}' is a vowel!")
else:
print(f"The character '{character}' is a consonant!")
7. palindrome
#palindrom
string=input(("Enter a letter:"))
if(string==string[::-1]):
print("The letter is a palindrome")
else:
print("The letter is not a palindrome")
8.prime no
## prime no
num =int(input("Enter your no >>> ")) # If given number
is greater than 1
if num > 1: # Iterate from 2 to n /
2
for i in range(2, int(num/2)+1): # If num is
divisible by any number between
if (num % i) == 0: # 2 and n / 2, it is not
prime
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
9. largest of three
# ### greater then 3 no
a = int(input("Enter your no 1 >>> "))
b = int(input("Enter your no 2 >>> "))
c = int(input("Enter your no 3 >>> "))
def maximum(a, b, c):
if (a >= b) and (a >= c):
largest = a
elif (b >= a) and (b >= c):
largest = b
else:
largest = c
return largest
10. average of five no ( only if all positive)
n = int(input("Enter number"))
sum = 0
# loop from 1 to n
for num in range(1, n + 1, 1):
sum = sum + num
print("Sum of first ", n, "numbers is: ", sum)
average = sum / n
print("Average of ", n, "numbers is: ", average)
Output
Enter number 10
Sum of first 10 numbers is: 55
Average of 10 numbers is: 5.5
11. century year
def find_century(year):
# No negative value is allow for year
if (year <= 0):
print("0 and negative is not allow for a year")
# If year is between 1 to 100 it
# will come in 1st century
elif (year <= 100):
print("1st century")
elif (year % 100 == 0):
print(year // 100,"century year")
else:
print(year // 100 + 1,"/n not a century year")
# Driver code
year = 1999
find_century(year)
12. upper case letter /lower case latter
str = "vandana"
# Traversing Each character
# to check whether it is in uppercase
for char in str:
k = char.isupper()
if k == False:
print('False')
# Break the Loop
# when you get any
# uppercase character.
break
# Default condition if the string
# does not have any uppercase character.
if(k == 1):
print('True')
13.multiple of 5 or not
i=int(input("enter your number >>>"))
def isMultipleof5(n):
if ( (n & 1) == 1 ):
n <<= 1;
x = n
x = ( (int)(x * 0.1) ) * 10
if ( x == n ):
return 1
return 0
if ( isMultipleof5(i) == 1 ):
print (i, "is multiple of 5")
else:
print (i, "is not a multiple of 5")
14. triangle is equilateral/ isosceles/ scalene
def checkTriangle(x, y, z):
if x == y == z:
print("Equilateral Triangle")
elif x == y or y == z or z == x:
print("Isosceles Triangle")
else:
print("Scalene Triangle")
x = 8
y = 7
z = 9
checkTriangle(x, y, z)
15. calculates the roots of a quadratic equation only if the discriminant is non-negative.
# Python program to find roots of quadratic equation
import math
# function for finding roots
def equationroots( a, b, c):
# calculating discriminant using formula
dis = b * b - 4 * a * c
sqrt_val = math.sqrt(abs(dis))
# checking condition for discriminant
if dis > 0:
print("real and different roots")
print((-b + sqrt_val)/(2 * a))
print((-b - sqrt_val)/(2 * a))
elif dis == 0:
print("real and same roots")
print(-b / (2 * a))
# when discriminant is less than 0
else:
print("Complex Roots")
print(- b / (2 * a), sqrt_val)
print(- b / (2 * a), sqrt_val)
# Driver Program
a = 1
b = 10
c = -24
# If a is 0, then incorrect equation
if a == 0:
print("Input correct quadratic equation")
else:
equationroots(a, b, c)
16.given number is perfect square or not
#### perfect sqare
num = 24
sqrt_num = num ** 0.5
if sqrt_num.is_integer():
print("The provided number is a perfect square")
else:
print("The provided number is not a perfect square")
17. number is palindrome or not
## palindrome no
num = int(input("Enter a value:"))
temp = num
rev = 0
while(num > 0):
dig = num % 10
revrev = rev * 10 + dig
numnum = num // 10
if(temp == rev):
print("This value is a palindrome number!")
else:
print("This value is not a palindrome number!")
18. fibonacii number
#fabonicis
number=int(input("\nplease enter range:"))
first=0
second=1
for num in range(0,number):
if(num<=1):
next=num
else:
next=first+second
first =second
second =next
print(next)
19. calculates the area of a rectangle only if both the length and width are positive.
# Python3 code to find area
# and perimeter of rectangle
# Utility function
def areaRectangle(a, b):
return (a * b)
def perimeterRectangle(a, b):
return (2 * (a + b))
# Driver function
a = 5
b = 6
print ("Area = ", areaRectangle(a, b))
print ("Perimeter = ", perimeterRectangle(a, b))
20. divisible by both 3/5
# python code to print numbers that
# are divisible by 3 and 5
n=50
for i in range(0,n):
# lcm of 3 and 5 is 15
if i%15==0:
print(i,end=" ")
Assignment 2 → Iterative Statements
1. Write a program to print the numbers from 1 to 10.
i=0
while(i<11) :
print(i)
i+=1
#i=i+1
2.Create a program to print the even numbers from 1 to 20.
# print Even Numbers
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
for num in range(start, end + 1):
if num % 2 == 0:
print(num, end=" ")
3.Write a program to print the multiplication table of a given
number.
number = int(input ("Enter the number : >> "))
print ("The Multiplication Table of: ", number)
for count in range(1, 11):
print (number, 'x', count, '=', number * count)
4.Create a program to calculate the factorial of a given
number.
x=int(input(" enter any no > "))
def fact(x):
if x==1:
return(1)
else:
return(x*fact(x-1))
print("The Factorial of given no >>: ",fact(x))
5. Write a program to print the Fibonacci series up to a given
number.
#fabonicis
number=int(input("\nplease enter range:"))
first=0
second=1
for num in range(0,number):
if(num<=1):
next=num
else:
next=first+second
first =second
second =next
print(next)
6. Create a program to calculate the sum of the first n natural
numbers.
#### sum of natural number
n=int(input(" Enter your number >> "))
sums=0
for s in range(1,n+1):
sums=sums+s
print(sums)
print("then sum given natural number is ",sums)
7.Write a program to print all the prime numbers between
two given numbers.
lower_value = int(input ("Please, Enter the Lowest Range Value: "))
upper_value = int(input ("Please, Enter the Upper Range Value: "))
print ("The Prime Numbers in the range are: ")
for number in range (lower_value, upper_value + 1):
if number > 1:
for i in range (2, number):
if (number % i) == 0:
break
else:
print (number)
8.Create a program to print the reverse of a given number.
##### reverse of a number
number = int(input("Enter the integer number: "))
revs_number = 0
while (number > 0):
remainder = number % 10
revs_number = (revs_number * 10) + remainder
number = number // 10
print("The reverse number is : {}".format(revs_number))
9. Write a program to calculate the average of a list of
numbers using a loop.
#### Python program to get average of a list
def calc_average(lst):
return sum(lst) / len(lst)
list = [24, 19, 35, 46, 75, 29, 30, 18]
average = calc_average(list)
print("The average of the list is ", round(average, 3))
10.Create a program to print the square of the first n
numbers.
###### sum Of Squares
def sumOfSquares(num) :
if num < 0:
return
sum = 0
for i in range(num+1):
sum += i*i
return sum
num = int(input('Enter a number : '))
sum = sumOfSquares(num)
print(f'Sum : {sum}')
11.Create a program to check if a given number is a
palindrome or not.
## palindrome no
num = int(input("Enter a value:"))
temp = num
rev = 0
while(num > 0):
dig = num % 10
revrev = rev * 10 + dig
numnum = num // 10
if(temp == rev):
print("This value is a palindrome number!")
else:
print("This value is not a palindrome number!")
12. Write a program to find the sum of the digits of a given
number.
#### compute sum of digits in number.
n = int(input(" enter a number :>>>> "))
def getSum(n):
sum = 0
for digit in str(n):
sum += int(digit)
return sum
print(getSum(n))
13.Create a program to find the sum of the even digits of a
given number.
#### print odd / even sum
list = [345, 893, 1948, 34, 2346]
print("The original list is : " + str(list))
odd_sum = 0
even_sum = 0
for sub in list:
for ele in str(sub):
if int(ele) % 2 == 0:
even_sum += int(ele)
else:
odd_sum += int(ele)
print("Odd digit sum : " + str(odd_sum))
print("Even digit sum : " + str(even_sum))
14. Write a program to check if a given number is an
Armstrong number or not.
#### armdstong
n = 153
s = n
b = len(str(n))
sum1 = 0
while n != 0:
r = n % 10
sum1 = sum1+(r**b)
n = n//10
if s == sum1:
print("The given number", s, "is armstrong number")
else:
print("The given number", s, "is not armstrong number")
15.Create a program to print the reverse of a given string
using a loop.
##### reverse string
str = input("enter str:>>>>> ")
def reverse_string(str):
for i in str:
str = i + str
return str
print("The original string is: ",str)
print("The reverse string is",reverse_string(str))