0% found this document useful (0 votes)
51 views

Swapping of Two Numbers in C: Without Using Third Variable Pointers Functions (Call by Reference) XOR

This document discusses different ways to swap two numbers in C programming without and with using a third variable. It provides code examples to swap numbers using a temporary variable, using addition and subtraction to swap the values directly without a third variable, and a program to reverse a given number by iterating through its digits.

Uploaded by

jance08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Swapping of Two Numbers in C: Without Using Third Variable Pointers Functions (Call by Reference) XOR

This document discusses different ways to swap two numbers in C programming without and with using a third variable. It provides code examples to swap numbers using a temporary variable, using addition and subtraction to swap the values directly without a third variable, and a program to reverse a given number by iterating through its digits.

Uploaded by

jance08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

C program to swap two numbers with and without using third variable, swapping in c using

pointers, functions (Call by reference) and using bitwise XOR operator, swapping means
interchanging. For example if in your c program you have taken two variable a and b where a = 4
and b = 5, then before swapping a = 4, b = 5 after swapping a = 5, b = 4
In our c program to swap numbers we will use a temp variable to swap two numbers.

Swapping of two numbers in c


#include <stdio.h>
int main()
{
int x, y, temp;
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n",x,y);
temp = x;
x
= y;
y
= temp;
printf("After Swapping\nx = %d\ny = %d\n",x,y);
return 0;
}
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two integers to swap\n");
scanf("%d%d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("a = %d\nb = %d\n",a,b);
return 0;
}

C program to reverse a number


#include <stdio.h>
int main()

int n, reverse = 0;
printf("Enter a number to reverse\n");
scanf("%d",&n);
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
printf("Reverse of entered number is = %d\n", reverse);
return 0;

You might also like