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

Hello Everyone
I'm AhsanSharif From Pakistan
Greetings you all, hope you all are well and enjoying a happy moment of life with steem. I'm also good Alhamdulillah.


image.png
Made in Canva

1 to 13709—how would you go?

The task is about starting from the number 1 and trying to reach a target number, like 100 or 13709, using only two operations:
+1, x2

We need to find an efficient way to go from one to our final result. A better way to do this is to use it in reverse. With the reverse method, we can easily reach our main point.

Apart from reaching the target after starting from one, we have to start from the target and reverse these operations to reach our one.

If any number is even that is formed by multiplication, then the inverse operation for it is division. We divide it by two. Similarly, if we are increasing by one, we will decrease its reverse operation by one.

If any of our target numbers are even, we can assume that this is achieved by multiplying any smaller number by two. So we'll divide that number by two.

If your target is odd, you can't divide it by two, so you can add one to it to make it even. But we will subtract one and then divide by two using the reverse function.

These steps we have to apply till we reach our target as we are using reverse function then we have to reach one using these steps.

For example, I start from 13709 to reach 1.

I start from 13709 is odd, so I subtract 1 to get 13708.
13708 is even, so I divide by 2 to get 6854.
6854 is even, so I divide by 2 to get 3427.
3427 is odd, so I subtract 1 to get 3426.
3426 is even, so I divide by 2 to get 1713.
1713 is odd, so I subtract 1 to get 1712.
1712 is even, so I divide by 2 to get 856.

Using this method I have to reach the end until the answer is one.

Now the reverse function of this example is....

  • 1 x 2 → 2.
  • 2 Add 1 → 3.
  • 3 x 2 → 6.
  • 6 x 2 → 12.
  • 12 Add 1 → 13.
  • 13 x 2 → 26.
  • 26 x 2 → 52.
  • 52 Add 1 → 53.
  • 53 x 2 → 106.
  • 106 Add 1 → 107.
  • 107 x 2 → 214.
  • 214 x 2 → 428.
  • 428 x 2 → 856.
  • 856 x 2 → 1712.
  • 1712 Add 1 → 1713.
  • 1713 x 2 → 3426.
  • 3426 Add 1 → 3427.
  • 3427 x 2 → 6854.
  • 6854 x 2 → 13708.
  • 13708 Add 1 → 13709.

Using this method I can find the shortest path and most efficient path to reach my final target. By dividing two we can quickly shorten the number. Thus we can find the universal and efficient solution of any number using our method.

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

Boolean Variable

Boolean variables are used to store values that are false or true. It was named after George Boole, a 19th-century mathematician. who introduced Boolean algebra, a famous branch of algebra that includes true false, and logic operations such as AND, OR, and NOT.

Purpose of Boolean Variable:

Decision Making:

Boolean variables are used to control the flow of any program. such as representing whether a condition is met or not. As the user is logged in or as the data has been verified. The program can take different actions based on these true and false conditions.

Simplifying Logic:

Boolean variables make it easy to handle and represent logic. For example, we use a number or a string to check any condition, but a boolean variable directly represents a logic state. Which is a simpler approach and improves the readability of the code.

Efficient Memory Storage:

Boolean variables take up much less space than other data types such as integers and strings because they are efficient and use the least amount of memory to store either false or true values.

Why are they called that?

He is called Boolean after George Boole because he pioneered the mathematical concepts behind binary logic. Boolean algebra has become the basis of computer science and digital circuit design because everything is represented as true and false. True is one and false is denoted with zero.

In short, Boolean variables are significant in programming because they control the flow of logical reasoning and program behavior. It was named after the mathematician who founded a modern logic system.

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

Conditional Operator

A conditional operator is used to evaluate an expression based on any condition, its main purpose is that it chooses one of two possibilities in programming that depend on false and true.

Purpose:

The conditional operator if-else simplifies the condition further, making it easier for us to understand. It provides a comprehensive way to choose between two values depending on a given condition, which shortens the code and in some cases makes it easier to read.

Types of Conditional Operator:

Ternary Operator(?:):

It evaluates any one condition and then returns one of the values based on whether the condition is true or false.

Syntax:
condition ? value_if_true : value_if_false;

Example:

C++

int a = 10, b = 20;
     int max = (a > b) ? a : b;  // If a is greater than b, max = a; otherwise, max = b

Null Operator (??):

It is used in some languages like JavaScript or C etc. This operator is used to provide a default value whose first value is undefined.

Syntax:
value1 ?? value2

Example:

CSharp

     string name = null;
     string displayName = name ?? "Guest";  // If name is null, displayName = "Guest"

Benefits:

  • This allows us to replace long IF-ELSE statements with short and readable expressions.
  • Sometimes these ternary operators make the code easier to follow when making quick decisions.
  • This can be computationally efficient for simple conditions and helps us reduce the number of lines.
Write a program to check if you’ve subscribed to 100 users.

Using a simple logic we are going to create a program in which we will check if we have subscribed to 100 users then we will count its value and store it in a variable and check. Whether it is equal to a hundred.

Here is the code:

image.png

Here is the output:

On 89 SUBOn 100 SUBOn 180 SUB

Explanation:

This program works in a way that first, it will take input with the help of cin how many users you have subscribed to. If you have subscribed to a full hundred users, if your number is equal to a hundred, it will show you a congratulatory message.

If your value drops below 100, it will tell you how many subscribers you have left to reach 100, even if there are more than 100, it's best.

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

We will create a program that will check if we have more subscribers on Steemit or if we have more posts. For this we will take two inputs from the user in one input he will tell his total subscribers and in the other input, he will tell his total posts. And then comparing them both will give us a result.

Here is the code:


image.png

Here is the output:

More PostMore SUB

Explanation:

The program first prompts the user to enter the number of subscribers and posts and then compares the two.

If our subscriber count is more than posts, it will print that you have more subscribers. If the number of posts exceeds the number of subscribers, it will print that you have more posts. If both are equal, it will print that you have the same number of subscribers and posts. We have created this program to compare and display what can easily be used with real data from our platform.

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

Here too we will create a program through which we will find out if we have more posts more subscribers or more steem power. Like the previous program, we can extend it and add a variable called steem power. And then they will compare these three.

Here is the code:

image.png

Here is the output:

More SP

image.png

More SUB


image.png

More Posts


image.png

Explanation:

It will also first take three inputs which will be the amount of subscriber post and steem power. Then if the subscriber is more than the post and steem power it will print that you have more subscribers. And if your post is more than subscriber and steem power then it will print that you have more posts. If the steem power is greater than both subscriber and post, it prints that you have more steem power. If none of these values are greater than each other, it prints that some of your values are the same.

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

Here we are writing a program that determines if the product or the sum of two given numbers is greater, so we will take the input of the two numbers and then calculate both their product and their sum.

Here is the code:


image.png

Here is the output:

Greater ProductGreater Sum

Explanation:

This program will receive two numbers from the user and then calculate the sum and product of the two numbers. And then will compare its sum and product.

If your product exceeds the sum, it will print that your product is high. If your sum exceeds the product, it will print that your sum is high. If both are the same, it will print that both values are the same. This is a simple program that helps you decide which is bigger by comparing them after doing the calculations.

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

To determine if two house numbers are on the same side of the street we can use the fact that we have our even numbers and odd numbers on different sides. Even is on one side and Odd is on the other side. If both the numbers are equal or even or odd then they will be on the same side.

Here is the code:


image.png

Here is the output:

One Odd And One Even NumberBoth are Even Number

Explanation:

In this, the program will first get the input of two houses from the user, the user will write two numbers. Then the program will check whether the two numbers are equal or even or odd. If both are equal then they are on the same side if both are even then they are on the same side. If one number is even and the other is odd, it is on opposite sides of the road.

With its help, this program identifies which side the house is on and it ensures that we can correctly determine the street side based on the equality of the house numbers.

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 we will create a program that will determine if a value greater than four digits is divisible by seven and not divisible by three and meets this criteria using the code below.

Here is the code:


image.png

Here is the output:

Meet ConditionDoes Not Meet

Explanation:

Function:
Function checkCondition(int number):

This function will check three types of condition

In the first condition, it will check if the number is greater than or equal to 10 thousand. In the second condition, it will check that the remainder is zero when divided by seven and in the third condition it will check that the remainder when divided by three is not zero.

If these three conditions are true then it will be a true count otherwise it will be a false count.

It prompts the user to enter a number and then evaluates the entered numbers. and then calls the checkCondition function and then its output states whether the number meets the specified conditions.

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 we will create a program that will determine which of the two digits is larger, the first digit or the second digit. We will create a simple program that takes two-digit numbers as input from the user and then compares them.

Here is the code:


image.png

Here is the output:

Invalid Input


image.png

Second Is GreaterFirst is Greater

Explanation:

First, the users' input will be verified here whether the number is between 10 to 99 if it is single then it will show the user that you have typed the wrong number please Fix it.

We can get the first digit 10 by dividing the number. And the second digit we will get using the modulus operator.

After that, it will compare the two values and tell which digit is bigger, the first part is bigger or the second one.

For the execution of codes, I use DEV C++. All code examples are in C++ Language. Thank you so much all for visiting my task. I invite my friends @chant, @josepha, and malikusman1 to join this challenge.

Cc:
@sergeyk

Dated: 06-10-2024 About Me