WEBVTT

00:00.460 --> 00:08.010
Are at the end of the section now so you should be doing some practice problems and start with the problem

00:08.010 --> 00:08.550
one.

00:09.980 --> 00:15.550
Write a program that will take you a number from the user and print the reverse of that number.

00:15.720 --> 00:24.420
So gives an example of the input and input is 1 2 3 4 and the output should be 4 3 2 1.

00:24.440 --> 00:27.940
So how would we approach this problem.

00:28.400 --> 00:40.580
Well you see here that if we could just get the last digit of the of the number each time.

00:40.580 --> 00:50.940
So if it could sort of break away this 4 and then sort of shift it to the last in a different number.

00:51.990 --> 00:59.160
And then again break away this three and then break this to and then break this one in sort of string

00:59.160 --> 01:02.020
it together by addition.

01:02.120 --> 01:06.320
So let's let me show you what I mean here.

01:06.320 --> 01:10.680
So let's say we had the number input number.

01:10.700 --> 01:19.230
So call it number and error and and have the reverse of the number we're incident to zero.

01:19.640 --> 01:22.490
So we do get it.

01:22.520 --> 01:25.680
This is from the user.

01:25.890 --> 01:31.460
These are number

01:38.100 --> 01:38.910
number

01:41.630 --> 01:45.180
in to do a loop.

01:45.280 --> 01:53.380
While we don't know really what the conditions should be at so let's just skip the condition for now

01:54.400 --> 01:59.470
and say reverse is

02:02.080 --> 02:07.790
what how would we break off this for.

02:08.230 --> 02:21.200
Well since these were all based 10 numbers if you if you actually get the remainder divided by 10.

02:21.260 --> 02:28.380
So if you divide this number by 10 and get the remainder you should get four.

02:28.400 --> 02:39.430
So you know how to do that and it's through the modulus operator number modulus 10 and so that will

02:39.430 --> 02:48.440
actually give you the last digit of the number so that we're not done.

02:48.590 --> 02:57.350
So we need to then take what we have previously and multiply it by 10.

02:57.350 --> 03:02.590
So this shifts it to the left by one place right.

03:02.600 --> 03:10.280
So this is the ones tens hundreds thousands right.

03:10.500 --> 03:21.360
And then we just add that and then in order to make it so that the number is less it will be one to

03:21.360 --> 03:26.350
three feet at the next iteration we'll divide that number by 10

03:31.420 --> 03:33.620
and you want to just print out the number

03:49.010 --> 03:54.220
urs Endal and let's run this.

03:54.230 --> 03:55.740
It's even.

03:55.880 --> 04:00.650
It's.

04:01.380 --> 04:06.800
So we still don't know what the condition is right for that.

04:06.810 --> 04:17.990
So really we're done when this number that we keep sort of breaking off pieces of is zero.

04:18.150 --> 04:29.990
So while number is greater than zero then we'll do this.

04:30.460 --> 04:31.230
Now it's Rinat

04:38.750 --> 04:43.970
in center 1 2 4 and there are Erse is 4 3 2 1.

04:43.970 --> 04:53.720
So again if you're not quite understanding how this works we're essentially taking the last digit of

04:53.720 --> 05:04.170
the number right here and we're adding it to what we had previously of the reverse.

05:04.170 --> 05:07.340
We start at zero of course because we don't have anything yet.

05:08.270 --> 05:11.320
So averse would be zero.

05:12.210 --> 05:16.100
Plus the last digit before.

05:16.400 --> 05:22.730
And then we multiply and then we then of course divide it by 10.

05:22.760 --> 05:27.670
So the next time would be 1 2 3 4 1 2 3.

05:28.430 --> 05:39.700
And then we take what we had before which is four times 10 to be 40 plus or 8 2 or 3 sorry.

05:40.560 --> 06:01.100
And then for the 0 2 and then 4 3 2 0 plus 1 and 3 2 1 K so that's sort of how it's working here.

06:04.500 --> 06:07.030
Hopefully that sort of makes sense.

06:11.560 --> 06:14.250
So let's go to the next problem.

06:16.210 --> 06:23.340
So says rent per game that takes into numbers from the user and Princess star pattern in a rectangular

06:23.340 --> 06:24.010
form.

06:24.030 --> 06:29.270
The first number will be the number of rows and the second number will be the number of columns.

06:29.460 --> 06:37.980
So an example here if rows was 5 in columns 6 then it would give out this star pattern.

06:37.980 --> 06:52.590
So there's six columns one two three four five six and five rows one two three four five or eight.

06:52.850 --> 06:54.940
So how would we do this.

06:54.980 --> 07:01.090
Let's just start with getting the rows and columns from the user rows.

07:02.440 --> 07:14.640
Zero were Cowen's so calls 0.

07:19.960 --> 07:23.520
Your number rose

07:26.970 --> 07:30.510
in rows right.

07:30.520 --> 07:33.100
And do any error checking for now.

07:37.260 --> 07:38.000
Enter

07:40.480 --> 07:42.500
number of columns.

07:48.350 --> 07:53.930
Number rows or columns.

07:54.580 --> 08:05.310
OK so now we have the rows and columns but how do we make it so that this pattern is outputted

08:12.080 --> 08:21.550
since we don't know how many columns and rows we have to output since it's given to us by the user.

08:21.710 --> 08:25.760
We're going to need more than one loop I think.

08:25.760 --> 08:32.520
So we're say into our four row.

08:32.690 --> 08:41.220
Or it's less than the number of rows is we're not using.

08:41.340 --> 08:50.240
We're not starting our loop at 1 using zero instead rope plus plus.

