Posts

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...