SEC-S20W4: Decision Making in programming; If-Then-Else

in #sec20w4sergeyk5 days ago (edited)
Gold Luxury Initial Circle Logo_20241005_002359_0000.png
edited with canva

Hello friends and I warmly welcome you all to the SEC S20/W4 It is really interesting to have to share my thoughts in this challenge. I do hope we enjoy my article.


"Find a universal and optimal solution to this problem"

I found an optimal solution to this specific number such as 100 or 1307, starting from 1 and by using only about two operations. Multiplying by 2 or adding one. I would implore the backward strategy from the target 1. It will help to us minimum steps to approach target numbers in a universal way.

Backward Approach: This shows how we can reduce the target number back to 1 using the reverse method. These are 2 rules in this respect. If the target is an even number, the inverse of multiplication by 2 is division by 2, this is because previous numbers can be obtained by multiplying by 2, thus the inverse would be gotten by multiplying by 2. If target number is odd number, the inverse of adding 1 is performed by subtracting 1 from number. This ensures that most few operation be used, thus we will be able to grow faster by multiplication by 2 and addition of 1 where needed


Divide 100 by 2 100 ÷ 2= 50
50 is even so divided by 2 50 ÷ 2= 25
25 is odd so subtract 1 25 - 1= 24
24 is even so divided by 2 24÷2= 12
12 is even so divided by 2 12÷2= 6
6 is even so divided by 2 6÷2= 3
3 is odd so subtract 1 3-1= 2
2 is even so divided by 2 2÷2= 1


Reverse Operation:


Multiply 1 by 2 = 2
Add 1 = 3
Multiply by 2= 6
Multiply by 2= 12
Multiply by 2= 24
Add 1= 25
Multiply by 2= 50
Multiply by 2= 100


Operation 13709:

Start with 13709 it is odd so subtract 1.

13709-1 =13708
13708 is even so divided by 2 13708÷2= 6854
6854 is even so divided by 2 6864÷2= 3427
3427 is odd so subtract 1 3427-1= 3426
3426 is even so divided by 2 3426÷2= 1713
1713 is odd so subtract 1 1713-1= 1712
1712 is even so divided by 2 1712÷2= 856
856 is even so divide by 2 856÷2= 428
428 is even so divide by 2 428÷2= 214
214 is even so divided by 2 214÷ 2= 107
107 is odd so subtract 1 107-1= 106
106 is even so divided by 2 106÷2= 53
53 is odd so subtract 1 53-1= 52
52 is even so divided by 2 52÷2= 26
26 is even so divided by 2 26÷2= 13
13 is odd number so subtract 1 13-1= 12
12 is even so divided by 2 12÷2= 6
6 is even so divided by 2 6÷2=3
3 is odd so subtract 1 3-1=2
2 is even so divided 2 2÷2=1

Reverse Operation to reach 13709 from 1.


Start with 1
Multiply by 2= 2
Add 1= 3
Multiply by 2= 6
Multiply by 2= 12
Add 1= 13
Multiply by 2= 26
Multiply by 2= 52
Add 1= 53
Multiply by 2= 106
Add 1= 107
Multiply by 2= 214
Multiply by 2= 428
Multiply by 2= 856
Multiply by 2= 1712
Add 1= 1713
Multiply by 2= 3426
Add 1= 3427
Multiply by 2= 6854
Multiply by 2= 13708
Add 1= 13709


I have performed task while using the reverse method in varying steps. I reached from 1 to 13907 in about 20 steps. This is the generally accepted method to reach target numbers.


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

Boolean Variables are important for identifying true or false values. These values are represented by 0 for false and 1 for true. They help manage decisions and flow of program. Thy are used in conditional structures, loops and logical operations.


Purpose of Boolean Variables


