Showing posts with label ICSE and ISC Computer Question Answer. Show all posts
Showing posts with label ICSE and ISC Computer Question Answer. Show all posts
Java program for vowels of each word of a sentence

Java program for vowels of each word of a sentence

Java program for vowels of each words of a sentence


Java program for vowels of each word of a sentence

 The following program will print each word’s total vowels. In the following program we have taken a sentence and then first segregate each word. After then each word's vowels is calculated in the loop.

import java.util.*;

public class Sent2Word

{

    public static void main(String args[])

    {

        String st, word="";

        char ch,ch1;

        int v;      // variable for vowel

        int c;      // variable for Consonents

        int w;      // variable for word

        int i,j;    // for loop

        int l,l1;   // l is for length of sentence and for word

        v=c=w=0;    // Initialization

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a sentence:");

        st=sc.nextLine();

        st=st + " ";    // one space add to sentence

        st=st.toUpperCase();

        l=st.length();  // length of the sentence

        for(i=0; i<l; i++)

        {

            ch=st.charAt(i);

            // if space is come then goto else calculate each words vowel

            if(ch!=' ')

            {

                word=word + ch;

            }

            else

            {

                l1=word.length();   // length of the word

                for(j=0; j<l1; j++)

                {

                    ch1=st.charAt(j);

                    if(ch1=='A' || ch1=='E' || ch1=='I' || ch1=='O' || ch1=='U')

                    {

                        v++;

                    }                  

                }

                // print the word total vowels

                System.out.println(word + " contain vowel=" + v);

                // Initialize the words and v for vowel

                word="";   

                v=0;

            }

        }

    }

}

 

Output:

Enter a sentence:

the quick brown dog

THE contain vowel=1

QUICK contain vowel=1

BROWN contain vowel=1

DOG contain vowel=1


More Java programs 

 

Selection sort in java with two arrays

Selection sort in java with two arrays

 

selection sort in java


Selection sort in java with two values (arrays)

Selection sort
In the following program, we have done a selection sort on two arrays, age, and name. Here selection sorting is done by name in ascending order.

public class SelectionSortWithTwoValue

{

    public static void main(String args[])

    {

        int age[]={15,17,20,18,14};  // age array

        String name[]={"Habib","Junaid","Vikash", "Anthony", "Dhoni"}; // name array

        int i, j, temp1;

        String temp2;   // temporary variable for name for swaping data

        System.out.println("Name\t\tAge");

        System.out.println("=====================");

        for(i=0;i<5;i++)

        {

             System.out.println(name[i] + "\t\t" + age[i]);

        }

// selection sort is started

        for(i=0;i<4;i++)

        {

            for(j=i+1;j<5;j++)

            {

                if(name[i].compareTo(name[j])>0)

                {

                    temp2=name[i];

                    name[i]=name[j];

                    name[j]=temp2;

                    temp1=age[i];

                    age[i]=age[j];

                    age[j]=temp1;

                }

            }

        }

// selection sort is ended

        System.out.println("Name\t\tAge");

        System.out.println("=====================");

        for(i=0;i<5;i++)

        {

             System.out.println(name[i] + "\t\t" + age[i]);

        }

    }

}

Output

Before sorting
Name Age
=====================
Habib 15
Junaid 17
Vikash 20
Anthony 18
Dhoni 14

After sorting
Name Age
=====================
Anthony 18
Dhoni 14
Habib 15
Junaid 17
Vikash 20

simple interest in java | what is simple interest | ICSE computer science question

simple interest in java | what is simple interest | ICSE computer science question

simple interest in java

What is simple interest?

We do calculate simple interest with principal, rate, and time or term with this formula p*t*r/100. It is not compounded as simple interest only calculated with the principal amount.

In the formula p*r*t/100

  • p is Principal
  • t is time for term
  • r is rate

Simple interest in java | what is simple interest 

import java.util.*;

public class SimpleInterest1

{

    public static void main()

    {

        double p, r, t, si;

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter Principal, Time and Rate--->");

        p=sc.nextDouble();

        t=sc.nextDouble();

        r=sc.nextDouble();

        si=(p*t*r)/100.0;

        System.out.println("Simple Interest=" + si);

    }       

}

In the above program, we have taken Scanner class for inputting principal, rate, and time or term from the user. After taking values of p, t, r then we will calculate simple interest in the si variable.

 

Second program using methods.

import java.util.*;

public class SimpleInterest

{

    double p, t, r, si; //Instance Variable

    public double simpleInterest(double p1, double t1, double r1) //Formal parameter

    {

        p=p1;

        t=t1;

        r=r1;

        si=(p1*t1*r1)/100.0;   

        return si;

    }

    public static void main(String args[])

    {

        SimpleInterest ob=new SimpleInterest();

        double pr, time, rate, s; // Local variable

        Scanner sc= new Scanner(System.in);

        System.out.println("Enter Principal, Time and rate :");

        pr= sc.nextDouble();

        time= sc.nextDouble();

        rate= sc.nextDouble();

        s= ob.simpleInterest(pr, time, rate); // Function call, with parameter(Actual parameter)

        System.out.println("Simple Interest=" + s);

    }

}

In the above program, we have calculated simple interest using the return method. All the values have taken from the main() method and send the values with the method as an actual parameter and that value have received by simpleInterest (double p1, double t1, double r1) method. After calculating the value, the method sends or returns it to the main() method and then prints it on the screen.

 

Question for ICSE exam

ABC bank declares a new tariff or rate for term deposit schemes for their general customers and senior citizens as given below:

Term

Rate of Interest for General

Rate of Interest for Senior Citizen

Up to 2 year

7.5%

8.0%

Up to 4 years

8.5%

9.0%

Up to 6 years

9.5%

10.0%

More than 6 years

10.0%

11.0%

The 'senior citizen's rates are applicable to the customers whose age is 65 years or more. Write a program to input the principal in term deposit scheme, age of the customer and the term or time. The program displays the information in the following format:

Principal Deposited

Term/Duration

Age

Interest earned

Amount Paid

xxxx

xxxx

xxxx

xxxx

xxxx

 

Solution

import java.util.*;

public class BankDepositSchemes

{

    // instance variables

    double term, principal, interest, amount, rate;

    int age;

    // input method for taking values

    void input()

    {

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter Principal :");

        principal=sc.nextDouble();

        System.out.println("Enter Term :");

        term=sc.nextDouble();

        System.out.println("Enter Age :");

        age=sc.nextInt();

    }

   // Calculate the si

    void calculate()

    {

        if(term<=2)

        {

            if(age>=65)

            {

                rate=8.0;

            }

            else

            {

                rate=7.5;

            }

        }

        else if(term>2 && term<=4)

        {

            if(age>=65)

            {

                rate=9.0;

            }

            else

            {

                rate=8.5;

            }

        }

        else if(term>4 && term<=6)

        {

            if(age>=65)

            {

                rate=10.0;

            }

            else

            {

                rate=9.5;

            }       

        }

        else

        {

            if(age>=65)

            {

                rate=11.0;

            }

            else

            {

                rate=10.0;

            }

        }

        interest=(principal*term*rate)/100.0;

        amount=principal + interest;

    }

    // print the values

    void display()

    {

        System.out.println("Amount Deposited\tTerm\tAge\tInterest Earned\t\tAmount Paid");

        System.out.println(principal + "\t\t\t" + term + "\t" + age + "\t" + interest + "\t" + amount);

    }

// main() method

    public static void main(String args[])

    {

        BankDepositSchemes ob=new BankDepositSchemes();

        ob.input();

        ob.calculate();

        ob.display();

    }           

}


More Java programs