We can swap the number without a third variable with the help of pointer with different approaches.

Description
x=10,  y=20
x=x+y    //Now see here x value updated now with 30
y=x-y     //Now y value is 30-20 is 10
x=x-y     //Now x value is 30-10 is 20

Program

#include<stdio.h>
#include<conio.h>
void swap ( int *x,int *y ) {
*x = (* x) + ( * y ) ;
*y = (* x) - ( * y ) ;
*x = (* x) - ( * y) ;
}
void main ( ) {
int  x , y ;
clrscr ( ) ;
printf ( “Enter two Number!” ) ;
scanf ( “%d% d ”, &x , &y ) ;
swap ( &x , &y ) ;
printf ( ‘The value of x after swap is %d  n”,x ) ;
printf ( ‘The value of y after swap is %d  n”,y ) ;
}

Output:
Enter two Number! 10 20
The value of x after swap is 20
The value of y after swap is 10

This article is written by our awesome writer
Comments to: Swap the number without third variable using pointer in C

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password