Loop Control: These variables can be used to control loop such as for or while. It continues to run as long as conditions is false and it stops when condition becomes true as shown below
bool done= false; while (!done){ // some operations... done= true; // loop exists when 'done' becomes true }

Conditional Logic: These are variables are essential in conditional statement as if, else, switch. They help determine whether a particular code should be executed or not.
bool is Raining= true; If ( is Raining) { cout << "Take an umbrella!"<< end l; }

The variables Raining is Boolean was initially true and checks conditions of rain then execute related code.

Explain Complicated Logic; These variables help to simply complicated logical problems by breaking them in bits into smaller parts. It improves readability and maintenance of code.


Why are they called that


The reason behind the Boolean traces down into the name George Boole The great English mathematician, he brought the Boolean algebra into play in the 19th century. He is known as the father of Boolean logic. The Boolean deals with binary values (0, 1) or (true, false). Everything encoded in the computer are in binary numbers of 0 and 1. Before understanding and interpreting. They are vital in running programs and proper decision making.


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

Conditional Operators are used in some condition, it provides a simple and express way to makinh decisions based on conditions. They are beneficial where if-else statement come to play. They allow to choose between two values or results all a function of the condition.

Types of Conditional Operator

Syntax Condition:
value_if_true: value_if_false It is written with a question mark? Guiding it, the true condition written after a colon is used and after that colon another condition

scenario;

int a= 10 b= 20; Int max = (a>b)? a:b; // if 'a' is greater than 'b' max is assigned 'a' otherwise 'b' cout <<Maximum value: "<<max <<end l;

int max= (a>b) is condition if true value will be a if condition is false value would be returned as an answer.

Ternary Operator:
It is the most common type of conditional operator, used to check conditions while returning one value from two as a result of condition whether true or false it involves three operand conditions, values if condition is true, values if condition is false.

AND Operators:
This operators allow to check two conditions at once. It turns true when both conditions are true and false if any on the condition differs.

ABA&&B
00false
01false
10false
11true

If two values are true then if will return true example of (a>b) && (c<d){ return true ; } {else return false;}

Logical Operators:
They are used to create complex conditions by combining simple ones. They used alongside conditional statement specifically if statement is used to check more than one condition at once.

OR Operators:
This allows us to check two conditions at once and returns, true when one condition is true otherwise in other condition remains false

ABA or B
00false
01true
10true
11true

NOT Operators:
This operators allows to reverse values of boola expression, turns true into false and false into true The truth table of NOT Operator is these.

A!A
TrueFalse
FalseTrue

Screenshot_20241004-085838_1.pngScreenshot_20241004-085838_2.png

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

Screenshot_20241004-080701.png

I have about 5134 posts and 393sp, 68 followers and 95 following, it compared all values if showed all the values and answer too


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

I would run the program as before on the C++ I would get the input from users, after that calculate the sum and product of numbers, then check the sum and product. If sum of numbers is greater then my program would return to sum. If otherwise product is greater then it will return to product of numbers if it's greater than sum. From the program they are both equal.

Screenshot_20241004-091223.png

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

This illustration shows that even house numbers are separated in one side, different from odd house numbers. Invariably if two eve house numbers are inputed on same side automatically they would be on one side, and same occurs in the odd house number. Importantly if two random numbers are picked both odd and even they would fall on opposite sides.

Screenshot_20241004-194405.png

Using conditional statement parameter % modulus operator, we use it to check whether odd or even, this if it gives a remainder then it's even, asides that it is odd. h1, h2 were variables used which stands for the houses respectively. It checks which house number belongs to each category. The message is relayed in the coed information above.


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

We are on the quest in search for a number which has more than 4 digits, divisible by 7 and not divisible by 3.

Screenshot_20241004-195620.png

I initialized a number as integers, program back testing the condition in the question, checking a number with four digits would be 9999, checking of it is divisible by 7 and it falls in line. Checking if it is divisible by 3. Therefore tells us all that the number meets all conditions, in other cases it may not meet all conditions.


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

I chose three integers, first digit and second digit, it must accept two digits and then Ill separate both digits of numbers using quotient/ remainder applying condition on a digital of number. Condition taking both numbers and checking whether first digit is greater than second digit.

Screenshot_20241004-200445.png

Program seeks for 2 digits number, first digit number are entered and is gotten by dividing by 10 thus the first digit is the quotient. Second digit is the remainder when the digit is divided by the 10 with modulus operator, program checks whether first number is great or equal to second. If integers are entered then both numbers are equal.

Cc;
@sergeyk

I have concluded my article inviting @starrchris @enamul17 @josepha @chant to join challenge
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