Index « Previous Next » Show QuestionWrite a program that prompts the user to enter number in two variables and Swap the contents of the variables. Source Code#include <stdio.h> int main() { int num1, num2, temp; printf("Enter first number :"); scanf("%d", &num1); printf("Enter second number :"); scanf("%d", &num2); temp = num1; num1 = num2; num2 = temp; printf("After swapping, first is %d and second is %d.", num1, num2); return 0; }Output
Enter first number :10 Enter second number :15 After swapping, first is 15 and second is 10.
Write a C program that swaps two numbers without using third variable. Pictorial Presentation: Sample Solution: C Code: Sample Output: Flowchart: C programming Code Editor: Contribute your code and comments through Disqus. Previous: Write a C program that accepts a distance in centimeters and prints the corresponding value in inches.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
How to check substring exists in a string in C? if(strstr(sent, word) != NULL) { /* ... */ }Note: strstr returns a pointer to the start of the word in sent if the word word is found. Ref : https://bit.ly/2VOpo2K
Write a program in C to swap two numbers using the function. C programming: swapping two variables Swapping two variables refers to mutually exchanging the values of the variables. Generally, this is done with the data in memory. The simplest method to swap two variables is to use a third temporary variable : Pictorial Presentation: Sample Solution: C Code: Sample Output: Flowchart: C Programming Code Editor: Have another way to solve this solution? Contribute your code (and comments) through Disqus. Previous: Write a program in C to find the square of any number using the function.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
How to check substring exists in a string in C? if(strstr(sent, word) != NULL) { /* ... */ }Note: strstr returns a pointer to the start of the word in sent if the word word is found. Ref : https://bit.ly/2VOpo2K Swapping two number in C programming language means exchanging the values of two variables. Suppose you have two variable var1 & var2. Value of var1 is 20 & value of var2 is 40. So, after swapping the value of var1 will become 40 & value of var 2 will become 20. In this blog will understand how to swap two variables in C. We will look at each one of them one by one. Swapping Two Numbers Using Third VariableLogic
Code: temp = var1; var1 = var2; var2 = temp;Now, let’s look at the complete code. Example #include <stdio.h> int main() { int var1, var2, temp; printf("Enter two integersn"); scanf("%d%d", &var1, &var2); printf("Before SwappingnFirst variable = %dnSecond variable = %dn", var1, var2); temp = var1; var1 = var2; var2 = temp; printf("After SwappingnFirst variable = %dnSecond variable = %dn", var1, var2); return 0; }Output: Moving on with this article on Swapping of Two Numbers in C Swapping Two Numbers Using Without Using Third VariableIn this variation of swapping two variables, we are not using any temporary variable to store the value. In the first variable we are storing the sum of both variable. Then, in next step we are we are extracting the value of 1st variable by subtracting the value of 2nd variable form the sum & storing it in 2nd variable. At last, we are extracting the original value of the 2nd variable & storing it in the 1st variable. Code: var1 = var1 + var2; var2 = var1 - var2; var1 = var1 - var2;Now, let’s look at the complete code. Example #include <stdio.h> int main() { int var1, var2, temp; printf("Enter two integersn"); scanf("%d%d", &var1, &var2); printf("Before SwappingnFirst variable = %dnSecond variable = %dn", var1, var2); var1 = var1 + var2; var2 = var1 - var2; var1 = var1 - var2; printf("After SwappingnFirst variable = %dnSecond variable = %dn", var1, var2); return 0; }Output: Moving on with this article on Swapping of Two Numbers in C Swapping Function in CYou can create a swapping function in C implementing any of the swapping variation, wherein you can call the function anytime when you want to swap two variables. Since we want the local variables of main to modified by swap function, we must them using pointers. Example void swap(int *num1, int *num2) { int temp = *num1; *num1 = *num2; *num2 = temp; } int main() { int var1, var2; printf("Enter Value of var1 "); scanf("%d", &var1); printf("nEnter Value of var2 "); scanf("%d", &var2); swap(&var1, &var2); printf("nAfter Swapping: var1 = %d, var2 = %d", var1, var2); return 0; }Output: Moving to the next variation we have swapping two numbers using pointers. Swap two numbers using pointers in CYou can also swap two variables using pointers, wherein you pass the address of the variables to two different variables. Then, swap their values. Code num1 = &var1; num2 = &var2; temp = *num2; *num2 = *num1; *num1 = temp;Now, let’s look at the complete code. Example #include <stdio.h> int main() { int var1, var2, *num1, *num2, temp; printf("Enter the value of var1 and var2n"); scanf("%d%d", &var1, &var2); printf("Before Swappingnvar1 = %dnvar2 = %dn", var1, var2); num1 = &var1; num2 = &var2; temp = *num2; *num2 = *num1; *num1 = temp; printf("After Swappingnvar1 = %dnvar2 = %dn", var1, var2); return 0; }Output: Moving to the last variation of swapping variables in C. Let’s understand how to swap two variables using bitwise XOR operator. Swap Two Numbers Using Bitwise XORXOR operator works in the similar manner as swapping without using temporary variable works. We extract calculate the XOR of both the variables. Then we extract individual values and swap them. Suppose var1 = 20 and var2 = 40. The binary values of 20 = 10100 and 40 = 101000. Example: Var1 = Var1 ^ Var2 Var1 = 10100 ^ 101000 Var1 = 111100 Var2 = Var1 ^ Var2 Var2 = 111100 ^ 101000 Var2 = 10100 Var1 = Var1 ^ Var2; Var1 = 111100 ^ 10100 Var1 = 101000 Final Values after swapping 2 numbers: var1 = 40 and var2 = 20 var1 = var1 ^ var2; var2 = var1 ^ var2; var1 = var1 ^ var2; Now, let’s look at the complete code. #include <stdio.h> int main() { int var1, var2, temp; printf("Enter two integersn"); scanf("%d%d", &var1, &var2); printf("Before SwappingnFirst variable = %dnSecond variable = %dn", var1, var2); var1 = var1 ^ var2; var2 = var1 ^ var2; var1 = var1 ^ var2; printf("After SwappingnFirst variable = %dnSecond variable = %dn", var1, var2); return 0; }Output: Now after going through the above programs you would have understood all the variations of swapping two numbers in C. I hope this blog is informative and added value to you. Now after executing the above program you would have understood the Swapping of Two Numbers in C. Thus we have come to an end of this article on ‘Quicksort in Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring. Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible. |