0% found this document useful (0 votes)
1K views

Java Coding Interview Questions

The document contains 23 code snippets demonstrating various Java programming concepts and examples, including: 1) Calculating the factorial of a number 2) Reversing a number 3) Checking if a number is a palindrome 4) Finding Armstrong numbers 5) Printing Armstrong numbers between 0-1000 6) Printing palindromes between 0-100 7) Counting digits in a number 8) Finding the sum of digits in a number 9) Swapping two numbers with and without a third variable 10) Checking if a number is even or odd 11) Counting even and odd numbers between 1-100 12) Printing the Fibonacci series 13) Printing Fibonacci series terms up to

Uploaded by

Binod Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Java Coding Interview Questions

The document contains 23 code snippets demonstrating various Java programming concepts and examples, including: 1) Calculating the factorial of a number 2) Reversing a number 3) Checking if a number is a palindrome 4) Finding Armstrong numbers 5) Printing Armstrong numbers between 0-1000 6) Printing palindromes between 0-100 7) Counting digits in a number 8) Finding the sum of digits in a number 9) Swapping two numbers with and without a third variable 10) Checking if a number is even or odd 11) Counting even and odd numbers between 1-100 12) Printing the Fibonacci series 13) Printing Fibonacci series terms up to

Uploaded by

Binod Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

1.

Find the Factorial of the given number

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

2. Find the reverse of the number

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

3. Check whether the number is palindrome or not

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

4. Check whether the number is amstrong or not

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

7. Print the count of the given number

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

8. Find the Sum of the digit

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

9.Swap of two number using third variable

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

10.Swap of two variable without using third variable

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

11. To find even/odd number:


Program:
public class EvenOrOdd{
public static void main(String[] args) {
Scanner e = new Scanner(System.in);
System.out.println("Enter a Number");
int n = e.nextInt();
if (n % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
}
}
Output:
Enter a Number
23
Odd

12. Count of even and odd count


Program:
public class OddEvenCount{
public static void main(String[] args) {
int evencount = 0, oddCount=0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
evencount++;
}
else {
oddCount++;
}
}
System.out.println("Even count is "+evencount);
System.out.println("Odd count is "+oddCount); }
}

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

14. Print the value in Fibonacci series up to 100


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 = 1; i <= 10; i++) {
int c = a + b;
if(c<=100)
a = b;
b = c;
System.out.println(c);
}
}
}

Output:
0
1
1
2
3
5
8
13
21
34
55
89

15. Reverse the String


Program:
public class ReverseString{
public static void main(String args[]) {
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
original = in.nextLine();
int length = original.length();
for (int i = length - 1; i >= 0; i--)
reverse = reverse + original.charAt(i);
System.out.println("Reverse of entered string is: " + reverse);
}
}

Output:
Enter a string to reverse
nishathi
Reverse of entered string is: ihtahsin

16.To Check the String is palindrome or not.

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.

17.Count of each Character in the String


Program:
public class CountOfCharacter {
public static void main(String args[]) {
{
String s = "vengatram";
HashMap<Character, Integer> emp = new HashMap<Character,
Integer>();
char[] ch = s.toCharArray();
for (char c : ch) {
if (emp.containsKey(c)) {
int x = emp.get(c);
emp.put(c, x + 1);
} else {
emp.put(c, 1);
}
}
System.out.println(emp);
}
}
}
Output:
{a=2, r=1, t=1, e=1, v=1, g=1, m=1, n=1}
18.Count of each Word
Program:
public class CountOfWord{
public static void main(String args[]) {
{
String s = "vengat ram";
String[] s1 = s.split(" ");
HashMap<String, Integer> emp = new HashMap<String,
Integer>();
for (String c : s1) {
if (emp.containsKey(c)) {
int x = emp.get(c);
emp.put(c, x + 1);
} else {
emp.put(c, 1);
}
}
System.out.println(emp);
}}
}
Output:
{vengat=1, ram=1}

19. Print the numbers in ascending order


Program:
public class AscendingOrder {
public static void main(String[] args)
{
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the numbers:");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Ascending Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}
Output:
Enter no. of elements you want in array: 10
Enter all the numbers:
20
30
40
50
60
70
80
90
100
120
Ascending Order:20,30,40,50,60,70,80,90,100,120

20.Print the numbers in descending order


Program:
public class DescendingOrder{
public static void main(String[] args) {
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Descending Order:");
for (int i = n - 1; i > 0; i--) {
System.out.print(a[i] + ",");
}
System.out.print(a[0]);
}
}
Output:
Enter no. of elements you want in array: 5
Enter all the elements:
90
50
35
48
12
Descending Order:90,50,48,35,12

21.Print Triangle with Stars


Program:
public class Triangle{
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5 - i; j++) {
System.out.print("* ");
}
for (int k = 1; k <= i; k++) {
System.out.print("* ");
}
System.out.println(" ");}}}
Output:
*
**
***
****
*****

