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:
");
scanf("%d%d", &a, &b );
printf("current values are:
a=%d
b=%d
", a, b);
temp=a;
a=b;
b=temp;
printf("After swapping:
a=%d
b=%d
", a, b);
}
// Overcomplicating things lol. Try this
#include <stdio.h>
int main()
{
int x, y;
printf("Enter Value of x ");
scanf("%d", &x);
printf("
Enter Value of y ");
scanf("%d", &y);
int temp = x;
x = y;
y = temp;
printf("
After Swapping: x = %d, y = %d", x, y);
return 0;
}
#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
", num1);
printf("num2: %i
", 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
", num1);
printf("num2: %i
", num2);
return 0;
}