SEC-S20W3 Writing the first code
I have always wanted to learn to program, and as such, I became so fascinated by programming. When the opportunity to learn to program in the Steemit Engagement Challenge season 20 presented itself, I held on to it and chose to participate in this course hosted by @sergeyk. I am here to perform homework Tasks for week 3, and I believe you will find this post interesting and informative.
Choose your programming environment. Find and show 1) the code entry window, 2) the output window 3) the RUN button and 4) the data entry window - if you can't find this item, don't worry, I'll show you.
For this Task, I chose a C++ shell compiler. The compiler is user-friendly, and during my search for an online C++ compiler, I saw so many compilers, but I chose C++shell because no other students used it. From the above screenshot, I have indicated the entry window which I labeled 1, which is the first window by the top, which is for code writing. At the lower part, there's an output window which I labeled 2. Then, right between the entry window and output window, there's a run button, which I labeled 3. The data entry window is the same as the entry window.
During my research for the task, I found that some online compilers have separate data entry windows, compilers like one compiler.
Find your way to act, from 1 to 100. Effective or not in this task is not important.
I have to solve this task in three versions, and I have to transverse my answer from 1 to 100 using only +1 and ×2. Here are the steps I took to reach from 1 to 100.
1+1+1+1+1+1=6×2=12×2=24×2=48×2=96+1+1+1+1=100
Narration of the steps;
begin with 1
add 1
(1+1=2)add 1
(2+1=3)add 1
(3+1=4)add 1
(4+1=5)Add 1
(5+1=6)multiply by 2
(6×2=12)multiplied by 2
(12×2=24)multiply by 2
(24×2=48)multiplied by 2
(48×2=96)add 1
(96+1=97)add 1
(97+1=98)add 1
(98+1=99)add 1
(99+1=100)
It took me 14 steps to complete the task.
Find the best, shortest way. No one found it in the last lesson. Search!
So here I am trying to reduce the previous 14 steps I took in solving from 1 to 100 using only +1 and ×2. Let's see how it goes;
1+1+1=3×2=6×2=12×2=24×2=48×2=96+1+1+1+1=100
You can see that I have reduced my 14 steps to 12 steps by performing +1 for three times to have 3, then I performed ×2 for five times to have 96, and finally, I performed +1 for four times to have 100. I thought of another shorter step than this one, and I came up with this;
1+1+1= 3×2= 6×2= 12×2= 24×2= 48+1+1=50×2= 100
From the above, I have reduced my steps to 10 steps and I found it's still very much I tried reducing it again, and I came up with the final one below;
1+1+1=3×2=6×2=12×2=24+1=25×2=50×2=100
This time it took me 9 steps to solve the problem as indicated above. This is how far I was able to reduce my steps.
This is the narration of the steps;
Begin with 1.
Add 1:
( 1 + 1 = 2 )Add 1:
( 2 + 1 = 3 )Multiply by 2:
( 3 x 2 = 6 )Multiply by 2:
( 6 x 2 = 12 )Multiply by 2:
( 12 x 2 = 24 )Add 1:
( 24 + 1 = 25 )Multiply by 2:
( 25 x 2 = 50 )Multiply by 2:
( 50 x 2 = 100 )
if in addition to +1and x2it is possible to create an additional variable int k;, how many steps will it save, you cannot k=n put a certain number in the variable, for example 7 or 23. You can only fix the value of the variable at a certain step n
I began operating with +1, then counted from 1 to 5, then stored it in the variable k, so k=5. Then again, I performed ×2 on 5 till I got 20. Finally, I multiplied the variable k by 20.
Here is the breakdown of the steps;
- 1+1+1+1+1=5
- set k=5
- 5×2=10
- 10×2=20
- 20×k
- 20×5=100
I have a total of 21 followers
Concerning the requirement, I have added 21 which is the number to be added to 21 followers. So 21+21=42
Now it's time to break 42
I have to get to 42 starting from 1 using +1 and ×2. These are the steps I took to get to 42 on the first attempt;
- 1+1+1+1+1=5( 5 steps)
- 5×2=10(1step)
- 10×2=20(1step)
- 20×2=40(1step)
- 40+1+1=42(2steps)
I arrived at 42 in 10 steps on my first attempt.
Here is the program(code)
#include <iostream> using namespace std; int main() { int a=1, count_operation=0; a=a+1+1+1+1; count_operation++; //5 a=a*2; count_operation++; //10 a=a*2; count_operation++; //20 a=a*2; count_operation++; //40 a=a+1; count_operation++; //41 a=a+1; count_operation++; //42 cout<<"I reached 42 from 1 in "<< count_operation <<" steps by using +1 and x2 commands"; return 0; }
I completed the program in 6 steps.
Take your number, I take my 49 and add its digits and also add 7: 4+9+7=16
We need to write a program to raise the number to the 16th power - all of you will have different numbers.
I have taken my number which is 42, then added its digits and also added 7, then I have;
4+2+7=13. I am going to write a program of 4^13, where 4 is the base and 13 is the power. Here is the program;
#include <iostream> using namespace std; int main() { int a=4*4*4*4*4*4*4*4*4*4*4*4*4,pow; // 4^13 pow=a*a; // 4^3 pow=pow*pow; // 4^6 pow=pow*pow; // 4^9 pow=pow*pow; // 4^12 pow=pow*pow; // 4^13 cout<<a<<"\n"; return 0; }
The program runs successfully, and 4^13=67,108,864.
Write a program to divide it into numbers. And now how to collect it from numbers back into a number? How to reverse a number. In other words, unfold. for example, how to get 39 from 93.
Again, I chose my number of followers, that is 21, and then I will write a program to divide it into 2 and 1. Here is the program;
#include <iostream> int main() { int number = 21; int onesDigit = number % 10; // Units int tensDigit = (number / 10) % 10; // Dozens std::cout << "Units Digit: " << onesDigit << std::endl; std::cout << "Tens Digit: " << tensDigit << std::endl; return 0; }
To separate the number, I used integers separation or division /
and module %
Take the first three numbers from your profile (the number of followers or posts) - divide it into numbers.
I took the first three numbers from my number of posts, which is 102, and I am going to divide the numbers with a program. Here is the program;
#include <iostream> int main() { int number = 102; int onesDigit = number % 10; // Units int tensDigit = (number / 10) % 10; // Dozens int hunsDigit = (number / 100) % 10; // Hundreds std::cout << "Units Digit: " << onesDigit << std::endl; std::cout << "Tens Digit: " << tensDigit << std::endl; std::cout << "Hundreds Digit: " << hunsDigit << std::endl; return 0; }
I saved or stored 102 in the number
variable. Then, hundredths, tenths, and units are extracted using arithmetic operators, as seen in my program.
Find a four-digit number in your profile (by writing out the first digits, or combining it with others, the first two of one and the first two of another.) Or a random number - the main thing is that it is not in other homework. Divide it into numbers - the first number is X, the second number is X, the third number is X, the fourth number is X
I chose the total number of my posts, which is 1028, and wrote a program to divide them into numbers, such that all the numbers will be x. Here is the program;
#include <iostream> int main() { int number = 1028; int onesDigit = number % 10; // Units int tens digit = (number / 10) % 10; // Dozens int hunsDigit = (number / 100) % 10; // Hundreds int thouthDigit = (number / 1000) % 10; // Thousands std::cout << "First digit (x): " << onesDigit << std::endl; std::cout << "Second digit (x): " << tensDigit << std::endl; std::cout << "Third digit (x): " << hunsDigit << std::endl; std::cout << "Fourth digit (x): " << thouthDigit << std::endl; return 0; }
I saved or stored the number 1028 in the number variable. Then I converted the number into the digits using the arithmetic operations.
First digit: number / 1000
Second digit: (number / 100) % 10
Third digit: (number / 10) % 10
Fourth digit: number % 10
Then we have the divided numbers as seen in the above image.
I invite;
@goodybest
@eliany
@blessedbee
Thank you very much, @eliany, for your support, I am grateful.