22. Assume the string is he,xa,wa,re and give the output as


hexaware
Program:
public class Replace {
public static void main(String[] args) {
String s="he,xa,wa,re";
String x = s.replace(",", "");
System.out.println(x);
}
}
Output:
Hexaware

23.Find the special character, uppercase, lowercase, Number of


digits in the given string
Program:
public class CharCount{
public static void main(String[] args) {
String s = "Hi Welcome To Java Classes Tommorow At 2.00
p.m!!";
int count = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
count++;
} else if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') {
count1++;
} else if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
count2++;
} else {
count3++;
}
}
System.out.println("total no of small letters: " + count);
System.out.println("total no of capital letters: " + count1);
System.out.println("total no of digits: " + count2);
System.out.println("total no of special characters: " + count3);
}
}
Output:
total no of small letters: 27
total no of capital letters: 7
total no of digits : 3
total no of special characters: 12

24. Print Reverse triangle without Space


Program:
public class ReverseTriangle{
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print("* ");
}
System.out.println();
}
}
}
Output:
*****
****
***
**
*

25 . Check Whether the given number is prime or not


Program:
public class Prime{
public static void main(String[] args) {
int n;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number");
n = input.nextInt();
int count = 0;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
count = 1;
}
}
if (count == 0) {
System.out.println("It is a prime number");
} else {
System.out.println("It is not a prime number");
}
}
}
Output:
Enter the number
17
It is a prime number

26. Print the prime numbers counts available between 1 to 100


Program:
public class PrimeNumberCount{
public static void main(String[] args) {
int count, c = 0;
for (int i = 1; i <= 100; i++) {
count = 0;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
count++;
}
}
if (count == 0) {
c++;
}
}
System.out.println(c);
}
}
Output:
26

27. Multiplication of the given number


Program:
public class Multiplication{
public static void main(String[] args) {
int n, j;
Scanner mt = new Scanner(System.in);
System.out.println("Enter the Table");
n = mt.nextInt();
System.out.println("Table upto");
j = mt.nextInt();
for (int i = 1; i <= j; i++) {
int c = n * i;
System.out.println(i + "*" + n + "=" + c);
}
}}
Output:
Enter the Table
7
Table upto
10
1*7=7
2*7=14
3*7=21
4*7=28
5*7=35
6*7=42
7*7=49
8*7=56
9*7=63
10*7=70

28. Biggest of 4 number


Program:
public class BiggestNumber{
public static void main(String[] args) {
int a, b, c, d;
Scanner bn = new Scanner(System.in);
System.out.println("The four numbers are");
a = bn.nextInt();
b = bn.nextInt();
c = bn.nextInt();
d = bn.nextInt();
if (a > b && a > c && a > d) {
System.out.println("The biggest number is= " + a);
} else if (b > a && b > c && b > d) {
System.out.println("The biggest number is= " + b);
} else if (c > a && c > b && c > d) {
System.out.println("The biggest number is= " + c);
} else {
System.out.println("The biggest number is= " + d);
}
}
}
Output:
The four numbers are
10
20
30
40
The biggest number is=40

29. Find the 3


rd
maximum Number in an given array
Program:
public class ThirdMax{
public static void main(String[] args) {
int a[]={-12,45,-23,64,-100,24};
for(int i=0;i<a.length;i++){
for(int j=i+1;j<a.length;j++){
int temp=0;
if(a[i]<a[j]){
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
for(int k=0;k<a.length;k++){
System.out.println(a[k]);
}
System.out.println("The Third maximum number is " +
a[a.length4]);
}
}
Output:
64
45
24
-12
-23
-100
The Third maximum number is 24

30. Separate reverse of each word in the string


Program:
public class Reverse{
public static void main(String[] args)
{
String name = "Greens Tech";
String [] s =name.split(" ");
String res = "";
for(int i=0;i<s.length;i++)
{
String t = s[i];
for(int j=t.length()-1;j>=0;j--)
{
char ch=t.charAt(j);
res=res+ch;
}
res=res+ " ";
}
System.out.println(res);
}
}
Output:
sneerG hceT
31. Number triangle
Program:
public class Welcome {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5 - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print(i+" ");
}
System.out.println(" ");
}
}
}
Output:
1
22
333
4444
55555

32. Find the duplicate count in an array


