Write a C C++ program to swap without using third variable

Write a C C++ program to swap without using third variable

C Language में उपलब्ध Operators और इनके विभिन्न प्रकारो को जानने के लिए देखें—Operators in C

C-Language में Operator Precedence और Associativity Rule क्या होता है जानने के लिए देखें— Precedence and Associativity in C

Flowchart to swap without using third variable

Swapping without using third variable flowchart

C program to swap without using third variable

#include<stdio.h>
int main()
{
    int a,b;
    printf("Enter a b: ");
    scanf("%d%d",&a,&b);
    a = a+b;
    b = a-b;
    a = a-b;
    printf("a = %d b = %d",a,b);
    return 0;
}

C++ program to swap without using third variable

#include<iostream>
using namespace std;
int main()
{
    int a,b;
    cout<<"Enter a b: ";
    cin>>a>>b;
    a = a+b;
    b = a-b;
    a = a-b;
    cout<<"a = "<<a<<" b = "<<b;
    return 0;
}

Output of C C++ program to swap without using third variable

Enter a b : 10 20
a = 20 b = 10
Enter a b : 18 77
a = 77 b = 18

More C and C++ programs

  1. Sum of two entered numbers
  2. Average of three entered numbers
  3. Simple interest
  4. Total and Percent
  5. Swapping values of two variables
  6. Celsius to Fahrenheit conversion
  7. Find area and circumference of circle
Share it to:

Published by

admin

I am a computer teacher, programmer and web developer