Saturday, February 24, 2024

12. Write a program that swaps two numbers using pointers.

 #include <stdio.h>


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

    int temp;

    temp = *a;

    *a = *b;

    *b = temp;

}


int main() {

    int num1, num2;

    int *ptr1, *ptr2;

    printf("Enter two numbers:\n");

    scanf("%d%d", &num1, &num2);

    ptr1 = &num1;

    ptr2 = &num2;

    printf("before swapping:\n");

    printf("Number 1 = %d\n", num1);

    printf("Number 2 = %d\n", num2);

    swap(ptr1, ptr2);

    printf("After swapping:\n");

    printf("Number 1 = %d\n", num1);

    printf("Number 2 = %d\n", num2);


    return 0;

}

No comments:

Post a Comment