ELECTRIC BILL:
u=int(input("enter the electric bill:"))
if u<=100:
print(" the electric bill",u*2)
elif u>=101 and u<=300:
a=(u-100)*5+200
print(" the electric bill",a)
elif u>=301 and u<=600:
a=(u-300)*7+1700
print(" the electric bill",a)
elif u>=601 and u<=1100:
a=(u-600)*7+3100
print(" the electric bill",a)
else:
print("invalid")
HCF & LCM:
n1=int(input("Enter first number:"))
n2=int(input("Enter second nmber:"))
if n1>n2:
smaller=n2
else:
smaller=n1
for i in range(1,smaller+1):
if (n1%i==0) and (n2%i==0):
hcf=i
lcm=int((n1*n2)/hcf)
print("HCF=",hcf)
print("LCM=",lcm)
The given series is:
This is the expansion of the exponential series up to n terms.
x = int(input("Enter the value of x: "))
n = int(input("Enter the number of terms: "))
sum_series = 1
fact = 1
power = 1
for i in range(1, n + 1):
power = power * x
fact = fact * i
sum_series = sum_series + (power / fact)
print("Sum of the series =", sum_series)
Write a program in Python to accept a number and check it is a special number or krishnamurty number or strong or not.
A number is a special number if the sum of the factorial of its digits is equal to the original number.
Examples:
145 is a special number because
1! + 4! + 5! = 1 + 24 + 120 = 145
num = int(input("Enter a number: "))
temp = num
sum_fact = 0
while temp > 0:
digit = temp % 10
fact = 1
for i in range(1, digit + 1):
fact *= i
sum_fact += fact
temp //= 10
if sum_fact == num:
print(num, "is a Krishnamurthy Number")
else:
print(num, "is NOT a Krishnamurthy Number")
Write a program to accept a number and check whether the number is a
palindrome or not. Display the message accordingly.
(A number is said to be a palindrome if the new number obtained after
reversing the digits is the same as the original number.)
Sample input: 707
Sample output: 707 is a palindrome number
num = int(input("Enter a number: "))
original = num
reverse = 0
while num > 0:
digit = num % 10
reverse = reverse * 10 + digit
num = num // 10
if original == reverse:
print(original, "is a Palindrome Number")
else:
print(original, "is not a Palindrome Number")
FIBONACCI SERIES:
nterms=int(input("enter n terms"))
first = 0
second = 1
print(first,end=" ")
print(second,end=" ")
for x in range(1,nterms-1):
third = first + second
print(third,end=" ")
first,second=second,third
ARMSTRONG NUMBER BETWEEN ANY RANGE
lower=int(input("enter the lower value:"))
upper=int(input("enter the lower value:"))
for num in range(lower, upper + 1):
order = len(str(num))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
if num == sum:
print(num)
PRIME NUMBER:
Check if a Number is Prime (Using for Loop)
num = int(input("Enter a number: "))
if num <= 1:
print(num, "is not a Prime Number")
else:
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")
Python Program to Check Whether a Number is a Harshad Number (Niven Number)
Definition:
A Harshad Number (or Niven Number) is an integer that is divisible by the sum of its digits.
Examples:
• 18 → Sum of digits = 1 + 8 = 9, and 18 ÷ 9 = 2 ✔ (Harshad Number)
• 19 → Sum of digits = 1 + 9 = 10, and 19 ÷ 10 ≠ Integer ✘ (Not a Harshad Number)
Algorithm
1. Read a number from the user.
2. Store the original number.
3. Find the sum of its digits using a while loop.
4. Check whether the original number is divisible by the sum of its digits.
5. If divisible, print "Harshad Number"; otherwise, print "Not a Harshad Number".
Python Program
num = int(input("Enter a number: "))
original = num
sum_digits = 0
while num > 0:
digit = num % 10
sum_digits = sum_digits + digit
num = num // 10
if original % sum_digits == 0:
print(original, "is a Harshad Number")
else:
print(original, "is not a Harshad Number")
The sum of the proper divisor of 123 should equal 426, and the proper divisor of 426 should equal 123.
How to check if two numbers are amicable in Python
Following are the steps to find whether two numbers are amicable or not.
• First, take the two-integer number as an input from the user.
• Get the proper divisor of the both numbers and do the sum of them.
• Now check whether the given number is equal to opposite numbers.
• If they are equal then they are Amicable else not.
• And we get the output.
1ST Number : 220
Enter second number : 284
Given numbers are Amicable!
x=int(input('Enter first number : '))
y=int(input('Enter second number : '))
sum1=0
sum2=0
for i in range(1,x):
if x%i==0:
sum1+=i
for j in range(1,y):
if y%j==0:
sum2+=j
if(sum1==y and sum2==x):
print('Given numbers are Amicable!')
else:
print('Given numbers are not Amicable!')
1. Pattern 1
1
12
123
1234
12345
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end="")
print()
2. Pattern 2
12345
1234
123
12
1
rows = 5
for i in range(rows, 0, -1):
for j in range(1, i + 1):
print(j, end="")
print()
3. Pattern 3
1
22
333
4444
55555
rows = 5
for i in range(1, rows + 1):
for j in range(i):
print(i, end="")
print()
4. Pattern 4
55555
4444
333
22
1
rows = 5
for i in range(rows, 0, -1):
for j in range(i):
print(i, end="")
print()
5. Pattern 5
1
23
456
78910
1112131415
rows = 5
num = 1
for i in range(1, rows + 1):
for j in range(i):
print(num, end=" ")
num += 1
print()
6. Pattern 6
5
54
543
5432
54321
rows = 5
for i in range(1, rows + 1):
for j in range(rows, rows - i, -1):
print(j, end="")
print()
7. Pattern 7
12345
2345
345
45
5
rows = 5
for i in range(1, rows + 1):
for j in range(i, rows + 1):
print(j, end="")
print()
8. Pattern 8
1
21
321
4321
54321
rows = 5
for i in range(1, rows + 1):
for j in range(i, 0, -1):
print(j, end="")
print()
9. Floyd's Triangle
1
23
456
7 8 9 10
11 12 13 14 15
rows = 5
num = 1
for i in range(1, rows + 1):
for j in range(i):
print(num, end=" ")
num += 1
print()
11. Square Number Pattern
11111
22222
33333
44444
55555
rows = 5
for i in range(1, rows + 1):
for j in range(rows):
print(i, end="")
print()
12. Multiplication Table Pattern
24
369
4 8 12 16
5 10 15 20 25
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(i * j, end=" ")
print()