08:50.340 --> 08:55.660
So that's why we're not saying greater than or equal to you is saying we're less than or equal to or

08:55.680 --> 08:56.910
missing less than

09:01.100 --> 09:02.030
we're going to need.

09:02.030 --> 09:07.070
So we saw in the nested loop lecture that for every

09:09.690 --> 09:15.760
for every one iteration of the outer loop we can do many iterations of the.

09:15.840 --> 09:17.400
And that's exactly what we need to do.

09:17.400 --> 09:25.440
So we're actually going to loop through all the rows of each row and then we're going to do this many

09:25.770 --> 09:30.160
iterations of the inner k.

09:30.290 --> 09:33.710
So that will give us our star pattern.

09:33.920 --> 09:52.410
So we're going to save for C for Column C is less than number of columns plus in or CC out a star and

09:54.020 --> 10:00.450
in that after each row is finished then we do.

10:00.470 --> 10:03.290
And all

10:06.720 --> 10:07.560
just

10:11.540 --> 10:28.040
so you know this that's for example find 18:6 and there we go.

10:28.060 --> 10:35.610
So as one two three four five six and there's five ropes.

10:37.040 --> 10:44.730
We can do this any number really say seven in 10.

10:44.810 --> 10:53.490
1 2 three four five six 10 7 call ropes here.

10:53.580 --> 10:54.890
So I think this is working.

11:00.100 --> 11:07.830
So now on to the next one which is kind of similar is

11:12.790 --> 11:13.370
so.

11:13.390 --> 11:19.020
Write a program that takes in a number and Prince pyramid's star pattern.

11:20.840 --> 11:25.640
The pattern for five would look like this.

11:26.860 --> 11:32.400
So there would be one two three four five.

11:32.460 --> 11:43.910
And there's kind of it's like symmetric so you have the one for the top and then you have three five

11:43.910 --> 11:54.080
and seven eight nine.

11:54.210 --> 12:01.980
So it's sort of similar to how we did the star pattern above.

12:01.990 --> 12:05.770
So the first thing we should do is just get the number for easier

12:08.610 --> 12:12.560
see each means.

12:14.230 --> 12:15.050
Sure.

12:15.220 --> 12:15.970
Number

12:23.150 --> 12:31.680
get the number so we'll have to do some nested loops again.

12:32.650 --> 12:39.520
So how this is going to work is just like the above.

12:39.700 --> 12:53.330
We need to go through all the spaces first and the spaces sort of Windell as you get lower and lower.

12:53.380 --> 13:01.720
So there's one space here but there's no space at the bottom K so the spaces are actually kind of inverted

13:01.720 --> 13:06.660
to the pier made itself the kind.

13:06.720 --> 13:15.740
Think of it that way so let's go for Cincy.

13:15.780 --> 13:19.020
I whose T.

13:19.030 --> 13:26.460
0 is less than number.

13:26.580 --> 13:35.590
Plus plus again I'm starting at zero because typically we usually don't start at 1.

13:35.630 --> 13:36.830
We actually start at zero.

13:36.840 --> 13:41.280
Every every programmer kind of starts in zero thinking

13:44.410 --> 13:50.480
so first we're do we're actually going to print all the trailing spaces.

13:50.510 --> 13:54.820
How many spaces are there.

13:55.060 --> 14:09.650
So Jay and Jay is going to be less than something G plus plus And we're going to print out the space

14:09.650 --> 14:17.240
first and then we're going to loop through and print out all the stars.

14:17.280 --> 14:24.940
So are the stars here in print spaces here.

14:29.010 --> 14:29.580
It's

14:33.630 --> 14:35.950
so rescript to you.

14:37.130 --> 14:39.420
Ch g zero.

14:40.780 --> 14:47.080
J is less than something and A-plus plus

14:51.750 --> 14:57.490
out star.

14:58.010 --> 14:59.970
And here he said

15:03.130 --> 15:05.200
so we don't know that the conditions should be.

15:05.260 --> 15:08.220
So what should the condition be.

15:09.070 --> 15:15.860
So how many spaces exists in this case.

15:17.610 --> 15:29.150
So there's one two three four and then the fifth one is the star and then one two three and then one

15:29.150 --> 15:45.700
two and then one so really it's the number minus the current row minus one right.

15:46.200 --> 15:55.850
So if the number was five current row is zero let's say this is 0 0 with row the first row.

15:56.270 --> 16:00.710
Second third fourth OK

16:04.390 --> 16:11.210
in and so the number would be five.

16:11.210 --> 16:27.980
In this case 5 minus zero is five minus 1 which is 4 and 5 minus 1 minus 1 is 3 5 minus 2 minus 1 is

16:27.980 --> 16:29.230
2.

16:30.780 --> 16:37.520
So that's kind of one way we can think about figure out all the space is.

16:37.530 --> 16:46.620
And for this one it's actually pretty simple it's always just two times the row last one

16:51.200 --> 16:57.070
K so the row here would be zero.

16:57.380 --> 17:01.470
So two times that is zero.

17:01.720 --> 17:04.390
And then plus 1 is 1.

17:04.390 --> 17:06.190
So two times the row here.

17:06.230 --> 17:23.290
So the row is 1 and 2 times one 2 plus 1 is 3 and 2 times the row here is four plus one is 5 Cancion.

17:23.360 --> 17:30.570
So let's run this and see fun.

17:31.030 --> 17:31.820
And there you go

17:34.860 --> 17:39.790
of more 7 or something.

17:40.870 --> 17:43.660
Is k.

17:43.690 --> 17:48.160
So in the next section we're going to start talking about functions.
