SEC-S20W4 Decision Making in Programming: If-Then-ElsesteemCreated with Sketch.

in Teachers & Studentslast month (edited)

🌟 Programming Tasks and Solutions: From Boolean Logic to Algorithm Design 🌟

Hey friends! I’ve come across an exciting challenge that I’m eager to join this week. It’s called Decision Making in Programming: If-Then-Else, hosted by @sergeyk. You can find more details and participate through this link: SEC S20W4 | : Decision Making in programming; If-Then-Else.

Beige and Green Cute Illustrative Class Syllabus Presentation.png
Image Source: canva

In this blog post, we'll explore 10 programming tasks that cover various aspects of coding, from basic concepts to algorithm design. Each task comes with a detailed explanation, solution, and example execution, making it perfect for both beginners and those looking to refresh their skills.

Task 1: Optimal Growth Algorithm

Problem: How can you grow from 1 to any number using only +1 and x2 operations?

Solution (Python Code) :

1.JPG

Explanation of the program:
This algorithm uses a greedy approach to work backwards from the target number. It repeatedly divides by 2 if the number is even and greater than 2, or subtracts 1 otherwise. This process continues until we reach 1. The steps are then reversed to give the sequence of operations from 1 to the target.

Example execution:

2.JPG


Task 2: Boolean Variables

Video Script Docs Banner in Indigo Pink Bold Style (1).png
Image Source: canva

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

Answer:
Boolean variables are named after George Boole, a mathematician who developed Boolean algebra. They are used to represent two states: true or false (1 or 0). They're essential in programming for conditional logic, flags, and representing binary states.


Task 3: Conditional Operators

Video Script Docs Banner in Indigo Pink Bold Style (2).png
Image Source: canva

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

Answer:
Conditional operators are used to make decisions in code based on certain conditions. The main types are:

  • If statement (if, else if, else)
  • Switch statement (in some languages)
  • Ternary operator (condition ? true_result : false_result)

Task 4: Subscription Check Program

Problem: Write a program to check if you've subscribed to 100 users.

Solution (Python Code):

3.JPG

Explanation of the program:
This program takes user input for the number of subscriptions and uses conditional statements to check if the number is equal to, less than, or greater than 100. It then outputs an appropriate message based on the comparison.

Example execution:

4.JPG


Task 5: Steemit Subscribers vs Posts Comparison

Problem: Write a program to check whether you have more subscribers or more posts on steemit.com.
Solution (Python Code):

5.JPG

Explanation of the program:
This program takes user input for the number of subscribers and posts, then compares these two values using conditional statements. It outputs a message indicating whether there are more subscribers, more posts, or an equal number of both.

Example execution:

6.JPG


Task 6: Steemit Triple Comparison

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

Solution (Python Code):

7.JPG

Explanation of the program:
This program takes user input for the number of subscribers, posts, and Steem Power. It then uses nested conditional statements to compare all three values and determine which one is the highest (or if there's a tie for the highest).

Example execution:

8.JPG


Task 7: Product vs Sum Comparison

Problem: Determine whether the product or sum of two numbers is greater. The user will input the two numbers one by one.

Solution (Python Code):

10JPG.JPG

Explanation of the program:
This program defines a function that prompts the user to input two integers, one at a time. It calculates both the product and the sum of these numbers and compares the two:

  • If the product is greater than the sum, it returns a message stating that.
  • If the sum is greater than the product, it returns the opposite message.
  • If both are equal, it informs the user accordingly.

Example execution:

11.JPG


Task 8: House Number Side Check

Problem: 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).

Solution (Python Code):

12.JPG

Explanation of the program:
This program defines a function that prompts the user to input two house numbers, one at a time. It uses the modulo operator to check if both house numbers are either even or odd:

  • If both numbers yield the same remainder when divided by 2, they are on the same side of the street, and a corresponding message is returned.
  • If the remainders differ, it indicates that the houses are on opposite sides of the street.

Example execution:

13.JPG


Task 9: Number Property Check

Problem

Given a number, determine if it satisfies the following conditions:

  1. It has more than four digits.
  2. It is divisible by 7.
  3. It is not divisible by 3.

Solution (Python Code):

177JPG.JPG

Explanation of the Program

  1. Function Definition: The function check_number_properties takes an integer number as input.
  2. Condition Check: Inside the function, three conditions are checked using a single boolean expression:
    • The number is greater than 9999 (it has more than four digits).
    • The number is divisible by 7 (number % 7 == 0).
    • The number is not divisible by 3 (number % 3 != 0).
  3. Conditional Return: The function uses a conditional expression (also known as a ternary operator) to return a string indicating whether the number satisfies all conditions.
  4. Input and Output: The user is prompted to enter a number, and the function is called with this input to display the result.

Example Execution

14.JPG


Task 10: Two-Digit Number Comparison

Problem

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

Solution (Python Code):

15.JPG

Explanation of the Program

  1. Function Definition: The function compare_digits takes an integer number as input.
  2. Validation Check: It first checks whether the number is between 10 and 99 (inclusive) to ensure it is a valid two-digit number.
  3. Digit Extraction:
    • The first digit is obtained by performing integer division by 10 (number // 10).
    • The second digit is obtained using the modulo operator (number % 10).
  4. Digit Comparison:
    • If the first digit is greater than the second digit

, it returns a message indicating that the first digit is larger.

  • If the second digit is greater, it returns a message indicating that the second digit is larger.
  • If both digits are equal, it returns a message indicating that they are equal.
  1. Invalid Input Handling: If the input number is not a valid two-digit number, it returns a message prompting the user to enter a valid number.

Example Execution

16.JPG


Conclusion

These 10 tasks cover a wide range of programming concepts, from basic conditional statements to more complex algorithm design. By working through these problems, you can improve your coding skills and deepen your understanding of various programming techniques. The explanations and example executions provided for each task should help you understand how these programs work in practice.



I want to invite @hudamalik20 , @walictd and @mahadisalim join the contest.


My introduction post on Steemit: link


About the Author

👋 Hi, I’m Kafio!
Software Engineer | Data Science Enthusiast | Trading Buff | Development Geek | Computer Science Lover 💻📊📈

I’m passionate about exploring the intersection of technology and innovation, with a special interest in data science, trading, and software development. Whether it’s diving into the latest in computer science or developing new projects, I’m always excited to learn and share insights. 🚀

I also love to travel

Got questions or just want to connect? Feel free to reach out to me at: [email protected] 🌟

image00001.jpeg

Sort:  
Loading...
 last month 

the post is shared on Twitter: Twitter Link.

sdfsdf.JPG

Hi friend, you have presented well and correctly friend. I think you have no doubt that you are one of the programmers. Good luck my friend. Thanks for the invitation...

 last month 

Thank you, my friend!

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

 last month 

Thank you, @eliany, for your support!

 last month (edited)

1000271126.jpg

https://app.gptzero.me/documents/15b41ac0-2b1d-4459-8aee-628434988af3/share

This article is most likely written by an AI, so does it deserve a vote?

Cc. @steemcurator02 @sergeyk

 last month 

No. I create the content for my posts and use tools to check my grammar. However, there is no tool that can definitively determine whether something was created by AI or not. look is that you text you write the tool saying with ai
IMG_4562.png

Coin Marketplace

STEEM 0.18
TRX 0.16
JST 0.030
BTC 80610.66
ETH 3119.79
USDT 1.00
SBD 2.71