Programming methodology & data Structure

Class :B.Sc (Computer Science ) I year 

Topic : Swapping Program 

Faculty : Asst Prof Sonika Mourya
 C program to swap two numbers, including using a temporary variable

 

#include <stdio.h>

#include<conio.h>

void main()

 {
                int a, b, c;

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

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

                  c= a; 
                a = b;    
                b = c;  

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

                getch();
}

 

C program to swap two numbers, without using third variable

 

#include<stdio.h>

#include<conio.h>

 

Void main()

{

int x, y;

printf("Input value for x & y: \n");

scanf("%d%d",&x,&y);

printf("Before swapping the value of x & y: %d %d",x,y);

x=x+y;

y=x-y;

x=x-y;

printf("\nAfter swapping the value of x & y: %d %d",x,y);

getch();

}

 

0 comments:

Post a Comment