DEAR FRIENDS, I M NEW TO JAVA,

PLEASE ANSWER MY QUESTIONS:
1. I am trying to use float and double in java? can u tell me how to use it?
2. I am trying to calculate simple interest in java ?

please fix my code:

public class simple
{
    public static void main (String[] args)
    {
        // this program calculates the simple interest.
        int principal, noy;
        double rate, si;

        principal = 1000;
        noy = 3;
        rate = 2.5;
        si = (principal * noy * rate) /1000;

        System.out.printf (" The principal is %d \n number of years: %d, \nrate is: %lf \n The calculated simple interest is:%lf\n ", principal,noy,rate,si);

    }

}

the code has error i know. %lf doesnt work.

please fix this.

Recommended Answers

All 6 Replies

Your code is basically correct. Please use the tag [CODE] for your code.
I have made a small modification. I am not sure if there is a term called "accumulated interests" which is different from simple interests.

import java.lang.*;
public class simple {
public static void main (String[] args){
// this program calculates the simple interest.
int principal, noy;
double rate, si;
principal = 1000;
noy = 3;
rate = 2.5;
si = (principal * noy * rate) /1000;
double d = (Math.pow((1.0 + rate/100), (double)noy) - 1)*100;
System.out.printf (" The principal is %d \n number of years: %d, \nrate is: %f \n The calculated simple interest is:%f\n ", principal,noy,rate,si);
System.out.printf("Accumulated interests: %5.2f\n", d);  // using C_style to define the output of the  double
    }
}

output:
The principal is 1000
number of years: 3,
ate is: 2.500000
The calculated simple interest is:7.500000
Accumulated interests: 7.69

commented: do not simply do other's homework. At the very least [i]explain[/i] the changes. Better, just tell them what to change. They don't learn anything this way. -2

DEAR FRIENDS, I M NEW TO JAVA,

PLEASE ANSWER MY QUESTIONS:
1. I am trying to use float and double in java? can u tell me how to use it?
By using the types float and double?

  1. I am trying to calculate simple interest in java ? please fix my code:
public class simple
{
    public static void main (String[] args)
    {
        // this program calculates the simple interest.
        int principal, noy;
        double rate, si;

        principal = 1000;
        noy = 3;
        rate = 2.5;
        si = (principal * noy * rate) /1000;

        System.out.printf (" The principal is %d \n number of years: %d, \nrate is: %lf \n The calculated simple interest is:%lf\n ", principal,noy,rate,si);

    }

}

the code has error i know. %lf doesnt work.

please fix this.

Okay. Fixed.

Seriously now. What compiler message and/or exception are you getting? If none, than what is your app actually producing and what do you expect it to deliver, and how do those two differ?

I, for one, am not going to play compiler, jvm, and consumer to try and figure it out, you tell me. Then, also, I am not simply going to "fix your code". I am going to tell you what you are doing wrong and give you hints as to how you can fix it.

Your code is basically correct. Please use the tag [CODE] for your code.
I have made a small modification.

Do not simply do other's homework. At the very least explain the changes. Better, just tell them what to change. They don't learn anything this way.

masijade , Thank you for your remind.
I have added code to show the difference between two kinds of interests: simple and accumulated.
I hope gudads will compare the output format between his and mine to make further decision about changes in the code for his output, perhaps also show difference between two kinds of interests if there is.

Thanx for ur reply, I will check the code in morning, by the I m working on mac ox, using java edit for writing the code, and working on terminal for java output.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.