Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

SIKSHAPATH

SIKSHAPATH Navigation

  • Home
  • Questions
  • Blog
    • Computer Science(CSE)
    • NPTEL
    • Startup
  • Shop
    • Internshala Answers
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Questions
  • Blog
    • Computer Science(CSE)
    • NPTEL
    • Startup
  • Shop
    • Internshala Answers
Home/Questions/Q 4113
Answered
Pavanagg
  • 2
Pavanagg
Asked: April 29, 20232023-04-29T17:21:00+05:30 2023-04-29T17:21:00+05:30In: Programming Language

How to Swap Two Numbers in C without Using Third Variable: 3 Simple Methods

  • 2

How to Swap Two Numbers in C without Using Third Variable.

Follow these 3 methods :

Write C program to swap two numbers using 

  • + and – operators.
  • * and / operators.
  • XOR operator.

Test Case: a=10 and b=40

c languageswapping of two numbers
  • 3 3 Answers
  • 563 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    Related Questions

    • Employee Management System
    • Shoprite is a grocery shop that has outlets nationwide. They give discounts to customers ...
    • What is a set of instructions used to perform some tasks?
    • functions can return enumeration constants in c true or false
    • Which of the following is NOT a container adapter in C++?
    • Which of the following is a user-defined data type in c language?
    • Create a student class object array of size four having ...

    3 Answers

    • Voted
    • Oldest
    • Recent
    1. Best Answer
      ADMIN 2
      2021-11-13T02:09:45+05:30Added an answer on November 13, 2021 at 2:09 am
      This answer was edited.

      C program using + and – operators to swap two numbers without using a third variable

      #include<stdio.h>
      int main()
      {
      int a = 10, b = 40;
      printf("Before swap: a = %d ; b = %d\n", a, b);
      a = a + b; // a = 50
      b = a - b; // b = 10
      a = a - b; // a = 40
      printf("After swap: a = %d ; b = %d\n", a, b);
      return 0;
      }
      • 1
      • Reply
      • Share
        Share
        • Share on WhatsApp
        • Share onFacebook
        • Share on Twitter
        • Share on LinkedIn
    2. ADMIN 2
      2023-04-29T14:04:39+05:30Added an answer on April 29, 2023 at 2:04 pm
      This answer was edited.

      C program using * and / operators to swap two numbers without using a third variable

      #include<stdio.h>
      int main()
      {
      int a = 10, b = 40;
      printf("Before swap: a = %d ; b = %d\n", a, b);
      a = a * b; // a = 400
      b = a / b; // b = 10
      a = a / b; // a = 40
      printf("After swap: a = %d ; b = %d\n", a, b);
      return 0;
      }
      • 0
      • Reply
      • Share
        Share
        • Share on WhatsApp
        • Share onFacebook
        • Share on Twitter
        • Share on LinkedIn
    3. ADMIN 2
      2023-04-29T14:36:14+05:30Added an answer on April 29, 2023 at 2:36 pm
      This answer was edited.

      How to swap two numbers in C without a third variable using XOR operator

      • XOR operator is a bitwise operation that returns 1 if the bits are different and 0 if they are the same
      • It can be used to swap two numbers by toggling their bits without using a third variable
      • For example, if a = 10 and b = 40, then their binary representations are: a = 1010 b = 101000
      • Applying XOR operator to both numbers gives:
        a = a ^ b // a = 100010 b = a ^ b // b = 1010 a = a ^ b // a = 101000
      • Converting back to decimal gives:
        a = 34 b = 10 a = 40
      • Now, a and b have been swapped

      Swapping Code:

      #include<stdio.h>
      int main()
      {
      int a = 10, b = 40;
      printf("Before swap: a = %d ; b = %d\n", a, b);
      a = a ^ b; // a = 34
      b = a ^ b; // b = 10
      a = a ^ b; // a = 40
      printf("After swap: a = %d ; b = %d\n", a, b);
      return 0;
      }


      Pros and cons of using XOR operator to swap two numbers in C

      XOR operator is a bitwise operation that returns 1 if the bits are different and 0 if they are the same.

      It can be used to swap two numbers by toggling their bits without using a third variable.

      Here are some of the benefits and drawbacks of this technique:

      Benefits

      1. It saves memory and improves performance by avoiding a third variable.]
      2. It works for both integers and floating-point numbers, unlike arithmetic operators like + and – or * and /.
      3. It avoids overflow or underflow issues that may occur with arithmetic operators.

      Drawbacks

      1. It is not very easy to understand or follow, unlike arithmetic operators.
      2. It may fail for some edge cases, such as when both numbers are zero or when both numbers are equal.
      3. It may not be compatible with some compilers or platforms, unlike arithmetic operators.
      • 0
      • Reply
      • Share
        Share
        • Share on WhatsApp
        • Share onFacebook
        • Share on Twitter
        • Share on LinkedIn

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    Sidebar

    store ads

    Stats

    • Questions 1k
    • Answers 1k
    • Posts 127
    • Best Answers 66
    • LINEAR PROGRAMMING AND OPTIMIZATION TECHNIQUES NOTES
    • What is the Difference Between Section, Article and Aside in HTML5?
    • NPTEL Cloud Computing Assignment 3 Answers 2023
    • NPTEL Ethical Hacking Assignment 3 Answers 2023
    • NPTEL Problem Solving Through Programming In C Week 1 & 2 Assignment Answers 2023

    Explore

    • Recent Questions
    • Questions For You
    • Answers With Time
    • Most Visited
    • New Questions
    • Recent Questions With Time

    Footer

    Sikshapath Logo

    Helpful Links

    • Contact
    • Disclaimer
    • Privacy Policy Notice
    • TERMS OF USE
    • FAQs
    • Refund/Cancellation Policy
    • Delivery Policy for Sikshapath

    Follow Us

    © 2021-23 Sikshapath. All Rights Reserved

    Ads Blocker Image Powered by Code Help Pro

    Ads Blocker Detected!!!

    We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

    Refresh

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.