Logic Building Python Programs
Yash Mali
4 min read
1)FACTORIAL OF A NUMBER
2)USD to INR
3)EVEN or ODD
Strong Number or not.
Celsius to fahrenheit
- Concatenation of string
Positive Negative or Zero
Pattern Printing Square
Right Arrow Pattern
Right ANGLED TRIANGLE
Pyramid
Inverted Right Angled Triangle
Pyramid (2 sided up and down)
diamond pattern
plus pattern +
parallel bars
cross +
#Pyramid
n = 5
for i in range(n):
for j in range(n-i-1):
print(" ",end= " ")
for j in range(2*i+1):
print("*",end=" ")
print()#Factorial of a number
n = 5
fact = 1
for i in range(i, n+1):
fact*=i
print(fact)
#function
def cal_fact(n):
fact = 1
for i in range(1,n+1):
fact *= i
print(fact)
cal_fact(10)
# 2) USD to INR
#USD to INR
def converter(usd_value):
inr_value = usd_value * 83
print(usd_value,"USd =",inr_value,"INR" )
converter(3)
# 3)Even or ODD
def check_num(a):
if a%2 == 0:
print("EVEN")
else:
print("ODD")
return(print)
check_num(24)
#4)Strong
def is_strong_number(n):
sum_of_factorials = 0
for digit in str(n):
factorial = 1
for i in range(1,int(digit)+1):
factorial *= i
sum_of_factorials += factorial
return 1 if sum_of_factorials == n else 0
#5)Celsius to fahrenheit
def celsius_to_fahrenheit(celsius):
fahrenheit = (celsius*9/5)+32
return fahrenheit
celsius = float(input("Enter temp in celsius:"))
fahrenheit = celsius_to_fahrenheit(celsius)
print(f"The celsius in fahrenheit is:{fahrenheit}")
#6)Concatenation of String
s1 = input("Enter 1st sentence: ")
s2 = input("Enter 2nd sentence: ")
result = ''.join([s1,s2])
print("Concaatenated string: ",result)
#7)positive negative or zero
num = float(input("Enter a Number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
# 8)Patter Printing Square
n = int(input())
for i in range(n):
for j in range(n):
print("*",end=" ")
print()
#9)Right Arrow Pattern
def right_arrow(n):
for i in range(1,n+1):
for j in range(i):
print("*",end=" ")
print()
for i in range(n-1,0,-1):
for j in range(i):
print("*", end=" ")
print()
n = int(input())
right_arrow(n)
#Right ANGLED TRIANGLE
n = 5
for i in range(1,n+1):
for j in range(i):
print("*",end= " ")
print()
#INVERTED RIGHT ANGLED TRIANGLE
n = 5
for i in range(n,0,-1):
for j in range(i):
print("*",end=" ")
print()
#Pyramid
n = 5
for i in range(n):
for j in range(n-i-1):
print(" ",end= " ")
for j in range(2*i+1):
print("*",end=" ")
print()
#Inverted Right angled Triangle with Mirror image
n = int(input())
for i in range(1,n+1):
for j in range(n-i):
print(" ",end="")
for j in range(i):
print("*",end="")
print()
for i in range(n-1,0,-1):
for j in range(n-i):
print(" ",end="")
for j in range(i):
print("*",end="")
print()
#pyramid
n = int(input())
for i in range(1,n+1):
for j in range(n-i):
print(" ",end="")
for j in range(2*i-1):
print("*",end="")
print()
for i in range(n-1,0,-1):
for j in range(n-i):
print(" ",end="")
for j in range(2*i-1):
print("*",end="")
print()
#Hollow triangle
n=int(input())
for i in range (n):
for j in range (n-i-1):
print(" ",end="")
for j in range (i):
if i==n-1 or j==0:
print("*",end="")
else:
print(" ",end="")
for j in range (i+1):
if i==n-1 or j==i:
print("*",end="")
else:
print(" ",end="")
print()
#hill Pattern
n = int(input())
for i in range(n):
for j in range(i,n):
print(" ",end=" ")
for j in range(i):
print("*",end=" ")
for j in range(i+1):
print("*",end=" ")
print()
#Diamond Pattern
n = int(input())
for i in range(n-1):
for j in range(i,n):
print(" ",end=" ")
for j in range(i):
print("*",end=" ")
for j in range(i+1):
print("*",end=" ")
print()
for i in range(n):
for j in range(i+1):
print(" ",end=" ")
for j in range(i,n-1):
print("*",end=" ")
for j in range(i,n):
print("*",end=" ")
print()
# + plus pattern
n= int(input())
for i in range(n):
for j in range(n):
if i == n//2 or j == n//2 :
print("*",end=" ")
else:
print(" ",end=" ")
print()
#parallel bars
n= int(input())
for i in range(n):
for j in range(n):
if j == 0 or j == n-1:
print("*",end=" ")
else:
print(" ",end=" ")
print()
# cross plus pattern
n= int(input())
for i in range(n):
for j in range(n):
if i == j or i+j == n-1 :
print("*",end=" ")
else:
print(" ",end=" ")
print()
# hollow triangle
n = int(input())
for i in range(n):
for j in range(i+1):
if (i == n-1 or j == 0 or j == i):
print("*",end=" ")
else:
print(" ",end=" ")
print()
0
Subscribe to my newsletter
Read articles from Yash Mali directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Open SourcePythonProgramming BlogsProductivityObject Oriented Programmingpython beginnerlogic buildingpythonprogramminglanguagescodecoding
Written by