Are at the end of the section now so you should be doing some practice problems and start with the problem
one.
Write a program that will take you a number from the user and print the reverse of that number.
So gives an example of the input and input is 1 2 3 4 and the output should be 4 3 2 1.
So how would we approach this problem.
Well you see here that if we could just get the last digit of the of the number each time.
So if it could sort of break away this 4 and then sort of shift it to the last in a different number.
And then again break away this three and then break this to and then break this one in sort of string
it together by addition.
So let's let me show you what I mean here.
So let's say we had the number input number.
So call it number and error and and have the reverse of the number we're incident to zero.
So we do get it.
This is from the user.
These are number
number
in to do a loop.
While we don't know really what the conditions should be at so let's just skip the condition for now
and say reverse is
what how would we break off this for.
Well since these were all based 10 numbers if you if you actually get the remainder divided by 10.
So if you divide this number by 10 and get the remainder you should get four.
So you know how to do that and it's through the modulus operator number modulus 10 and so that will
actually give you the last digit of the number so that we're not done.
So we need to then take what we have previously and multiply it by 10.
So this shifts it to the left by one place right.
So this is the ones tens hundreds thousands right.
And then we just add that and then in order to make it so that the number is less it will be one to
three feet at the next iteration we'll divide that number by 10
and you want to just print out the number
urs Endal and let's run this.
It's even.
It's.
So we still don't know what the condition is right for that.
So really we're done when this number that we keep sort of breaking off pieces of is zero.
So while number is greater than zero then we'll do this.
Now it's Rinat
in center 1 2 4 and there are Erse is 4 3 2 1.
So again if you're not quite understanding how this works we're essentially taking the last digit of
the number right here and we're adding it to what we had previously of the reverse.
We start at zero of course because we don't have anything yet.
So averse would be zero.
Plus the last digit before.
And then we multiply and then we then of course divide it by 10.
So the next time would be 1 2 3 4 1 2 3.
And then we take what we had before which is four times 10 to be 40 plus or 8 2 or 3 sorry.
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.
Hopefully that sort of makes sense.
So let's go to the next problem.
So says rent per game that takes into numbers from the user and Princess star pattern in a rectangular
form.
The first number will be the number of rows and the second number will be the number of columns.
So an example here if rows was 5 in columns 6 then it would give out this star pattern.
So there's six columns one two three four five six and five rows one two three four five or eight.
So how would we do this.
Let's just start with getting the rows and columns from the user rows.
Zero were Cowen's so calls 0.
Your number rose
in rows right.
And do any error checking for now.
Enter
number of columns.
Number rows or columns.
OK so now we have the rows and columns but how do we make it so that this pattern is outputted
since we don't know how many columns and rows we have to output since it's given to us by the user.
We're going to need more than one loop I think.
So we're say into our four row.
Or it's less than the number of rows is we're not using.
We're not starting our loop at 1 using zero instead rope plus plus.
So that's why we're not saying greater than or equal to you is saying we're less than or equal to or
missing less than
we're going to need.
So we saw in the nested loop lecture that for every
for every one iteration of the outer loop we can do many iterations of the.
And that's exactly what we need to do.
So we're actually going to loop through all the rows of each row and then we're going to do this many
iterations of the inner k.
So that will give us our star pattern.
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
in that after each row is finished then we do.
And all
just
so you know this that's for example find 18:6 and there we go.
So as one two three four five six and there's five ropes.
We can do this any number really say seven in 10.
1 2 three four five six 10 7 call ropes here.
So I think this is working.
So now on to the next one which is kind of similar is
so.
Write a program that takes in a number and Prince pyramid's star pattern.
The pattern for five would look like this.
So there would be one two three four five.
And there's kind of it's like symmetric so you have the one for the top and then you have three five
and seven eight nine.
So it's sort of similar to how we did the star pattern above.
So the first thing we should do is just get the number for easier
see each means.
Sure.
Number
get the number so we'll have to do some nested loops again.
So how this is going to work is just like the above.
We need to go through all the spaces first and the spaces sort of Windell as you get lower and lower.
So there's one space here but there's no space at the bottom K so the spaces are actually kind of inverted
to the pier made itself the kind.
Think of it that way so let's go for Cincy.
I whose T.
0 is less than number.
Plus plus again I'm starting at zero because typically we usually don't start at 1.
We actually start at zero.
Every every programmer kind of starts in zero thinking
so first we're do we're actually going to print all the trailing spaces.
How many spaces are there.
So Jay and Jay is going to be less than something G plus plus And we're going to print out the space
first and then we're going to loop through and print out all the stars.
So are the stars here in print spaces here.
It's
so rescript to you.
Ch g zero.
J is less than something and A-plus plus
out star.
And here he said
so we don't know that the conditions should be.
So what should the condition be.
So how many spaces exists in this case.
So there's one two three four and then the fifth one is the star and then one two three and then one
two and then one so really it's the number minus the current row minus one right.
So if the number was five current row is zero let's say this is 0 0 with row the first row.
Second third fourth OK
in and so the number would be five.
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
2.
So that's kind of one way we can think about figure out all the space is.
And for this one it's actually pretty simple it's always just two times the row last one
K so the row here would be zero.
So two times that is zero.
And then plus 1 is 1.
So two times the row here.
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.
So let's run this and see fun.
And there you go
of more 7 or something.
Is k.
So in the next section we're going to start talking about functions.