Posts

Showing posts from December, 2023

Reversing the String ; Doubling Each Letter

Image
Step 1: Create a Scanner object to take user input Scanner Initialization: Create a Scanner object named scanner to take user input from the console. Step 2: Prompt the user to enter a name User Input: Prompt the user to enter a name using System.out.println("Enter the name:"); and Step 3: Read the user input (name) using the Scanner Read the input using String name = scanner.next(); Step 4: Display the original name Display Original Name: Print a message indicating that the entered name is going to be displayed. Step 5: Reverse the input string using StringBuilder Reverse the Name: Use StringBuilder to reverse the input string (StringBuilder(name).reverse()). Step 6: Display the reversed name Display Reversed Name: Print the reversed name Step 7: Create a new StringBuilder to store the doubled name Step 8: Iterate through each character in the reversed name Double Each Letter: Iterate through each character in the reversed name and append it twice to a new StringBuilder (dou...

How to form a Pyramid Pattern

Image
 Step 1         Outer Loop (for loop with variable i):         This loop controls the number of rows in the pyramid. It iterates from 1 to the specified number of              rows (rows).         Step 2( Inner Loop 1 (for loop with variable j):         This loop is responsible for printing spaces before the stars on each row.         It prints a number of spaces equal to rows - i. As i increases, fewer spaces are printed for each row,            creating a left-align effect.         Step 3 (Inner Loop 2 (for loop with variable k)         This loop prints the stars on each row.         It prints a number of stars equal to 2 * i - 1. The number of stars increases as i increases, creating            the   pyramid shape. Ste...

Swabbing Tow no's

Image
  Method 1: Using a Temporary Variable (commented out): This is a traditional approach using a temporary variable t to store the value of b before swapping. It then swaps the values of a and b using the temporary variable Method 2: Without Using a Temporary Variable: This is a mathematical approach to swap the values without using a temporary variable. It leverages the fact that a = a + b adds the values of a and b , b = a - b subtracts the original value of b from the sum (resulting in the original value of a ), and a = a - b subtracts the new value of b (original value of a ) from the sum (resulting in the original value of b ).

Sort the array in ASC and DESC

Image
Sort the array in ASC and DESC a = {1, 4, 5, 7}: 1.After sorting in ascending order using Arrays.sort(a), the array becomes a = {1, 4, 5, 7}. 2.Array Sorting: The Arrays.sort(a) method is used to sort the array a in ascending order. 3.Printing Sorted Array: The sorted array is then printed to the console using System.out.println(Arrays.toString(a)). 4.Reversing Array (Descending Order): A new array named descendingorder is created to store the reversed (descending) order of the sorted array. A loop is used to iterate through the sorted array and populate the descendingorder array in reverse order. Let's apply this with the current values: When i = 0 , descendingOrder[0] = a[4 - 1 - 0] = a[3] = 7 . When i = 1 , descendingOrder[1] = a[4 - 1 - 1] = a[2] = 5 . When i = 2 , descendingOrder[2] = a[4 - 1 - 2] = a[1] = 4 . When i = 3 , descendingOrder[3] = a[4 - 1 - 3] = a[0] = 1 . 5.Printing Reversed Array: The reversed array is then printed to the console using System.out.println(Arrays...

ArrayList Handling : Compare two different Arrays with matching indeces.

Image
 1. Array Initialization: Two integer arrays a and b are declared and initialized with values. 2.Function Call: The findmatchingnumbers function is called with arrays a and b as ; 3.Function Definition: A private static function named findmatchingnumbers is defined. It takes two arrays of integers ( array1 and array2 ) as parameters and returns an ArrayList of integers. 4.Array List Initialization: Inside the function, an Array List named matchednumber is initialized to store the matching numbers. 5.Nested Loop: A nested loop is used to compare each element of array1 with each element of array2 . If a match is found, the matching number is added to the matchednumber ArrayList. 6. Return Statement: The function returns the matchednumber ArrayList containing the matching numbers.

"Java Programming: Arrays, Methods, and Stream API"

Image
Main  Method 'main' Step 1: This is the main method or starting point of the program. Step 2: I declared StudentA as a array name and initialized their value value{100,98,..}. Step 3: The 'Arraysum' Method is called with with StudentA and result will stored in the sum name as a integer. Step 4: print the sum.  'Arraysum' Method Step 5: Arraysum method takes an array of integers int[] studentA  as a parameter and returns an integer. Step 6: Iteration have started here , Instead for loop and used stream operation to reduce the lines and more readable.