Blog Moved

This website completely moved to new domain. For latest content, visit www.programmingposts.com

Search This Blog

2 Aug 2012

C Program to Swap Two Numbers using Temp/Third Variable


/* C Program to Swap Two Numbers using Temp/Third Variable */

#include <stdio.h>

main()
{
   int a, b, temp;

   printf("\n Enter the values of a and b: ");
   scanf("%d %d", &a, &b);  // Taking The values of x and y from user

   printf("\n Before Swapping: \n a = %d b = %d \n",a,b);

   temp = a; //temp stores value of a
   a = b;    //a stores b value
   b = temp; //b stores temp value

   printf("\n After Swapping: \n a = %d b = %d \n",a,b);

   return 0;
}

Output:  ( using GNU GCC Compiler with Code Blocks IDE )

2 comments:

  1. Have you checked this Write a program to Swap two variables or number.One can find almost all the methods of swapping of two numbers.

    ReplyDelete
  2. this link might be help you C program to swap two numbers

    http://programmergallery.com/c-program/c-program-swap-two-numbers.php

    ReplyDelete