Today we are learning about how to swapping two number using java

1.I put the value of a in temp;
temp=a;
2.now a is empty so i put the value of b on a;
a=b;
3.now a is full with the value of b;
4.and b is empty so i put the value of temp in b
5.first we have three container temp, a,b;
package com.ritesh;
import java.util.Scanner;
public class swapping {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value for a");
byte a= sc.nextByte();
System.out.println("Enter the value for b");
byte b= sc.nextByte();
System.out.println("The value of a before swapping is "+a);
System.out.println("The value of b before swapping is "+b);
byte temp;
//first we have three container temp, a,b;
//I put the value of a in temp;
temp=a;
//now a is empty so i put the value of b on a;
a=b;
//now a is full with the value of b;
// and b is empty so i put the value of temp in b
b= temp;
System.out.println();
System.out.println("The value of a after swapping is "+a);
System.out.println("The value of b after swapping is "+b);
//we successfully change the value of a and b in each other.
}
}
Comments
Post a Comment