Answers for "c function to swap two numbers"

C
1

swap two numbers in c

#include <stdio.h>
int main() {
    double a, b;
    printf("Enter a: ");
    scanf("%lf", &a);
    printf("Enter b: ");
    scanf("%lf", &b);

    // Swapping

    // a = (initial_a - initial_b)
    a = a - b;   
  
    // b = (initial_a - initial_b) + initial_b = initial_a
    b = a + b;

    // a = initial_a - (initial_a - initial_b) = initial_b
    a = b - a;

    printf("After swapping, a = %.2lf\n", a);
    printf("After swapping, b = %.2lf", b);
    return 0;
}
Posted by: Guest on March-26-2021
0

c program to swap two numbers using call by reference

#include <stdio.h>
 
void swap(int*, int*);
 
int main()
{
   int x, y;
 
   printf("Enter the value of x and y\n");
   scanf("%d%d",&x,&y);
 
   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
 
   swap(&x, &y); 
 
   printf("After Swapping\nx = %d\ny = %d\n", x, y);
 
   return 0;
}
 
void swap(int *a, int *b)
{
   int temp;
 
   temp = *b;
   *b = *a;
   *a = temp;   
}
Posted by: Guest on February-18-2021
0

c program for swapping of two numbers using temporary variable

#include <stdio.h>
int main()
{
    int a, b, temp;
    printf("enter the values of a and b: \n");
    scanf("%d%d", &a, &b );
    printf("current values are:\n a=%d\n b=%d\n", a, b);
    temp=a;
    a=b;
    b=temp;
    printf("After swapping:\n a=%d\n b=%d\n", a, b);
}
Posted by: Guest on April-19-2021
0

c function to swap two numbers

#include<stdio.h>

void swap(int *,int *);
int main()
{

    int n1,n2;
	printf("\n\n Function : swap two numbers using function :\n");
	printf("------------------------------------------------\n");	   
    printf("Input 1st number : ");
    scanf("%d",&n1);
    printf("Input 2nd number : ");
    scanf("%d",&n2);	

    printf("Before swapping: n1 = %d, n2 = %d ",n1,n2);
	//pass the address of both variables to the function.
    swap(&n1,&n2);

    printf("\nAfter swapping: n1 = %d, n2 = %d \n\n",n1,n2);
    return 0;
}

void swap(int *p,int *q)
{
	//p=&n1 so p store the address of n1, so *p store the value of n1
	//q=&n2 so q store the address of n2, so *q store the value of n2

    int tmp;
    tmp = *p; // tmp store the value of n1
    *p=*q;    // *p store the value of *q that is value of n2
    *q=tmp;   // *q store the value of tmp that is the value of n1
}
Posted by: Guest on September-07-2021
2

how to swap values in variables in c

#include <stdio.h>
#include <stdlib.h>

int main()
{
  	//initialize variables
	int num1 = 10;
	int num2 = 9;
  	int tmp;
  	
  	//create the variables needed to store the address of the variables
  	//that we want to swap values
  	int *p_num1 = &num1;
  	int *p_num2 = &num2;
  
  	//print what the values are before the swap
  	printf("num1: %i\n", num1);
    printf("num2: %i\n", num2);
  
  	//store one of the variables in tmp so we can access it later
  	//gives the value we stored in another variable the new value
  	//give the other variable the value of tmp
  	tmp = num1;
  	*p_num1 = num2;
  	*p_num2 = tmp;

  	//print the values after swap has occured
   	printf("num1: %i\n", num1);
    printf("num2: %i\n", num2);
  	
	return 0;
}
Posted by: Guest on October-16-2020

Code answers related to "c function to swap two numbers"

Code answers related to "C"

Browse Popular Code Answers by Language