Friday, March 17, 2023

C Programming. Program to swap two numbers using pointers.

 Program to swap two numbers using pointers.

// Program to swap two numbers using pointers.

#include<stdio.h>

int main() {
    int a, b, *p1, *p2, t;
    p1 = &a;
    p2 = &b;
    printf("Enter two numbers : ");
    scanf("%d%d",&a, &b);
    printf("Before swapping a = %d, b = %d", a, b);
    t=*p1;
    *p1=*p2;
    *p2=t;
    printf("\nAfter swapping a = %d, b = %d", a, b);
    return 0;
}

Output :

Enter two numbers : 5 7 Before swapping a = 5, b = 7 After swapping a = 7, b = 5

No comments:

Post a Comment

Selection Sort Using C programming.

Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the un...