Posts

Showing posts with the label Switch Statements

Take Number From User And Print Its Multiplication Table | Java Programs

Output: *** Multiplication Table *** Enter any number for its Multiplication Table: 9 9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81 9 x 10 = 90 Click Here For  Java Online Compiler Solution: import java.util.Scanner ; class PrintMultiplicationTable { public static void main ( String [] args ) { Scanner scanner = new Scanner ( System . in ); System . out . println ( "*** Multiplication Table ***" + "\n" ); System . out . print ( "Enter any number for its Multiplication Table: " ); int number = scanner . nextInt (); for ( int i = 1; i <= 10; i ++) { System . out . println ( number + " x " + i + " = " + number * i ); } } }

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 ; } } }

Sum Of First "n" Natural Numbers | Java Programs

Output: *** Sum of Natural Numbers *** Enter the value of n: 10 Sum of first 10 natural numbers is: 55 Click Here For  Java Online Compiler Solution: import java.util.Scanner ; public class SumOfNNaturalNums { public static void main ( String [] args ) { int num = 0; int total = 0; System . out . println ( "*** Sum of Natural Numbers ***" + "\n" ); System . out . print ( "Enter the value of n: " ); Scanner scan = new Scanner ( System . in ); num = scan . nextInt (); for ( int i = 1; i <= num ; i ++) { total = total + i ; } System . out . println ( "Sum of first " + num + " natural numbers is: " + total ); } }

Sum Of Natural Numbers Using FOR Loop | Java Programs

Output: *** Sum Of Natural Numbers Using FOR Loop *** For lOOp used !!! Sum of first 10 natural numbers is: 55 Click Here For  Java Online Compiler Solution: public class SumNaturalForLoop { public static void main ( String [] args ) { int num = 10; // <------ How many numbers??? int count ; int total = 0; for ( count = 1; count <= num ; count ++) { total = total + count ; } System . out . println ( "*** Sum Of Natural Numbers Using FOR Loop ***" + "\n" ); System . out . println ( "For lOOp used !!!" + "\n" ); System . out . println ( "Sum of first 10 natural numbers is: " + total ); } }

Find Second Smallest Element In An Array | Java Programs

Output: *** Second Smallest Number In Array *** Original Array is: [-10, 42, 90, 21, 72, -3] Second smallest number is : -3 Click Here For  Java Online Compiler Solution: import java.util.Arrays ; public class SecondSmallestInArray { public static void main ( String [] args ) { int [] array = {-10, 42, 90, 21, 72, -3}; System . out . println ( "*** Second Smallest Number In Array ***" + "\n" ); System . out . println ( "Original Array is: " + Arrays . toString ( array )); int min = Integer . MAX_VALUE ; int secondMin = Integer . MAX_VALUE ; for ( int i = 0; i < array . length ; i ++) { if ( array [ i ] == min ) { secondMin = min ; } else if ( array [ i ] < min ) { secondMin = min ; min = array [ i ]; } else if ( array [ i ] < secon...

Find Second Largest Element In An Array | Java Programs

Output: *** Second Largest Number In An Array *** Original array is: [1, 2, 4, 7, 8, 3, 5, 10] Second largest number is : 8 Click Here For  Java Online Compiler Solution: import java.util.Arrays ; class SecondLargestInArray { public static void main ( String [] args ) { int [] array = {1, 2, 4, 7, 8, 3, 5, 10}; int max = array [0]; int secondMax = array [0]; System . out . println ( "*** Second Largest Number In An Array ***" + "\n" ); System . out . println ( "Original array is: " + Arrays . toString ( array )); for ( int i = 0; i < array . length ; i ++) { if ( array [ i ] > max ) { secondMax = max ; max = array [ i ]; } else if ( array [ i ] > secondMax ) { secondMax = array [ i ]; } } System . out . println...

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 ++; } } }

Sum Of Natural Numbers Using WHILE Loop | Java Programs

Output: *** Sum Of Natural Numbers Using WHILE Loop *** While lOOp used !!! Sum of first 10 natural numbers is: 55 Click Here For  Java Online Compiler Solution: public class SumNaturalWhile { public static void main ( String [] args ) { int num = 10; // <------ How many numbers??? int count = 1; int total = 0; while ( count <= num ) { total = total + count ; count ++; } System . out . println ( "*** Sum Of Natural Numbers Using WHILE Loop ***" + "\n" ); System . out . println ( "While lOOp used !!!" + "\n" ); System . out . println ( "Sum of first 10 natural numbers is: " + total ); } }

Sum Of Digits Of A Number Using Recursion | Java Programs

Output: *** Sum Of Digits Of A Number Using Recursion *** Enter the number: 197 Sum Of Digits : 17 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 ); } }