In the previous blog we saw how to find the ASCII value of the character .Now, we will see how to swap two numbers using a variable.
Basic Data Type:- int, char, float, double
int Datatype:-Its an integer datatype .Its of 2 bytes .Its range is −32,768 to 32,767.
char Datatype:-Its an character datatype. Its of 1 byte. Its range is -128 to 127 .
float Datatype:-Its for floating point numbers. Its of 4 byte.
double Datatype :-Its for large floating point numbers .Its of 8 byte
To swap two numbers using a variable :-
import java.io.*;
import java.util.*;
public class swap{
public static void main (String [] args)
{
int number1,number2,temp;
System.out.println("Enter two numbers:");
Scanner sc =new Scanner(System.in);
number1=sc.nextInt();
number2=sc.nextInt();
System.out.println("Before swapping "+number1+" "+number2);
temp=number1;
number1=number2;
number2=temp;
System.out.print("After swapping "+number1+" "+number2);
}
}
0 Comments