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 

 

java program to capitalize the first character of each word in a String

java program to capitalize the first character of each word in a String

 

Capitalize The First Character Of Each Word In A String


Capitalize The First Character Of Each Word In A String

In this java program, all the words of a sentence will be capitalized. Here we are going to explain step by step.

Step 1: A string is taken as String st1="the red book keep on the red table with a red paper";

Here we need to convert with the help of the Java Program to capitalize each word in String.

Step 2: Then we have to define a string as word and st2 and each initialize as st1=”” and word=”” and also some more variables as String first (for first the character of each word), excpetFirstChar(for entire characters except the first character), char ch, ch1 for taking the character from each word.

Step 3: st1= st1  + " "; in this line, space is added as here we have to take characters of each word till space encounter, so last word of the string has no space so the space is added.

Step 4:  length=st1.length(); length of the string has been taken.

Spep 5: In the for loop we have to take one character with the help of charAt() method and add to word variable till space is encounter. If encounter the need to convert the first character of word capital and the rest to remain small and this done with this statement   

  • first=word.substring(0,1); 
  • excpetFirstChar=word.substring(1);
  • st2+=first.toUpperCase()+excpetFirstChar+" ";

This process will be run till the end of the sentence

Step 6 : Print the value finally.

 

public class CapitalizeEachWord

{

    public static void main(String args[])

    {

        String st1="the red book keep on the red table with a red paper";

        String st2="", word="";

        String first, excpetFirstChar;

        char ch, ch1;

        int i, length, chartoint;

        st1= st1  + " ";

       //st1 = " " + st1;

        length=st1.length();

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

        {

            ch=st1.charAt(i);

            if(ch!=' ')

            {

                word = word + ch;

            }

            else

            {

                first=word.substring(0,1); 

                excpetFirstChar=word.substring(1);

                st2+=first.toUpperCase()+excpetFirstChar+" ";

                word="";

            }

        }

        System.out.println("Original Sentence:" + st1);

        System.out.println("Changed Sentence :" + st2);

    }          

}

Output:

Original Sentence: the red book keep on the red table with a red paper

Changed Sentence: The Red Book Keep On The Red Table With A Red Paper


More Java programs