Underrated step for logic building in programming.

Image
Logic building is a crucial and complex skill in programming. In essence, it is ability to come-up with solution of coding problem and write precise instructions ( or code) that a computer can execute autonomously. This skill requires aligning your thought process with computer and its capabilities. And running through code some-what abstractly to know and predict the behavior of code before it is executed. To be able to do this, one essential step that many beginner programmers overlook is performing dry runs. Understanding Dry Runs The concept of a dry run in programming is straightforward: can you mentally execute your code and predict its output without actually running it on a computer? While this seems simple, it is a challenging task. Typically, we are taught to write code, run it, and observe the output. This cycle is essential because code needs to run to be validated. However, if you rely solely on running your code to understand its behavior, you may struggle with building

Python Code for begineer's #8

 # function with argument


def isArmstrong(number):
original_number = number
num_digits = len(str(number))
sum_of_digits = 0

while number > 0:
digit = number % 10
sum_of_digits += digit ** num_digits
number //= 10

if sum_of_digits == original_number:
return True
else:
return False

result = isArmstrong(45)
if result:
print('it is armstrong')
else:
print('it is not armstrong')

# function without argument

def isArmstrong(number):
number = int(input('enter number: '))
original_number = number
num_digits = len(str(number))
sum_of_digits = 0

while number > 0:
digit = number % 10
sum_of_digits += digit ** num_digits
number //= 10

if sum_of_digits == original_number:
return True
else:
return False

result = isArmstrong(45)
if result:
print('it is armstrong')
else:
print('it is not armstrong')

# procedure without argument

def isArmstrong():
number = int(input("enter number: "))
original_number = number
num_digits = len(str(number))
sum_of_digits = 0

while number > 0:
digit = number % 10
sum_of_digits += digit ** num_digits
number //= 10

if sum_of_digits == original_number:
print(f"{original_number} is an Armstrong number.")
else:
print(f"{original_number} is not an Armstrong number.")

isArmstrong()

# procedure with argument

def isArmstrong(number):
original_number = number
num_digits = len(str(number))
sum_of_digits = 0

while number > 0:
digit = number % 10
sum_of_digits += digit ** num_digits
number //= 10

if sum_of_digits == original_number:
print(f"{original_number} is an Armstrong number.")
else:
print(f"{original_number} is not an Armstrong number.")

isArmstrong(145)
---------------------------------------------------------------------------------------------


def MaxTwo(num1,num2):
if num1>num2:
return True
else:
return False

n1 =int(input('enter first num: '))
n2= int(input("enter second num: "))

result = MaxTwo(n1,n2)

if result:
print("first number is greater")
else:
print("second number is greater")
---------------------------------------------------------------------------------------------
def MaxThree(num1, num2):
num3 = int(input('enter third num:'))
if num1 >= num2:
if num1 >= num3:
greatest = num1

else:
greatest = num3
else:
if num2 >= num3:
greatest = num2

else:
greatest = num3

print(f"The greatest number among {num1}, {num2}, and {num3} is: {greatest}")


n1 = int(input('enter first num: '))
n2 = int(input("enter second num: "))

MaxThree(n1, n2)

Comments

Popular posts from this blog

Building JavaScript Array Methods from Scratch in 2024 - Easy tutorial for beginners # 1

Build eisenhower matrix with React, firebase and neumorphism ui . (Part one)

Creating a Dynamic Search Bar in React: A Step-by-Step Tutorial