Write a C C++ program to swap values of two variables

Write a C C++ program to swap values of two variables

C Language में Data type क्या होता है जानने के लिए देखें—Data types in C

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

Flowchart to swap values of two variables

Swapping values of two variables flowchart

C program to swap values of two variables

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

C++ program to swap values of two variables

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

Output of C C++ program to swap values of two variables

Enter a b : 1 2
a = 2 b = 1
Enter a b : 33 54
a = 54 b = 33

Some important C C++ programs of Sequential logic

  1. Sum of two entered numbers
  2. Average of three entered numbers
  3. Simple interest
  4. Total and Percent
  5. Swapping without using third variable
  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