Verify a number is Even/Odd and Swapping Numbers without using 3rd variable

import java.util.Scanner;
public class EvenOdd{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter a number which you want to check whether that is even or odd");
int n = in.nextInt();
if(n%2==0){
System.out.println(n+" is an even number.");
}else{
System.out.println(n+" is an odd number.");
}
}
}

Output:
Enter a number which you want to check whether that is even or odd
4
4 is an even number.

  

Odd and Swapping Numbers without using 3rd variable


import java.util.Scanner;
public class Swapping{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter the 1st number: ");
int x = in.nextInt();
System.out.println("Enter the 2nd number: ");
int y = in.nextInt();
System.out.println("Initial value of x: "+x+" and y: "+y);
x = x+y;
y = x-y;
x = x-y;
System.out.println("After swapping value of x: "+x+" and y: "+y);
}
}

Output:

Enter the 1st number:
43
Enter the 2nd number:
56
Initial value of x: 43 and y: 56
After swapping value of x: 56 and y: 43

No comments:

Post a Comment

Floyd Triangle

Note- Floyd Triangle is like 1 2 3 4 5 6 7 8 9 10 ------------ import java.util.Scanner; public class FloydTria...

Popular Post