swapping values in c without using third variable code example
Example 1: how to swap values in variables in c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1 = 10;
int num2 = 9;
int tmp;
int *p_num1 = &num1;
int *p_num2 = &num2;
printf("num1: %i\n", num1);
printf("num2: %i\n", num2);
tmp = num1;
*p_num1 = num2;
*p_num2 = tmp;
printf("num1: %i\n", num1);
printf("num2: %i\n", num2);
return 0;
}
Example 2: 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);
}