swapping of two number.
algorithm for swapping of two number.
- first take two input from user as (a ,b)
- then create one temporary variable as (temp)
- and then store the value of a into temporary variable as (temp=a) so that value of a not change
- and then assign the value as (a=b; and b=temp)
- 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
Post a Comment