Practical - 7 - Write a program to swap the value of two variables without using third variable.

Practical - 7

Aim: Write a program to swap the value of two variables without using third variable.

Program:

#include <stdio.h>

#include <conio.h>

 

void main()

{

    clrscr();

    int a, b;

 

    printf("Enter the value of 'A' = ");

    scanf("%d", &a);

 

    printf("Enter the value of 'B' = ");

    scanf("%d", &b);

 

    a = a + b;

    b = a - b;

    a = a - b;

    printf("\nA = %d", a);

    printf("\nB = %d", b);

 

    getch();

}

 

 

Output:

 

Enter the value of 'A' = 4

Enter the value of 'B' = 5

 

A = 5

B = 4

 

Algorithm:

Step-1: START

Step-2: Declare a and b.

Step-3: Enter the value of a and b.

Step-4: a = a + b.

Step-5: b = a – b.

Step-6: a = a – b.

Step-7: Display a and b.

Step-8: STOP.

 

Flowchart:

 



Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.