C Program to Swap Two Numbers Last Updated : 01 May, 2025 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Swapping two numbers means exchanging their values. In this article, we will learn how to swap values of two numbers in a C program.The easiest method to swap two numbers is to use a temporary variable. First, we assign the value of first variable to temporary variable, then assign the value of second variable to first variable and in the last assign the value of temporary variable to second variable, which is the value of first variable. C #include <stdio.h> int main() { int a = 5, b = 10, temp; // Swapping values of a and b temp = a; a = b; b = temp; printf("a = %d, b = %d\n", a, b); return 0; } Outputa = 10, b = 5 Without Using a Temporary VariableIn this method, we use arithmetic operations to swap the values without using a temporary variable. C #include <stdio.h> int main() { int a = 5, b = 10; // Arithmetic operations to swap values a = a + b; b = a - b; a = a - b; printf("a = %d, b = %d\n", a, b); return 0; } Outputa = 10, b = 5 Using Bitwise XOR OperatorWe can also use the bitwise XOR operator to swap the values without using a temporary variable. C #include <stdio.h> int main() { int a = 5, b = 10; // Apply XOR operations in the given order // to swap values a = a ^ b; b = a ^ b; a = a ^ b; printf("a = %d, b = %d\n", a, b); return 0; } Outputa = 10, b = 5 Comment More infoAdvertise with us Next Article C Program to Swap Two Numbers kartik Follow Improve Article Tags : C Programs C Language Basic Coding Problems Samsung Swap-Program +1 More Practice Tags : Samsung Similar Reads Reverse Number Program in C The reverse of a number of means reversing the order of digits of a number. In this article, we will learn how to reverse the digits of a number in C language. Example:Input: 12354Output: 45321Explanation: The number 12354 when reversed gives 45321Input: 623Output: 326Explanation: The number 623 whe 2 min read C Program to Make a Simple Calculator A simple calculator is a program that can perform addition, subtraction, multiplication, and division of two numbers provided as input. In this article, we will learn to create a simple calculator program in C.ExampleInput: a = 10, b = 5, op = +Output: 15.00Explanation: Chosen operation is addition, 3 min read C Program to Swap Adjacent Characters of a String In this article, we will learn how to swap adjacent characters of a string in C. To swap all adjacent characters in a string, the string must have an even number of characters. If the number of characters is odd, the last character remains unswapped since it has no adjacent element.The most straight 3 min read Palindrome Number Program in C Palindrome numbers are those numbers that remain the same even after reversing the order of their digits. In this article, we will learn how to check whether the given number is a palindrome number using C program.ExamplesInput: 121Output: YesExplanation: The number 121 remains the same when its dig 3 min read C Program to Interchange Two Random Rows in a Matrix In this article, we will write a C program to interchange two random rows in a matrix. Below are the inputs that will be taken from the user: The number of rows & columns in the matrixThe elements in the matrixThe rows that will be interchanged Examples: Input: rows = 3, columns = 3 arr[i, j] = 2 min read C Program For Char to Int Conversion Write a C program to convert the given numeric character to integer.Example:Input: '3'Output: 3Explanation: The character '3' is converted to the integer 3.Input: '9'Output: 9Explanation: The character '9' is converted to the integer 9.Different Methods to Convert the char to int in CThere are 3 mai 3 min read C Program For Int to Char Conversion To convert the int to char in C language, we will use the following 2 approaches: Using typecastingUsing sprintf() Example: Input: N = 65 Output: A1. Using TypecastingMethod 1:Declaration and initialization: To begin, we will declare and initialize our integer with the value to be converted.Typecast 2 min read C Programs To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and 8 min read How to Convert an Integer to a String in C? In C, integers can be represented as strings with each digit represented by corresponding numeric character. In this article, we will learn how to convert integers into the stringExamplesInput: 1234Output: "1234"Explanation: The integer 1234 is converted to the string "1234".Input: -567Output: "-567 3 min read Converting String to Long in C Here, we will see how to build a C Program For String to Long Conversion using strtol() function. Syntax: long int strtol(char *string, char **ptr, int base)The first argument is given as a stringThe second argument is a reference to an object of type char*The third argument denotes the base in whic 4 min read Like