swapping of two number in java with algorithm.

 swapping of two number.

  algorithm for swapping of two number.

  1. first take two input from user as (a ,b)
  2. then create one temporary variable as (temp)
  3. and then store the value of a into temporary variable as (temp=a) so that value of a not change
  4. and then assign the value as (a=b; and b=temp)
  5. and lastly print the value of a and b

  program in java   

//swapping of two number
package com.company;

import java.util.Scanner;
import java.io.*;

public class Main {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter two number ");
int a,b;
int temp; // here we use temporary varaiable to store data
System.out.println("first : ");
a= input.nextInt();
System.out.println("second : ");
b= input.nextInt();
temp=a;
a=b;
b=temp;
System.out.println("After swapping ");
System.out.println("a = "+a);
System.out.println("b = "+temp);
}
}
Enter two number 
first : 
23
second : 
66
After swapping 
a = 66
b = 23

 




Comments

Popular posts from this blog

Phone Dictionary Application in c

Dollar to Rupee and Rupee to Dollar app .