Skip to content

Commit fc5403e

Browse files
committed
Basic Programs
1 parent e0301e0 commit fc5403e

14 files changed

+70
-70
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/*
44
* Add two numbers a,b without using + operator
5-
*
5+
* --------------------------------------------
66
* Use bitwise operators instead of using + operator
77
* bitwise and = &
88
* XOR operator = ^

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import java.util.Scanner;
55

66
/*
7-
* Digits of a Given Number
8-
*
7+
* Digits of a Given Number
8+
* -------------------------
99
* For any positive number print the digits
1010
* of the number.
1111
*

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import java.util.TreeSet;
55

66
/*
7-
* Divisors of N
8-
*
7+
* Divisors of N
8+
* -------------
9+
*
910
* This program finds all the divisors of the
1011
* Given number N
1112
*
@@ -18,7 +19,6 @@
1819
*/
1920

2021
public class Divisors {
21-
2222
public static void main(String[] args) {
2323
Scanner scanner = new Scanner(System.in);
2424
System.out.println("Enter the N value : ");

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import java.util.Scanner;
44

55
/*
6-
* EVEN OR ODD
7-
*
8-
* Divide the given number by 2
9-
* If the reminder is 0 then its EVEN
10-
* If the reminder is 1 then its ODD
6+
* EVEN OR ODD
7+
* ------------
8+
* Divide the given number by 2
9+
* If the reminder is 0 then its EVEN
10+
* If the reminder is 1 then its ODD
1111
*
12-
* 22 EVEN number
13-
* 23 ODD number
12+
* 22 EVEN number
13+
* 23 ODD number
1414
*
1515
*/
1616
public class EvenOrOdd {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import java.util.Scanner;
44

55
/*
6-
* Factorial
7-
*
6+
* Factorial
7+
* ----------
88
* In mathematics, the factorial of a positive integer n,
99
* denoted by n!, is the product of all positive integers
1010
* less than or equal to n:
@@ -31,8 +31,8 @@ public static void main(String[] args) {
3131
for(int i=1; i<=N; i++)
3232
result = result * i;
3333

34-
String output = String.format("Factorial of %d is %d", N,result);
35-
System.out.println(output);
34+
System.out.print("Factorial of "+N);
35+
System.out.print(" is "+result);
3636
scanner.close();
3737
}
3838
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import java.util.Scanner;
44

55
/*
6-
* Factorial Using Recursion
7-
*
6+
* Factorial Using Recursion
7+
* --------------------------
88
* In mathematics, the factorial of a positive integer n,
99
* denoted by n!, is the product of all positive integers
1010
* less than or equal to n:

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import java.util.Scanner;
44

55
/*
6-
* Factors of a Given Number
6+
* Factors of a Given Number
7+
* --------------------------
78
*
89
* A number which divides the given number and gives
910
* reminder as zero, then that number is called as Factor.
@@ -13,9 +14,9 @@
1314
*
1415
* say Given number is 40, factors are
1516
* 1, 2, 4, 5, 8, 10, 20, 40
17+
*
1618
*/
1719
public class FactorsOfANumber {
18-
1920
public static void main(String[] args) {
2021
Scanner scanner = new Scanner(System.in);
2122
System.out.println("Enter any positive integer :: ");

InterviewPrograms/src/com/java/basic/FindTheSmallest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.java.basic;
22
/*
33
* Find the Smallest of 3 (a,b,c) without using < or > symbol
4-
*
4+
* ----------------------------------------------------------
55
* Mostly to find max or min we use < or > symbol
66
* But here without using < or > symbol how to find smallest
77
*

InterviewPrograms/src/com/java/basic/LeapYear.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import java.util.Scanner;
44

55
/*
6-
* LEAP YEAR
7-
* This program is to check the given number is leap year or not.
6+
* LEAP YEAR
7+
* ---------
8+
* This program is to check the given number is leap year or not.
89
*
9-
* Leap Year Conditions
10+
* Leap Year Conditions
1011
* 1. Year should be divisible by 4
1112
* 2. If it is divisible by 100 then should be divisible by 400
1213
* 3. If both conditions are not satisfied, then given input is not leap year.

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
import java.util.Scanner;
44

55
/*
6-
* Positive Or Negative
7-
*
8-
* An integer is a whole number that can be either
9-
* greater than 0, called positive, or less than 0,
10-
* called negative.
6+
* Positive Or Negative
7+
* ---------------------
8+
* An integer is a whole number that can be either
9+
* greater than 0, called positive, or less than 0,
10+
* called negative.
1111
*
1212
* Zero is neither positive nor negative.
1313
*
14-
* 100 is Positive Number
15-
* -22 is Negative Number
14+
* 100 is Positive Number
15+
* -22 is Negative Number
1616
*
1717
*/
1818
public class PositiveOrNegative {

InterviewPrograms/src/com/java/basic/Print10WithoutLoop.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.java.basic;
22

33
/*
4-
* Write the java program to print 1 to 10
5-
* without using any loop
4+
* Print 1 to 10 without using loop
5+
* --------------------------------
6+
*
7+
* Solution:
8+
* Use recursion to print without using loop
69
*
7-
* Solution: use recursion to print without using loop
810
*/
911
public class Print10WithoutLoop {
10-
1112
public static void main(String[] args) {
1213
printWihtoutLoop(1);
1314
}
@@ -18,7 +19,6 @@ public static void printWihtoutLoop(int n){
1819
printWihtoutLoop( n+1 );
1920
}
2021
}
21-
2222
}
2323
/*
2424
OUTPUT

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@
33
import java.util.Scanner;
44

55
/*
6-
* Swap Two Numbers
6+
* Swap Two Numbers
7+
* -----------------
8+
* It is possible to swap two numbers without
9+
* using third variable.
710
*
8-
* It is possible to swap two numbers without
9-
* using third variable.
10-
*
11-
* Approach 1::
12-
* using +, - operators
13-
* a = 15, b = 20
11+
* Approach 1::
12+
* using +, - operators
13+
* a = 15, b = 20
1414
*
15-
* a = a + b // after a = 35, b = 20
16-
* b = a - b // after a = 35, b = 15
17-
* a = a - b // after a = 20, b = 15
15+
* a = a + b // after a = 35, b = 20
16+
* b = a - b // after a = 35, b = 15
17+
* a = a - b // after a = 20, b = 15
1818
*
1919
*/
2020
public class SwapApproach1 {
21-
2221
public static void main(String[] args) {
2322
Scanner scanner = new Scanner(System.in);
2423
System.out.println("Enter the value for a : ");

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
import java.util.Scanner;
44

55
/*
6-
* Swap Two Numbers
6+
* Swap Two Numbers
7+
* -----------------
8+
* It is possible to swap two numbers without
9+
* using third variable.
710
*
8-
* It is possible to swap two numbers without
9-
* using third variable.
10-
*
11-
* Approach 2::
12-
* using *, / operators (Multiplication , Division)
13-
* a = 15, b = 10
11+
* Approach 2::
12+
* using *, / operators (Multiplication , Division)
13+
* a = 15, b = 10
1414
*
15-
* a = a * b // after a = 150, b = 10
16-
* b = a / b // after a = 150, b = 15
17-
* a = a / b // after a = 10, b = 15
15+
* a = a * b // after a = 150, b = 10
16+
* b = a / b // after a = 150, b = 15
17+
* a = a / b // after a = 10, b = 15
1818
*
1919
*/
2020
public class SwapApproach2 {

InterviewPrograms/src/com/java/basic/SwapApproach3.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,22 @@
33
import java.util.Scanner;
44

55
/*
6-
* Swap Two Numbers
6+
* Swap Two Numbers
7+
* ----------------
8+
* It is possible to swap two numbers without
9+
* using third variable.
710
*
8-
* It is possible to swap two numbers without
9-
* using third variable.
10-
*
11-
* Approach 3::
12-
* using ^ (XOR) operator
13-
* a = 3, b = 10
14-
* (0011) (1010)
11+
* Approach 3::
12+
* using ^ (XOR) operator
13+
* a = 3, b = 10
14+
* (0011) (1010)
1515
*
16-
* a = a ^ b // a = 0011 ^ 1010 = 1001
17-
* b = a ^ b // b = 1001 ^ 1010 = 0011
18-
* a = a ^ b // a = 1001 ^ 0011 = 1010
16+
* a = a ^ b // a = 0011 ^ 1010 = 1001
17+
* b = a ^ b // b = 1001 ^ 1010 = 0011
18+
* a = a ^ b // a = 1001 ^ 0011 = 1010
1919
*
2020
*/
2121
public class SwapApproach3 {
22-
2322
public static void main(String[] args) {
2423
Scanner scanner = new Scanner(System.in);
2524
System.out.println("Enter the value for a : ");

0 commit comments

Comments
 (0)