C-SHARP - 3.13 Practice
Hello everyone and welcome to this new video. In this video, I'm going to give you an exercise that will summarize everything we've seen so far, that is to say that all your knowledge will be required in order to succeed in this exercise! Your exercise will be as follows: You will have to write a program that will calculate the factorial of a number entered by a user. If for example j enters 5, the program will display me 120. And if you don't know what a factorial is, a factorial is the way to write the product of all the positive integers less than or equal to a number n . For example for the number 5 it would give 1 x 2 x 3 x 4 x 5. You must therefore create a program which will ask the user for a number, then the program will calculate the factorial of the number entered by the user and will display it afterwards. On the other hand, there is a condition if we want a larger factorial, we will have to use a long type because we are leaving the Int range and here I want you to work on the conditions as well. So if the user enters a number less than or equal to 31 then we display the factorial otherwise we display: The number is too large, we leave the int. So here I recap: You must ask the user for a number (be careful here there will be a small particularity for you to guess) Then you are going to create a program which will retrieve this number and will calculate the factorial. And finally, if the user enters a number less than or equal to 31 then its factorial will be displayed, otherwise it will be displayed The number is too large, we leave the Int So it's up to you. I hope that I have been understandable enough, and that you will get there! I don't doubt it, it's very simple, don't hesitate to skim over the old videos, if you don't remember certain notions. Pause the video and we meet just after for the correction. We meet now for the correction of the exercise, I hope you succeeded, otherwise pay attention to the correction! The first step was to ask the user to enter a number. So we will first do a Console.WriteLine() which will prompt the user to enter a number. We will put quotation marks and we will write you want the factorial of which number? and do not forget the; Now, you remember that to interact with the user, we have a function that allowed you to do that, do you remember? This is the Console.ReadLine() function! It allows to retrieve what the user will enter. So here we are going to create a variable of type string because the Console.ReadLine() function exclusively returns strings then we are going to give it a name for example number number_user and we are going to assign it the function Console.ReadLine() Here the variable number_user will store what the user will enter in order to be able to reuse it right after in our program. On the other hand, I told you that you had to be careful because there was a small particularity. Indeed here we have a number in string type. And you can't do an operation on a string so what do you have to do to be able to use this variable? Well, we have to convert our variable from type string to type int, remember? we are going to create a variable of type int that we are going to call conversion and do you remember the function that converts a string into an int? This is int.Parse() and between the parentheses we put our variable of type string. So now we have converted to Int and we have stored in the conversion variable the number that the user has entered. Now let's go to the heart of the program, we said that we are going to calculate a factorial. We have already said that a factorial is the way of writing the product of all positive integers less than or equal to a number n, i.e. the numbers must be between the number 1 and the number entered, then we gave the same example as for 5 it will give 1*2*3*4*5 here we see that these are multiplications which are repeated several times. Who says many times, what are you thinking? To the curls exactly! Here we will therefore make a for loop which will make it possible to produce the product of all the integers in order to obtain the factorial like the example of 5 above. First of all, we will create a factorial variable which will be initialized to 1 because we said that the factorial must be between 1 and the given number. And logically also if we make the product with a 0 the result will be constantly 0 So now, we will type for between parentheses we will create a variable i which we will initialize to 1, it will allow us to make the product at each turn of the loop so we must initialize it to 1. Then we give the condition, what is it here? It is that as long as i < or equal to the number given by the user then the instructions are executed. Here, since we initialize the ia 1, we lose a turn because usually, we initialize it to 0 and that the product also includes the number that the user enters then the condition must understand when the loop reaches this number and must execute the instructions then we must put a = in addition to the lower one. We finish by incrementing the ia each turn of the loop, so here i++And in the braces of the for we will put the following operation. We will write factorial is equal to factorial * i; If the user enters 5, on the 1st round, a will give factorial* i so 1*1 and we will store a in factorial then 2nd round i is incremented so the product changes to be 1*2 so will store 2 in factorial in round 3 a will be 2*3 because factorial is 2 and i is 3 so we will store 6 and so on until the condition put in the for is met And we will have at the end of this loop, the factorial of the number entered by the user to store in the variable factorial. Now let's go to the display, I had imposed a condition on you, it was to display the factorial only if the number entered by the user was less than or = 31. Indeed it was possible to use another type to manage the excess but I wanted to make you work the maximum of notions in this exercise, that is why I limited to 31 to be able to remain in the slice of the int. So to do that, what did you have to do? You must write if and in parentheses put the following condition (conversion <= 31) and in the braces you can display the factorial as you wish but I want you to review all the notions I will do a Console.WriteLine () and inside the parentheses, do you remember that there was a function that allowed you to avoid certain errors in the display? It was the function string.Format well I'm going to use it inside my Console.WriteLine() I type string.Format() between parentheses the first parameter was a format string so I'm going to do The factorial of here I will retrieve the number that the user has entered so I put braces with a zero inside is and here I put braces with a 1 inside to display the factorial and then after the quotes I put the user_number and factorial variables. And to finish we had that Otherwise if the number between is too large then we display The number is too large, we leave the int. So we do an else and inside we put a Console.WriteLine() with the sentence the number is too big, we get out of the int If we run the program, the program displays You want the factorial of which number for example 5 we enter. And it shows us the factorial of 5 is 120. If we try for 6 for example we restart the program we type 6 we enter it shows us the factorial of 6 is 720. 720 is indeed the factorial of 6 then our program perfectly. Now we will test for example for a number greater than 31 I restart the program and I will type 50, what's going to happen ? Well we enter the case of else, it does not meet the if condition so it shows us directly that the number is too large. Last thing before concluding, you can also put the for directly in the if to avoid executing the loop for the cases that the program does not handle. Why I didn't do it here, to avoid confusing you too much, we mixed ifs and fors etc. We're taking it easy so that you understand as many things as possible. But what you need to remember is that here, as a developer, we have to worry about the operations we ask the compiler here we make it work more when in reality we can go directly to else you see? and who says that we make it work more, either requires more memory energy etc. and our objective is to create the most optimized programs and to ask the computer for the fewest possible operations. Anyway, that's all for this video, I hope you succeeded in the exercise, otherwise it doesn't matter, don't hesitate to do it again I hope you liked this video and that everything went well clear to you! see you next time in a new video either requires more memory energy etc. and our objective is to create the most optimized programs and to ask the computer for as few operations as possible. Anyway, that's all for this video, I hope you succeeded in the exercise, otherwise it doesn't matter, don't hesitate to do it again I hope you liked this video and that everything went well clear to you! see you next time in a new video either requires more memory energy etc. and our objective is to create the most optimized programs and to ask the computer for as few operations as possible. Anyway, that's all for this video, I hope you succeeded in the exercise, otherwise it doesn't matter, don't hesitate to do it again I hope you liked this video and that everything went well clear to you! see you next time in a new video I hope you succeeded in the exercise otherwise it doesn't matter don't hesitate to do it again I hope you liked this video and that everything was clear for you! see you next time in a new video I hope you succeeded in the exercise otherwise it doesn't matter don't hesitate to do it again I hope you liked this video and that everything was clear for you! see you next time in a new video