C Program to swap two number using pointer [functions, bit-wise XOR ]

C Program to swap two number using pointer [functions, bit-wise XOR ]

This is the example of c-programming. You can edit and compile code in your own way on clicking button. If you have better solution or you found any error on code, then give us suggestion.

Full Code:


Code Explanation :


This is the example of the C program to swap two numbers given by the user.

swap means the interchange of the value between two variables. for example,

Initially, there are two variables number1 and number2 and values on these variables are 10 and 20 respectively.

After swapping, the value of number1 will be 20 and the value of number2 will be 10.

I hope, you know what the swap is.

Let's go through the example.

In this example, we use pointer to swap the value of two variables. We have created two variables of integer type and we take inputs from user.

After that we have called function that we have created (user-defined function) as swap().

Remeber that, we have not passed the variables value in the function. We have passed the variable location using &.

Now, We have passed the variables location in the function but we use the location value using pointer variables *a. We easily swap two variables using c variables and print them.

Goint through the algorith and flowchart, I will recommend not to use the function. Just simply go through the simple synatx as if...else, while and many more.

We can do this program with different method.

C program to swap two number using simple code

#include <stdio.h>
int main()
{
  int x, y, t;

  printf("Enter two integersn");
  scanf("%d%d", &x, &y);

  printf("Before SwappingnFirst integer = %dnSecond integer = %dn", x, y);

  t = x;
  x = y;
  y = t;

  printf("After SwappingnFirst integer = %dnSecond integer = %dn", x, y);

  return 0;
}

C program to swap two number without using extra variable

#include <stdio.h>
int main()
{
   int x, y;
   
   printf("Input two integers (x & y) to swapn");
   scanf("%d%d", &x, &y);
   
   printf("Before SwappingnFirst integer = %dnSecond integer = %dn", x, y);
   
   x = x + y;
   y = x - y;
   x = x - y;

   printf("After SwappingnFirst integer = %dnSecond integer = %dn", x, y);
   return 0;
}

C program to swap two number using pointer without function

#include <stdio.h>
int main()
{
   int x, y, *a, *b, c;

   printf("Enter the value of x and yn");
   scanf("%d%d", &x, &y);

   printf("Before Swappingnx = %dny = %dn", x, y);
   
   a = &x;
   b = &y;
   
   c = *b;
   *b   = *a;
   *a   = c;

   printf("After SwappingnFirst integer = %dnSecond integer = %dn", x, y);
   
   return 0;
}e

C program to swap two number using bit-wise XOR

#include <stdio.h>
int main()
{
  int x, y;

  printf("Enter the value of x and yn");
  scanf("%d%d", &x, &y);

  printf("Before SwappingnFirst integer = %dnSecond integer = %dn", x, y);

  x = x ^ y;
  y = x ^ y;
  x = x ^ y;

  printf("After SwappingnFirst integer = %dnSecond integer = %dn", x, y);

  return 0;
}

Although there used different method but the final output of program is same as given above. So, that we are representing common algorithm and flowchart.

Algorithm

Step 1 : Start 

Step 2 : Declare x, y, x

Step 3 : Read x and y

Step 4 : Print "Before swapping, x and y"

Step 5 : Set value of x on z i.e z = x;

Step 6 : Set value of y on y i.e  x = y;

Step 7 : Set value of z on y i.e y = z;

Step 8 : Print "After swapping, x and y"

Step 9 : end

Flowchart

Program to swap two number using pointer, function,

I hope, you understand the code. If you got any issue or difficulty in understandding the example then your can comment below. We will reply you with details explanation.

Thank you!



Our example mainly covers following topics in any programing

  • Basic Programs
  • Number Programs
  • Array Programs
  • Matrix Programs
  • Pattern Programs
  • String Programs
  • Tree Programs
  • Singly Linked List Programs
  • Circular Linked List Programs
  • Doubly Linked List Programs

Here we share programs on various topics such as array strings, series area & volume of geometric figures, mathematical calculation algorithms for sorting & searching and much more.Our goal is to provide you with the perfect solution to all the programming problems you may have encountered either during interviews or in class assignments. If you don’t find what you are looking for then please drop a line in the comment section of example page or you can request from account page so that we can get it added to the below collection of programs. Happy Learning!!


If you are confident with the above programs and are able to successfully understand and run them without any problems then it is time for you to take a step further and learn comprehensive programming principles using examples and flow diagrams. Here is the link: Programming tutorial lists .


If you find this example helpful, don't forget to share it with your friends. And stay updated with us by subscribing adzetech.


If you find any code error or grammetical error then you can suggest for example improvement on clicking below button.

Leave comment

Comments (1)

// SEARCH

What are you searching for....?

// SUBSCRIBE

Subscribe us on youtube to get our videos updates firstly on youtube.

// SUBSCRIBE

To receive updates on the latest news, tutorials and special offers, subscribe to our email newsletter today!

// CATEGORIES

x
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Privacy Policy That's Fine