C program example for swapping 2 numbers:
What is swapping?
Swaping is nothing but interchanging. It may be the value of 2 numbers or something to exchange between them. For example, consider below example.
- Before swapping, the values of A and B are as below.
A = 10
B = 20 - After swapping, the values of A and B will be as below.
A = 20
B = 10
Example C program for swapping 2 numbers with temp variable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> int main() { int A, B, temp; printf("Please enter the 1st number : "); scanf("%d",&A); printf("\nPlease enter the 2nd number : "); scanf("%d",&B); printf("\nBefore swapping:\n"); printf("A - %d \nB - %d", A, B); temp = A; A = B; B = temp; printf("\nAfter swapping:\n"); printf("A - %d \nB - %d", A, B); return 0; } |
Output of C program for swapping 2 numbers with temp variable:
Please enter the 1st number : 20
Please enter the 2nd number : 30 Before swapping: A – 20 B – 30 After swapping: A – 30 B – 20 |
Example C program for swapping 2 numbers without temp variable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> int main() { int A, B; printf("Please enter the 1st number : "); scanf("%d",&A); printf("\nPlease enter the 2nd number : "); scanf("%d",&B); printf("\nBefore swapping:\n"); printf("A - %d \nB - %d", A, B); A = A - B; B = A + B; A = B - A; printf("\nAfter swapping:\n"); printf("A - %d \nB - %d", A, B); return 0; } |
Output of C program for swapping 2 numbers without temp variable:
Please enter the 1st number : 45
Please enter the 2nd number : 67 Before swapping: A – 45 B – 67 After swapping: A – 67 B – 45 |
OTHER C PROGRAMS:
- C program for prime number
- C program for factorial
- C program for fibonacci series
- C program for palindrome
- C program for swapping 2 numbers with and without temp variables
- C program to find leap year
- C program to find armstrong number
- C program to find simple and compound interest
- C program to find largest of given 3 numbers
- C program to find smallest of given 3 numbers
- C program to convert lower case into upper case and vice versa
- C program to find sum and average of given 3 numbers
- C program to sum up all individual digits
- C program to reverse given number
- C program to reverse given string
- C program to find strong number
- C program to find square and cube of given number
- C program to print hello world without using semi colon
- C program to sort given names in alphabetical order
- C program to copy content of one file to another
- C program to sort given numbers in ascending order
- C program to sort given numbers in descending order
- C program to search given number in an array
- C program for recursive function
- C program for calculator application
- C program for bank application
- C program to check given number is perfect number or not
- C program to find array size
- C program to find whether given character vowel or not
- C program to check whether given number is positive or negative
- C program to find sum of n numbers
- C program to compare 2 numbers without if statement
- C program to generate random numbers
- C program to compare 2 arrays whether they are equal or not
- C program to print number from 1 to 500 without using any loop conditions
- C program to insert an element into an array
- C program to delete an element from an array
- C program to find hcf (gcd) and lcm
- C program to print diamond pattern
- C program to print pascal triangle
- C program to add two complex numbers
- C program for matrix addition
- C program for matrix multiplication
- C program for bubble sort
- C program for insertion sort
- C program for selection sort