Java Coding Interview Questions
Java Coding Interview Questions
Program:
public class Factorial {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
int factorial=1;
for(int i=1;i<=n;i++)
{
factorial= factorial*i;
}
System.out.println(factorial);
}
}
Output:
Enter the number
5
120
Program:
public class ReverseTheNumber {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
int a,i=0,j=0;
a=n;
while(a>0) {
i=a%10;
j=(j*10)+i;
a=a/10;
}
System.out.println("The reverse number is "+j);
}
}
Output:
Enter the number
12345
The reverse number is 54321
Program:
public class Palindrome {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
int a,i=0,j=0;
a=n;
while(a>0) {
i=a%10;
j=(j*10)+i;
a=a/10;
}
if(n==j) {
System.out.println("It is panlidrome");
}
else {
System.out.println("It is not a panlindrome");
}
}
}
Output:
Enter the number
11
It is panlidrome
Program:
public class Amstrong{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
int a,i=0,j=0;
a=n;
while(a>0) {
i=a%10;
j=(i*i*i)+j;
a=a/10;
}
if(n==j) {
System.out.println("It is amstrong");
}
else {
System.out.println("It is not a amstrong");
}
}
}
Output:
Enter the number
153
It is amstrong
5. Print the amstrong number available between 0 to 1000
Program:
public class Amstrong{
public static void main(String[] args) {
for (int n = 1; n <= 1000; n++) {
int a, i = 0, j = 0;
a = n;
while (a > 0) {
i = a % 10;
j = j + (i * i * i);
a = a / 10;
}
if (n == j) {
System.out.println(n);
}
}
}
}
Output:
1
153
370
371
407
6. To print the palindrome available between 0 to 100
Program:
public class Palindrome {
public static void main(String[] args) {
for (int n = 1; n <= 100; n++) {
int a, i = 0, j = 0;
a = n;
while (a > 0) {
i = a % 10;
j = (j * 10) + i;
a = a / 10;
}
if (n == j) {
System.out.println(n);
}
}
}
Output:
1
2
3
4
5
6
7
8
9
11
22
33
44
55
66
77
88
99
Program:
public class CountOfNumber{
public static void main(String[] args) {
int n,i=0;
System.out.println("Enter a number");
Scanner get=new Scanner(System.in);
n=get.nextInt();
while(n>0)
{
n=n/10;
i++;
}
System.out.println("No of digits present: "+i);
}
}
Output:
Enter a number
12345
No of digits present: 5
Program:
public class SumOfDigits{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
int a,i=0,j=0;
a=n;
while(a>0) {
i=a%10;
j=j+i;
a=a/10;
}
System.out.println("Sum of the digits "+j);
}
}
Output:
Enter the number
123
Sum of the digits 6
Program:
public class Swap{
public static void main(String[] args) {
int a, b, c;
Scanner sw = new Scanner(System.in);
System.out.println("The numbers are");
a = sw.nextInt();
b = sw.nextInt();
c = a;
a = b;
b = c;
System.out.println("Swapping numbers are");
System.out.println(a);
System.out.println(b);
}
}
Output:
The numbers are
12
24
Swapping numbers are
24
12
Program:
public class SwapTwoNumber{
public static void main(String[] args) {
int a, b;
Scanner sw = new Scanner(System.in);
System.out.println("The numbers are");
a = sw.nextInt();
b = sw.nextInt();
a = a + b;
b = a - b;
a = a - b;
System.out.println("Swapping numbers are");
System.out.println(a);
System.out.println(b);
}
}
Output:
The numbers are
12
24
Swapping numbers are
24
12
Output:
Even count is 50
Odd count is 50
13. Fibonacci series:
Program:
public class Fibonacci {
public static void main(String[] args) {
int a = 0, b = 1;
System.out.println(a);
System.out.println(b);
for (int i = 2; i <= 10; i++) {
int c = a + b;
System.out.println(c);
a = b;
b = c;
}
}
}
Output:
0
1
1
2
3
5
8
13
21
34
55
Output:
0
1
1
2
3
5
8
13
21
34
55
89
Output:
Enter a string to reverse
nishathi
Reverse of entered string is: ihtahsin
Program:
public class Palindrome {
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}
Output:
Enter a string to check if it is a palindrome
madam
Entered string is a palindrome.