codescracker


c

C Program to Swap Two Numbers



« Previous Program Next Program »


Swap Two Numbers in C

To swap two numbers in C programming, you have to first ask to the user to enter the two number, make a temp variable of same type. Now place first number in the temp, then place second number in the first, and then place temp in the second.

C Programming Code to Swap Two Numbers

Following C program ask to the user to enter the two number to swap them and display the output on the screen :

/* C Program - Swap Two Numbers */
		
#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	int num1, num2, swap;
	printf("Enter two number : ");
	printf("\nFirst Number : ");
	scanf("%d",&num1);
	printf("Second Number : ");
	scanf("%d",&num2);
	swap=num1;
	num1=num2;
	num2=swap;
	printf("The value of first and second number after swapping is \n");
	printf("First Number = %d\nSecond Number = %d",num1, num2);
	getch();
}

When the above c program is compile and executed, it will produce the following result:

c program to swap two numbers

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test