Skip to content

Commit f52d657

Browse files
basic programs python
0 parents  commit f52d657

9 files changed

+114
-0
lines changed

armstrong.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def i_am_strong(num):
2+
# a positive integer is called an arm strong number if abc = a**3 + b**3 + c**3
3+
# 153 = 1 ** 3 + 5 ** 3 + 3 ** 3
4+
# order of number
5+
# access each digit and put ** to it
6+
# sum all the exponented number
7+
# check if they are equal to num
8+
order = len(str(num))
9+
sum =0
10+
temp = num
11+
while temp > 0:
12+
digit = temp % 10 # We can get our last digit by % 10...153 % 10 == 3
13+
sum += digit ** order # so we got 3 from digit operation and now will apply exponent of order of num
14+
temp //= 10 # this will reduce our number to 0 and will exit the loop after temp < 0:
15+
if sum == num:
16+
return True
17+
18+
print(i_am_strong(153))

factorial.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def fact_o_real(num):
2+
result = 1
3+
while num > 1:
4+
print(result)
5+
result *= num
6+
7+
num = num -1
8+
return result
9+
10+
print(fact_o_real(5))
11+
12+
# factorial of 0 is 1
13+
# factorial does not exist for negative number

fibonacci.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def fibonacci(num):
2+
# 0 1 1 2 3 5 8 13 fibonacci series
3+
# if num == 0 or num < 1:
4+
# print(num)
5+
a = 0
6+
b = 1
7+
print(a)
8+
print(b)
9+
while a + b <= num:
10+
c = a + b
11+
print(c)
12+
a = b
13+
b = c
14+
15+
fibonacci(100)

largest_among_three.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def largest_num(nums):
2+
largest = nums[0]
3+
for i in range(len(nums)):
4+
if nums[i] > largest:
5+
largest = nums[i]
6+
return largest
7+
8+
large = largest_num([4,3,5,2,6,1,7])
9+
10+
print(large)

leapyear.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def leap(num):
2+
3+
if num % 4 == 0: # Then its an LEAP Year
4+
if num % 100 == 0: # if its an century then its not Leap Year
5+
if num % 400 == 0: # if that century is 4th century then its leap year
6+
print('Leap Year')
7+
else:
8+
print("Ordinary Year")
9+
else:
10+
print("Leap Year")
11+
else:
12+
print("Ordinary Year")
13+
14+
leap(2020)

multipliacation_table.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def multiplication_table(num):
2+
for i in range(1,13):
3+
print(str(num) +" x " + str(i) +" = "+ str(num * i))
4+
5+
multiplication_table(2)

prime.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def prime(num):
2+
if num > 1:
3+
for i in range(2, num):
4+
if num % i == 0:
5+
print(str(num)+" is not Prime number")
6+
print(str(i)+" times "+str(num//i)+" is "+str(num))
7+
break
8+
else:
9+
print(str(num)+" is Prime number")
10+
11+
else:
12+
print("number is less than 1 it cannot be prime.")
13+
14+
15+
prime(6)

prime_interval.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def prime_interval(lower, upper):
2+
for num in range(lower, upper+1):
3+
if num > 1:
4+
for i in range(2, num):
5+
if num % i == 0:
6+
# print(str(i)+" times "+str(num//i)+" is"+ str(num))
7+
# print("Not Prime")
8+
break
9+
else: # we have written this else block outside of for loop to ensure the prime no is not repeated till num value..
10+
print(num)
11+
12+
prime_interval(3, 11)

sum_natural_numbers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def sum_natural(num):
2+
sum = 0
3+
while num > 0:
4+
sum += num
5+
num -= 1
6+
return sum
7+
8+
def sum_natural_formula(num):
9+
return num*(num+1)/2
10+
11+
print(sum_natural(5))
12+
print(sum_natural_formula(5))

0 commit comments

Comments
 (0)