How to Convert an Algorithm to C Program
Computer Science
Hello viewers. In this video, I'm going to explain how to convert an algorithm to a simple C program. In this video, I'm going to explain how we can convert an algorithm into a simple program. Using C language, so before conversion of algorithm to a program, let us discuss what is an algorithm. So algorithm is a step by step instruction which tells the computer what to do, or it is the instruction to perform any task using computer. Let us discuss one simple example of addition of two numbers. For adding two numbers, this is the way how we can write an algorithm. Step one start, step two, input a comma B so these are the variables, which are going to hold two values, step three, C is equal to a plus B so whatever values which are entered by the user are added and the addition is stored in another variable C step four, print C and step 5 stop. So when we convert this algorithm to a C language program, the step one is start in case of C language. We write a simple structure of C program where we start. The execution of C program in the beginning. So this is hash include STD, hash include corner dot H, void mean, opening bracket gets it, and closing back. So this is the basic structure of C program, which is required for any C program to run. So after step one, step two is input a comma B so here we are asking user to enter variable a and B so to enter the variable, they should be created in the program first. So we create these variables. In the computer, then we ask the user to enter the value of a and we ask the user to enter the value of B using scanf function. So print a function is guiding the user to enter which value. And scan a function is scanning the value for that variable. As a and B both are integers, we are using percent as a format specifier. After getting these two values, this should be added to the variable C so this next step is step three, C is equal to a plus B in this step, we are simply saying the statement C is equal to a plus B so the values which are entered by user in a and B are added it and it is stored in another integer type of variable C after addition, we can print this value on the screen using a four step print C, this can be converted into a C language code as printf slash and a plus B is equal to. By sending the format specified, which brings the value of a variable whichever you pass to the printf function. So this is the information string of printf function. It is going to print a plus B is equal to and the value of C whatever is there for a and B addition is going to be printed here. This is the way how a simple addition program can be converted. Algorithm can be converted to a C program. Now let us discuss one more example. This is the algorithm to find reverse of a given number. So step one is start. Then step two, we are asking user to enter any number for that we need a variable. We take the value of that variable from the user, then we take another variable reverse, which is initialized to zero. This is the variable which is going to contain the reverse of this number. Then, using for loop, we are doing some calculations on this number to find out its reverse. So this is the logic to find out the reverse. As we do not want to disturb the value of this number, we are copying this number into another variable that is numb. So now is a variable, which is actually running this for loop. So now with initialized to the variable NO value, then this loop is going to execute until the value of num is greater than zero. And after executing all the statements, every time we are going to reduce the number by ten by dividing it by ten. So in the four loop, R is equal to 9%, ten. So R is another variable which contains the reverse remainder of the number. When we divide it by ten, then that remainder is added to the existing value of variable reverse. So reverse in the beginning is initialized to zero. Then, in the every iteration of for loop, reverse existing value is multiplied by ten and the new value of R is R is added to it. So as this loop terminates, we get the reverse of a number in the variable reverse, and finally we print the divorce. So this is the simple algorithm to find reverse offer. Given number. Now let us discuss how we can convert the same algorithm into a C program. So step one start. So this is the basic structure of C program, hash include exterior door touch, hash include corner dot H, void mean, opening like it gets the agent closed. So start, then we are asking user to enter the number to get any number from the user, first of all, we need to create a variable of that type. So integer variable NO is created, then we are asking user to enter any number, then whatever number is used, entered by the user. We are scanning it using scanf function. So this is the scan of function to scan the value of this number. Which is entered by the user. After scanning it, reverses initialize to zero. So here we need one more variable. So another variable reverse is created, which is again off integer type. And its value is initialized to zero. Now the next fourth step is for loop. If we see this for loop, this loop is actually run by a variable num, so we need one more variable num here. And when more variable is created, R, which contains the remainder of the number num, when it is divided by ten. So two variables num and RR created. Now same this for loop is converted as such into its C language code. So for now is equal to NO. Now I'm greater than zero. Now miss it was two number ten, opening, closing bracket, R is equal to num percent ten, semicolon is always given after every statement in C language. And reverse is equal to diverse into ten plus R so this is the way how this fourth step is converted into its C language code. After finding a reverse of a number, we are going to print this reverse of a number for printing reverse of a number. We just use printf statement where we are going to print the value of reverse. So percent needs a format specifier for reverse because it is an integer type of variable. This is the simple way how we can convert and algorithm into its C program. Now, let us do this practically. Blind reverse of a number. Void mean. Gate CH to hold the screen. And closing bracket. So in the beginning, we created one variable NO. We need one more variable reverse. Another variable number was created. And one more R so these are the four variables. Which we need for this program, then we are initializing reverse with zero because it is going to contain the reverse of a given number. See, to clear the screen for the output, now let us accept value of N from the user. So slash M enter any number. Here to scan the value using scanner function. And now this number is present in variable NO, so we are providing the address of variable NO. After getting this number, we are going to find out reverse of this number using for loop. So far, now is a variable which we are going to utilize, so it is initialized with NO. We are going to run this loop till the value of num is greater than zero. And every time we are going to decrement num by dividing it by ten. Now R is equal to existing value of num. So our contains remainder of number num, then reverse is initialized with existing value of reverse variable. Multiplied by ten. And a new value of remainder R is added to it. After this forward loop, let us print the reverse of number. So we were of sandy is nothing but the number which we entered is equals two. So for first format recipe, we are going to print the value of NO that is the actual number which we entered. And then its reverse, which is present in variable reverse. Let us compile this program. Now if we run this, enter any number, suppose one, two, three, four, so reverse of one, two, three, four is four, three, two, one. So while writing any C program, take care that we are giving, you are giving semicolons properly. If you give the semicolon after for loop, then it is not going to execute. Even though you get no compilation error, still it does not execute. And that's where you get a wrong output because this for loop does not execute as your adding semicolon at the end. So make sure whenever you are writing any code in C language, you are following the syntax properly. So this is the way how we can convert any algorithm to a safe program. Thanks for watching this video.