Posts

Showing posts from September, 2021

Find the HCF of 2 numbers using Java code

Image
  Today we'll try to solve a common mathematical question using Java code. We will find the HCF of two numbers.  What is HCF - its the Highest Common Factor of two numbers. Which means it is the biggest number which divides two numbers such that the remainder is 0. What will be utilized in this logic: 1. While loop to iterate till we arrive at the solution 2. % Operator to find the remainder Let's check the code! /**  * Find the HCF of two numbers  * @author computengine.com  * @param num1  * @param num2  * @return  */ public static void findHCF ( int num1 , int num2 ) { int remainder = 0 ; System . out . println ( " Input Num1: " + num1 ) ; System . out . println ( " Input Num2: " + num2 ) ; //loop till we find a number which divides exactly while ( num1 % num2 > 0 ) { //find the remainder of the division remainder = num1 % num2 ; System . out . println ( " Remainder: " + remainder ) ; //put

Find the Longest Streak of 1's in a Binary Array using Java Code

Image
  You have been given an Integer Array which contains only 1s and 0s, and asked to find the longest running streak of 0 or 1 in the array. Sound familiar? Its a common interview question. Let's try to find a solution using Java.  We would required two counters here, 1 counter to keep track of the running count, and the second one to keep track of the maximum running streak found.  We'll loop through the array using a for loop.  Here is the code for your reference: /**  * Find the longest Streak of 1s  * @author computengine.com  * @param a  * @return the length of the longest streak  */ public int findLongestStreak ( int [ ] b ) { System . out . println ( " Array for processing: " + Arrays . toString ( b ) ) ; int count = 0 , max = 0 ; //loop through the array for ( int i = 0 ; i < b . length ; i + + ) { //increment count when 1 is found if ( b [ i ] = = 1 ) count + = 1 ; //if count is greater than previous max streak,

JavaScript Input Field Validation Function with Input field color highlight and Error Message Display

Image
  We use a lot of Forms regularly. If you are using a Form in your website, have you ever tried to create some validations on the form so that the inputs made by user can be checked? There are a lot of libraries out there which can help you do the input validation using JavaScript, but that is additional code that needs to be included, and third party libraries can often slow down your page load speeds.  Today, we'll write a small JavaScript function that we can include in our website and which does the input validation very quickly and efficiently. This function also highlights the input field in a color of your choice, and you can display an error message to your end user. If you have a form with multiple fields, and want to run validations on all of them, you'll have to loop through the fields one by one using a for loop, and run the validation on each one.  You can pass the field ID to the function and run the validation on it.  Below is our function which will do the valid