C Topics
- C Intro
- C Control Statement
- C Control Loops
- C String Functions
- C Math Functions
- C Header Files
- C Interview Programs
- Abundant Number
- Amicable Number
- Armstrong Number
- Average of N Numbers
- Automorphic Number
- Biggest of three numbers
- Binary to Decimal
- Common Divisors
- Composite Number
- Condense a Number
- Cube Number
- Decimal to Binary
- Decimal to Octal
- Disarium Number
- Even Number
- Evil Number
- Factorial of a Number
- Fibonacci Series
- GCD
- Happy Number
- Harshad Number
- LCM
- Leap Year
- Magic Number
- Matrix Addition
- Matrix Division
- Matrix Multiplication
- Matrix Subtraction
- Matrix Transpose
- Maximum Value of an Array
- Minimum Value of an Array
- Multiplication Table
- Natural Number
- Number Combination
- Odd Number
- Palindrome Number
- Pascalβs Triangle
- Power of 2
- Power of 3
- Pronic Number
- Perfect Number
- Perfect Square
- Prime Factor
- Prime Number
- Smith Number
- Strong Number
- Sum of Array
- Sum of Digits
- Swap Two Numbers
- Triangular Number
- C Star Pattern
- C Number Pattern
- C Alphabet Pattern
C Program to Swap Two Numbers
Photo Credit to CodeToFun
π Introduction
In the world of programming, performing basic operations on numbers is a fundamental task. Swapping two numbers is one such operation that involves exchanging the values of two variables. This operation is commonly used in various algorithms and programs.
In this tutorial, we will explore a simple C program designed to swap the values of two numbers.
π Example
Let's delve into the C code that accomplishes this task.
#include <stdio.h>
// Function to swap two numbers
void swapNumbers(int * a, int * b) {
// Temporary variable to hold the value of 'a'
int temp = * a;
// Assign the value of 'b' to 'a'
* a = * b;
// Assign the original value of 'a' (stored in 'temp') to 'b'
* b = temp;
}
// Driver program
int main() {
// Replace these values with the numbers you want to swap
int num1 = 5, num2 = 10;
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
// Call the function to swap the numbers
swapNumbers( & num1, & num2);
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
π» Testing the Program
To test the program with different numbers, modify the values of num1 and num2 in the main program.
Before swapping: num1 = 5, num2 = 10 After swapping: num1 = 10, num2 = 5
Compile and run the program to see the result of swapping the two numbers.
π§ How the Program Works
- The program defines a function swapNumbers that takes two integer pointers, a and b, as parameters and swaps their values using a temporary variable.
- Inside the main program, replace the values of num1 and num2 with the numbers you want to swap.
- The program prints the values of num1 and num2 before and after swapping.
π§ Understanding the Concept of Swapping Two Numbers
Swapping two numbers involves exchanging their values. In the provided program, a temporary variable is used to hold the value of one variable while the values of the two variables are exchanged.
π’ Optimizing the Program
While the provided program is a straightforward way to swap two numbers, consider exploring and implementing alternative approaches or optimizations.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!.
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (C Program to Swap Two Numbers) please comment here. I will help you immediately.