c function to swap two numbers

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

showing results for - "c function to swap two numbers"
Eleonora
03 Sep 2017
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
Alois
17 Apr 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 
Rodrigo
14 May 2020
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}
Jonah
02 Apr 2020
1#include<stdio.h>
2
3void swap(int *,int *);
4int main()
5{
6
7    int n1,n2;
8	printf("\n\n Function : swap two numbers using function :\n");
9	printf("------------------------------------------------\n");	   
10    printf("Input 1st number : ");
11    scanf("%d",&n1);
12    printf("Input 2nd number : ");
13    scanf("%d",&n2);	
14
15    printf("Before swapping: n1 = %d, n2 = %d ",n1,n2);
16	//pass the address of both variables to the function.
17    swap(&n1,&n2);
18
19    printf("\nAfter swapping: n1 = %d, n2 = %d \n\n",n1,n2);
20    return 0;
21}
22
23void swap(int *p,int *q)
24{
25	//p=&n1 so p store the address of n1, so *p store the value of n1
26	//q=&n2 so q store the address of n2, so *q store the value of n2
27
28    int tmp;
29    tmp = *p; // tmp store the value of n1
30    *p=*q;    // *p store the value of *q that is value of n2
31    *q=tmp;   // *q store the value of tmp that is the value of n1
32}
33
34
Haven
29 Jul 2019
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
queries leading to this page
swap two numbers with using third variable in cswap 2 variables without using 3rd variable in cwrite a program to swap two numbers in cc program to swap two numbers using third variableswap to numbers in cswapping by call by value in cc program to swap three numbers using fourth variableswapping of two numbers in c using 2 variablesc swapwrite a c program to swap two numbers using third variableswap numbers without using third variable in chow to swap values in variables in cswapping numbers in cuse of swap in c swap tow number in cswap every two number of an integers in cswap the numbers in cwrite a c program to check a swapswap with pointers in cswapping two values in cwrite down a program in c for swapping of two numbers using bitwise operatorsswap the value in c using functionswitch 2 variables in cc different methods to swap 2 numbersswap values of two variables without using third variable in cwrite a program in c to swap two numbersswap numbers code in cwrite a c program to swap two numbers without using third variableswap 2 numbers using functions in chow to swap value in cwrite a c program to swap two numbers without using third variable c to swap swap two numbers in c call by value functionswapping two variables in cwrite a c program that swaps two numbers without using third variable c program to swap two numbers explainedswap two variableswrite a c program to swap two numbers without using temporary variablewrite a c program to swap two numberswrite a program to accept two numbers from user and swap their values c function to swap two numbersswapping of two numbers in c without third variableswap value without third variable in cswap number in cwrite a program to swap two numbers in c programwrite down a program in c for swapping of two numbers using 2b 2f operatorswap two numbers in c call by reference functionswap two variables in cswap digits in a number cswap by value function in cwap to swap the values of two variables using switch case swapping 2 numbers in cswap two numbers without using third variable in cwrite a program that will make a swap between two defined variables using their addressesbest way to swap two variables without using temp in cswap logic in cswap numbers using function in cswapping values in c without using third variableswap 2 numbers by using address function in cswap function inc cswping two numbersfunction that swaps two integersswap the two numbers in chow to swap two numbers in cc swap valuesc program to swap two numbers without a third variableswap 2 number in cc program to swap two numbers without using third variable swap function cswapping of two numbers in c using functionc program to swap two numbers without using third variableswapping elements in cc program to swap two numbers using pointersc swap integer bytrsswap cswap two numbers using function in ccode for swaping two numbers in c using pointerswrite a c program to swap two numbers without using temporary variable and by passing by referenceswapping of two no using function in cc program swapping without using third variableswap two integers without using third variable in cwrite a program to swap 2 numbers without thirdvariable in cswap function in c programmingswap two numbers in c using call by valueswitching 2 variables in cc program for swapping of two numbers using temporary variableswap two bit in cwrite a program with a function to swap the values of 2 given integer variablesswap 2 numbers in cswapping with permanent number in cswap using function in cswapping program in cc ode to swap 2 numberswrite a function to swap the values between the variables in chow to define swap in cswapping of two numbers using function in cfunction swap in cc program to swap three numbersswapping logic in cswap function to swap two numbers in array in cwrite a c program to swap two number using call by referenceswapping of two numbersswap two numbers in chow to use swap in chow to swap two numbers in c using arraysswape values in cswap two numbers in c using functionswap function in c using pointersswaps example in programming languageswap without third variable in cswapping without temporary variable in cswap function in cwrite a programme to swap two numbers in cswap elements in cswap two var cswap by value in carray before swap and after swap programswap code in cswap values cc swap to variablesswapping two numbers in c without using bitwise operatorsprogram to swap digits within a number cswapping two number different type of method in cswap using 2 variables cconsider this c code to swap two integers and these five statements 3a the codeswap to nos in cwrite a program to swap two numbersswapping number in cswaping numbers in cc program to swap values of two variables using pointershow to swap in 3dc swap two variablesswap two numbers in c using call by referenceswap value in c functionadding two digit numbers and swap in cc program to swap values of two variables using third v ariableexcahnge of integers with tem variable in cswap in c without temporary variableswapping with temporary variable in cswap the value of two variables in chow to swap values in cswap 2 variables in cswap 2 numbers c efficientswapping of two numbers without using third variable in cc program to swap two numbers using function call by valuec function to swap two numbers using bitwise operation swapping of two numbers in c without thirdvariableprogram for swapping of two numbers in cswap two integers without using temporary variable in cc program that swaps two numbersswap two numbers function in cswap the number using macro in cswap two user given integerhow to swap numbers in cswap program in cswap using two variables in cswap the n numbers in c5 methods to swap numbers in cswap two numbers in c call by valueswapping of two numbers in c using functionshow to swap two numbersswap two integers using function in cc swap nulbersswap function in cswapnumbers in cswapping two numbers program in cswapping of two numbers in c by call by reference in cswapping of two variables in cwhy can 27t you swap variables in cwrite a program to swap the numbers in c programizswap the values of two variables in cwrite a c program to swap two given numbers using call by reference swapping of two numbers in c without temporary variablewrite a function to swap two integers in cc program to show interchange of two numbers using functions in c programmingcode for swapping of two numbers without using temporary variable using c swap two numbers c programswap two number in cc swap two variables functionvoid swap in cwrite a function in c to swap the values of two variables using call by reference swap 2 variables without temp in cswap 2 number cswap numbers in cswap two numbers without using bitwise in cswap of two numbers using third variableswap using xor operator in cc program to interchange two numbersswapping without third variable in cswap with two variables in cwrite down a program in c for swapping of two numbers using 2a and 2f operatorswap in c programminghow to swap structures in cswap two numbers without using temporary variable in cswapping 3 numbers in cc how to swap values with pointerc program exchange two numbersprogram to swap two numbers in cswapping of variables in cswap 2 numbers in c programmingswap variable values in cswapping two numbers in cswapping two numbers in c using mathematical expressionhow to swap cprogram to swap values of two variables using call by reference method in cswapping in cswaping a number in cswap the value of given number in cswaping function in cprogram to swap two numberswrite a c program to swap two numbers c function without arguments and without return valueswap variable in cc program to swap two numbers using pass by referencesc program to swap two numbers using functionsfunction to swap in cwrite a c program to swap two numbers entered by user write down a program in c for swapping of two numbers a 29 using 2b 2f operatorswap the value in cis swap a function in cswapping of two numbers in c in single linehow to swap two numbers in c using functionc swap two integersswaping numbers using swap in cswap numbers in chow to swap two numbers in c without using third variableswap two numbers using bitwise operator in cswap values in cmy swap in cwrite a program to swap 2 numbers without 3rd variable in cwrite a program in c to swap two numbers using functions 28use call by reference 29how to swap three numbers in cc program to swap two numbers using call by referencec code to swap two numbersc swap elements in arraywrite a c program to swap two numbers swap two numbers using predefined variable c programwrite a program to swap the numbers in cswap numbers in c from a functionswapping variable values in cswap function code in cswap using 3 variables in chow to make a swap function with void 2aswap two digits in cswap of two numbers in cswapng two variable in cwrite a c program to swap these two numbers swap function for integersc program to swap two numbers without a third variable using conditionalsc program to swap two numbersc program to show interchange of two numbers in c programmingswap two integer function in cc swap functionwrite a program for swapping of two numbers swapping two numbers in c programsswaping in cc swap intsswap values in c with two functionsswap to number without using 3rd variable in c1 wap to swap the value of two variablecall by value and call by reference in c swapping two numbers usinghow to swap 4 number in chow to swap two values in a functionwrite a programme to swap two numbers using thirdveriable in cfunction to swap two numbers in cwrite c program to swap two numbersc program to swap two numbers without third variable algorithemc program to swap two numbers using and without using third variablec program to swap two numbers without third variablehow to swap two digit in chow can we swap in cwrite a program in c to swap two numbers using the function program to swap two values in cswap int cswap program using 2 variablesc how to make 2 variables swapswapping two variables without temp in cswap 2 numbers using call by reference in cswap fuction in ccreate a function that swaps the value of two integers c program to swap two number without using third variableswap two numbers without third variable in cswap program in c without using third variableswapping of two numbers in cswap 4 numbers in ccode for swapping two numbers in chow to swap two integers in cswap every two integers in cc program for swapping of two numbersswap of 2 numbers in cswap two values using function in cswap numbers c write a program to swap 2 numbers without third variable in cswap two numbers using third variable in cwrite 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 c program to swap two nosswap variable tempsswap values in c exampleswap 3 numbers in cswap in c languageswap the values of 2 given integer variables how to swap variables in cswaping cswap values without third variable in cdifferent method to swap the variables in c languageswap varaibles cswap two numbers in c without using third variableswap in cswapping values in cswap value in cswapping of two numbers in c logicswap 2 numberswrite a c program to swap two numbers with function without arguments and without return valueswap variables in cswap 2 numbers without using 3rd variable in cprogram to swap two numbers without using third variable in cswapping number c programmingc program to swap two numbers using call by valuewrite a program to swap two variables using pass by reference function in cswapping cc programming to swap two variablesc function to swap two numbers