Java program to find total vowel in each word of a sentence

Java program to find total vowel in each word of a sentence

Java program to find total vowel in each word of a sentence

Java program to find total vowel in each word of a sentence


This program will print the total vowel of each word in a given sentence. We have taken a sentence in the 'st' variable and find the total length of the sentence. After then a for loop started and pick a word. Then find the length of the word and then find the vowels. Then print the word with a total number of vowels.

Vowel finder program

import java.util.*;
public class PrintEachWordFromSent
{
    public static void main(String args[])
    {
        int i, l, l1, j;
        String st, word;
        int v;
        // Initialize the variable to 0
        v = 0;
        word = "";
        char ch, ch1;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a sentence :");
        st = sc.nextLine();
        st = st + " ";
        l = st.length(); // Total length of the String
        for(i=0; i<l ;i++)
        {
            ch = st.charAt(i);
            if(ch != ' ')
            {
                word = word + ch;
            }
            else
            {
                l1 = word.length();
                for(j=0;j<l1;j++)
                {
                    ch1 = st.charAt(j);
                    if(ch1 == 'A' || ch1 == 'a' ||ch1 == 'E' ||ch1 == 'e' ||ch1 == 'I' ||ch1 == 'i' ||
                     ch1 == 'O' ||ch1 == 'o' ||ch1 == 'U' ||ch1 == 'u' )
                     v++;
                }
                System.out.println("Total Vowels contain in " + word + "=" + v);
                word="";  
                v = 0;
            }              
        }
    }
}


Output

Enter a sentence :
today is very cold out there
Total Vowels contain in today=2
Total Vowels contain in is=1
Total Vowels contain in very=2
Total Vowels contain in cold=2
Total Vowels contain in out=1
Total Vowels contain in there=2

Java program for Prime no. and AutomorphicNumber

Java program for Prime no. and AutomorphicNumber

Java program for Prime no. and Automorphic Number

Automorphic Number

According to wikipedia "In mathematics an automorphic number (sometimes referred to as a circular number) is a number whose square "ends" in the same digits as the number itself. For example, 52 = 25, 62 = 36, 762 = 5776, and 8906252 = 793212890625, so 5, 6, 76 and 890625 are all automorphic numbers. The only automorphic Kaprekar number is 1, because the square of a Kaprekar number cannot start with zero. " Source: https://en.wikipedia.org/wiki/Automorphic_number.

Prime Number: The number divisible by 1 and by itself is called prime number.
Here using switch case (menu driven) we are going to write the following program.

import java.io.*;
public class MenuPrimeAutomorphicNumber
{
   public static void main(String args[])throws IOException
   {
       int i,ch,a,no,m,f=1,c=0,p;
       InputStreamReader in=new InputStreamReader(System.in);
       BufferedReader br=new BufferedReader(in);
       System.out.println("1. For Prime number");
       System.out.println("2. For Automorphic number");
       System.out.print("Enter your choice:");
       ch=Integer.parseInt(br.readLine());
       switch(ch)
       {
           case 1:
            System.out.print("Enter number to check prime:");
            no=Integer.parseInt(br.readLine());
            for(i=2;i<no;i++)
            {
                if(no%i==0)
                {
                    f=0;
                    break;
                }
            }
            if(f==1)
             System.out.print("Prime number");
            else
             System.out.print("Not Prime number");
            break;
           case 2:
            System.out.print("Enter number to check Automorphic:");
            no=Integer.parseInt(br.readLine());
            m=no*no;
            while(m!=0)
            {
                m/=10;
                c++;
            }
            p=(int)Math.pow(10,c-1);
            a=no%p;
            if(no==a)
             System.out.println("Automorphic number");
            else
             System.out.println("Not Automorphic number");
            break;
           default:
            System.out.println("Wrong choice");
        }
    }
             
}

Output

1. For Prime number
2. For Automorphic number
Enter your choice:1
Enter number to check prime:5

Prime number
1. For Prime number
2. For Automorphic number
Enter your choice:2
Enter number to check Automorphic: 25
Automorphic number

Also visit
Kaprekar number


Characters program in java

Characters program in java

Frequency of Characters program in java



Characters program in java

class FrequencyOfChar
{
public static void main(String args[])
{
String st="hello Hi, Bolo Jara to";
int i,l,j,f=0;
char ch;
l=st.length();
st=st.toUpperCase();
for(i=65;i<=90;i++)
{

for(j=0;j<l;j++)
{
ch=st.charAt(j);
if(ch==(char)i)
f++;
}
System.out.println((char)i+" appears="+f);
f=0;
}
}
}


Java program to find total Vowels of words from a given sentence.


