Answers for "swap 2 numbers without using 3rd variable and using pointer"

2

swap two numbers using pointers

#include<stdio.h>

#include<conio.h>

void swap(int *a,int *b);

void main()

{

int a = 20;

int b = 30;

printf("Before swapped: a = %d and b = %dn",a,b);

swap(&a,&b);

printf("Swapped result: a = %d and b = %dn",a,b);

getch();

}

void swap(int *a, int *b)

{

int temp;

temp = *a;

*a = *b;

*b = temp;

}
Posted by: Guest on March-18-2021
4

swap 2 numbers without using 3rd variable

a=10;
b=20;
a=a+b;//a=30 (10+20)    
b=a-b;//b=10 (30-20)    
a=a-b;//a=20 (30-10)
Posted by: Guest on May-16-2021

Code answers related to "swap 2 numbers without using 3rd variable and using pointer"

Browse Popular Code Answers by Language