Loading...

Swapping of two numbers C Program


C Program of Swapping of two numbers:

#include <stdio.h>
main()
{
   int x, y, temp;

   printf("Enter two values: ");
   scanf("%d%d", &x, &y);

   printf("Before Swapping\nx = %d\ny = %d\n",x,y);

   temp = x;
   x    = y;
   y    = temp;

   printf("After Swapping\nx = %d\ny = %d\n",x,y);
}

Also by without using third varibale:

#include <stdio.h>
main()
{
    int a, b;

    printf("Enter two values: ");
    scanf("%d%d", &a, &b);

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

    a = a + b;
    b = a - b;
    a = a - b;

    printf("After Swapping\nx = %d\ny = %d\n",a,b);
}

Also by Making Function but Calling by Values of it:

#include <stdio.h>
void swap(int,int);
main()
{
   int x, y;

   printf("Enter two values: ");
   scanf("%d%d", &x, &y);

   swap(x,y);
}
void swap(int x,int y)
{
    int temp;

   printf("Before Swapping\nx = %d\ny = %d\n",x,y);

   temp = x;
   x    = y;
   y    = temp;

   printf("After Swapping\nx = %d\ny = %d\n",x,y);
}

Also by using Function but Calling by Reference:

#include <stdio.h>
void swap(int*, int*);
main()
{
   int x, y;

   printf("Enter two values: ");
   scanf("%d%d",&x,&y);

   printf("Before Swapping\nx = %d\ny = %d\n", x, y);

   swap(&x, &y);

   printf("After Swapping\nx = %d\ny = %d\n", x, y);
}
void swap(int *a, int *b)
{
   int temp;

   temp = *b;
   *b   = *a;
   *a   = temp;
}

Rate this posting:
{[['']]}

0 comments:

Post a Comment

 
TOP