ccplusplus.com
Learn C, C++ Concepts
Thursday, September 15, 2011
swap two numbers in c
/****************************************************** * File : swap-two-numbers.c * Author : Saurabh Gupta * Desc : swap two numbers usign a temp variable * Source : http://saurabhgupta0527.blogspot.com/ * Created : AM 09:45 15 September 2011 * Note : *****************************************************/ #include
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 with a temporary variable */ nTempNum = nFirstNumber; nFirstNumber = nSecondNumber; nSecondNumber = nTempNum; 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.c -o swap-two-numbers [sgupta@rhel54x64 c]$ ./swap-two-numbers 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]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment