SLC21 Week1 - Learn more about variable types. Subroutines. Practice problems.

in #slc21w1sergeyk5 hours ago

Assalam-o-Alaikum!!!

image.png

Greetings to my all Steemit member. I hope you will be fine and enjoying your best life on STEEMIT. I am also fine ALHAMDULILLAH. Today I am trying to share my homework tasks with you guys, I hope you will like and appreciate it more and more

Come up with your own example similar to float f=7/2; that illustrates the loss (distortion) of value during division. Show how to fix it. Explain why this happens and how you fixed it.

In this code, it will give us a resut of 2 instead of 2.5, Even that if we simpley calcultae then its answer will be 2.5. But it will show 2. instead of 2.5. We can fix it by giveinf at least one operand is a floating point number. Because the result is included in decimal part

image.png

Choose a data type independently and illustrate the limitations of its range: demonstrate the lower boundary (crossing the minimum value) and the upper boundary (crossing the maximum value). Also, demonstrate crossing the boundary during multiplication, explaining the results.

In this program we will use int data type. First we'll assign the inter data type so we can define integerand then we'll have a binary representation of the integers for its range. will and the minimum value for them should be If we subtract this minimum value one by one, it overflows which reaches the maximum positive value.

image.png

Find your own example of 0.1!=0.1, meaning there is a certain value in a variable, but the check shows a different number. Or 0.1+0.2!=0.3. Why is this happening?
  • Why this happen

We all know that int has fixed range. Any operation that attempts to exceed the limit results in an overflow due to the way integers are represented in binary.

image.pngimage.png
Based on the example of counting digits in a number, write a program that determines: (one of your choice): the sum of the digits of a number, (3456 => 3+4+5+6=18), sum(3456) = 18

In this program we will use a loop to find out the required numbver. it will out the last number and add the last digit to the sum. After that we will remove it by dividing it by the tenth 10 day number. When the loop ends, the number will be zero and the sum of the digits will be printed.

image.png

SO the Output is

image.png

As preparation for the next lesson and to review the past, find the largest of two numbers, then the largest of three numbers (this is a different subtask), then the largest of four, five, six, seven - stop when you understand that you are not following a rational path and for larger quantities you should do it differently. Complete the task only using the conditional operator if().

We'll use the operator here and start with the largest of the two numbers, then the three numbers, and then the four numbers as we go. This would be impractical for large sets of numbers. Will go and finally find the biggest one and give us a show

image.pngimage.png
image.pngimage.png
image.pngimage.png
  • So the Output is:

image.png

Perform the previous task using the ternary operator.

We'll also add a ternary, which can be very helpful for dealing with small sets of values ​​and searching for the largest of two, three, and four numbers. And using the ternary operand, we used the method of finding the largest number among the numbers, but in this we will solve this question by using the ternary method.

image.pngimage.png
image.pngimage.png

So the output is
image.png

Perform the previous task by writing a function max() and using it.

Now its a turn of using the function max() to find out the largest number. And in this way we will find the greatest number by using this function

image.pngimage.png
image.pngimage.png
  • So the Output is:

image.png

Write functions:that will print all the divisors of a given number print_div(number) - you can skip printing one and the number itself print_div(11) => (empty), print_div(12) => 2 3 4 6a function sum_div(number) that will calculate the sum of its divisors that are less than itselfsum_div(6) = 1+2+3=6, sum_div(10) = 1+2+5=8,a function that will find and print perfect numbers from 1 to n (the number passed to the function)perfect numbers are those where the sum of divisors less than the number equals the number itself.

There is a different function in this program when it will print the divisor of the air value and also omit the number itself, then return the sum of the divisors of a number and its leaves the number the range we set will find common numbers up to that range and print

image.pngimage.png

image.png

So the Output is:

image.png

So here I completed all tasks. I tried my best to complete this. And I hope you will appreciate me

Special thanks to @sergeyk

Invite Frends: @ruthjoe , @abdullahw2 , @memamun

Regards: @arinaz08

Coin Marketplace

STEEM 0.16
TRX 0.17
JST 0.029
BTC 69382.30
ETH 2479.89
USDT 1.00
SBD 2.54