20_10

Write a C program to swap two integers using pointers.

 Source code:

/*Write a C program to swap two integers using pointers. You have to write a swap function that will accept the address of two integer and swap their values.  */


#include <stdio.h>

void swap(int *a,int *b)

{

    int t;

     t   = *a;

    *a   = *b;

    *b   =  t;

}

int main()

{

    int num1,num2;

    printf("Enter 1st number: ");

    scanf("%d",&num1); /*Enter value of num1 */

    printf("Enter 2nd number: ");

    scanf("%d",&num2); /*Enter value of num2 */

     

    swap(&num1,&num2);

     

    /*print values after swapping */

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

    return 0;

}

Output:

C program to swap two integers using pointers


No comments:

Post a Comment

Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...