C program for swapping two numbers using function

If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>
void swap(int *n1,int *n2);
main()
{
 int num1,num2;
 printf("Enter first number\n");
 scanf("%d",&num1);
 printf("Enter second number\n");
 scanf("%d",&num2);
 printf("First number is : %d \nSecond number is : %d\n\n",num1,num2);
 swap(&num1,&num2);
 printf("After Swapping\n\n First number = %d\n Second number = %d",num1,num2);
}
void swap(int *n1,int *n2)
{
 int temp;
 temp = *n1;
 *n1 = *n2;
 *n2 = temp;
}

OUTPUT:

Enter first number
2
Enter second number
8
First number is : 2
Second number is : 8

After Swapping

First number = 8
Second number = 2

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.