Uploaded by palaniappan.mdu

python

advertisement
1. Python Program to print addition of two numbers:
a=eval(input(“enter first no”))
b=eval(input(“enter second no”))
c=a+b
print(“the sum is “,c)
2. Python Program to print area of rectangle
l=eval(input(“enter the length of rectangle”))
b=eval(input(“enter the breath of rectangle”))
a=l*b
print(a)
3. Python Program to print Area & circumference of circle
r=eval(input(“enter the radius of circle”))
a=3.14*r*r
c=2*3.14*r
print(“the area of circle”,a) print(“the circumference of circle”,c)
4. Python Program to print eligible to vote or not
age=eval(input(“enter ur age”)) If(age>=18):
print(“eligible for voting”)
else:
print(“not eligible for voting”)
5. Python Program to print sum of n natural numbers
n=eval(input('enter the number'))
i=1
sum=0
while(i<=n):
sum=sum+i
i=i+1
print(sum)
1
6. Python Program to Check Whether a Given Year is a Leap Year
year=int(input("Enter year to be checked:"))
if(year%4==0 and year%100!=0 or year%400==0):
print("The year is a leap year!)
else:
print("The year isn't a leap year!)
7. Python Program to Compute Simple Interest
principle=float(input("Enter the principle amount:"))
time=int(input("Enter the time(years):"))
rate=float(input("Enter the rate:"))
simple_interest=(principle*time*rate)/100
print("The simple interest is:",simple_interest)
8. Python Program to Check if a Number is Positive, Negative or 0
num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
9. Python Program to Check if a Number is Odd or Even
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("number is Even")
else:
print("number is Odd")
10. Python Program to Check biggest of 3 numbers
a=eval(input('enter first number'))
b=eval(input('enter second number'))
2
c=eval(input('enter third number'))
if((a>b) and (a>c)):
print('a is big')
elif(b>c):
print('b is big')
else:
print('c is big')
11. Python Program to swap two numbers:
x=eval(input('enter first number'))
y=eval(input('enter second number'))
temp = x
x=y
y = temp
print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))
Output:
Enter value of x: 14
Enter value of y: 36
The value of x after swapping: 36
The value of y after swapping: 14
12. Python Program to Check Armstrong Number
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.
num = int(input("Enter a number: "))
# initialize sum
sum = 0
# find the sum of the cube of each digit
3
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
Output:
1. Enter a number: 153
153 is an Armstrong number
2. Enter a number: 126
126 is not an Armstrong number
13. Python Program to Display the multiplication Table
# To take input from the user
num = int(input("Display multiplication table of? "))
for i in range(1, 6):
print(num,'x',i,'=',num*i)
Output:
Display multiplication table of? 6
6x1=6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
4
Download