File tree 3 files changed +100
-0
lines changed
Interview Coding Questions and Solutions
3 files changed +100
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+
3
+ Q: Write a program to check if the year is leap or not.
4
+ 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
+
6
+ */
7
+
8
+ import java .util .Scanner ;
9
+ public class CheckLeapYear {
10
+ // Method to check the year is leap or not
11
+ static void checkLeap (int year ) {
12
+ // Condition to check the year is leap or not
13
+ if ((year %4 == 0 && year %100 !=0 ) || year %400 == 0 )
14
+ System .out .println ("Leap Year" );
15
+ else
16
+ System .out .println ("Not a Leap Year" );
17
+ }
18
+ // Main method
19
+ public static void main (String [] args ) {
20
+ Scanner scan = new Scanner (System .in );
21
+ System .out .println ("Enter the year:" );
22
+ int year = scan .nextInt ();
23
+ checkLeap (year );
24
+ scan .close ();
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+
3
+ Q: Write a java program to check if the number is prime or not.
4
+ Explaination: Prime number is a number which is divisible only by itself and 1.
5
+
6
+ */
7
+
8
+ import java .util .Scanner ;
9
+ public class CheckPrime {
10
+ // Method to check the number is prime or not
11
+ static void checkPrime (int num ) {
12
+ int count = 0 ;
13
+ // Condition to get the check factors for the number
14
+ for (int i = 2 ; i <= num /2 ; i ++) {
15
+ if (num %i == 0 ) {
16
+ count ++;
17
+ break ;
18
+ }
19
+ }
20
+
21
+ // Number is prime if the count increases else number is not a prime
22
+ if (count == 0 )
23
+ System .out .println (num + " is a prime number." );
24
+ else
25
+ System .out .println (num + " is not a prime number." );
26
+ }
27
+ // Main method
28
+ public static void main (String [] args ) {
29
+ Scanner scan = new Scanner (System .in );
30
+ System .out .println ("Enter the number:" );
31
+ int n = scan .nextInt ();
32
+ checkPrime (n );
33
+ scan .close ();
34
+ }
35
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+
3
+ Q: Write a java program to print all the prime numbers in a given interval.
4
+
5
+
6
+ */
7
+
8
+ import java .util .Scanner ;
9
+ public class PrintPrime {
10
+ // Method to print the prime number in the given range
11
+ static void printPrime (int start , int end ) {
12
+ System .out .println ("Prime numbers between " + start + " and " + end + " are:" );
13
+ int count ;
14
+ // Looping the number from start to end
15
+ for (int i = start ; i <= end ; i ++) {
16
+ count = 0 ;
17
+ // Check if the number is prime and if it is prime then print
18
+ for (int j = 2 ; j <= i /2 ; j ++) {
19
+ if (i % j == 0 ) {
20
+ count ++;
21
+ break ;
22
+ }
23
+ }
24
+ if (count == 0 ) {
25
+ System .out .print (i + " " );
26
+ }
27
+ }
28
+ }
29
+ // Main method
30
+ public static void main (String [] args ) {
31
+ Scanner scan = new Scanner (System .in );
32
+ System .out .println ("Enter the starting number: " );
33
+ int start = scan .nextInt ();
34
+ System .out .println ("Enter the ending number: " );
35
+ int end = scan .nextInt ();
36
+ printPrime (start , end );
37
+ scan .close ();
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments