Course Content

Course Content

Swap 2 Numbers

Hello everyone my name is (name) and we are continuing this C programming course (7 seconds pause, music) in which we have come into our practical module and here we will consider different problem statements and we will see how to implement functions that we have learned the C programming concepts till now, whether it is pointer, array, if and else statement, for loop, functions or any other concepts, so how to implement these if we have given any problem statement where there is a question or a problem for which we have to derive a solution by creating a program.

In that situation how will we handle it? How will we use our concepts and write a program in C language which would be solving that problem statement.

In this particular module, we will see all those things: how we can approach a problem statement, how the logic is developed and how the logic is converted concept wise and a program is written and the execution over that program and all those things will be seen.

In this video we would be seeing our second practical which is called swapping the two numbers.

This problem statement may not be understood while listening to its name.

What does swapping the two numbers exactly mean? So, we will quickly go and see what exactly is swapping of two numbers.

If I tell you concept wise or theoretically.

What exactly do we want to do in this problem statement, what do we do normally if we want to perform any operations or calculations with two numbers? What we do is we make two separate variables for them and after that we add those variables and whatever operations we want to perform on them, we get them done.

But, here the problem statement, what is that? Problem statement is exactly that if you have made two variables to store two different numbers, the problem statement is that the numbers that you have stored in those two variables, you have to actually swap the values inside the variable with each other, which means if suppose you have store 5 in A and 10 in b.

When you will swap both the values, the values inside both the variables then in your A, 10 will be stored and in B, 5 will be stored.

So, in this way we have to swap our values within our variables.

So how will that be done? How it should be approached, all these things we will be seeing over here.

2:54

So, here we have come inside our text editor, which means inside our Visual Studio Code.

And now we will be making such a program over here where we will have to create two variables according to the problem statement.

In those variables, we will have to store some values, and we'll have to swap those values, which means in the first variable, second variable’s value and in second variable first variable’s value.

So, in this way, we will have to do the swapping and after that what we will do is we will also print those values again.

From which we will get the satisfaction that we have actually swapped the values inside the two variables.

So, here if we start the program, while starting, the first thing that we have to write while starting is the pre-processor command because initially if we need to do perform any small things in our program like input, output or we have to use any file or we have to handle the strings, in those situations, what we do is we make use of preprocessor commands, and we import few files.

So, if we pay attention here, we have to make one program of swapping the two numbers, what all things will be required in this, we will have to make the variables in it.

After making the variables we'll have to store the values, after storing the values we'll have to swap them.

How and In which way we will swap them, we will see that over here and we will also do brainstorming regarding it.

They have swapped it and after swapping we will print it on our terminal, on our screen with which it will be clear that we have swapped the values.

So here, for now, with the outline, we have understood that the minimum things that we'll have to perfom is the input and output.

At least the values that are coming inside the variable, we will have to take an input of that and after swapping the values we will have to give the output.

4:55

So, here to perform the input and output operations, we all know that we use STDIO (spell) dot h file and for that we will have to write a preprocessor command which starts with #include (hash include).

So, we will write it over here.

So, you can see that we have written (#) hash include command over here, which means we have written our preprocessor command which is the STDIO (spell) dot h file, which provides standard input and output functionalities in our program, in our C program, that we have imported over here.

After importing this, if we think, we remember that if we were making functions, instead of writing the main function, we would have to write function definition or if we are making something like structure or union, in that situation before the main function, we have to write all those things.

But if you see this question, this problem statement, we know that we don't have to make it very complex by creating a function.

We will do it easily in a normal simple way.

We will not be making any function over here.

We will not write any function definition.

That's why just after the preprocessor command, we will be making our main function and we all know how we write our main function.

We write it in this way.

Here we write int and main.

So here we have made our main function as well.

So, here in these round brackets, obviously, we are not putting any values as a parameter, why? Because we will not be passing any arguments over here from our command line, which means in the terminal we're not passing any extra arguments, why? Because we don't need it over here, we have one simple aim or intention.

Which is the values inside our variables, we have to exchange them, we have to swap them.

So, this was about a problem statement.

That's why we will not do any such thing here, which will make the program complex.

Through this, when we will go ahead, definitely we will include more concepts and we'll be making bigger programs.

But for now, for this particular problem statement we aren't at least required to give arguments from the command line.

So, we will keep the main function parameters empty.

After that, we will see whenever we write a function or any other thing like if else, etc.

so at that time, in that situation, we make one body which means such a part in which we can write more statements.

7:27

Now, that part is defined by what? That is defined with the curly brackets.

So, when we make the curly brackets, it basically makes its scope or part of availability.

So, here if we talk about the main function, which is the start and end of the main factions, from where to where it is happening.

So, that will be told to us by our curly brackets.

So, here we have written the curly brackets, after writing the curly brackets, what will be the the first situation that will occur? we will have to define our variables.

If we want to store the values so before storing them, we will require live variables.

So, we will have to make the variables.

And to make the variables, we not only have to make two such variables - a and b, but this time we will actually require a third variable as well.

Now, where will this third one be used? As when the programme will go ahead, we'll be seeing that, but for now, we will know here that if we consider two variables and we wish to exchange the values, we will not require two but actually three variables!

Because if we use only two variables, then the value of one variable out of the two will be missed by us. It will be missed out, to counter that situation we will be using three variables.

Now there is no need to worry about when this third one will be used, as and when we will go ahead with the programme we will see that. But for now, We will go here and simply make the three variables.

9:01

Now we will see here that in which data type we have made this variable, we have made it in Double data type basically and double key size we all know that is of eight bytes which means eight multiplied by eight bits.

That much size - eight multiplied by eight is 64. 64 bits is a size of this particular data type.

So this was about what our data type is. It is double, after that if we go inside we can see that we have made three variables.

First variable is first, second variable is second and third variable we have named as TEMP (spell)which means that if we give it a full form, we will call it a temporary variable and why is it temporary and why we have named it TEMP ,so we have given it for our understanding and also because we will be using it temporarily and the value inside will be stored temporarily.

10:05

So, according to that, we have named TEMP and created it over here.

Now, where exactly will this TEMP be used? We will go ahead and see in our programme for now, we have made our variables ready, we have defined them, now the time comes to initialise our variables, what is the meaning of initialise? storing the values in the variable.

Here we want that we should not store the values in our programme, when we will be running a programme, so at that runtime, we will input the values from our user which we will be able to swap later.

So, we are wanting to create this kind of scenario here because there is no point in swapping the numbers chosen by us.

So, what we will do is, we will take two numbers from users and then we will swap them over here.

So, what will we need to take the numbers from the users? We will need a scan-f function because we know that we use scan-f to take input. But if I ask, we can take it from scanf but how will the user know that we have to input some value.

So for that, we will also require one print-f statement, through which we will ask him to enter the value that he wants to take in variable, all things we will be doing over here, So quickly we will write that part also.

So, you can see that 1st we have written one printf statement, in which we have told our user that you have to put your first number, you have to type it, enter it through your terminal.

So, this is the work of printf.

Printf statement has given the output as a message, it has printed it on the screen.

Now, after that we have written the scanf function, if you see in the scanf function, what we have done here is we have put our format specifier and after putting our format specifier what we have done is “ and first”, ‘and’ means address of our first variable, we are referring it over here because we know that scanf will go on that particular address and store the value at that particular address that's why we are making use of ampersand over here.

12:28

So, we are creating Double data type values over here, that’s why we have also taken the format specifier also, specified with that only.

Which means we have used our ‘percent Lf’ format specifier over here.

So, this was about how we have to take the first number as input, we have printed first few messages, after reading that message, the user will understand that they have to add the number over here, enter a number and the user entered the number and after that in the same way we will also take the second number.

We will be taking the second number in the same way, “enter the second number”, the user will understand that they have to put the second number now and with the help of scanf, we will store the second number in the second variable.

Now here the topic is over, we have made our variables, we have also initialised them.

Now the time comes of the main logic, where we will be swapping our values.

So how will swapping be done?

13:30

If you see here.

We have made two variables before, our first variable and the second variable.

Inside the first variable we have taken one value from the user and in the second variable also we have taken one value from the user.

Now the topic comes to where the third variable is used.

So, if you see what we have to do is, the value in the first variable we have to store inside the second variable.

So, we can say that what we have done is, in the first, directly initialised the second’s value, it was assigned.

So, what happened due to this was, the value in our first variable got overwritten, which means new value has come over it.

Now the old value which was there, got missed out by our first variable.

So, the old value which got missed, how will we assign that to our second variable again, we have missed it, now we cannot assign it.

So, to counter this situation.

We have taken a third variable here and what we will do in the third variable, put the value of the first variable in the temporary variable. Like in this case we have made a variable named TEMP.

So, we will first store the first variable’s value in a TEMP named variable.

After that we will assign second variable’s value inside the first variable.

And the TEMP variable that we have made, in which we had stored the old value of first.

Now from TEMP we will initialise that value in the second variable, we will assign it and, in this way, we will be helped to exchange both the values of the variable.

So, let's see how we have written the program.

So, the first step that we have done is the TEMP variable in that we have assigned first variable’s value, we have stored it in it.

15:32

You can see that the TEMP is equal to first, which means whatever is the value in first, you will have to assign it in TEMP.

And the way we are assigning a number like we used to write A is equal to 3 and here we used to understand that 3 has got assigned in A variable.

In the same way, if first we have written on the right hand side, so first is telling us that inside the first variable, whichever value is there, that will get assigned to our TEMP variable and in this way, if we will go ahead, the second variable which is there, the value inside our second variable, will go and get assigned to our first variable.

If you see over here, our first variable which was there, the value in it we have put into TEMP.

16:22

Even if our value gets missed from the first variable, even if it disappears.

We don't have to worry because that value is already there in the TEMP variable.

So here, when we stored the second variable’s value in the first one, We didn't face any problem because our first value was missing.

But since we have stored it in some other variable, we didn't face any problem over here even if it was missed.

So, what we did here is, we have stored the first variable’s value in TEMP and we have stored the second variable's value in first.

Now if we go in the third step, you can see in the third step that the temp value which was remaining which means the first variable's old value which was left, we have stored it in the second variable.

So, how did the process take place here? we have put First value in temp and we have put the second value in first.

And again, those values that were stored in temp, we have put it in second, in this way none of our value was not missed.

Just with the help of an extra variable what we did is we exchanged the variable's value.

17:40

So, in the first variable, the second variable's value is stored and in the second variable, our first variable's value is stored.

So, this is complete over here, we have made a logic and we have used our idea of how we have to swap and we were successful to swap.

Now the time comes to print it.

How will we tell on the screen that we have exchanged the values over here, we have swapped the values.

So, in that situation we will make use of the print-f statement.

If we see over here, we have printed two printf statements.

What is the first printf statement saying, it is just saying that “after swapping first number =” which means now after that we have swapped the value, if we see what is stored there in the first number, if we want to see that over here, What we have done here is, we have printed first variable in first printf statement and we have also given the format specifier over here, in the same way after swapping, the second number, which value has got stored inside the second variable, to see that, we have used the second printf statement also.

Now that we have done our calculations, we have completed the swapping, we have also printed them. Now the time comes to close the main function, because we had started the main functions with our curly brackets.

Now we will also have to close it because till the time we don't close it How will our compiler know that the program has been completed? So, for that situation, what we will do is we will put return zero, with return zero, the compiler gets to know that here our program has ended.

The main function has ended over here and after return zero, we have put a curly bracket and after that our program has completed over here.

19:40

So, this entire process which was there, how we will swap the values inside two variables, that was made here and we saw how it worked here.

So, how it is working here, to see that we will go here inside our terminal and after going in our terminal.

We will compile it, so to compile we will write GCC and then we will write the file’s name.

We will put the dot C extension and we will compile it and as soon as we compile it, we will write any of the characters and we will enter, our program will run over here.

Here it is saying that “the term A is not recognised”.

So, what we will do here is we will compile it once again.

So, here once again, okay.

20:35

So, now we will come inside our terminal.

And after coming into the terminal we will make one command prompt window.

After making the command prompt window.

We have to compile our program first.

So, to compile it we will write this command “gcc swap numbers dot c”, swap numbers is my file’s name.

If your file's name is something else, you can write the name accordingly.

As we are seeing the program here, line by line, you can also follow it in your computer, on your system, because of which you will understand the program and you will be able to recall it easily.

So, here we have compiled it and after compilation we are not seeing any errors for now.

Now we will run it and as soon as we run it, it is asking me my first number because we will be requiring the first number then second number, only then will we be able to swap them both.

So, for swapping, let’s suppose we will first put 45 as the first number and second number we will put 89.

So, these are two different numbers so that we get to know if we have really swapped or not.

As soon as we press it, you can see over here that we have printed our 2 printf statements.

So, you can see that after swapping “first number=89.00”, in the same way, after swapping “second number = 45.00”.

So, somewhat in this way, we saw that the value that we have put in the second number has come in the first number and the value that we had put in the first number which is 45 has come in the second number.

So, this was about how we can exchange two numbers, two values in two variables, we can swap it, so we have got a simple solution of this problem statement and we executed it and we saw how it works over here.

So, this was a very simple program where we use three normal variables for swapping.

we interchange the three values among themselves so that the first and second values of both the variables respectively are not missed.

And that's why we have used three variables and after swapping, after exchanging the values, we have got it printed over here.

After printing them here as soon as we run it.

Our programme ran smoothly.

We were not getting any errors to see over here.

So, whenever you want to compile or run your program, you will have to run it in the command prompt terminal, do not run it in PowerShell terminal.

So, this was about our swaps number program, how we have swapped the numbers over here.

 If you think that there is any part of this video or this topic, you might have not understood then you can ask us any query or question related to that topic without any inhibition.

If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere