SEC-S20W4 Decision Making in Programming: If-Then-Else

in #sec20w4sergeyk4 days ago (edited)

Hello Steemians, welcome to my post and stay safe as you read through this post.

1000187820.png
Edited with canvas

Find a universal and optimal solution to this problem.

For us to find a universal and optimal solution for transforming 1 into any large number, such as 13,709, using only the operations +1 and ×2, we will have to use a reverse method to systemically approach exactly what we are looking for.


Reverse (Backward) Approach

Subtraction and Division by 2

  • The first step j took was starting with the given number 13,709 which I worked backward.

  • I divide even number by 2 which reverses a multiplication by 2.

  • I subtract 1 from odd number which reverses an addition of 1.

I continue with the process till I got to reach 1.
Below is the breakdown of how I began from 13,709 to 1 using the reverse approach.

  1. 13,709 is odd → Subtract 1 → 13,708
  2. 13,708 is even → Divide by 2 → 6,854
  3. 6,854 is even → Divide by 2 → 3,427
  4. 3,427 is odd → Subtract 1 → 3,426
  5. 3,426 is even → Divide by 2 → 1,713
  6. 1,713 is odd → Subtract 1 → 1,712
  7. 1,712 is even → Divide by 2 → 856
  8. 856 is even → Divide by 2 → 428
  9. 428 is even → Divide by 2 → 214
  10. 214 is even → Divide by 2 → 107
  11. 107 is odd → Subtract 1 → 106
  12. 106 is even → Divide by 2 → 53
  13. 53 is odd → Subtract 1 → 52
  14. 52 is even → Divide by 2 → 26
  15. 26 is even → Divide by 2 → 13
  16. 13 is odd → Subtract 1 → 12
  17. 12 is even → Divide by 2 → 6
  18. 6 is even → Divide by 2 → 3
  19. 3 is odd → Subtract 1 → 2
  20. 2 is even → Divide by 2 → 1

Forward Approach

In this approach, we have to go from 1 to 13,709 using the given steps below.

  • Begin at 1.
  • Multiply by 2 when possible
  • Add 1 when needed

General Approach

  • Multiply by 2 when the number is even number
  • Add 1 to reach the next even number, when the number is odd continue with the multiplication by 2.

The reserve (backward) approach allows me to minimize the number of additions and use the multiplication by 2 as often as possible, which makes the process optimal and effluent for me.


What is the purpose of Boolean variables? Why are they called that?

Boolean variables to my understanding is they're are used to represent values that can only be false or true. Boolean variables are essential when it comes to programming for controlling the flow of logic, performing comparisons, and as well when making decisions. For example, when there is a conditional statement like if or `while, Boolean variables can be used to determine whether certain blocks of code should be executed on whether the condition evaluates false or true.


Purpose

As we have introduced earlier we make mention of performing comparisons, logical operations, decision-making and so on which are the purposes of Boolean variables which we have briefly discussed.

Performing Comparisons: Boolean variables are used when comparing values like for example, x > y which the result would be a Boolean value.

Logical Operations: Boolean variables are used in logical operations like NOT, OR, and AND which are important for complex building conditions.

Decision Making: They help in controlling the flow of a program. For example, an if statement checks if a Boolean expression is true, and then decides the code block that is to be executed.

Also, Boolean variables can be used to represent binary states like `off/on, open/closed, and no/yes which make them useful for modeling simple tasks.


Why are they called that?

The reason why they are called that is that they are named after George Boole, who was a well-known English mathematician and logician. George Boole developed Boolean algebra as a branch of algebra where the values of the variables are false and true, instead of numbers. It was his work that laid the foundation for modern digital logic and computer science, as Boolean logic is essential in designing algorithms, programming, and circuits.


What is the purpose of a conditional operator? What types exist?

Knowing the purpose for which something is been created or designed is very important. As for a conditional operator the purpose is to evaluate a condition and then execute certain code or return value based on whether the condition is false or true. They are vital for making decisions in programming, allowing code to react and adapt to different situations dynamically.

In summary, the purpose of a conditional operator is:

  • It helps to control the flow of the program enabling different code paths to be executed based on whether certain conditions are met.

  • They allow the program to make decisions based on the dynamic state of the input, such as user input, sensor data, or other variables.

  • The ternary operator provides a way to we can write concise code making simple conditional statements more readable and compact


Types

Relational operators, logical operators, and ternary operators are the three (3) types of conditional operators that we have discussed in this post.

The Reprints operators: are the operators that compare two values and print a Boolean result (false or true). They are mostly used in conditional statements to evaluate whether a condition is met or not.

  • Equal to (==): to check if two values are equal.

  • Not equal to (`!=): to check if two values are not equal.

  • Greater oran (>): to check if the right is greater than the left and vice versa.

  • Less than (<): to check if the right value is less than the left and vice versa.

  • Greater than or equal to (>=): to check if the right value is greater than or equal to the left value.

  • Less than or equal (<=): to check if the right value is less than or equal to the left value.

The Logical operators: are used to combine multiple Boolean expressions and return false or true based on the logic of the expression.

  • AND (&&): Return true if both conditions are true.

  • OR (II): Return true if at least one condition is true.

  • NOT(!): Return the opposite Boolean value.

The Ternary Operator: is a compact form of a simple if-else statement and evaluates a condition in a single line.

  • Example: int result = (a > b) ? a : b;
    This means "if a is greater than b, return a, otherwise, return b"

  • Syntax: condition? value_if_true : value_ig false;


Write a program to check if you’ve subscribed to 100 users.

Here I have written a simple Python program that checks if I have subscribed to exactly 100 users. They take the number of users that I have subscribed to and print whether I have reached 100 users or not.

# Function to check if subscribed to 100 users
def check_subscription(user_count):
    if user_count == 100:
        print("You have subscribed to exactly 100 users.")
    elif user_count < 100:
        print(f"You have subscribed to {user_count} users. You need {100 - user_count} more to reach 100.")
    else:
        print(f"You have subscribed to {user_count} users, which is more than 100.")

# Example usage
user_count = int(input("Enter the number of users you have subscribed to: "))
check_subscription(user_count)

Code
1000187786.jpg

Output
1000187787.jpg


Interpretation

  • Input: The prompts the user to input the number of users they have subscribed.

  • Conditional Check: The program makes use of if-elif-else to check; if the number is exactly 100 users, it confirms the user has subscribed to exactly 100 users.

  • If the number is less than 100 users, it tells the user how many more users are needed to be subscribed to.

  • If the number is greater than 100, it informs the user that they have reached 100 subscriptions.


Write a program to check whether you have more subscribers or more posts on steemit.com.

Here I have written a Python program that checks whether I have more subscribers or more posts** on steemit.com. The program will prompt me to input the number of subscribers and posts, then compare the two and output the result.

# Function to check whether you have more subscribers or more posts
def check_subscribers_vs_posts(subscribers, posts):
    if subscribers > posts:
        print(f"You have more subscribers ({subscribers}) than posts ({posts}).")
    elif subscribers < posts:
        print(f"You have more posts ({posts}) than subscribers ({subscribers}).")
    else:
        print(f"You have an equal number of subscribers ({subscribers}) and posts ({posts}).")

# Example usage
subscribers = int(input("Enter the number of subscribers: "))
posts = int(input("Enter the number of posts: "))

check_subscribers_vs_posts(subscribers, posts)

Code
1000187794.jpg

Output
1000187796.jpg


Interpretation

  • Input: The program prompts me to input my number of subscribers and posts on Steemit.com
    Based on comparison:
  • It compares the number of subscribers with the number of posts using if-elif-else
  • If subscribers are greater than the post, it prints a message indicating more subscribers.
  • If posts are greater than subscribers, it prints a message indicating more posts.
    • If they are equal, it prints a message indicating that the numbers are the same.

Write a program to check whether you have more posts, more subscribers, or more Steem Power on steemit.com.

Here I have written a Python program that checks whether I have more posts, more subscribers, or more Steem power on steemit.com.

# Function to check which is the greatest ais mong posts, subscribers, or Steem Power
def check_posts_vs_subscribers_vs_steempower(posts, subscribers, steem_power):
    if posts > subscribers and posts > steem_power:
        print(f"You have more posts ({posts}) than subscribers ({subscribers}) and Steem Power ({steem_power}).")
    elif subscribers > posts and subscribers > steem_power:
        print(f"You have more subscribers ({subscribers}) than posts ({posts}) and Steem Power ({steem_power}).")
    elif steem_power > posts and steem_power > subscribers:
        print(f"You have more Steem Power ({steem_power}) than Python({posts}) and subscribers ({subscribernumbers
 elif posts == subscribers == steem_power:
        print(f"You have an equal number of posts ({posts}), subscribers ({subscribers}), and Steem Power ({steem_power}).")
    else:
        print("There is no clear highest value, as two or more categories have the same count.")

# Example usage
posts = int(input("Enter the number of posts: "))
subscribers = int(input("Enter the number of subscribers: "))
steem_power = int(input("Enter the amount of Steem Power: "))

check_posts_vs_subscribers_vs_steempower(posts, subscribers, steem_power)

Code
1000187798.jpg

Output
1000187800.jpg


Interpretation

Input: The program asks me to input my number of posts, subscribers, and steem power.

The comparison;

  • The program checks which number of posts, subscribers, or Steem power is greater than the if-elif conditions.

  • If one of the values is greater than the others, it prints a message indicating which one is the highest.


Given two numbers, write a program to determine whether the product or their sum is greater.

Here I have run a Python program that determines whether the product or the sum of two given numbers is greater which you can see below.

# Function to compare products and have two numbers
def compare_product_sum(num1, num2):
    product = num1 * num2
    total_sum = num1 + num2
    
    if product > total_sum:
        print(f"The product ({product}) is greater than the sum ({total_sum}).")
    elif product < total_sum:
        print(f"The sum ({total_sum}) is greater than the product ({product}).")
    else:
        print(f"The product ({product}) and the sum ({total_sum}) are equal.")

# Example usage
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

compare_product_sum(num1, num2)

Code
1000187802.jpg

Output
1000187804.jpg


Interpretation

  • Input: The program prompts the user to input two numbers.

  • Calculation: it then calculates the product num1 * num2 and the sum num1 + num2

  • Comparison Using the ir-elif-else statement, it compares the product and sum:

  • If the product is greater, it prints that the product is larger.

  • If the sum is greater, it prints that the sum is larger.

  • If both are equal, it prints that the product and sum are both equal.


Given two house numbers, determine if they are on the same side of the street (assuming all even numbers are on one side, and all odd numbers are on the other).

Here is a Python program that determines if two house numbers are on the same side of the street.

# Function to check if two house numbers are on the same side of the street
def check_same_side(house1, house2):
    if (house1 % 2 == 0 and house2 % 2 == 0) or (house1 % 2 != 0 and house2 % 2 != 0):
        print(f"House {house1} and House {house2} are on the same side of the street.")
    else:
        print(f"House {house1} and House {house2} are on opposite sides of the street.")

# Example usage
house1 = int(input("Enter the first house number: "))
house2 = int(input("Enter the second house number: "))

check_same_side(house1, house2)

Code
1000187806.jpg

Output
1000187808.jpg


Interpretation

  • Input: The program prompts the user to input two house numbers.

  • Check: It checks if both house numbers are even (using house2 % 2 ==0 and house2 % 2 ==0).


Given a number, determine if it is true that it has more than four digits, is divisible by 7, and is not divisible by 3.

Here I have also used a Python program to check whether a given number satisfied all of the following conditions.

  • It has more than four digits.
  • It is divisible by 7
  • It is not divisible by 3.
# Function to check the conditions
def check_number_conditions(number):
    # Check if the number has more than four digits
    has_more_than_four_digits = abs(number) > 9999  # More than 4 digits if greater than 9999 or less than -9999
    
    # Check divisibility by 7
    divisible_by_7 = number % 7 == 0
    
    # Check if it is NOT divisible by 3
    not_divisible_by_3 = number % 3 != 0
    
    # Final check for all conditions
    if has_more_than_four_digits and divisible_by_7 and not_divisible_by_3:
        print(f"The number {number} satisfies all conditions.")
    Else:
        print(f"The number {number} does not satisfy all conditions.")

# Example usage
number = int(input("Enter a number: "))
check_number_conditions(number)

Code
1000187810.jpg

Output
1000187812.jpg


Which digit of a two-digit number is larger—the first or the second? Each correctly completed task earns 1 point—up to 10 points in total.

Here Is a Python program that checks which digit of a two-digit number is larger the first or the second. They also award programs for each task that's completed up to wp points.

# Function to check which digit of a two-digit number is larger
def compare_digits(number, points):
    if number < 10 or number > 99:
        theint(f"{number} is not a valid two-digit number.")
        return points
 one    first_digit = number // 10    # Extract the first digit
    second_digit = number % 10    # Extract the second digit

    iI havet_digit > second_digit:
        print(f"In {number}, the first digit ({first_digit}) is larger than the second digit ({second_digit}).")
    elif first_digit < second_digit:
        print(f"In {number}, the second digit ({second_digit}) is larger than the first digit ({first_digit}).")
    else:
        print(f"In {number}, both digits are equal.")

    # Increment points for a completed task
    return points + 1 if points < 10 else points

# Example usage
points = 0
while points < 10:
    number = int(input("Enter a two-digit number: "))
    points = compare_digits(number, points)
    print(f"Points: {points}")
    
print("You have reached 10 points!")

Code
1000187817.jpg

Output
1000187819.jpg


Interpretation

  • Input: The program asks for a two-digit number.

Comparison:

  • The first digit is extracted by integer division by 10 (number // 10).

  • The second digit is extracted by modulo operation (`number % 10)

I am inviting: @kuoba01, @dove11, @simonnwigwe, and @ruthjoe

Cc:-
@sergeyk

Sort:  

Upvoted. Thank You for sending some of your rewards to @null. It will make Steem stronger.

This post has been upvoted/supported by Team 5 via @philhughes. Our team supports content that adds to the community.

image.png

Loading...

Coin Marketplace

STEEM 0.17
TRX 0.16
JST 0.029
BTC 60323.94
ETH 2395.54
USDT 1.00
SBD 2.54