swapping of two numbers with using third variable in c

There are many methods for swapping two numbers but this is the simplest method for swap the number with the third variable. Beginners can understand this tutorial very easily. We are creating simple Swapping of two numbers with a third variable.

Program

#include<stdio.h>
 int main() 
{ 
int x,y,z; printf("n Enter first number for x :");
 scanf("%d",&amp;x);
 printf("n Enter second number for y :n"); 
scanf("%d",&amp;y);
 z=x; x=y; y=z;
 printf("n After Swapping the value of x is :%d", x);
 printf("n After Swapping the value of y is :%d", y);
 return 0; }

Output:-
Enter first number for x :22 Enter second number for y :33 After Swapping the value of x is :33 After Swapping the value of y is :22

Explanation of Program :

In this program, three variables are created here x,y and z. The value initialized into only two variables x & y.
We can understand by given below pattern.
x=10 and target is x=20
y=20 y=10
z = x    x value copy into z is 10
x = y    y value copy into x is 20
y = z    z value copy into y is 10

This article is written by our awesome writer
Comments to: Swapping of two numbers In C

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password