Answers for "c program for swapping of two numbers using temporary variable"

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
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 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

Code answers related to "c program for swapping of two numbers using temporary variable"

Browse Popular Code Answers by Language