Program:
public class ArrayDuplicate {
public static void main(String[] args)
{
int n, count=0;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array: ");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the numbers: ");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if(a[i]==a[j]) {
count++;
}
}}
System.out.println(count);
}
}
Output:
Enter no. of elements you want in array: 5
Enter all the numbers:
10
20
10
30
10
3

33.Find the duplicate count in the string


Program:
public class ListDuplicate {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("b");
list.add("c");
list.add("a");
list.add("a");
list.add("a");
System.out.println("Count all with frequency");
Set<String> uniqueSet = new HashSet<String>(list);
for (String temp : uniqueSet) {
System.out.println(temp + ": " +
Collections.frequency(list, temp));
}
}
}
Output:
Count all with frequency
a: 4
b: 2
c: 2
d: 1

34.Count of the palindrome number


Program:
public class PalindromeCount{
public static void main(String[] args) {
int c = 0;
for (int n = 1; n <= 1000; 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) {
c++;
}
}
System.out.println(c);
}
}
Output:
106

35. Count of the amstrong number


Program:
public class AmstrongCount {
public static void main(String[] args) {
int c = 0;
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) {
c++;
}
}
System.out.println(c);
}
}
Output:
5

36.Construct the triangle pyramid


Program:
public class TrianglePyramid{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("How Many Rows You Want In Your Pyramid?");
int noOfRows = sc.nextInt();
int rowCount = 1;
System.out.println("Here Is Your Pyramid");
for (int i = noOfRows; i >= 1; i--)
{
//Printing i*2 spaces at the beginning of each row
for (int j = 1; j <= i*2; j++)
{
System.out.print(" ");
}
//Printing j where j value will be from i to noOfRows
for (int j = i; j <= noOfRows; j++)
{
System.out.print(j+" ");
}
for (int j = noOfRows-1; j >= i; j--)
{
System.out.print(j+" ");}
System.out.println();
//Incrementing the rowCount
rowCount++;
}
}
}
Output:
How Many Rows You Want In Your Pyramid?
5
Here Is Your Pyramid
5
454
34543
2345432
123454321

37. Count of vowels and non vowels


Program:
public class VowelsCount {
public static void main(String[] args) {
String a = "welcome";
int vowels = 0;
int nonVowels = 0;
for (int i = 0; i < a.length(); i++) {
char ch = a.charAt(i);
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i'
|| ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u'
|| ch == 'U') {
vowels++;
} else {
nonVowels++;
}
}
System.out.println("Count of vowels is "+vowels);
System.out.println("Count of Non Vowels is "+nonVowels);
}
}
Output:
Count of vowels is 3
Count of Non Vowels is 4

37.Remove duplicates from stored array


Program:
public class RemoveDuplicate {
public static int[] removeDuplicates(int[] input){
int j = 0;
int i = 1;
//return if the array length is less than 2
if(input.length < 2){
return input;
}
while(i < input.length){
if(input[i] == input[j]){
i++;
}else{
input[++j] = input[i++];
}
}
int[] output = new int[j+1];
for(int k=0; k<output.length; k++){
output[k] = input[k];
}
return output;
}
public static void main(String a[]){
int[] input1 = {2,3,6,6,8,9,10,10,10,12,12};
int[] output = removeDuplicates(input1);
for(int i:output){
System.out.print(i+" ");
}
}
}
Output:
{2 3 6 8 9 10 12}

38.Sum of the odd and even number


Program:
public class SumOfOdd{
public static void main(String[] args) {
int oddCount = 0,evenCount=0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 1) {
oddCount= oddCount + i;
}
else {
evenCount=evenCount+i;
}
}
System.out.println("Count of odd number is "+oddCount);
System.out.println("Count of even number is "+evenCount);
}
}
Output:
Count of odd number is 2500
Count of even number is 2550

39.Count of Uppercase, lowercase, digits, special character


Program:
public class Test {
public static void main(String[] args) {
int lCaseCount = 0, uCaseCount = 0, numbersCount = 0,
sCharCount = 0;
String s = "Welcome To JAVA Clas @ 12345";
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (Character.isLowerCase(ch)) {
lCaseCount++;
} else if (Character.isUpperCase(ch)) {
uCaseCount++;
} else if (Character.isDigit(ch)) {
numbersCount++;
} else {
sCharCount++;
}
}
System.out.println("Upper Case Count: " + uCaseCount);
System.out.println("Lower Case Count: " + lCaseCount);
System.out.println("Numbers Count: " + numbersCount);
System.out.println("Special Characters Count: " + sCharCount);
}
}
Output:
Upper Case Count: 7
Lower Case Count: 10
Numbers Count: 5
Special Characters Count: 6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy