-->

Hello,This is me!

ITEngineer8995

Professional Coder Coding is my passion

About me

Hello

I'mITEngineer8995

Spatial Data Specialist

Do you want all of your codes in one place?? Then you are landed on the right blog... Here you will find varieties of codes of various languages starting from noob to advance with their samples. Do learn, execute and share your knowledge with others. Do visit my YouTube Channel for execution of these codes .

Experience

Video Clone Detector using Hadoop

2017-2018

A detector which differentiates copied videos from the original ones using Hadoop.

Portfolio

Java Program To Sum the elements of the Array


Java Program To Sum the elements of the Array

In this Java Program, we sum up all the elements of the array.

Array is a data structure consisting of collections of elements each identified by atleast one array index or key. 

E.g. If you want to store 5 numbers in an array,

        datatype arrayname [arraysize];

         int a[5];

Step 1 : Create and Initialize array

Step 2 : Create variable sum and initialize it to zero

Step 3 : Apply advanced for loop where int number : array

Step 4 : Add sum to num and store it in the variable sum

Step 5 : Print the Result

Program :

public class SumArray 
    {
        public static void main(String[] args)
        {
            int[] array = {10, 20, 30, 40, 50, 10};
            int sum = 0;
            //Advanced for loop
            for(int num : array)
            {
                sum = sum + num;
            }
            System.out.println("Sum of array elements is: "+sum);
        }
    
}

Output :

Sum of array elements is: 160

For the execution of the same do visit my YouTube Channel :


Woh Lamhe




                                                     






For video do visit my Youtube Channel :






                


Java Program To Calculate Average of the Number using Array Entered by the User.


Java Program To Calculate Average of Number using Array Entered by the User.

In this Java Program, we calculate average of number using array entered by the user.

Average is a number that is calculated by adding all the numbers together and dividing the total by the number of quantities. It is also known as mean.

E.g. Average of 5 numbers :

       Average = 1 + 2 + 3 + 4 + 5 = 15 / 5 = 3

Array is a data structure consisting of collections of elements each identified by atleast one array index or key. 

E.g. If you want to store 5 numbers in an array,

        datatype arrayname [arraysize];

         int a[5];

Step 1 : Import java.util.Scanner

Step 2 : Ask user how many number he want to enter by using System.out.println method.

Step 3 : Create object of Scanner class

Step 4 : Create variable n and store the entered number of the user.

Step 5 : Create an array arr and store the contents of n in it.

Step 6 : Create a variable total and initialize it to 0

Step 7 : Apply for loop, where i is equal to zero, i should be less than length of the array and increment i.

Step 8 : Print the entered element number by adding 1 in it.

Step 9 : Store value of sc.nextDouble() into array of i.

Step 10 : Close the Scanner.

Step 11 : Apply another for loop, where i is equal to zero, i should be less than length of the array and increment i.

Step 12 : Add total + array of i and store it in total.

Step 13 : Display the Result.

Program :

import java.util.Scanner;

public class AverageArray2 
    {
        public static void main (String []args)
        {
            System.out.println("How many numbers you want to enter?");
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            //Declaring array of n elements, the value of n is provided by the user
            
            double[] arr = new double[n];
            double total = 0;
            
            for(int i = 0; i<arr.length; i++)
            {
                System.out.print("Enter Element No."+(i+1)+": ");
                arr[i] = sc.nextDouble();
            }
            sc.close();
            for(int i = 0; i<arr.length; i++)
            {
                total = total + arr[i];
                
            }
            
            double average = total / arr.length;
            
            System.out.format("The average is : %.3f", average);
        }
    
}

Output :

How many numbers you want to enter?
5
Enter Element No.1: 12.7
Enter Element No.2: 18.9
Enter Element No.3: 20
Enter Element No.4: 13.923
Enter Element No.5: 16.225
The average is : 16.350

For execution of the same do visit my YouTube Channel :




Java Program To Calculate Average of Number using Array


Java Program To Calculate Average of Number using Array

In this Java Program, we calculate average of number using array.

Average is a number that is calculated by adding all the numbers together and dividing the total by the number of quantities. It is also known as mean.

E.g. Average of 5 numbers :

       Average = 1 + 2 + 3 + 4 + 5 = 15 / 5 = 3

Array is a data structure consisting of collections of elements each identified by atleast one array index or key. 

E.g. If you want to store 5 numbers in an array,

        datatype arrayname [arraysize];

         int a[5];

Logic :  double[] arr = {19, 12.89, 16.5, 200, 13.7};
              double total = 0;
            
                  for(int i = 0; i<arr.length; i++)
                    {
                       total = total + arr[i];
                    }

Step 1 : Create an array of data type double named arr and store the numbers in it.

Step 2 : Create a variable total with data type double and initialize it to 0.

Step 3 : Apply for loop, where i should be equal to zero, i should be less than the length of the array and increment i. arr.length returns the number of elements present in the array.

Step 4 : Add total and the a[i] and store it in the variable total.

Step 5 : Create a variable average and store the result of the division of total and arr.length.

Step 6 : Print the output. 

Program :

public class AverageArray 
    {
        public static void main (String[] args)
        {
            double[] arr = {19, 12.89, 16.5, 200, 13.7};
            double total = 0;
            
            for(int i = 0; i<arr.length; i++)
            {
                total = total + arr[i];
            }
            
            //arr.length returns the number of elements present in the array
            
            double average = total / arr.length;
            
            //This is used for displaying the formatted output
            //If you give %.4f then the output would have 4 digits after decimal point
            System.out.format("The average is : %.3f", average);
        }
}


Output :

The average is : 52.418

For execution of the same do visit my Youtube Channel :




Java Program To Count Vowels and Consonants in a String


Java Program To Count Vowels and Consonants in a String

In this Java Program, we count the vowels and consonants in a string.

The alphabet contains 26 letters, among them A, E, I,  O, U (small and upper case) are known as vowels while the remaining 21 letters are known as consonants.  

Logic : String str = "Itengineer8995";
             int vcount = 0, ccount = 0;

             str = str.toLowerCase();
             for(int i = 0; i < str.length(); i++)
             {
                char ch = str.charAt(i);
                if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
                  {
                      vcount++;
                   }
                else if((ch >= 'a' && ch <= 'z'))
                        {
                            ccount++;
                        }
                }

Step 1 : Create variable str and store the string in it.

Step 2 : Create and initialize variable vcount to 0 and ccount to 0.

Step 3 : Convert all the chars to lowercase by using toLowerCase method of the String class and store it in the variable str.

Step 4 : Apply for loop where i is equal to 0, i should be less than length of the string and increment i.

Step 5 : Create variable char ch and store the string at char position i.

Step 6 : apply if else if statement. If ch is equal to a or ch is equal to e or ch is equal to i or ch is equal to o or ch is equal to u then increment the vcount. else if  ch is greater than or equal to a and ch is less than or equal to z then increment ccount.

Step 7 : Print number of vowels and consonants.

Program :

