swap two numbers in c

Solutions on MaxInterview for swap two numbers in c by the best coders in the world

showing results for - "swap two numbers in c"
Leon
23 Apr 2016
1#include <stdio.h>
2int main() {
3    double a, b;
4    printf("Enter a: ");
5    scanf("%lf", &a);
6    printf("Enter b: ");
7    scanf("%lf", &b);
8
9    // Swapping
10
11    // a = (initial_a - initial_b)
12    a = a - b;   
13  
14    // b = (initial_a - initial_b) + initial_b = initial_a
15    b = a + b;
16
17    // a = initial_a - (initial_a - initial_b) = initial_b
18    a = b - a;
19
20    printf("After swapping, a = %.2lf\n", a);
21    printf("After swapping, b = %.2lf", b);
22    return 0;
23}
24
Adrián
03 Jul 2018
1#include <stdio.h>
2 
3void swap(int*, int*);
4 
5int main()
6{
7   int x, y;
8 
9   printf("Enter the value of x and y\n");
10   scanf("%d%d",&x,&y);
11 
12   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
13 
14   swap(&x, &y); 
15 
16   printf("After Swapping\nx = %d\ny = %d\n", x, y);
17 
18   return 0;
19}
20 
21void swap(int *a, int *b)
22{
23   int temp;
24 
25   temp = *b;
26   *b = *a;
27   *a = temp;   
28}
29 
Lucas
30 Sep 2017
1#include <stdio.h>
2int main()
3{
4    int a, b, temp;
5    printf("enter the values of a and b: \n");
6    scanf("%d%d", &a, &b );
7    printf("current values are:\n a=%d\n b=%d\n", a, b);
8    temp=a;
9    a=b;
10    b=temp;
11    printf("After swapping:\n a=%d\n b=%d\n", a, b);
12}
Leon
29 Jan 2020
1#include <stdio.h>
2#include <stdlib.h>
3
4int main()
5{
6  	//initialize variables
7	int num1 = 10;
8	int num2 = 9;
9  	int tmp;
10  	
11  	//create the variables needed to store the address of the variables
12  	//that we want to swap values
13  	int *p_num1 = &num1;
14  	int *p_num2 = &num2;
15  
16  	//print what the values are before the swap
17  	printf("num1: %i\n", num1);
18    printf("num2: %i\n", num2);
19  
20  	//store one of the variables in tmp so we can access it later
21  	//gives the value we stored in another variable the new value
22  	//give the other variable the value of tmp
23  	tmp = num1;
24  	*p_num1 = num2;
25  	*p_num2 = tmp;
26
27  	//print the values after swap has occured
28   	printf("num1: %i\n", num1);
29    printf("num2: %i\n", num2);
30  	
31	return 0;
32}
33
Leni
04 Jan 2019
1#include <stdio.h>
2
3int main()
4{
5    int x = 20, y = 30, temp;
6    temp = x;
7    x = y;
8    y = temp;
9    printf("X = %d and Y = %d", x, y);
10    
11    return 0;
12}
Agustina
24 Apr 2018
1#include<stdio.h>
2
3void swapping(int, int);  //function declaration
4int main()
5{
6    int a, b;
7
8    printf("Enter values for a and b respectively: \n");
9    scanf("%d %d",&a,&b);
10
11    printf("The values of a and b BEFORE swapping are a = %d & b = %d \n", a, b);
12
13    swapping(a, b);      //function call
14    return 0;
15}
16
17void swapping(int x, int y)   //function definition
18{
19    int third;
20    third = x;
21    x    = y;
22    y    = third;
23
24    printf("The values of a and b AFTER swapping are a = %d & b = %d \n", x, y);
25}
queries leading to this page
swap two variables in cwrite a c program to swap two numbers c function without arguments and without return valuehow to make a swap function with void 2aswap two variablesswap two numbers in cswap int chow to swap two integers in cswap with two variables in cexcahnge of integers with tem variable in cwrite a c program to swap two numbers without using third variablehow to swap 4 number in cswap the value in c using functionwrite a function in c to swap the values of two variables using call by reference how to swap two numbers in cswap 2 numbers in cswapping of two numbers in cswap 2 numbers in c programmingc swap two variablesswapping two numbers in c without using bitwise operatorsfunction to swap in cswap variables in cswap the value of given number in chow to swap two numberswrite down a program in c for swapping of two numbers using bitwise operatorsprogram to swap values of two variables using call by reference method in carray before swap and after swap programswapping in cswap 2 numbers by using address function in cswap value in cswap two numbers without using temporary variable in cswap to number without using 3rd variable in cswaping cwrite a c program to swap two number using call by referencebest way to swap two variables without using temp in chow to use swap in cswapping two variables without temp in cswaps example in programming languageswap two number in cprogram to swap two numbers in cc code to swap two numbersc program to swap two numbershow to swap two numbers in c using functionswap two numbers in c using call by referencewrite a program with a function to swap the values of 2 given integer variablesc program to swap two numbers explainedswap values cswap two integer function in cc swap two variables functionswap values in c with two functionsswapping of two variables in cwrite a programme to swap two numbers in cswap every two integers in cswap 2 variables in cswap two integers using function in chow to swap numbers in cwrite a programme to swap two numbers using thirdveriable in cswap the values of 2 given integer variables why can 27t you swap variables in cswapping of two numbers in c in single linec different methods to swap 2 numberswrite a c program to swap two numbers entered by user swap 2 numbers using call by reference in cswap variable in cswap logic in cfunction that swaps two integersswapping two variables in cwrite a program to swap the numbers in c programizwrite a program to swap two numbers in cwrite a c program to swap two given numbers using call by reference c program to swap two numbers using and without using third variableswap every two number of an integers in cswapping 2 numbers in cswapping with permanent number in cswaping in cfunction to swap two numbers in cswap 2 numbers using functions in cswapping in c programmingwap to swap the values of two variables using switch case swap 2 numbers without using 3rd variable in cc swap two integersswap varaibles cc program to swap two numbers using pointersc program to swap values of two variables using pointersswap values of two variables without using third variable in cswap numbers in cswap digits in a number cswap number in cwrite a program in c to swap two numbers using functions 28use call by reference 29function swap in cswap the two numbers in cswap variable values in cswitch 2 variables in cc how to swap values with pointerswapping of two numbersc how to make 2 variables swapswapnumbers in cswap function in cswapping of two numbers using function in chow to swap two numbers in c without using third variableswapping without temporary variable in cprogram to swap two values in cswap value without third variable in cwrite a program to swap 2 numbers without thirdvariable in cprogram for swapping of two numbers in cc program to swap two numbers using function call by valuec program to swap two numbers using functionsc program to show interchange of two numbers using functions in c programmingswap function cswapping values in c without using third variableuse of swap in c swap without third variable cwrite a program to swap 2 numbers without 3rd variable in cc program that swaps two numberscode for swaping two numbers in c using pointersswap two integers without using third variable in chow to swap two values in a functionwrite a program to accept two numbers from user and swap their values c swap elements in arrayc program to swap two numbers without third variablec program to interchange two numbersc swap to variablesswap numbers in c from a functionswaping numbers using swap in cswap function to swap two numbers in array in cwrite down a program in c for swapping of two numbers using 2a and 2f operatorswap two integers without using temporary variable in cwrite a program to swap two numbers in c programc program to swap two numbers using pass by referencesswapping 3 numbers in cswap numbers c c swap integer bytrsswap of 2 numbers in cswap function in cswaping a number in cswap two var cswape values in cswapping two numbers in c using mathematical expressionhow to swap cswap by value in cswap values in c examplec swap valuescall by value and call by reference in c swapping two numbers usingswapping of two numbers in c without temporary variableswap in cswap numbers using function in cswap the numbers in c5 methods to swap numbers in cswap variable tempsc program to swap three numbersswap values in cprogram to swap digits within a number cswapping two numbers in c programsswap two numbers using bitwise operator in cadding two digit numbers and swap in cswap 2 numbersswap two numbers using predefined variable c programswap two numbers without using third variable in cc program for swapping using functionswapping variable values in cswaping numbers in cswapping of two numbers without using third variable in cswap two numbers function in chow to swap two numbers in c using arraysswap two numbers without using bitwise in cswapping of two numbers in c using functionswap function in c using pointerswrite a c program to swap two numbers two swaps cswapping program in cswap two numbers with using third variable in cswapping logic in cswapping two numbers in cswap cc program to swap values of two variables using third v ariablec program to swap two numbers using third variableswapping with temporary variable in ccode for swapping two numbers in cswap two numbers in c call by reference functionwrite a program in c to swap two numbersswap of two numbers in cswapping by call by value in cswap the values of two variables in cswap program in chow can we swap in cswap the value in cswap function inc cswap fuction in cwrite c program to swap two numbersswap two numbers in c call by value functionswap function code in cc program swapping without using third variablec swap functionswap elements in cswap the pointer of two variables in cswap in c languagec program to swap two nosc program to swap two numbers without using third variable swap two user given integerdifferent method to swap the variables in c languagecode for swapping of two numbers without using temporary variable using c swapping of two numbers in c logicswap using 2 variables cswapping number in cc program to swap two numbers using call by valueswap tow number in cswap to nos in cwrite a c program to check a swapswapping two numbers program in chow to swap values in variables in cswapping of two numbers in c without third variableswap function in c programmingc swap intsswap of two numbers using third variableswap two numbers in c using functionswaping function in cc program to swap two numbers without a third variable using conditionalsc programming to swap two variableswrite a c program to swap these two numbers swap values without third variable in cprogram to swap two numbers without using third variable in cswapping number c programmingswap the value of two variables in cswap two bit in cswap two values using function in cprogram to swap two numbershow to swap in 3dis swap a function in cc program to swap two numbers without third variable algorithem1 wap to swap the value of two variablehow to swap three numbers in cc program for swapping of two numbers using temporary variableswap 2 variables without temp in chow to swap two digit in cc program to swap two numbers without a third variableswap numbers code in cwrite a function to swap two integers in cswap without third variable in cswapping of two numbers in c using functionswrite a program to swap the numbers in cswapping of two numbers in c without thirdvariablec ode to swap 2 numbersmy swap in cswapping elements in cswap numbers without using third variable in cswap 4 numbers in cc program to swap two numbers using call by referencehow to swap values in cswapping of variables in cwrite a c program to swap two numberscreate a function that swaps the value of two integersswitching 2 variables in cc function to swap two numbers using bitwise operation swap with pointers in cswap using 3 variables in cwrite a program to swap 2 numbers without third variable in cswap program in c without using third variableswapping values in cswap value in c functionswap using xor operator in cswapping without third variable in cwrite a c program that swaps two numbers without using third variable c swap nulberswrite a program which asks the user to enter two integers a and b print the values the values are passed on to two functions which return the swapped values of a and b swap two numbers in c call by valuewrite a program to swap two numberswrite a c program to swap two numbers without using third variable c program exchange two numbersswap two digits in cswping two numbersswap using function in cswapping of two numbers in c using 2 variablesswap program using 2 variableswrite a program in c to swap two numbers using the function swapng two variable in c c program to swap two number without using third variableswap in c without temporary variableswap function for integerswrite a c program to swap two numbers with function without arguments and without return valueswap 2 number cswap code in cswap two numbers without third variable in cswap numbers in cwrite a c program to swap two numbers without using temporary variable and by passing by referencehow to swap two variables without third variable in cc program to show interchange of two numbers in c programmingwrite a program to swap two variables using pass by reference function in cswap two numbers using third variable in cswap the n numbers in cswap two numbers c programhow to swap value in cswapping of two numbers in c by call by reference in cswap two numbers in c using call by valuec program to swap three numbers using fourth variablec to swap write a c program to swap two numbers using third variableswap 2 numbers c efficientswapping numbers in cwrite a c program to swap two numbers without using temporary variablewrite down a program in c for swapping of two numbers using 2b 2f operatorhow to swap variables in cc program to swap two numbers without using third variableswap two numbers in c without using third variablec swapswapping of two no using function in cwrite a function to swap the values between the variables in cswapping cswap to numbers in cc function to swap two numbershow to swap structures in cswap 2 number in cswap two numbers using function in cvoid swap in chow to define swap in cswap 2 variables without using 3rd variable in cswap using two variables in cswap the number using macro in cswap 3 numbers in cwrite a program for swapping of two numbers swap in c programmingc program for swapping of two numbersswap by value function in cconsider this c code to swap two integers and these five statements 3a the codewrite a program that will make a swap between two defined variables using their addresseswrite down a program in c for swapping of two numbers a 29 using 2b 2f operatorswapping two values in cswapping two number different type of method in cswap two numbers in c