149k views
1 vote
Write a complete C program that obtains two integers from the user, saves them in the memory, and calls the function void swap (int *a, int *b) to swap the content of the two integers. The main function of your program should display the two integers before and after swapping.

User Tran Quan
by
5.1k points

1 Answer

2 votes

C program for swapping numbers

#include <stdio.h>

void swap(int *num1, int *num2)/*Defining function for swapping the numbers*/

{

int temp; /*using third variable to store data of numbers*/

temp = *num1;

*num1 = *num2;

*num2 = temp;

}

int main ()//driver function

{

int a,b;

printf("Enter the numbers for swapping\\");//taking input

scanf("%d %d",&a,&b);

printf("The numbers before swapping is a =%d and b=%d \\",a,b);

swap(&a, &b);//calling function for swaping

printf("The numbers after swapping is a=%d and b=%d",a,b);

return 0;

}

Output

Enter the numbers for swapping 3,4

The numbers before swapping is a =3 and b=4

The numbers after swapping is a=4 and b=3

User Tedsuo
by
5.5k points