Skip to content

Commit 924d848

Browse files
committed
Basic Programs
1 parent 2103d13 commit 924d848

File tree

11 files changed

+65
-54
lines changed

11 files changed

+65
-54
lines changed

InterviewPrograms/src/com/java/basic/AddWithoutPlus.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Use bitwise operators instead of using + operator
77
* bitwise and = &
8-
* xor operator = ^
8+
* XOR operator = ^
99
* shift operator = <<
1010
*
1111
*/

InterviewPrograms/src/com/java/basic/DigitsOfNumber.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class DigitsOfNumber {
2222
public static void main(String[] args) {
2323
Scanner scanner = new Scanner(System.in);
2424
System.out.println("Enter any positive integer :: ");
25-
int num = Integer.parseInt(scanner.nextLine().trim());
25+
int num = scanner.nextInt();
2626

2727
ArrayList<Integer> digitsList = new ArrayList<>();
2828
while(num > 0){

InterviewPrograms/src/com/java/basic/Divisors.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class Divisors {
2222
public static void main(String[] args) {
2323
Scanner scanner = new Scanner(System.in);
2424
System.out.println("Enter the N value : ");
25-
int N = Integer.parseInt(scanner.nextLine().trim());
25+
int N = scanner.nextInt();
2626

2727
TreeSet<Integer> divisors = new TreeSet<>();
2828
for(int i=1;i<N;i++){
@@ -44,11 +44,13 @@ public static void main(String[] args) {
4444
}
4545
}
4646
/*
47+
OUTPUT
4748
Enter the N value : 45
4849
The divisors of 45 are :
4950
[1, 3, 5, 9, 15, 45]
5051
Number of Divisors are : 6
5152
53+
OUTPUT
5254
Enter the N value : 60
5355
The divisors of 60 are :
5456
[1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60]

InterviewPrograms/src/com/java/basic/EvenOrOdd.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class EvenOrOdd {
1717
public static void main(String[] args) {
1818
Scanner scanner = new Scanner(System.in);
1919
System.out.println("Enter the any number : ");
20-
int num = Integer.parseInt(scanner.nextLine().trim());
20+
int num = scanner.nextInt();
2121
if( num % 2 == 0)
2222
System.out.println("Given number is EVEN");
2323
else
@@ -27,10 +27,10 @@ public static void main(String[] args) {
2727
}
2828
/*
2929
OUTPUT
30-
3130
Enter the any number : 22
3231
Given number is EVEN
3332
33+
OUTPUT
3434
Enter the any number : 25
3535
Given number is ODD
3636
*/

InterviewPrograms/src/com/java/basic/Factorial.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* n! can also be written as
1616
* n! = n * (n-1)!
1717
*
18-
* eg: Factorial 4 ( 4! ) is equal to 24.
18+
* say N = 4: Factorial 4 ( 4! ) is equal to 24.
1919
* 4! = 4 * 3 * 2 * 1;
2020
* 5! = 5 * 4 * 3 * 2 * 1;
2121
* 5! = 5 * 4!
@@ -25,7 +25,7 @@ public class Factorial {
2525
public static void main(String[] args) {
2626
Scanner scanner = new Scanner(System.in);
2727
System.out.println("Enter the any number : ");
28-
int N = Integer.parseInt(scanner.nextLine().trim());
28+
int N = scanner.nextInt();
2929

3030
int result =1;
3131
for(int i=1; i<=N; i++)
@@ -38,11 +38,10 @@ public static void main(String[] args) {
3838
}
3939
/*
4040
OUTPUT
41-
4241
Enter the any number : 5
4342
Factorial of 5 is 120
4443
44+
OUTPUT
4545
Enter the any number : 10
4646
Factorial of 10 is 3628800
47-
4847
*/

InterviewPrograms/src/com/java/basic/FactorialUsingRecursion.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,22 @@ public static int factorial(int n){
3636
public static void main(String[] args) {
3737
Scanner scanner = new Scanner(System.in);
3838
System.out.println("Enter the any number : ");
39-
int N = Integer.parseInt(scanner.nextLine().trim());
39+
int N = scanner.nextInt();
4040

4141
int result = factorial(N);
4242

43-
String output = String.format("Factorial of %d is %d", N,result);
44-
System.out.println(output);
43+
System.out.println("Factorial of "+N+" is ");
44+
System.out.println(result);
45+
4546
scanner.close();
4647
}
4748
}
4849
/*
4950
OUTPUT
50-
5151
Enter the any number : 5
5252
Factorial of 5 is 120
5353
54+
OUTPUT
5455
Enter the any number : 10
5556
Factorial of 10 is 3628800
5657
*/

InterviewPrograms/src/com/java/basic/FactorsOfANumber.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class FactorsOfANumber {
1919
public static void main(String[] args) {
2020
Scanner scanner = new Scanner(System.in);
2121
System.out.println("Enter any positive integer :: ");
22-
int num = Integer.parseInt(scanner.nextLine().trim());
22+
int num = scanner.nextInt();
2323

2424
System.out.println("The factors of the given number are :");
2525
printFactors(num);
@@ -40,6 +40,7 @@ private static void printFactors(int num){
4040
The factors of the given number are :
4141
1, 2, 4, 5, 8, 10, 20, 40
4242
43+
OUTPUT
4344
Enter any positive integer :: 75
4445
The factors of the given number are :
4546
1, 3, 5, 15, 25, 75

InterviewPrograms/src/com/java/basic/PositiveOrNegative.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static void main(String[] args) {
2020
Scanner scanner = new Scanner(System.in);
2121
System.out.println("Enter any integer : ");
2222

23-
int n = Integer.parseInt(scanner.nextLine().trim());
23+
int n = scanner.nextInt();
2424
if(n > 0 )
2525
System.out.println(n+" is a Positive Number.");
2626
else if( n < 0 )
@@ -33,13 +33,14 @@ else if( n < 0 )
3333
}
3434
/*
3535
OUTPUT
36-
3736
Enter any integer : 155
3837
155 is a Positive Number.
3938
39+
OUTPUT
4040
Enter any integer : -77
4141
-77 is a Negative Number.
4242
43+
OUTPUT
4344
Enter any integer : 0
4445
Zero is neither Positive nor Negative.
4546
*/

InterviewPrograms/src/com/java/basic/SwapApproach1.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,38 @@ public class SwapApproach1 {
2222
public static void main(String[] args) {
2323
Scanner scanner = new Scanner(System.in);
2424
System.out.println("Enter the value for a : ");
25-
int a = Integer.parseInt(scanner.nextLine().trim());
25+
int a = scanner.nextInt();
2626
System.out.println("Enter the value for b : ");
27-
int b = Integer.parseInt(scanner.nextLine().trim());
27+
int b = scanner.nextInt();
2828

29-
String str = String.format("Before swapping a & b is %d & %d", a,b);
30-
System.out.println(str);
29+
System.out.println("Before swapping a & b : ");
30+
System.out.println("a = "+a+", b = "+b);
3131

3232
//using +, - operators
3333
a = a + b;
3434
b = a - b;
3535
a = a - b;
3636

37-
str = String.format("After swapping a & b is %d & %d", a,b);
38-
System.out.println(str);
37+
System.out.println("After swapping a & b : ");
38+
System.out.println("a = "+a+", b = "+b);
3939

4040
scanner.close();
4141
}
4242
}
4343
/*
4444
OUTPUT
45-
4645
Enter the value for a : 15
4746
Enter the value for b : 20
48-
Before swapping a & b is 15 & 20
49-
After swapping a & b is 20 & 15
47+
Before swapping a & b :
48+
a = 15, b = 20
49+
After swapping a & b :
50+
a = 20, b = 15
5051
52+
OUTPUT
5153
Enter the value for a : 2
5254
Enter the value for b : 7
53-
Before swapping a & b is 2 & 7
54-
After swapping a & b is 7 & 2
55-
55+
Before swapping a & b :
56+
a = 15, b = 10
57+
After swapping a & b :
58+
a = 10, b = 15
5659
*/

InterviewPrograms/src/com/java/basic/SwapApproach2.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,41 @@
1818
*
1919
*/
2020
public class SwapApproach2 {
21-
2221
public static void main(String[] args) {
2322
Scanner scanner = new Scanner(System.in);
2423
System.out.println("Enter the value for a : ");
25-
int a = Integer.parseInt(scanner.nextLine().trim());
24+
int a = scanner.nextInt();
2625
System.out.println("Enter the value for b : ");
27-
int b = Integer.parseInt(scanner.nextLine().trim());
26+
int b = scanner.nextInt();
2827

29-
String str = String.format("Before swapping a & b is %d & %d", a,b);
30-
System.out.println(str);
28+
System.out.println("Before swapping a & b : ");
29+
System.out.println("a = "+a+", b = "+b);
3130

3231
//using *, / operators
3332
a = a * b;
3433
b = a / b;
3534
a = a / b;
3635

37-
str = String.format("After swapping a & b is %d & %d", a,b);
38-
System.out.println(str);
36+
System.out.println("After swapping a & b : ");
37+
System.out.println("a = "+a+", b = "+b);
3938

4039
scanner.close();
4140
}
4241
}
4342
/*
4443
OUTPUT
45-
4644
Enter the value for a : 15
4745
Enter the value for b : 20
48-
Before swapping a & b is 15 & 20
49-
After swapping a & b is 20 & 15
46+
Before swapping a & b :
47+
a = 15, b = 20
48+
After swapping a & b :
49+
a = 20, b = 15
5050
51+
OUTPUT
5152
Enter the value for a : 15
5253
Enter the value for b : 10
53-
Before swapping a & b is 15 & 10
54-
After swapping a & b is 10 & 15
55-
54+
Before swapping a & b :
55+
a = 15, b = 10
56+
After swapping a & b :
57+
a = 10, b = 15
5658
*/

0 commit comments

Comments
 (0)