Spread the word.

Share the link on social media.

Share
  • Facebook
Have an account? Sign In Now

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

SIKSHAPATH Latest Questions

Pavanagg
  • 2
  • 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
  • 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
  • 3k Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    Related Questions

    • Write a program in NetBeans IDE 8.2 for NASA to determine if the weather conditions are favorable for the launch ...
    • Which of the following best defines a variable in programming?
    • 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++?

    3 Answers

    • Voted
    • Oldest
    • Recent
    1. ADMIN 2
      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 on Facebook
        • Share on Twitter
        • Share on LinkedIn
    2. ADMIN 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 on Facebook
        • Share on Twitter
        • Share on LinkedIn
    3. ADMIN 2
      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 on Facebook
        • 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 150
    • Best Answers 89
    • Cloud Computing Notes 101: Learn Fundamentals, Service Models, and Virtualization!
    • This Free AI Tool Translates Entire Books in Minute !
    • AI News: 🎬 Hollywood’s AI Studios, 🎓 OpenAI’s Latest Gift to Educators, 🚚 Class8 Bags $22M, 🧠 Google Gemini’s Memory Upgrade
    • AI NEWS: Legal Action Against OpenAI, $16M Paid, & Elon Musk’s Praise from Investor 🤖💰📑 | AI Boosts Cloud Seeding for Water Security 🌱💧
    • AI News: 🎬AI Video Tool Scam Exposed🤯, 🛰️ AI-Powered Drones to Ukraine 😱, Google’s $20M AI Push, Sam Altman Joins SF’s Leadership Team

    Explore

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

    Footer

    SIKSHAPATH

    Helpful Links

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

    Follow Us

    © 2021-24 Sikshapath. All Rights Reserved

    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.