Computer Science, asked by Soñador, 1 year ago

CLASS 11th
IT
TOPIC = Programming (C++)

QUESTION = Write a program to enter any 2 numbers and swap it.



PLZ PLZ PLZ TELL ME THE SOLUTION FOR THIS.......
PLZZZZZZZZZZZ
ITS A BIT URGENT
PLZ HELPPPPPPPPP
NO SPAMMING

Answers

Answered by Anonymous
21
Heya !!

Here is your answer..

The coding for the following program is :

___________________

#include<iostream.h>
#include<conio.h>

void main()
{
int a,b,c;
clrscr();
cout<<"Enter the value of A: ";
cin>>a;
cout<<"Enter the value of B: ";
cin>>b;
c=a;
a=b;
b=c;
cout<<"Swapping the values: "<<a<<"b: "<<b;
getch();
}

___________________

Hope it helps...
Answered by HarishAS
22

Hey friend, Harish here.


Here is your answer.


1st program.


#include<iostream.h>

#include<conio.h>


void main()

{

int a,b,c;

clrscr();

cout<<"Enter value of a: ";

cin>>a;

cout<<"Enter value of b: ";

cin>>b;

c=a;

a=b;

b=c;

cout<<"After swap a: "<<a<<"b: "<<b;

getch();

}


2nd method:


#include<iostream.h>

#include<conio.h>


void main()

{

int a,b;

clrscr();

cout<<"Enter value of a: ";

cin>>a;

cout<<"Enter value of b: ";

cin>>b;

a=a+b;

b=a-b;

a=a-b;

cout<<"After swap a: "<<a<<"b: "<<b;

getch();

}


3rd method:


#include<iostream>

using name_space std;

{

b = a + b;

a = b - a;

b = b - a;

}


int main()

{

int a, b;


cout << "Enter two numbers to be swapped : ";

cin >> a >> b;

swap(a, b);

cout << "The two numbers after swapping become :" << endl;

cout << "Value of a : " << a << endl;

cout << "Value of b : " << b << endl;

}


These are programs using Temporary variable and without temporary variable.


If you face any error kindly comment below this answer.

______________________________________________________


Hope my answer is helpful to you.


Soñador: thank you
HarishAS: Welcome sis.
nitish8089: actually one error see out you call swap method but forget signature please check out...
Similar questions