answersLogoWhite

0

C program to swap three variables?

Updated: 8/11/2023
User Avatar

Wiki User

14y ago

Best Answer

#include<stdio.h>

#include<conio.h>

void main()

{

int a=2,b=3;

swap(a,b);

printf("%d%d",a,b);

getch()

}

swap(int *x,int *y)

{

int t;

t=*x;

*x=*y;

*y=t;

printf("%d%d",x,y);

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

#include <stdio.h>

#include<conio.h>

int main()

{

int a=10,b=5,c;

clrscr();

printf("\n The value of `a` before swapping=%d",a);

printf("\n The value of `b` before swapping=%d",b);

c=a;

a=b;

b=c;

printf("\n\nThe value of `a` after swapping=%d",a);

printf("\n\nThe value of `b` after swapping=%d",b);

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

void swap(int* x, int* y)

{

if( !x !y ) return;

(*x)^=(*y)^=(*x)^=(*y);

}

int main()

{

int a=1, b=2;

std::cout<<"a="<<a<<", b="<<b<<std::endl;

swap(&a, &b);

std::cout<<"a="<<a<<", b="<<b<<std::endl;

return(0);

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Swapping three variable without using temp variable

a = 2 b = 1 c = 3

a = a+b+c = 6

b = a -b-c = 2

c = a -b- c = 1

a = a-b-c = 3

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C program to swap three variables?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How we can get address of variables in Java as pointer variables in C?

At any given point of time you cann't get the address of a variables of java program. This is meant for security purpose only.


Swap two number using pointer?

The only way to swap two values using call by value semantics is to pass pointer variables by value. A pointer is a variable that stores an address. Passing a pointer by value copies the address, the value of the pointer, not the pointer itself. By passing the addresses of the two values to be swapped, you are effectively passing those values by reference. Both C and C++ use pass by value semantics by default, however C++ also has a reference data type to support native pass by reference semantics. By contrast, Java uses pass by reference semantics by default. In C, to swap two variables using pass by value: void swap (int* p, int* q) { int t = *p; *p = *q; *q = t; } In C++, to swap two variables using pass by reference: void swap (int&amp; p, int&amp; q) { std::swap (p, q); } Note that C++ is more efficient because std::swap uses move semantics; there is no temporary variable required to move variables. With copy semantics, a temporary is required. However, with primitive data types, there is a way to swap values without using a temporary, using a chain of exclusive-or assignments: void swap (int* p, int* q) { *p^=*q^=*p^=*q; }


Even number progamm in c language?

swap(&amp;grades[num],&amp;grades[num+1]); what it make in a program?


How many structure variables of a given type can you use in a C program?

You can use unlimited number of variables for a structure and you can also declare array of structures.


What is the Algorithm for exchanging values of two variables?

There are three primary algorithms to exchange the values of two variables. Exchange with Temporary Variable temp = a; a = b; b = temp; Exchange Without Temporary Variable Using Exclusive Or a = a ^ b; b = b ^ a; a = a ^ b; Exchange Without Temporary Variable Using Arithmetic a = a + b; b = b - a; a = a - b;

Related questions

How do you write a program in C plus plus plus plus How do you write a program in C to swap two variables without using the third oneo swap two variables without using the third one?

To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;


How do you write a program in C to swap two variables without using the third one using XOR?

a=a^b; b=a^b; a=a^b;


How do you swap 4 variables?

tmp= a; a= d; d= b; b= c; c= tmp; Other permutations are also possible.


What are the feutures of a good c program?

c program is fast and efficient.And c programm can be write with vareity of type variables and operations.


How do you write a program in c to swap two variables without using the add and multiplication?

int varA, varB; int tmp; // common way to swap 2 variables in the same type; tmp = varA; // store the original value of varA varA = varB; // assign A with B varB = tmp; // B now has the original value of varA


What is turbo c variables?

Turbo C variables are memory place holders for storage of data during the execution of a Turbo C program. Types of variables include integer, real and char.


How do you write a program in C to swap two variables using the third variable?

Consider the following declarations:int x = 0;int y = 1;In order to swap the values, we need to use a temporary variable:int t = x;x = y;y = t;However, it is possible to swap the values without using a third variable:x ^= y ^= x ^= y;


What is union in c program?

It's a fragment of memory shared by multiple variables.


Simple c program for the call by reference?

In call by reference, you are calling the program by passing the variables address(reference) to it. This is done through the use of functions. Any changes made in the variables to the function will be reflected even the calling function.Here is a code snippet for swapping two numbers.#includevoid swap( int *a, int *b){int temp = *a;*a = *b;*b = temp;}int main(){int a=8, b=9;printf("The value of a and b before swap = %d %d\n", a, b);swap(a,b);printf("The value of a and b after swap = %d %d\n", a, b);return 0;}


How we can get address of variables in Java as pointer variables in C?

At any given point of time you cann't get the address of a variables of java program. This is meant for security purpose only.


How do you swap two numbers in a C program?

Ellipses (...) used to emulate indentation... swap(int *a, int *b) { ... int temp; ... temp = *a; ... *a = *b; ... *b = temp; }


What are the differences between global variables and local variables in C plus plus?

Global variables can be seen in all blocks of your program, when local variables are visible only within the block where it's declared.