How to Swap to values of varriable

Swapping values of variable

Steps to do this :-
1. take input 3 integers a,b,c;
2.take two value from user and store it in a,b respectively
3.now put c=a // here c has now a's value
a=b // now a has b's value
b=c //now b has a's value
4. print a and b
5.save and run
The Source code
 import java.util.Scanner;
class swap
{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int a,b,c;// a is for 1st value b is for 2nd value c is for storing one value
        System.out.println("enter the first number");
        a=s.nextInt();
        System.out.println("enter the second number");
        b=s.nextInt();

        c=a;//now c has a value
        a=b;// a has b value
        b=c;// b has a value
        System.out.println("now the 1st value is : "+a );
        System.out.println("now the second value is : "+b);
    }
}

OUTPUT:-

WATCH MY VIDEO

 

Comments

Popular Posts