ccplusplus.com
Learn C, C++ Concepts
Thursday, September 15, 2011
swap two numbers without third variable in c
/*********************************************************************** * File : swap-two-numbers-without-using-third-variable.c * Author : Saurabh Gupta * Desc : swap two numbers without using third variable * Source : http://saurabhgupta0527.blogspot.com/p/c.html * Created : AM 09:47 15 September 2011 * Note : ***********************************************************************/ #include <stdio.h> int main(int argc, char **argv) { printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); printf ("XXXXX swapping two numbers XXXXX\n"); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); int nFirstNumber, nSecondNumber, nTempNum; printf("enter the value of First and Second Number\n"); scanf("%d%d", &nFirstNumber, &nSecondNumber); printf ("\nBefore Swap values are\n"); printf ("First Number\t=\t%d\n", nFirstNumber); printf ("Second Number\t=\t%d\n", nSecondNumber); /* * here we swap two numbers without third variable */ nFirstNumber = nFirstNumber + nSecondNumber; nSecondNumber = nFirstNumber - nSecondNumber; nFirstNumber = nFirstNumber - nSecondNumber; printf ("\nAfter Swap values are\n"); printf ("First Number\t=\t%d\n", nFirstNumber); printf ("Second Number\t=\t%d\n\n", nSecondNumber); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); return 0; } /* * OUTPUT * [sgupta@rhel54x64 c]$ gcc swap-two-numbers-without-using-third-variable.c -o swap-two-numbers-without-using-third-variable swap-two-numbers-without-using-third-variable.c:58:4: warning: no newline at end of file [sgupta@rhel54x64 c]$ ./swap-two-numbers-without-using-third-variable XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXX swapping two numbers XXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX enter the value of First and Second Number 10 20 Before Swap values are First Number = 10 Second Number = 20 After Swap values are First Number = 20 Second Number = 10 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [sgupta@rhel54x64 c]$ */
See also
Other popular tricky C Sample Codes and language Concept
.
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment