Answers for "swap pointers"

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
1

swap using pointers c++

#include <stdio.h>

void swap(int *x,int *y){
	int t=*x;
	*x=*y;
	*y=t;
}

int main()
{
	int a,b;
	scanf("%d %d",&a,&b);
    printf("Before Swap : %d %d",a,b);
	swap(&a,&b);
    
	printf("After Swap : %d %d",a,b);
}
Posted by: Guest on July-12-2021

Browse Popular Code Answers by Language