Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: using integers

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default using integers

    hey guys what's up?

    I was trying to solve a problem that I met in my book Java how to program it's about compound interests.

     
    public class Interests {
     
    public static void main (String[] args) {
     
    double p = 1000;
    double rate = 0.05;
    double amount;
    for (int year = 1; year <= 10; year++){
     
    amount = p * (Math.pow((1+rate),years));
    System.out.printf("the amount for year %d is: %,20.2f", year, amount);
    }//end for
     
    }//end main
     
    }//end class

    this was the original code. THE QUESTION IS: Modify the application to use only integers
    to calculate the compound interest. [Hint: Treat all monetary amounts as integral numbers
    of pennies. Then break the result into its dollars and cents portions by using the division and remainder
    operations, respectively. Insert a period between the dollars and the cents portions.]

    THE PROBLEM IS: it's ok to treat all monetary amounts as integral numbers of pennies then break the result into dollars and cents, but how can I convert the rate to an integer without losing data ?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: using integers

    but how can I convert the rate to an integer without losing data ?
    Can you post the program's output and show what the output should be?
    5% = 5 / 100
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: using integers

    actually 5/100 cannot be an integer

    that output should be the amount in pennies then I've to convert it using the reminder and the division operators to dollars and cents.
    for example, if the p = 1000$, the year = 2, the rate = 0.05 I should get 1102.5$
    what should I convert to: the p = 100000 pennies, the year = 2, I don't know what should the rate be but after calculating I should get an integer which should be 110250 ,then I've separate the dollars and the cents using the reminder and the division operator (I already know how to do this).

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: using integers

    Are you getting the desired answer now?
    I don't see where you posted the output from the program and explain what is wrong with it and what you want the output to be.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Non-Repeating Integers
    By Tecness2 in forum Java Theory & Questions
    Replies: 5
    Last Post: June 7th, 2012, 09:38 AM
  2. sequence of integers
    By einjhelclint12 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 1st, 2012, 07:49 PM
  3. Sum of even and odd integers
    By spikezone2004 in forum Object Oriented Programming
    Replies: 10
    Last Post: February 8th, 2012, 06:03 PM
  4. [SOLVED] Writing Integers to .txt File; Returning Random Characters Instead of Integers
    By verbicidalmaniac in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 8th, 2011, 09:42 PM
  5. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM