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

in #sec20w4sergeyklast month (edited)

It is a thing of joy to once again participate in this challenge hosted by @sergeyk, and this week is about decision-making. Below are the Tasks I am performing;

Screenshot_20241006-023903_1.jpg


what if you had to go from 1 to 13709—how would you go? This task has a very simple and easy solution. Find this idea, and you'll have it all. Find a universal and optimal solution to this problem.


The number 13709 is odd; as such, I need to take a logical approach to solving the problem. The first thing I have to do is to make the number an even number, such that it can be divided by two, and I did this by subtracting 1 from 13709 to have 13708. To have the desired number from 1 in fewer steps, I applied the reverse or backward method of solving the problem, that is, from 13708 to 1.

  • Therefore, I divided 13708 by 2 to have 6854

  • I divided 6854 by 2 to have 3427

  • I subtracted 1 from 3427 to have an even number, which was 3426, then divided it by 2.

I continued with the method of having an even number, then dividing by 2 until I got to 1.

Therefore I applied the reverse operations to go from 1 to 13709, as shown below;

  • I began with 1
  1. I multiplied 1 by 2 to have 2

  2. I added 1 to 2 to have 3

  3. I multiplied 3 by 2 to have 6

  4. I multiplied 6 by 2 to have 12

  5. I added 1 to 12 to have 13

  6. I multiplied 13 by 2 to have 26

  7. I multiplied 26 by 2 to have 52

  8. I added 1 to 52 to have 53

  9. I multiplied 53 by 2 to have 106

  10. I added 1 to 106 to have 107

  11. I multiplied 107 by 2 to have 214

  12. I multiplied 214 by 2 to have 428

  13. I multiplied 428 by 2 to have 856

  14. I multiplied 856 by 2 to have 1712

  15. I added 1 to 1712 to 1713

  16. I multiplied 1713 by 2 to have 3426

  17. I added 1 to 3426 to have 3427

  18. I multiplied 3427 by 2 to have 6854

  19. I multiplied 6854 by 2 to have 13708

  20. I added 1 to 13708 to have 13709

As seen from above, it took me 20 steps to go from 1 to 13709 applying just +1 and ×2 operations. The reverse method is the universal and optimal method of solving the problem, and I applied this.


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


The primary objective of Boolean variables is for the storage of true or false values. In programs like C++, they are usually 0 and 1, whereby 0 is false and 1 is true. They help a programmer in decision-making and the flow of the program. I can only use these variables in conditional writing, loops, and logical operations. This variable is named boolean to honor the founder of boolean algebra, George Boole. Below are the key purposes or objectives of the variables;

  • Conditional structures: Boolean are pertinent in conditional writing(statements) such as; if, else, and switch. For example;

bool PeterIsWorking = true: if (peter is working) { cout << "don't employ him !" << end1: }

From the above, the variable PeterIsWorking Is a bool variable, whereby the working state of Peter is checked for the code to be executed.

  • Loop control: Loop like; while and for are controlled by Boolean variables. For example;

bool working = true: while ( ! done) { / / some operations…. working = true: / / loop exits when 'working' becomes true. }

  • Flags: Boolean variables are used as flags to indicate specific conditions of a program.

  • To Simplify Complex Logic

I had stated the reason Boolean variables are called such earlier, but I will still mention it again. This variable is named boolean to honor the founder of boolean algebra, George Boole.


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


The objective of the conditional operator is to enable me to make decisions in my program writing. For example, if a specific condition is true, I will do one thing, but if it is false, I will do something else. Conditional operator makes my programs more dynamic and interactive. Invariably, I can run a specific code based on a specific condition.

Types Of Conditional Operators

  • The "if/else": Applying this operator would allow me to write a logical condition which would depend on whether the condition is true or false. I am going to write a program below using python language to apply this operator;

a = 4 if a %2 = = 0: print ( " even " ) else: print ( " odd " )

From the above program, I am checking if a is divisive by 1, and if a is divisive by 1, and going to print even, but if it is not, I am going to print odd. This kind of structure would especially help me when I am dealing with binary situations.

  • The ternary operator: I can apply the ternary operator if I want to write a simple condition in a line. I see the ternary operator as a condensed version of the if/else operator. I used python language to apply the ternary operator in the program below;

a = 4 result = "positive" if a > 0 else "negative" print ( result )

From the above program, I applied the principle I applied on the if/else operator but this time, I did so in one line. That is if a is greater than 0, the program would print a positive result, but if it is less than 0, it would print a negative result.

  • The "match-case," which is similar to the "switch-case" operator: I will apply this operator using the python language. I can apply this operator if I have many possible cases to avoid a long chain of if/else operators. Applying this operator would make my code more readable. For example;

letter = a match letter case " a " : print ( " Beginning of letters " ) case " b " : print ( " second letter " ) case " c " : print ( " third letter " ) case _: print ( " unknown letter " )

From the above program using python language, I used match-case to handle various cases based on the value of the letter variable. It helps me to make the program more readable.


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


I am going to write a program using the python language. To check if I have subscribed to the accounts of 100 users, I will first of all have a list of the users I would love to subscribe to, then simulate the process using this list. For example;

# if I have a list that contains the names of the users I have subscribed to subscriptions = ["user1", "user2", "user3", "user4", "user5", "user6",..., "user100"] #check if I have subscribed to at least 100 users if len(subscriptions) >= 100: print("I have subscribed to 100 or more users.") else: print(f"I am presently subscribed to {len( subscription ) } users . " )

From the program above;

  • I simulated the list with a python language list which contains strings that signifies the name of users I have subscribed to.

  • I used len () function to check if the users I subscribed to are more than 100 or equal to 100. If it's greater than or equal (>=) 100, I will know I have subscribed to 100 or more users.

  • finally, I displayed the result


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


I am going to write a program to check whether I have more subscribers or more post on steemit.com using the python language. Here is the example of the program;

# enter the number of posts and subscribers on Steemit number_of_posts = int(input("Enter the number of my posts on Steemit: ")) number_of_followers = int(input("Enter the number of my followers on Steemit: ")) #Comparison between the number of posts and the number of followers if number_of_posts > number_followers: print("You have more posts than followers.") elif number_followers > number_of_posts: print("You have more followers than posts.") else: print("You have as many posts as followers.")

From the above program;

  1. I manually input my number of posts and followers. The program is written such that the user is asked to input his number of Posts and followers.

  2. I compared two values. The program is written such that values are compared.

  3. The result indicates if I have more number of posts than followers

  4. The result indicates if I have more number of followers than posts

  5. The result indicates if my number of followers equal number of posts

The following is the example of the program execution:

  • I input: number_of_posts = 1038, number_of_followers = 26

  • The program displayed "You have more posts than followers

  • I input: number_of_posts = 26, number_of_followers = 1038

  • The program displayed "You have more followers than posts

  • I input: number_of_posts = 26, number_of_followers = 26

  • The program displayed "You have as many Posts as followers


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


I am going to use python language to write a program to check if I have more posts, more subscribers or more steem power on steemit.com

# enter the number of posts, followers, and Steem Power number_of_posts = int(input("Enter the number of posts: ")) number_of_followers = int(input("Enter the number of followers: ")) steem_power = float(input("Enter your Steem Power: ")) #Comparison between the three values if number_of_posts > number_followers and number_of_posts > steem_power: print("You have more posts than followers and Steem Power.") elif number_followers > number_of_posts and number_followers > steem_power: print("You have more followers than posts and Steem Power.") elif steem_power > number_of_posts and steem_power > number_followers: print("You have more Steem Power than posts and followers.") else: print("followers, posts and steem power are equal.")

From the above program;

  • I manually input number of posts, followers, and steem power. The program is written such that the user is asked to input number of followers, posts and steem power. Notably, the function float() is used so I can treat the values with decimal.

  • I compared the three values to know which is greater.

  • If my number of posts is greater than number of followers and steem power, the result would be " You have more posts than followers and steem power".

  • if my number of followers is greater than number of posts and steem power, the result would be "You have more followers than posts and steem power".

  • if my steem power is greater my number of followers and posts, the result would be "You have more steem power than followers and posts".

  • if none of the three values is greater, the result would be "followers, posts and steem power are equal".

The following python language program explain what I mean more practically because I am using my number of Posts, followers and steem power;

# Simulate the number of posts, followers and Steem Power number_of_posts = 1047 # Ex: I have 1047 posts number_of_followers = 26 # Ex: I have 26 followers steem_power = 136 # Ex: I have 136 Steem Power #Comparison if number_of_posts> number_of_followers and number_of_posts > steem_power: print("You have more post than followers and Steem Power.") elif number_of_followers > number_of_posts and number_of_followers > steem_power: print("You have more followers than posts and Steem Power.") elif steem_power > number_of_posts and steem_power > number_of_followers: print("You have more Steem Power than posts and followersrs.") else: print("steem power, number of followers and posts are equal.")

When I run the above program, it would give me the result if I have more steem power than followers and posts or if the values are equal.


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


I am going to write a program using the python language to determine whether their products or sum is greater, and in this context, I am going to use 2 and 3. The program is below;

# enter two numbers number1 = float(input("2: ")) number2 = float(input("3: ")) #I calculate the sum and the product of the two numbers sum = number1 + number2 product = number1 * number2 # I compare the sum and the product if product > sum: print("The product of the two numbers is greater than their sum.") elif sum > product: print("The sum of the two numbers is greater than their product.") else: print("The sum and product of the two numbers are equal.")

From my above program;

  • I input two numbers numbers 2 and 3 as the first and second number respectively, then used the function float() to allow decimal numbers

  • I calculated the sum and product of 2 and 3 using the functions + and * respectively.

  • I compared

The Execution:

  • Sum = 2+3= 5

  • product= 2×3=6

As seen, the product is greater, therefore the program would display "The product of the two numbers is greater than their sum".


8- 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).


I will write a program using python language to determine if two houses are on the same side of the street, such that houses with even numbers are on one side of the street while those with odd numbers are on the other side. In my following python program, the first house has the number 1 and the second has the number 9;

# enter two house numbers house_number1 = int(input("4: ")) house_number2 = int(input("9: ")) #I check if the two numbers are on the same side of the street if (house_number1 % 2 == 0 and house_number2 % 2 == 0) or (house_number1 % 2 != 0 and house_number2 % 2 != 0): print("Both houses are on the same side of the street.") else: print("Both houses are not on the same side of the street.")

From the above practical program;

  • I input the number of the first house as 4 and the number of the second house as 9.

  • I checked if the number of the houses are both even, both odd, or one is even and the other is odd by using the module operator %. If after I have divided the number by 2 and the remainder is 0, then the number of the house is even, but if the remainder is greater than 0, then it's odd.

The Execution:

One number is even while the other is odd, therefore program would print "both houses are not on the same side of the street".


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


I am going to write a program using python language to check if a given number has digits more than four, is divisive by 7 and is not divisive by 3. Here is my program;

# enter a number number = int(input("Enter a number: ")) #I check if the number has more than four digits has_more_than_four_digits = len(str(abs(number))) > 4 #I check if the number is divisible by 7 is_divisible_by_7 = (number % 7 == 0) #I check if the number is not divisible by 3 is_not_divisible_by_3 = (number % 3 != 0) #I check all conditions if has_more_than_four_digits and is_divisible_by_7 and is_not_divisible_by_3: print("The number has more than four digits, is divisible by 7, and not divisible by 3.") else: print("The number does not meet all the conditions.")

From my above program;

  • I used the function len(str(abs(number))) > 4 to check if the number has more than four digits.

  • I used the function number % 7 == 0 to check if the number is divisive by 7

  • I used the function number % 3 != 0 to check if the number is not divisive by 3

  • I combined the condition, If all are met, the program would display "The number has more than four digits, is divisive by 7 and not divisive by 3", and if it doesn't meet the condition, the program would display "The number does not meet all the conditions".

Sample Execution:

  • I input the number 11900

  • The number has more than four digits

  • 11900÷7 = 1700( is divisive by 7)

  • 11900÷3 = 3966.66( is not divisive by 3)

  • The program would display "The number has more than four digits, is divisive by 7 and not divisive by 3".


10- Which digit of a two-digit number is larger—the first or the second?


I am going to write a program using python language to find which digit of two digit number is bigger. Below is my practical program because I am using 27;

# enter a two-digit number digits number = int(input(" 27: ")) #I separate the first and second digits first_digit = number // 10 # I take the tens digit second_digit = number % 10 # I take the digit units #I compare the two digits if first_digit > second_digit: print("The first digit is greater.") elif second_digit > first_digit: print("The second digit is greater.") else: print("Both digits are equal.")

From my above program;

  • I input 27 as my two digit number.

  • I separated the digits. I got the first digit by using the function number // 10, while I got the second by using the function number % 10.

  • I compared the two digits.

The Execution:

  • First digit: 2

  • second digit: 7

  • The program displayed "The second digit is greater".

But if I were to use 33 or 73, The Execution would be;

  • Input: 33

  • first digit: 3

  • second digit: 3

  • The program would display "Both digits are equal".

  • Input: 73

  • First digit: 7

  • second digit: 3

  • The program would display "The first digit greater".


Thank you very much for reading.

I invite;

@goodybest
@blessedbee
@usherray

Best Regards

@iddy

Sort:  

This is outstanding coding.

Thank you very much, you can participate

Wil definitely join the next

IMG_20240930_084439.png

Congratulations!!🎉🎉 Your post has been upvoted by content seekers using steemcurator05. Continue making creative and quality content on the blog. By @eliany

Thank you very, @eliany, for your support.

Loading...

Coin Marketplace

STEEM 0.21
TRX 0.20
JST 0.034
BTC 90913.25
ETH 3176.54
USDT 1.00
SBD 2.97