public class VowelConsonant2 

    {
        public static void main(String[] args)
        {
            String str = "Itengineer8995";
            int vcount = 0, ccount = 0;
            
            //converting all the chars to lowercase
            str = str.toLowerCase();
            for(int i = 0; i < str.length(); i++)
            {
                char ch = str.charAt(i);
                if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
                {
                    vcount++;
                }
                else if((ch >= 'a' && ch <= 'z'))
                        {
                            ccount++;
                        }
            }
            System.out.println("Number of Vowels :" +vcount);
            System.out.println("Number of Consonants :" +ccount);        
        }


Output :

Number of Vowels :5
Number of Consonants :5

For execution of the same do visit my YouTube Channel :





Java Program To Find Occurrence of a Character in a String


Java Program To Find Occurrence of a Character in a String

In this Java Program, we find the occurrence of the character in a string.

Logic :   int counter [] = new int[256];
               int len = str.length();
                    for(int i = 0; i < len; i++)
                    counter[str.charAt(i)]++;
                           char array[] = new char[str.length()];
                            for(int i = 0; i < len; i++)
                                {
                                    array[i] = str.charAt(i);
                                    int flag = 0;
                                        for (int j = 0; j <=i; j++)
                                               {
                                                    if(str.charAt(i) == array[j])
                                                    flag++;
                                                }
                                        if (flag == 1)
                System.out.println("Occurrence of char" + str.charAt(i)+"in the String is:"+counter[str.charAt(i)]);    
                                       }

Step 1 : Create a class Occurrence

Step 2 : Create a method countEachChar()

Step 3 : Create an array counter and store the ASCII values which ranges upto 256 in it.

Step 4 : Create a variable len and store the length of the string in it.

Step 5 : Apply for loop. Initialize i to 0, i should be smaller than length and increment i.

Step 6 : Increment the counter with the string at charAt position i.

Step 7 : Create another array with the size of the string and store the new length of the string in it.

Step 8 : Apply for loop. Initialize i to 0, i should be smaller than length and increment i.

Step 9 : Inside the for loop of i store the string at char i into array[i].

Step 10 : Initialize flag to 0.

Step 11 : Apply for loop of j. Initialize j to 0, i should be smaller and equal to i and increment j.

Step 12 :  If a char is found in String then set the flag so that we can print the occurrence of the character.

Step 13 : Create main method, specify the string of which you want to find the occurrence of the character.

Program : 

public class Occurrence 
    {
        static void countEachChar(String str)
        {
            //ASCII values ranges upto 256
            int counter [] = new int[256];
            
            //String length
            int len = str.length();
            
            //This array holds the occurrence of each char
            //For example, ASCII value of A is 65 so if A is found twice then
            //counter [65] would have the value 2, here 65 is the ASCII value of A
            
            for(int i = 0; i < len; i++)
                counter[str.charAt(i)]++;
            
            //We are creating another array with the size of String
            char array[] = new char[str.length()];
            for(int i = 0; i < len; i++)
            {
                array[i] = str.charAt(i);
                int flag = 0;
                for (int j = 0; j <=i; j++)
                {
                    //If a char is found in String then set the flag so that
                    //we can print the occurrence
                    if(str.charAt(i) == array[j])
                        flag++;
                    
                }
                if (flag == 1)
                    System.out.println("Occurrence of char" + str.charAt(i)+"in the String is:"+counter[str.charAt(i)]);
                
            }
            
        }
        public static void main(String[] args)
        {
            String str = "itengineer8995";
            countEachChar(str);
        }
    
}

Output :

Occurrence of chariin the String is:2
Occurrence of chartin the String is:1
Occurrence of charein the String is:3
Occurrence of charnin the String is:2
Occurrence of chargin the String is:1
Occurrence of charrin the String is:1
Occurrence of char8in the String is:1
Occurrence of char9in the String is:2
Occurrence of char5in the String is:1

For execution of the same do visit my YouTube Channel :




Java Program To Perform Bubble Sort on String


Java Program To Perform Bubble Sort on String

In this Java Program, we perform bubble sort on the string.

Bubble Sort is the simple sorting algorithm. In which, each adjacent pair of the elements is compared and swapped if they are not in the correct order. This algorithm is not suitable for large datasets as its average and worst case complexity are of  Ο(n2,where n is the number of times.

Algorithm :

begin BubbleSort(list)

for all elements of list
    if list [i] > list [i + 1]
            swap (list [i], list [i+1])
    end if
end for

    return list
end BubbleSort

Example:

Let us take an array of unsorted element. As the algorithm takes Ο(n2), we keep it short and simple.
The algorithm will move in rounds.

Round 1 :

                                            52,  65 , 24 , 31 , 10

The algorithm will start with the comparison of first two elements by comparing which one is greater.

                                            5265 , 24 , 31 , 10

In the above case, 52 is smaller than 65, so it is already in a sorted locations. So the algorithm will sort next two numbers i.e. 65 and 24.

                                             52,  65 , 24 , 31 , 10

In the above case, 24 is smaller than 65, so it will be swapped to enter into the correct location.

                                             52, 24 , 65 , 31 , 10

Now the algorithm will move forward and compare the elements 65 and 31

                                             52, 24 , 65 , 31 , 10

As the element 31 is smaller than 65 it will be swapped.

                                             52, 24 , 31 , 65 , 10

Now the algorithm will move forward and compare the elements 65 and 10

                                             52, 24 , 31 , 65 , 10

As the element 10 is smaller than 65 it will be swapped.

                                             52, 24 , 31 , 10 , 65

As all the elements are sorted in the array, the algorithm will move to round 2 till it reaches the correct order locations of each element.

Round 2 :

From Round 1 we have the sorted array as:

                                            52, 24 , 31 , 10, 65

In the above case, 24 is smaller than 52, so it will be swapped to enter into the correct location.

                                             24, 52 , 31 , 10 , 65

Now the algorithm will move forward and compare the elements 52  and 31

                                             24, 52 , 31 , 10 , 65

As the element 31 is smaller than 52 it will be swapped.

                                             24, 31 , 52 , 10 , 65

Now the algorithm will move forward and compare the elements 52 and 10

                                             24, 31 , 52 , 10 , 65

As the element 10 is smaller than 52 it will be swapped.

                                             24, 31 , 10 , 52 , 65

As the element 52 is smaller than 65 it will not be swapped as they are already in their correct position.
                                     
                                             24, 31 , 10 , 52 , 65

Yet the array from round 2 is not completely sorted, hence it will move into round 3.

Round 3 :

                                            24, 31 , 10 , 52 , 65

In the above case, 24 is smaller than 31, so it will not be swapped as they are already in their correct position.

                                             2431 , 10 , 52 , 65

Now the algorithm will move forward and compare the elements 31  and 10

                                             24, 31 , 10 , 52 , 65

As the element 10 is smaller than 31 it will be swapped.

                                             24, 10 , 31 , 52 , 65

Now the algorithm will move forward and compare the elements 31 and 52 

                                             24, 10 , 31 , 52 , 65

As the element 31 is smaller than 52 it will not be swapped as they are already in their correct position.

                                             24, 10 , 31 , 52 , 65

Now the algorithm will move forward and compare the elements 52 and 65

                                              24, 10 , 31 , 52 , 65

As the element 52 is smaller than 65 it will not be swapped as they are already in their correct position.
                                     
                                              24, 10 , 31 , 52 , 65

Yet the array from round 3 is not completely sorted, hence it will move into round 4.

Round 4 :

                                               24, 10 , 31 , 52 , 65

In the above case, 10 is smaller than 24, so it will be swapped.

                                              24, 10 , 31 , 52 , 65

Now the algorithm will stop as all the elements are in their sorted positions.

                                              1024 , 31 , 52 , 65

Hence in this way we sort the elements by using bubble sort.

Step 1 : Create an array named str[] and store the values in it.

Step 2 : Create another variable temp

Step 3 : Print "Strings in sorted order" by using System.out.println method

Step 4 : Apply for loop of j , initialize j to 0, j should be smaller than str.length and increment j.

Step 5 : Apply for loop of i, initialize i = j + 1, i should be smaller than str.length and increment i.

Step 6 : Compare the adjacent strings. If str[i].compareTo(str[j])  is smaller than 0, then store str[j] in temp, store str[i] in str[j], and store temp in str[i].

Step 7 : Print str[i]

Program :

public class BubbleSort 
    {
        public static void main(String[] args)
        {
            String str[] = { "Rekha", "Mamta", "Trupti", "Sandhya"};
            String temp;
            System.out.println("Strings in sorted order:");
            for(int j = 0; j < str.length; j++)
            {
                for(int i = j + 1; i < str.length; i++)
                {
                    //comparing adjacent strings
                    if(str[i].compareTo(str[j]) < 0)
                    {
                        temp = str[j];
                        str[j] = str [i];
                        str[i] = temp;
                    }
                }
                System.out.println(str[j]);
                }
        }
}

Output :

Strings in sorted order:
Mamta
Rekha
Sandhya
Trupti

For execution of the same do visit my YouTube Channel :








                                            








Java Program To Reverse Words in a String


Java Program To Reverse Words in a String

In this Java Program, we will perform the reverse operation on the words in a string.

Logic : String[] words = str.split(" ");
             String reversedString = "";
             for(int i = 0; i < words.length; i++)
             {
                String word = words[i];
                String reverseWord = "";
                for (int j = word.length()-1; j >= 0; j--)
                {         
                    reverseWord = reverseWord + word.charAt(j);    
                }
                reversedString = reversedString + reverseWord + " ";   
            }

Step 1 : Create class ReverseWords

Step 2 : Create methods reverseWordInMyString(String str)

Step 3 : Create an array of String named words and store the result of split method.
             str.split(" "); is a split method of string class which splits the string in several strings.

Step 4 : Create a variable reversedString = "";

Step 5 : Apply for loop in which variable i should be initialized to 0, i should be less than words.length and increment i.

Step 6 : In that for loop, create a variable word and store the result of words[i]. words[i] is the word from the for loop.

Step 7 : Create variable ReverseWord

Step 8 : Apply another for loop. Initialize j to word.length()-1, j should be greater than equal to 0 and decrement j.

Step 9 : Store the addition of the reverseWord and word.charAt(j) in the reverseWord variable.
The charAt(i) function returns the character at the given position in a string.

Step 10 : Close the for loop of j.

Step 11 : Store the result of reversedString + reverseWord + " " in the variable reverseString.

Step 12 : Close the for loop of i.

Step 13 : Print the String & reverseString.

Step 14 : Create the main method

Step 15 : Create the object of the ReverseWords Class

Step 16 : Call the method reverseWordInMyString of the ReverseWords Class.

Program :

public class ReverseWords 
    {
        public void reverseWordInMyString(String str)
        {
            //The split() method of String class splits string in several strings
            //based on the delimeter passed as an argument to it
            
            String[] words = str.split(" ");
            String reversedString = "";
            for(int i = 0; i < words.length; i++)
            {
                String word = words[i];
                String reverseWord = "";
                for (int j = word.length()-1; j >= 0; j--)
                {
                    //The charAt() function returns the character at the given position in a string
                    
                    reverseWord = reverseWord + word.charAt(j);
                    
                }
                reversedString = reversedString + reverseWord + " ";
                
            }
            System.out.println(str);
            System.out.println(reversedString);
            
        }
        public static void main(String[] args)
        {
            ReverseWords obj = new ReverseWords();
            obj.reverseWordInMyString("Welcome to my channel");
            obj.reverseWordInMyString("This is an easy Java Program");
            
        }
}

Output :

Welcome to my channel
emocleW ot ym lennahc 
This is an easy Java Program
sihT si na ysae avaJ margorP

For execution of the same do visit my YouTube Channel : 

Java Program To Check Palindrome Using While Loop with charAt() Function


Java Program To Check Palindrome Using While Loop with charAt() Function

In this Java Program, we check palindrome using for loop with charAt() function.

A word, phrase, number or sequence of words that reads same backward as forward are known as palindromes.

E.g. madam, civic, level, refer, 121 1331 etc.

Logic :   while(i >= 0)
            {
                reverseString = reverseString + inputString.charAt(i);
                i--;
            }

Step 1 : Import java.util.Scanner

Step 2 : Create variable reverseString

Step 3 : Ask the user to enter the string

Step 4 : Create variable inputString and store the user entered input into it.

Step 5 : Create variable length and store the length of the user entered input string into it.

Step 6 : Create variable i and store the length-1 output in it.

Step 7 : Apply while loop

Step 8 : Apply if else and display the result.

Program :

import java.util.Scanner;

public class PalindromeWhile 
    {
        public static void main(String[] args)
        {
            String reverseString="";
            
            Scanner sc = new Scanner(System.in);
            
            System.out.println("Enter the String:");
            String inputString = sc.nextLine();
            
            int length = inputString.length();
            
            int i = length-1;
            while(i >= 0)
            {
                reverseString = reverseString + inputString.charAt(i);
                i--;
            }
            
            if(inputString.equals(reverseString))
                System.out.println("Input String is a Palindrome");
            else
                System.out.println("Input String is not a Palindrome");
        }
    
}

Output :

Enter any String : abccba
abccba
The input String is a Palindrome.

Enter any String : abc
abc
The input String is not a Palindrome.

For execution of the same do visit my YouTube Channel :




ITEngineer8995
India

SEND ME A MESSAGE

Search This Blog

Powered by Blogger.

Pages