EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Swapping in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Swapping in C

swapping in c

Overview of Swapping in C

C language is the base language for all programming languages. Like other programming languages, there are various inbuilt functions available in C language. Inbuilt functions are used to solve complex problems quickly and make code easy. In order to built-in functions, C language also allows us to create customized functions to develop logic. In this article, we are going o discussed how to swap numbers or variables in C language. It means exchanging two numbers or variables with another using C language syntax.

How to Swap Numbers in C?

Suppose there are two variables, A and B. Variable A exchanges its data with variable B and variable B exchange its data with B. Swapping can be done by using two variables or three variables, it depends on the requirement. In this section, we are going to discussed how to swap two numbers in C and three numbers in C.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Swap Two Numbers in C

In this section, we are going to discussed how to swap two numbers in C language with the help of example and explanation.

Example: In the following C program, the user can enter 2 numbers he wishes to swap, then the result will be displayed on the screen. The program for swapping two numbers in C is as follows.

Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int first_number, second_number, temp;
printf("Enter first number: "); //allow user to add first number
scanf("%d",&first_number);
printf("Enter second number: "); //allow user to add second number
scanf("%d",&second_number);
printf("Before swapping \n");
printf("First number: %d \n", first_number);
printf("Second number: %d \n", second_number);
temp = first_number;  //first number is assigned to temp
first_number = second_number; //second number is assigned to first number
second_number = temp; //first number is assigned to secind number
printf("After swapping \n");
printf("First number: %d \n", first_number);
printf("Second number: %d \n", second_number);
return 0;
}

Output:

output 1 (swapping in c)

Explanation of the above program: To swap two numbers, first, we initialize two variables i.e. first_number and second_number. With these two numbers, a temporary variable named temp is also initialized to store a number temporarily. Then scan function allows the user to assigned numbers according to their wish. Then to swap numbers, we use the temp variable for storing numbers temporarily. First, we transfer first_number to temp variable and make first_number empty. As first_number is empty, we assigned second_number to the first number.  Like this second_number is transferred to first_number. In the end, as a second variable is now empty, we transfer first_number that is stored in the temp variable is assigned to second. Like this, we transferred first_number to second. This is how we swap two numbers using the temp variable.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)
Note: statement mentioned after // Is considered as a comment.
Comments are used to help people to understand the code easily.

Swap Three numbers in C

In this section, we are going to discussed how to swap 3 numbers in C language with the help of example and explanation.

Example: In the following C program, the user can enter 3 numbers he wishes to swap, then the result will be displayed on the screen. Program for swapping two numbers in C. Program for swapping three numbers in C is as follows:

Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int first_number, second_number, third_number, temp;
printf("Enter first number: "); //allow user to add first number
scanf("%d",&first_number);
printf("Enter second number: "); //allow user to add second number
scanf("%d",&second_number);
printf("Enter third number: "); //allow user to add third number
scanf("%d",&third_number);
printf("Before swapping \n");
printf("First number: %d \n", first_number);
printf("Second number: %d \n", second_number);
printf("Third number: %d \n", third_number);
temp = first_number;  //first number is assigned to temp
first_number = second_number; //second number is assigned to first number
second_number = third_number; //third number is assigned to second number
third_number = temp; //first number is assigned to third number
printf("After swapping \n");
printf("First number: %d \n", first_number);
printf("Second number: %d \n", second_number);
printf("Third number: %d \n", third_number);
return 0;
}

Output:

output2 (swapping in c)

Explanation of the above program: The concept for swapping three numbers is the same as two numbers only difference is exchanging numbers. To swap three numbers, first, we initialize three variables i.e. first_number, second_number, and third_number. With these three numbers, a temporary variable named temp is also initialized to store a number temporarily. Then scan allows the user to assigned numbers according to their wish. Then to swap numbers, we use the temp variable for storing numbers temporarily. First, we transfer the first number to the temp variable and make the first number empty. As the first number is empty, we assigned the second number to the first number and make the second number empty. This second number is transferred to the first number. As the second number is empty, we assigned the third number to the second number. Then this third number is transferred to the second number. In the end, as a third number is now empty, we transfer the first number that is stored in the temp variable is assigned to third. This is how we swap 3 numbers using the temp variable.

Recommended Articles

This is a guide to Swapping in C. Here we discuss how the use of C programming language could be swapped using two variables with appropriate code and output. You can also go through our other related articles to learn more –

  1. Patterns in C Programming
  2. Swapping in Java
  3. Swapping in C++
  4. C Programming Matrix Multiplication

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More