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 beginner's #6

# Print greatest number amoung 500 number and their average excluding greatest number

import random

numbers = [random.randint(1, 1000) for _ in range(500)]

max_number = max(numbers)

filtered_numbers = []
index = 0
while index < len(numbers):
num = numbers[index]
if num != max_number:
filtered_numbers.append(num)
index += 1

average = sum(filtered_numbers) / len(filtered_numbers)

print("Maximum Number:", max_number)
print("Average (excluding maximum):", average)
---------------------------------------------------------------------------------------------

def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result

def isStrong(num):
original_num = num

sum_of_factorials = 0
while num > 0:
digit = num % 10
sum_of_factorials += factorial(digit)
num //= 10

if sum_of_factorials == original_num:
print(f"{original_num} is a strong number.")
else:
print(f"{original_num} is not a strong number.")
---------------------------------------------------------------------------------------------

def sum(n1, n2):
return n1 + n2


num1 = int(input('enter first num: '))
num2 = int(input('enter second num: '))

result = sum(num1, num2)

print(result)
---------------------------------------------------------------------------------------------

def area(l,b):
return l*b

length = int(input('length: '))
breadth = int(input('breadth: '))

print(area(length,breadth))
---------------------------------------------------------------------------------------------


def sumOfNumbers(n):
sum = 0
for i in range(n + 1):
sum += i

return sum


num = int(input('enter num : '))
print(sumOfNumbers(num))
---------------------------------------------------------------------------------------------

num = int(input("Enter a number: "))
temp = num
reverse_num = 0

while num > 0:
digit = num % 10
reverse_num = reverse_num * 10 + digit
num = num // 10

if temp == reverse_num:
print(f"{temp} is a palindrome.")
else:
print(f"{temp} is not a palindrome.")
---------------------------------------------------------------------------------------------

number = int(input("Enter a number: "))

original_number = number
num_digits = len(str(number))
sum_of_digits = 0

# Loop to calculate the sum of each digit raised to the power of the number of digits
while number > 0:
digit = number % 10
sum_of_digits += digit ** num_digits
number //= 10

# Check if the sum of digits is equal to the original number
if sum_of_digits == original_number:
print(f"{original_number} is an Armstrong number.")
else:
print(f"{original_number} is not an Armstrong number.")

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