answersLogoWhite

0

Program of swapping two no

Updated: 12/21/2022
User Avatar

Wiki User

15y ago

Best Answer

There are several ways to do this in C:

// nums to swap

int a = 5;

int b = 10;

// using a temporary variable - often the fastest method

int temp;

temp = a;

a = b;

b = temp;

// using addition (no additional storage needed)

a = a + b;

b = a - b;

a = a - b;

// using xor (no additional storage needed)

a = a^b;

b = a^b;

a = a^b;

// using only one line (no additional storage needed) // note, this appears to be a compiler-dependent way to swap variables

a = a + b - (b=a);

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Program of swapping two no
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

A program in c for swapping?

This is not a question.


C program to swapping two numbers using call by value method?

You have to pass the address of the variables.void swap (int *pa, int *pb){...}


What is the meaning of 'swapping' in computer architecture?

Swapping means to swap the values of two addresses in main memory.


Differences between swapping and overlays?

1. In overlays there is no need for the whole program to reside in main memory .here if say if a program has two functions say f1(50k) and f2(70k) such that they are in mutual exclusion with each other then, the memory of only 70k will be allocated for both the functions and the one that is needed at a particular moment can reside in the main memory . this even saves the memory ,But it is not in case of swapping . here the whole program needs to reside in main memory 2. overlays require careful and time consuming planning while in swapping it is not needed 3. swapping needs to issue system calls for requesting and releasing memory which is not in case of overlays


What is the purpose of swapping?

Swapping was an older form of memory management. It was moving from/to secondary storage a whole program at a time, in a scheme known as roll-in/roll-out. Now swapping is a fairly close synonym of paging.


Is page swapping the same as thrashing?

Page Swapping is not the same as Thrashing. Thrashing is the significant degradation of performance caused by overuse of a computers resource, most commonly virtual memory. Page Swapping is a normal memory management function of most Operating Systems. It occurs any time a computer is running. Thrashing most often takes the form of too much Page Swapping. That occurs when a program needs more virtual memory than the computer can provide at the moment and pages of memory are moved on and off the disc so often that the program running cannot progress in its function. Thrashing is, therefore, not equivalent to Page Swapping and vice versa.


What is the advantage of swapping two variables without using 3 variable?

Nothing. Never do that.


What is the meaning of swapping in c?

Swapping has nothing to do with programming languages.


What is 'swaping' in financial markets?

When it comes to finance, swapping means when two different parties trade a cash flow for one's financial instrument. There are many different benefits in swapping that vary by case.


What is program in c to swap entered two numbers without using third variable?

The required c program is given below /*Swapping(interchange) the two entered numbers*/ #include<stdio.h> main() { /*Without using third variable*/ int a,b,t; printf("Enter a:"); scanf("%d",&a); printf("Enter b:"); scanf("%d",&b); a=a+b; b=a-b; a=a-b; printf("\n After swapping without using third variable"); printf("\na=%d\nb=%d",a,b); }


How do you write a C program to find the GCD of two given integers using non recursive functions?

Use the following function: int gcd (int a, int b) { while (b != 0) { a %= b; a ^= b ^= a ^= b; } return a; } Note that a ^= b ^= a ^= b is an efficient method of swapping two values.


What is program of Swapping of two numbers in assembly language?

LDA A //Load first number STA TMP LDA B //Load second number STA A LDA TMP STA B HLT IMPROVED: You can visit the link below to see the source code of the program. Make sure you have required library installed in MASM to run the program else you can see the logic and coding. http://infinityloopers.com/swapping-two-numbers-in-assembly-programming/