Skip to content

Commit 0f12eea

Browse files
committed
Added interview coding solutions - under #1
1 parent 117c1b8 commit 0f12eea

9 files changed

+257
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
3+
Q: Write a java program to check whether a given number is Armstrong number or not.
4+
Explainaion(case - sfor 3 digits number): An Armstrong number is a number whose sum of cubes of its digits is equal to the number itself.
5+
6+
Example for Armstrong number(3 digit numbers): 153, 370, 371, 407.
7+
8+
*/
9+
10+
import java.util.Scanner;
11+
public class ArmstrongNumber {
12+
//Method to check if the given number is armstrong number or not
13+
static void checkArmstrong(int num) {
14+
int temp = num;
15+
int sum = 0;
16+
int digit;
17+
while(num != 0) {
18+
digit = num % 10;
19+
sum += (digit * digit * digit);
20+
num = num /10;
21+
}
22+
if(temp == sum)
23+
System.out.println( temp + " is an Armstrong number");
24+
else
25+
System.out.println(temp + " is not an Armstrong number");
26+
}
27+
// Main method
28+
public static void main(String[] args) {
29+
Scanner scan = new Scanner(System.in);
30+
System.out.println("Enter a number:");
31+
int num = scan.nextInt();
32+
checkArmstrong(num);
33+
}
34+
}

Interview Coding Questions and Solutions/CheckLeapYear.java

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
Q: Write a program to check if the year is leap or not.
44
Exaplaintation: Leap year is a year which is divisible by 4 but not by 100, if the year is divisible by 400 then it is a leap year.
5+
Sample Input: 2016
6+
Sample Output: Leap Year
57
68
*/
79

Interview Coding Questions and Solutions/CheckPrime.java

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
Q: Write a java program to check if the number is prime or not.
44
Explaination: Prime number is a number which is divisible only by itself and 1.
5+
Sample Input: 5
6+
Sample Output: 5 is a prime number.
57
68
*/
79

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
3+
Q: Write a java program to find the factorial of a number using recursion.
4+
Sample Input: 5
5+
Sample Output: 120
6+
7+
*/
8+
9+
import java.util.Scanner;
10+
public class Factorial {
11+
// Method to find the recursion of the number using recursion
12+
static int findFactorial(int n) {
13+
if(n==0)
14+
return 1;
15+
else
16+
return (n*findFactorial(n-1));
17+
}
18+
// Main method
19+
public static void main(String[] args) {
20+
Scanner scan = new Scanner(System.in);
21+
System.out.println("Enter a number: ");
22+
int num = scan.nextInt();
23+
int fact = findFactorial(num);
24+
System.out.println("Factorial of " + num + " is " + fact);
25+
scan.close();
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
3+
Q: Write a java program to print the Fibonacci series upto n terms.
4+
Explanation: The Fibonacci series is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it.
5+
Sample Input: 5
6+
Sample Output: 0 1 1 2 3 5
7+
8+
*/
9+
10+
import java.util.Scanner;
11+
public class Demo {
12+
// Method to print the Armstrong numbers in the given range
13+
static void printFibonacciSeries(int limit) {
14+
if(limit > 0) {
15+
int first = 0, second = 1;
16+
System.out.println("Fibonacci series upto " + limit + " are:");
17+
System.out.print(first + " " + second);
18+
int sum;
19+
while(second <= limit) {
20+
sum = first + second;
21+
first = second;
22+
second = sum;
23+
// Condition to check the number to printed must be less than limit
24+
if(sum <= limit)
25+
System.out.print(" " + sum);
26+
}
27+
}
28+
else {
29+
System.out.println("Wrong input...! Try again");
30+
}
31+
}
32+
// Main method
33+
public static void main(String[] args) {
34+
Scanner scan = new Scanner(System.in);
35+
System.out.println("Enter the limit:");
36+
int limit = scan.nextInt();
37+
printFibonacciSeries(limit);
38+
scan.close();
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
3+
Q: Write a java program to check if a given string is a palindrome or not.
4+
5+
Sample Input: malayalam
6+
Sample Output: Entered string is a palindrome.
7+
8+
*/
9+
10+
import java.util.Scanner;
11+
12+
public class Palindrome {
13+
//Method to check if the given string is palindrome or not
14+
static void checkPalindrome(String str) {
15+
String revstr = "";
16+
// Loop for reversing the string
17+
for(int i = str.length()-1; i >= 0; i--) {
18+
revstr += str.charAt(i);
19+
}
20+
// Condition checking if the reversed string is equals to main string or not
21+
if(str.equalsIgnoreCase(revstr)) {
22+
System.out.println("Entered string is a palindrome");
23+
}
24+
else {
25+
System.out.println("Entered string is not a palindrome");
26+
}
27+
}
28+
// Main method
29+
public static void main(String[] args) {
30+
Scanner scan = new Scanner(System.in);
31+
System.out.println("Enter a string: ");
32+
String str = scan.next();
33+
checkPalindrome(str);
34+
scan.close();
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
3+
Q: Write a java program to print all the Armstrong numbers between given range.
4+
Explainaion: An Armstrong number is an integer such that the sum of the respective power of its digits is equal to the number itself.
5+
Sample Input: 1, 999.
6+
Sample Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407.
7+
8+
*/
9+
10+
import java.util.Scanner;
11+
12+
public class PrintArmstrongNumbers {
13+
//Method to print the Armstrong number in the given range
14+
static void printArmstrongNumber(int low, int high) {
15+
System.out.println("Armstrong numbers in the range of " + low + " and " + high);
16+
// Looping through the numbers in the given range
17+
for(int num = low; num <= high; num++) {
18+
int temp = num;
19+
int digitCount = 0;
20+
int digit;
21+
int sum = 0;
22+
// Looping for counting the number of digits
23+
while(temp != 0) {
24+
temp = temp / 10;
25+
digitCount++;
26+
}
27+
// Resetting the temp
28+
temp = num;
29+
// Finding the sum of the respective power of the digits in the number
30+
while(temp != 0) {
31+
digit = temp % 10;
32+
sum = (int) (sum + Math.pow(digit, digitCount));
33+
temp = temp /10;
34+
}
35+
// Checking and printing only if the number is Armstrong number
36+
if(num == sum)
37+
System.out.print(num + " ");
38+
}
39+
}
40+
41+
// Main method
42+
public static void main(String[] args) {
43+
Scanner scan = new Scanner(System.in);
44+
System.out.println("Enter a starting number:");
45+
int start = scan.nextInt();
46+
System.out.println("Enter a ending number:");
47+
int end = scan.nextInt();
48+
printArmstrongNumber(start, end);
49+
scan.close();
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
3+
Q: Write a java program to reverse a number.
4+
5+
Sample Input 1: 12345
6+
Sample Output 1: 54321
7+
8+
Sample Input 3: -12345
9+
Sample Output 3: -54321
10+
11+
*/
12+
13+
import java.util.Scanner;
14+
15+
public class ReverseNumber {
16+
//Method to reverse the given number
17+
static void reverseNumber(int num) {
18+
int digit;
19+
int rev = 0;
20+
while(num != 0) {
21+
digit = num%10;
22+
rev = rev*10 + digit;
23+
num = num/10;
24+
}
25+
System.out.println("Reversed number:");
26+
System.out.println(rev);
27+
}
28+
// Main method
29+
public static void main(String[] args) {
30+
Scanner scan = new Scanner(System.in);
31+
System.out.println("Enter a number: ");
32+
int n = scan.nextInt();
33+
reverseNumber(n);
34+
scan.close();
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
3+
Q: Write a java program to find the sum of digits of a number.
4+
Sample Input: 12345
5+
Sample Output: 15
6+
7+
*/
8+
9+
import java.util.Scanner;
10+
public class SumOfDigits {
11+
//Method to find the sum of the digits of the number
12+
static void findDigitSum(int num) {
13+
int digit, sum = 0;
14+
while(num > 0) {
15+
digit = num%10;
16+
sum += digit;
17+
num = num/10;
18+
}
19+
System.out.println(sum);
20+
}
21+
// Main method
22+
public static void main(String[] args) {
23+
Scanner scan = new Scanner(System.in);
24+
System.out.println("Enter a number: ");
25+
int n = scan.nextInt();
26+
findDigitSum(n);
27+
scan.close();
28+
}
29+
}

0 commit comments

Comments
 (0)