- C program to swap two numbers using functions temporary variable.
- We can swap two numbers by using temporary variable.
- Lets see an example C program to swap two numbers without using any function.
- C program to swap two numbers using third variable
Program #1: Write a C program to swap two numbers using temporary variable without using any function and by using third variable.
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, char *argv[])
- {
- int a,b, temp;
- printf("Please enter two integer number to swap\n");
- scanf("%d%d",&a,&b);
- printf("before swapping\n");
- printf("a=%d\n",a);
- printf("b=%d",b);
- temp=a;
- a=b;
- b=temp;
- printf("\nafter swapping\n");
- printf("a=%d\n",a);
- printf("b=%d",b);
- return 0;
- }
Output:
Program #2: Write a C program to swap two numbers using temporary variable with using function and using third variable.
- C program to swap two numbers using functions call by value
- #include <stdio.h>
- #include <stdlib.h>
- void swap(int a, int b);
- int main(int argc, char *argv[])
- {
- int a,b;
- printf("Please enter two integer numbers to swap\n");
- scanf("%d%d",&a,&b);
- printf("before swapping\n");
- printf("a=%d\n",a);
- printf("b=%d",b);
- swap(a,b);
- return
- }
- void swap(int a, int b){
- int temp=a;
- a=b;
- b=temp;
- printf("\nafter swapping\n");
- printf("a=%d\n",a);
- printf("b=%d",b);
- }
Output:
- Please enter two integer numbers to swap
- 10
- 20
- before swapping
- a=10
- b=20
- after swapping
- a=20
- b=10
No comments