public class VowelFromWord
{
    public static void main(String args[])
    {
        // Sentence taken
        String st="the quick brown dog jumpe over a lazy fox";
        String word=""; // initialize the variable word
        int i,l,l1,j,v;
        char ch,ch1;
        st=st+" ";
        l=st.length();
        for(i=0;i<l;i++)
        {
            v=0;
            ch=st.charAt(i);
            if(ch!=' ')
            {
                word=word+ch;  // picking the word
            }
            else
            {
                l1=word.length();
                for(j=0;j<l1;j++)
                {
                    ch1=word.charAt(j);
                    if(ch1=='a' || ch1=='e' || ch1=='i'|| ch1=='o' || ch1=='u')
                     v++;   // calculating the vowel in the picked word
                }
                // printing the word with vowel
                System.out.println(word+" contain " + v+" vowel");
                        word="";
            }
        }
    }
}

Output
quick contain 2 vowel
brown contain 1 vowel
dog contains 1 vowel
jump contains 2 vowel
over contain 2 vowel
a contain 1 vowel
lazy contain 1 vowel
fox contains 1 vowel

Java program to find a total number of characters from a given sentence.


import java.io.*;
public class CharacterFinder
{
    public static void main(String args[])throws IOException
    {
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        char ch,ch1;
        int i,l,c=0;
        String st;
        System.out.print("Enter String :");
        st=br.readLine();
        l=st.length();
        System.out.print("Enter a Character :");
        ch1=(char)br.read();
        for(i=0;i<l;i++)
        {
            ch=st.charAt(i);
            if(ch==ch1)
             c++;
        }
        System.out.println("Total :"+c);
    }
}

Output
Enter String: The quick brown fox
Enter a Character: q
Total: 1
Enter String: He is a good boy. He is doing a good job.
Enter a Character :o
Total: 7


Java program to find Word pattern by giving a position


import java.util.*;
public class StringPattern
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        String st;
        int i,l,j,n;
        char ch;
        System.out.println("Enter a String and position:");
        st=sc.next();
        n=sc.nextInt();
        l=st.length();
        
        for(i=0;i<l-n;i++)
        {
            for(j=0;j<l-n;j++)
            {
                System.out.print(" ");
            }
            ch=st.charAt(i);
            System.out.println(ch);
        }
        System.out.println(st);
        for(i=0;i<l-n;i++)
        {
            for(j=0;j<l-n;j++)
            {
                System.out.print(" ");
            }
            ch=st.charAt(i+n);
            System.out.println(ch);
        }
    }
}
Output
HELLO
3
  H
  E
HELLO
  L
  O
Enter a String and position:
EDUCATION
4
        E
        D
        U
        C
        A
EDUCATION
        A
        T
        I
        O
        N

Visit also


Menu driven program in java using switch case

Menu driven program in java using switch case

MENU DRIVEN PROGRAM IN JAVA


Menu-driven program in java using switch case

Write a java program to print series 0, 3, 8, 15, 24, and s=1/2+3/4+5/6+..+19/20 with help of Menu using a switch case statement.

Solution:
import java.io.*;
public class MenuSeries
{
    public static void main(String args[])throws IOException
    {
        InputStreamReader in=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(in);
        char choice;
        int n,i;
        double s=0;
        System.out.print("Enter n number:");
        n=Integer.parseInt(br.readLine());
        System.out.println("A or a for series 0,3,8,15,24……");
        System.out.println("B or b fro series s=1/2+3/4+5/6+..+19/20");
        System.out.println("Enter your choice:");
        choice=(char)br.read();
        switch(choice)
        {
            case 'A':
            case 'a':
           
                for(i=1;i<=n;i++)
                {
                    System.out.print((i*i)-1+",");
                }
            break;
            case 'B':
            case 'b':
           
                for(i=2;i<=20;i+=2)
                {
                    s=s+(double)(i-1)/i;
                }
                System.out.println("Sum="+s);
                break;
            default:
                System.out.print("Wrong choice.....");
        }
    }
}

Java Menu driven program to find Composite number and Smallest digit search

Java Menu driven program to find Composite number and Smallest digit search

import java.io.*;
public class Menu
{
    public static void main(String args[])throws IOException
    {
        InputStreamReader in=new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        int n, count, ch, small, a, i;
        System.out.println("Enter 1 for Composite number");
        System.out.println("Enter 2 for Smallest digit search");
        System.out.print("Enter your choice :");
        ch=Integer.parseInt(br.readLine());
        switch(ch)
        {
            case 1:
                count=0;
                System.out.print("Enter a number :");
                n= Integer.parseInt(br.readLine());
                for(i=2; i<=n/2; i++)
                {
                    if(n % i == 0)
                     count++;
                }
                if(count>0)
                 System.out.println("Entered number is Composite number");
                else
                 System.out.println("Entered number is not Composite number");
                break;
            case 2:
                System.out.print("Enter a number :");
                n= Integer.parseInt(br.readLine());
                small=99;
                while(n!=0)
                {
                    a = n % 10;
                    if(a < small)
                    {
                        small = a;
                    }
                    n = n / 10;
                }
                System.out.println("Smallest number is " + small);
                break;
            default:
                  System.out.println("Wrong choice");
        }
    }
               
}
menu driven calculator

MENU DRIVEN PROGRAM IN JAVA SIMPLE CALCULATOR

import java.io.*;
public class Menu
{
    private int a;  //Instance Variable
    private int b;
    private int c;
    private static InputStreamReader in=new InputStreamReader(System.in);
    private static BufferedReader br=new BufferedReader(in);
    void menu()
    {
        //System.out.println("\f");
        System.out.println("1. Addition");
        System.out.println("2. Subtraction");
        System.out.println("3. Multiplication");
        System.out.println("4. Division");
        System.out.println("5. Exit");
        System.out.println("Press your choice.........>");
    }

    void sum (int a, int b)
    {
        this.a=a;
        this.b=b;
        this.c=this.a+this.b;
        System.out.println("Sum="+this.c);
        // this.a=Integer.parseInt(br.readLine());
    }

    void sub (int x, int y)
    {
        a=x;
        b=y;
        c=a-b;
        System.out.println("Subtraction="+c);
    }

    void mult (int x, int y)
    {
        a=x;
        b=y;
        c=a*b;
        System.out.println("Multplication="+c);
    }

    void div (int x, int y)
    {
        a=x;
        b=y;
        c=a/b;
        System.out.println("Division="+c);
    }

    public static void main(String args[])throws IOException
    {
        Menu ob=new Menu();

        int ch,a,b;
        ch=0;
        do
        {
            ob.menu();
            ch=Integer.parseInt(br.readLine());
            switch(ch)
            {
                case 1:
                System.out.println("Enter two number");
                a= Integer.parseInt(br.readLine());
                b= Integer.parseInt(br.readLine());
                ob.sum(a, b);
                break;
                case 2:
                System.out.println("Enter two number");
                a=Integer.parseInt(br.readLine());
                b=Integer.parseInt(br.readLine());
                ob.sub(a, b);
                break;
                case 3:
                System.out.println("Enter two number");
                a=Integer.parseInt(br.readLine());
                b=Integer.parseInt(br.readLine());
                ob.mult(a, b);
                break;
                case 4:
                System.out.println("Enter two number");
                a=Integer.parseInt(br.readLine());
                b=Integer.parseInt(br.readLine());
                ob.div(a, b);
                break;
                case 5:
                System.exit(0);
                default:
                System.out.println("Wrong choice:");
            }
        }while(ch!=5);
    }       
}

Sample Output
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Press your choice.........>
1
Enter two number
2
5
Sum=7
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Press your choice.........>


5


Write a menu driven program to find the sum of the following series depending on the user choosing 1 or 2

1. S=1/4+1/8+1/12………upto n terms
2. S=1/1! - 2/2! + 3/3!…….upto n terms
Where ! Stands for factorial of the number and the factorial value of a number is the product of all integers from 1 to that number, e.g. 5! = 1 × 2 × 3 × 4 × 5.  (use switch-case).       


import java.util.*;
public class SeriesClass
{
    double s;
    int f, i, j , t, n;
    SeriesClass() // Constructor
    {
        s=0.0;
    }

    double series1(int no)
    {

        t=4;
        n=no;
        for(i=0; i<n; i++)
        {
            s = s + (double)1/t; // 1/4, 1/8, 1/12......
            t=t+4;
        }
        return s;
    }

    double series2(int no)
    {
        n=no;
        for(i=1; i<=n; i++)
        {
            f=1;
            //for factorial
            for(j=1;j<=i; j++)
            {
                f=f*j;
            } // end of factorial
            if(i%2==0)
            {
                s = s - (double)i/f;
            }
            else
            {
                s = s + (double)i/f;
            }
        }
        return s;
    }
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        SeriesClass ob=new SeriesClass();
        int ch, num;
        double sum;
        System.out.println("1 for S=1/4+1/8+1/12………upto n terms");
        System.out.println("2 for S=1/1! - 2/2! + 3/3!…….upto n terms");
        System.out.println("Enter your choice---->");
        ch=sc.nextInt();
        switch(ch)
        {
            case 1:
            System.out.println("Enter n terms:");
            num=sc.nextInt();
            sum=ob.series1(num);
            System.out.println("Sum="+sum);
            break;
            case 2:
            System.out.println("Enter n terms:");
            num=sc.nextInt();
            sum=ob.series2(num);
            System.out.println("Sum="+sum);
            break;
            default:
            System.out.println("Wrong Choice...");
        }
    }
}

Variable/Method Description or Documentation
Sr. No
Variable / Method
Type
Description
1
s
double
d is assign for Summation
2
I, j
int
i and j are use for Loop
3
t
int
For increment 4
4
n
int
N is use to assign the number
5
SeriesClass()

It is a constructor
6
series1(int no)
double
For the first series
7
Series2(int no)
double
For the second series
8
main()
void
This is a main method from where java program will run
9
sc
object
Object of Scanner class
10
ob
Object
Object of Class SeriesClass
11
ch
int
It is used for choice
12
num
int
For number
13
sum
double
For summation