Showing posts with label Java Programs. Show all posts
Showing posts with label Java Programs. Show all posts

Java Programs


Set array elements to 1 if Prime

  • Program which will set the array element a[i] to 1 if i is prime, and to 0 if i is not prime.
Program

import java.util.Scanner;

public class PrimeArray {

            private static Scanner sc;
            public static void main(String[] args) {
                       
                        int limit,value;
                        PrimeArray ob=new PrimeArray();
                        sc=new Scanner(System.in);
                        int[] number=new int[1000];
                        System.out.println("Enter the number of elements in the array:");
                        limit=sc.nextInt();
                        System.out.println("Enter the elements of the array:");
                        for(int i=0;i<limit;i++) {
                                    number[i]=sc.nextInt();
                        }
                        for(int i=0;i<limit;i++) {
                                    value=ob.checkPrime(number[i]);
                                    number[i]=value;
                        }
                        System.out.println("Array after re-setting:");
                        for(int i=0;i<limit;i++) {
                                    System.out.println(number[i]);
                        }
                       
            }
           
            public int checkPrime(int num) {
                       
                        int flag=0;
                        for(int i=1;i<=num;i++) {
                                    if(num%i==0) {
                                                flag++;
                                    }
                        }
            if(flag==2) {
                        return 1;//is prime
            }
            else {
                        return 0;//is not prime
            }
                       
            }

}

Output

Enter the number of elements in the array:
5
Enter the elements of the array:
19
4
7
3
23
Array after re-setting:
1
0
1
1
1


Count vowels in a string

  • Program to count the number of vowels in a given string.
Program

import java.util.Scanner;

public class CountVowels {

            private static Scanner sc;
            public static void main(String[] args) {
                       
                        int flag=0,count=0;
                        sc=new Scanner(System.in);
                        System.out.println("Enter the String:");
                        String str=sc.nextLine();
                        for(int i=0;i<str.length();i++) {
                                    if((str.charAt(i)=='a')||(str.charAt(i)=='A')||(str.charAt(i)=='e')||(str.charAt(i)=='E')||
                                                            (str.charAt(i)=='i')||(str.charAt(i)=='I')||(str.charAt(i)=='o')||(str.charAt(i)=='O')||
                                    (str.charAt(i)=='u')||(str.charAt(i)=='U')) {
                                               
                                                count++;
                                                flag=1;
                                    }
                        }
                        if(flag==0) {
                                    System.out.println("There are no vowels in the string.");
                        }
                        else {
                                    System.out.println("The string contains "+count+" vowels.");
                        }
            }

}

Output

Enter the String:
Count the number of vowels.

The string contains 8 vowels.

Java Programs


Sort in Dictionary Order

  • Program that accepts up to n words and outputs them in dictionary order. (Hint: sort an array of numbers that represent the words; don’t directly sort the words.) Do NOT use the sort functions provided in the standard library.
Program

import java.util.Scanner;

public class SortAsDictionary {

            private static Scanner input;
            private static int limit;
            public static void main(String[] args) {
                        input=new Scanner(System.in);
                        System.out.println("Enter the limit:");
                        limit=input.nextInt();
                        String[] arr=new String[limit];
                        System.out.println("Enter the elements of the array:");
                        for(int j=0;j<limit;j++) {
                                                arr[j]=input.next();
                                    }
                        SortAsDictionary ob=new SortAsDictionary();
                        ob.sort(arr);
            }

            public void sort(String[] arr) {
                       
                        for(int i=0;i<limit;i++) {
                                    for(int j=i+1;j<limit;j++) {
                                                if(arr[j].compareTo(arr[i])<0) {
                                                            String temp=arr[i];
                                                            arr[i]=arr[j];
                                                            arr[j]=temp;
                                                }
                                    }
                        }
                        System.out.println("The sorted array is:");
                        for(int j=0;j<limit;j++) {
                                                System.out.println(arr[j]);
                                    }
            }
}

Output

Enter the limit:
5
Enter the elements of the array:
Ricky
Michael
Watsnon
Shane
Johnson
The sorted array is:
Johnson
Michael
Ricky
Shane
Watsnon


Count the words in a file

  • Program to count number of words in a given text file.
Program

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class CountWordsInFile {

            private static Scanner input;
            public static void main(String[] args) throws FileNotFoundException {
                       
                        int count=0;
                        input=new Scanner(new File("path\\test.txt"));
                        while(input.hasNext()) {
                                    input.next();
                                    count++;
                        }
                        System.out.println("The file contains "+count+" words.");
            }
}

Output

The file contains 51 words.


Java Programs


Partial Search for a string in an array

  • Function to check whether a string exists in an array of strings.The function should do a partial search for the string.
Program

import java.util.Scanner;

public class StringSearch {

            private static Scanner input;
            private static int limit,flag=0;
            public static void main(String[] args) {
                       
                        input=new Scanner(System.in);
                        System.out.println("Enter the limit of the array:");
                        limit=input.nextInt();
                        String[] str=new String[limit];
                        System.out.println("Enter the strings of the array:");
                        for(int i=0;i<limit;i++) {
                                    str[i]=input.next();
                        }
                        StringSearch.search(str);

            }
           
            public static void search(String[] str) {
                        String item;
                        System.out.println("Enter the element to be searched:");
                        item=input.next();
                        for(int i=0;i<limit;i++) {
                                    if(str[i].contains(item)) {
                                                flag=1;
                                                System.out.println("String was found at the position:"+(i+1));
                                    }
                        }
                        if(flag==0) {
                                    System.out.println("No such element");
                        }
            }

}

Output

Enter the limit of the array:
5
Enter the strings of the array:
Sehwag
Sachin
Kohli
Yuvraj
Dhoni
Enter the element to be searched:
Yuv
String was found at the position:4


Row-Wise Sorting of Array elements

  • Program to sort array elements (row wise).
Program

import java.util.Scanner;

public class RowWiseSort {

            private static Scanner input;
            private static int r,c;
            public static void main(String[] args) {
                        input=new Scanner(System.in);
                        System.out.println("Enter the no. of rows and columns of the array:");
                        r=input.nextInt();
                        c=input.nextInt();
                        int[][] arr=new int[r][c];
                        System.out.println("Enter the elements of the array:");
                        for(int j=0;j<r;j++) {
                                    for(int i=0;i<c;i++) {
                                                arr[j][i]=input.nextInt();
                                    }
                                   
                        }
                        RowWiseSort ob=new RowWiseSort();
                        ob.sort(arr);

            }
           
            public void sort(int[][] arr) {
                        int temp;
                        for(int j=0;j<r;j++) {
                                    for(int i=0;i<c;i++) {
                                                for(int k=0;k<c-1;k++) {
                                                            if(arr[j][k]>arr[j][k+1]) {
                                                                        temp=arr[j][k];
                                                                        arr[j][k]=arr[j][k+1];
                                                                        arr[j][k+1]=temp;
                                                            }
                                                }
                                    }
                        }
                        System.out.println("The row vise sorted array is:");
                        for(int j=0;j<r;j++) {
                                    for(int i=0;i<c;i++) {
                                                System.out.print(arr[j][i]+"\t");
                                    }
                                    System.out.println();
                        }
            }

}

Output

Enter the no. of rows and columns of the array:
3 3
Enter the elements of the array:
3 2 1
6 5 4
9 8 7
The row vise sorted array is:
1          2          3         
4          5          6         
7          8          9