Posts

Showing posts with the label Java Interview Questions

Generate Random Numbers | Java Programs

Output: Random Numbers: *************** 47 60 18 9 67 ---------------- Random Numbers: *************** 78 68 14 79 69 Click Here For  Java Online Compiler Solution: import java.util.* ; class GenerateRandomNumbers { public static void main ( String [] args ) { Random r = new Random (); int howManyRandom = 5; System . out . println ( "Random Numbers:" ); System . out . println ( "***************" ); for ( int i = 1; i <= howManyRandom ; i ++) { System . out . println ( r . nextInt (100)); //Here 100 shows that random number } //will not exceed 100 } }

Finding Factorial Using "For Loop" | Java Programs

Output: *** Factorial Using For LOOp Only *** Find Factorial Of: 4 Factorial of 4 is: 24 Click Here For  Java Online Compiler Solution: import java.util.Scanner ; class FactorialUsingForLoop { public static void main ( String [] args ) { Scanner scanner = new Scanner ( System . in ); System . out . println ( "*** Factorial Using For LOOp Only ***" + "\n" ); System . out . print ( "Find Factorial Of: " ); int factorialOf = scanner . nextInt (); long fact = 1; for ( int i = 1; i <= factorialOf ; i ++) { fact = fact * i ; } System . out . println ( "Factorial of " + factorialOf + " is: " + fact ); } }

Print Fibonacci Series Using "For Loop" Only | Java Programs

Output: *** Fibonacci Series Using For Loop *** Fibonacci Series upto 7 numbers: 0 1 1 2 3 5 8 Click Here For  Java Online Compiler Solution: public class FibonacciSeriesUsingForLoop { public static void main ( String [] args ) { int count = 7; int num1 = 0; int num2 = 1; System . out . println ( "*** Fibonacci Series Using For Loop ***" + "\n" ); System . out . print ( "Fibonacci Series upto " + count + " numbers: " ); for ( int i = 1; i <= count ; ++ i ) { System . out . print ( num1 + " " ); int sumOfPrevTwo = num1 + num2 ; num1 = num2 ; num2 = sumOfPrevTwo ; } } }

Find Factorial Of A Number Entered By User | Java Programs

Output: *** Factorial Program *** Enter the number: 10 Factorial of 10 is: 3628800 Click Here For  Java Online Compiler Solution: import java.util.Scanner ; class FactorialInputNumber { public static void main ( String [] args ) { int number ; System . out . println ( "*** Factorial Program ***" + "\n" ); System . out . print ( "Enter the number: " ); Scanner sc = new Scanner ( System . in ); number = sc . nextInt (); long fact = 1; int i = 1; while ( i <= number ) { fact = fact * i ; i ++; } System . out . println ( "Factorial of " + number + " is: " + fact ); } }

Finding Factorial Using "While Loop" | Java Programs

Output: *** Factorial Using While lOOp Only *** Factorial of 5 is: 120 Click Here For  Java Online Compiler Solution: class FactorialUsingWhileLoop { public static void main ( String [] args ) { int factorialOf = 5; long fact = 1; int i = 1; while ( i <= factorialOf ) { fact = fact * i ; i ++; } System . out . println ( "*** Factorial Using While lOOp Only ***" + "\n" ); System . out . println ( "Factorial of " + factorialOf + " is: " + fact ); } }

Print Fibonacci Series Based On User Input | Java Programs

Output: *** Fibonacci Series *** How many numbers you want in the sequence: 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8 Click Here For  Java Online Compiler Solution: import java.util.Scanner ; class FibonacciSeriesUpto { public static void main ( String [] args ) { int howMany = 0; int num1 = 0; int num2 = 1; System . out . println ( "*** Fibonacci Series ***" + "\n" ); System . out . print ( "How many numbers you want in the sequence: " ); Scanner sc = new Scanner ( System . in ); howMany = sc . nextInt (); System . out . print ( "Fibonacci Series of " + howMany + " numbers: " ); int i = 1; while ( i <= howMany ) { System . out . print ( num1 + " " ); int sumOfPrevTwo = num1 + num2 ; num1 = num2 ; num2 = sumOfPrevTwo ;...

Print Fibonacci Series Using "While Loop" Only | Java Programs

Output: *** Fibonacci Series Using While Loop Only *** Fibonacci Series of 10 numbers: 0 1 1 2 3 5 8 13 21 34 Click Here For  Java Online Compiler Solution: class FibonacciSeriesUsingWhileLoop { public static void main ( String [] args ) { int count = 10; int num1 = 0; int num2 = 1; System . out . println ( "*** Fibonacci Series Using While Loop Only ***" + "\n" ); System . out . print ( "Fibonacci Series of " + count + " numbers: " ); int i = 1; while ( i <= count ) { System . out . print ( num1 + " " ); int sumOfPrevTwo = num1 + num2 ; num1 = num2 ; num2 = sumOfPrevTwo ; i ++; } } }

Reverse Any Integer Number | Java Programs

Output: *** Reverse An Integer *** Enter any number : 1234567 The Original Number is: 1234567 Reversed Number is : 7654321 Click Here For  Java Online Compiler Solution: import java.util.Scanner ; class ReverseAnInteger { public static void main ( String [] args ) { System . out . println ( "*** Reverse An Integer ***" + "\n" ); Scanner scanner = new Scanner ( System . in ); System . out . print ( "Enter any number : " ); int num = scanner . nextInt (); System . out . println ( "\nThe Original Number is: " + num ); int is_positive = 1; if ( num < 0) { is_positive = -1; num = -1 * num ; } int sum = 0; while ( num > 0) { int r = num % 10; int maxDiff = Integer . MAX_VALUE - sum * 10; if ( sum > Integer . MAX_VALUE / 10 ...

Separate Whole And Fractional Parts From A "double" Number | Java Programs

Output: *** Whole and Fractional Parts In A Double Original number is : 12.368 Integral part : 12.0 Fractional part : 0.3680000000000003 Click Here For  Java Online Compiler Solution: class WholeFractionalOfDouble { public static void main ( String [] args ) { double anyNumber = 12.368; double fractional_part = anyNumber % 1; double integral_part = anyNumber - fractional_part ; System . out . println ( "*** Whole and Fractional Parts In A Double" + "\n" ); System . out . println ( "Original number is : " + anyNumber + "\n" ); System . out . println ( "Integral part : " + integral_part ); System . out . println ( "Fractional part : " + fractional_part ); } }

Take Input From User And Count How Many Prime Numbers Come Before That Number | Java Programs

Output: *** How Many Prime Numbers Are There Before Entered Number *** Enter a number: 8 Before 8 there are 4 prime numbers . Click Here For  Java Online Compiler Solution: import java.util.* ; public class HowManyPrimes { public static void main ( String [] args ) { Scanner scanner = new Scanner ( System . in ); System . out . println ( "*** How Many Prime Numbers Before Entered Number ***" ); System . out . println (); System . out . print ( "Enter a number: " ); int x = scanner . nextInt (); System . out . print ( "Before " + x + " there are " + countPrimes ( x ) + " prime numbers." ); System . out . println (); } static int countPrimes ( int n ) { if ( n <= 0 || n == 1 || n == 2) { return 0; } else if ( n == 3) { return 1; } BitSet set ...

Swap Two Numbers Without Using Third Temporary Variable | Java Programs

Output: *** Swap Two Numbers Without Using Third Variable *** *** Before swapping *** a = 123 b = 456 *** After swapping *** a = 456 b = 123 Click Here For  Java Online Compiler Solution: public class SwapTwoNumbers2 { public static void main ( String [] args ) { int a = 123; int b = 456; System . out . println ( "*** Swap Two Numbers Without Using Third Variable ***" + "\n" ); System . out . println ( "*** Before swapping ***" ); System . out . println ( "a = " + a ); System . out . println ( "b = " + b ); a = a + b ; b = a - b ; a = a - b ; System . out . println ( "*** After swapping ***" ); System . out . println ( "a = " + a ); System . out . println ( "b = " + b ); } }

Swap Two Numbers Using Third Temporary Variable | Java Programs

Output: *** Swap Two Numbers Using Third Variable *** *** Before swapping *** a = 15 b = 27 *** After swapping *** a = 27 b = 15 Click Here For  Java Online Compiler Solution: public class SwapTwoNumbers1 { public static void main ( String [] args ) { int a , b , temp ; a = 15; b = 27; System . out . println ( "*** Swap Two Numbers Using Third Variable ***" + "\n" ); System . out . println ( "*** Before swapping ***" ); System . out . println ( "a = " + a ); System . out . println ( "b = " + b ); temp = a ; a = b ; b = temp ; System . out . println ( "*** After swapping ***" ); System . out . println ( "a = " + a ); System . out . println ( "b = " + b ); } }

Sum Of The Digits Of A Number | Java Programs

Output: *** Sum Of Digits Of A Number *** Enter the number: 345 The number is: 345 Sum of digits : 12 Click Here For  Java Online Compiler Solution: import java.util.Scanner ; public class SumDigitsOfNumber { public static void main ( String args []) { Scanner scanner = new Scanner ( System . in ); System . out . println ( "*** Sum Of Digits Of A Number ***" + "\n" ); System . out . print ( "Enter the number: " ); int number = scanner . nextInt (); int sum = 0; int num = number ; System . out . println ( "The number is: " + number ); while ( num > 0) { int lastDigit = num % 10; sum += lastDigit ; num /= 10; } System . out . println ( "Sum of digits : " + sum ); } }