This page contains the collection of java interview programs with output that we have posted for beginners to learn java programming before the interview in the simple and easy way.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Scanner; class SwapNumberProgram { public static void main(String args[]) { int x, y, temp; System.out.println("Enter number x and y"); Scanner in = new Scanner(System.in); x = in.nextInt(); y = in.nextInt(); System.out.println("Before Swapping\nx = "+x+"\ny = "+y); temp = x; x = y; y = temp; System.out.println("After Swapping\nx = "+x+"\ny = "+y); } } |
Output:
Enter number x and y
10
20
Before Swapping
x value: 10
y value: 20
After Swapping
x value: 20
y value: 10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.*; class ArmstrongNumber { public static void main(String args[]) { int n, sum = 0, temp, r; Scanner in = new Scanner(System.in); System.out.println("Enter a number to check if it is an armstrong number:"); n = in.nextInt(); temp = n; while( temp != 0 ) { r = temp%10; sum = sum + r*r*r; temp = temp/10; } if ( n == sum ) System.out.println("Entered number is an armstrong number."); else System.out.println("Entered number is not an armstrong number."); } } |
Output:
Enter a number to check if it is an armstrong number:
153
Entered number is an armstrong number.
Enter a number to check if it is an armstrong number:
5
Entered number is not an armstrong number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class FibonacciNumber{ public static void main(String args[]) { int n1=0,n2=1,n3,i,count=10; System.out.print(n1+" "+n2);//printing 0 and 1 for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed { n3=n1+n2; System.out.print(" "+n3); n1=n2; n2=n3; } }} |
Output:
0 1 1 2 3 5 8 13 21 34
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Scanner; class SwapNumbers { public static void main(String args[]) { int x, y; System.out.println("Enter the number x and y:"); Scanner in = new Scanner(System.in); x = in.nextInt(); y = in.nextInt(); System.out.println("Before Swapping:\nx = "+x+"\ny = "+y); x = x + y; y = x - y; x = x - y; System.out.println("After Swapping:\nx = "+x+"\ny = "+y); } } |
1 2 3 4 5 6 7 8 9 |
import java.net.InetAddress; class IPAddress { public static void main(String args[]) throws Exception { System.out.println(InetAddress.getLocalHost()); } } |
Output:
javac IPAddress.java
java IPAddress
124.253.78.30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.util.Scanner; public class Demo { public static void main(String[] args) { int year; Scanner scan = new Scanner(System.in); System.out.println("Enter any Year:"); year = scan.nextInt(); scan.close(); boolean isLeap = false; if(year % 4 == 0) { if( year % 100 == 0) { if ( year % 400 == 0) isLeap = true; else isLeap = false; } else isLeap = true; } else { isLeap = false; } if(isLeap==true) System.out.println(year + " is a Leap Year."); else System.out.println(year + " is not a Leap Year."); } } |
Output:
Enter any Year:
2000
2000 is a Leap Year.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class CompareStringsProgram { public static void main(String[] args) { String style = new String("Bold"); String style2 = new String("Bold"); boolean result = style.equals("Bold"); System.out.println(result); result = style2 == "Bold"; System.out.println(result); result = style == style2; System.out.println(result); result = "Bold" == "Bold"; System.out.println(result); } } |
Output:
true
false
false
true
To increase your Java knowledge, practice all Java programs, here is a collection of 100+ Java problems with solutions.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.