This is a C Program to swap two integers without using temporary variables and bitwise operations.
This C Program swap two integers without using temporary variables and bitwise operations.
Take input from the user and swap two integers without using bitwise operations as shown in the program below.
Here is source code of the C Program to swap two integers without using temporary variables and bitwise operations. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* * C Program to Swap two Integers without using Temporary Variables * and Bitwise Operations */ #include <stdio.h> // Function Prototype void swap(int *, int *); void main() { int x, y; printf("Enter 2 nos: \n"); scanf("%d %d", &x, &y); printf("\nYou have entered x = %d y = %d \n", x, y); swap(&x,&y); // passing the 2 nos to the swap function } // function to swap the two numbers void swap(int *a, int *b) { *a = *a + *b; *b = *a - *b; *a = *a - *b; printf("Swapped . . . .\n"); // printing the swapped numbers printf("x = %d y = %d\n", *a, *b); }
In this C Program, we are reading the integer value using ‘x’ and ‘y’ variables respectively. The swap function is used to swap the two numbers.
Compute the summation of ‘a’ and ‘b’ variable values and the difference of ‘a’ and ‘b’ variable values. Assign the swapped numbers to the ‘a’ and ‘b’ variables. Print the swap of two integers using printf statement.
$ gcc bit28.c $ a.out Enter 2 nos: 4 7 You have entered x=4 y=7 Swapped . . . . x=7 y=4
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Practice BCA MCQs
- Check Computer Science Books
- Practice Computer Science MCQs
- Apply for C Internship
- Apply for Computer Science Internship