Javascript required
Skip to content Skip to sidebar Skip to footer

Find Interest Value in Java Hackerrank Solution

How to write a Java program to calculate compound interest? Here we share the code in five different formats to find compound interest program. Using standard values, using recursion, using command line arguments, using while loop, for loop, and using function method etc.

  • Hollow Mirrored Right Triangle Star Pattern
  • Fibonacci Series In Java Program – 4 Multiple Ways

We can even write the following Java code to find compound interest in more than 10+ ways as a beginner we share only the basic versions here. If you have any doubts related to the following code, then do leave a comment here. Here is the complete list of beginners programs and java interview programs. Also, check out the core java interview questions .

Table Of Contents :

  • How to calculate compound interest in math?

In simple, compound interest is a interest on interest. It's the result of reinvesting.

  • What's the formula to find the compound interest?

Where,

  • A = " Ending Amount "
  • P = " Principal "
  • R =" Interest Rate "
  • N = " Number of compounding a year "
  • T = " Total Number Of Years "

Here we go Basic java program with standard values ( Formula-based ) to print compound Interest followed by recursion and control statements. At the end of the codes, we embedded an online execution tool where you can execute and print the output for the following programs. Check out the sample simple CI program -1 below with outputs as well.

Find CI using standard values

  • ( Using standard values ) by using the simple/basic math formula you can write any program based on the following example. #Formula based Program#. Also, check the output and execute the following program at the end of the post.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

class CompundInterest

{

public static void main ( String args [ ] )

{

double amount = 0 , principle = 1000 , rate = 10 , time = 3 , ci ;

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

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

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

amount = principle * ( ( 1 + rate / 100 ) * ( 1 + rate / 100 ) * ( 1 + rate / 100 ) ) ;

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

ci = amount - principle ;

System . out . println ( "compound interest=" + ci ) ;

}

}

Output ( Standard Values ) :

principle = 1000.0

rate = 10.0

time = 3.0

amount = 1331.0000000000005

compound interest = 331.00000000000045

Using Command Line Arguments

  • Java program to find compound interest values taking inputs through command line arguments. If you have no idea about command line arguments then do check out the guide here: Command line arguments in java with examples. Where inputs were taken through the console. #Java Parse#.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

import java . util . Scanner ;

import static java . lang . Math . pow ;

class CompundInterest

{

public static void main ( String args [ ] )

{

double amount , principle , rate , time , ci , sum ;

principle = Double . parseDouble ( args [ 0 ] ) ;

rate = Double . parseDouble ( args [ 1 ] ) ;

time = Double . parseDouble ( args [ 2 ] ) ;

rate = ( 1 + rate / 100 ) ;

rate = pow ( rate , time ) ;

amount = principle * rate ;

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

ci = amount - principle ;

System . out . println ( "compound interest=" + ci ) ;

}

}

Output ( Using command line arguments ) :

javac CompundInterest . java

ava CompundInterest 1000 10 2

amount = 1210.0000000000002

compound interest = 210.00000000000023

 

Using  For Loop

Here we go another basic and simple method to print CI values, java program to calculate compound interest using For loop .

Executing a set of statements repeatedly is known as looping. We have written an article about for loop here . Check out. For loop is the most used loop in Java, where it can work out with both while and do while . Check out the program below with outputs.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

import java . util . Scanner ;

import static java . lang . Math . pow ;

class CompundInterest

{

public static void main ( String args [ ] )

{

double amount = 0 , principle , rate , time , ci , t = 1 ;

Scanner sc = new Scanner ( System . in ) ;

System . out . println ( "enter principle " ) ;

principle = sc . nextDouble ( ) ;

System . out . println ( "enter rate" ) ;

rate = sc . nextDouble ( ) ;

System . out . println ( "enter time" ) ;

time = sc . nextDouble ( ) ;

rate = ( 1 + rate / 100 ) ;

for ( int i = 0 ; i < time ; i ++ )

t *= rate ;

amount = principle * t ;

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

ci = amount - principle ;

System . out . println ( "compound intrest=" + ci ) ;

}

}

Output ( Using For Loop ) :

enter principle

10000

enter rate

5

enter time

5

amount = 12762.815625000003

compound interest = 2762.815625000003

 

Using While Loop

  • Another simple program to print CI values, here we used a while loop syntax to find the compound interest using while loop.
  • The difference between for loop and while loop is nothing, but as per universal laws, the execution of statement differs with that code thereby all looping will have a different syntax which can impact on the execution of the statement.
  • The definition of while loop is executing a set of statements repetitively until the condition is true. Check out the program.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

import java . util . Scanner ;

import static java . lang . Math . pow ;

class CompundInterest

{

public static void main ( String args [ ] )

{

double amount = 0 , principle , rate , time , ci , t = 1 ;

Scanner sc = new Scanner ( System . in ) ;

System . out . println ( "enter principle " ) ;

principle = sc . nextDouble ( ) ;

System . out . println ( "enter rate" ) ;

rate = sc . nextDouble ( ) ;

System . out . println ( "enter time" ) ;

time = sc . nextDouble ( ) ;

rate = ( 1 + rate / 100 ) ;

int i = 0 ;

while ( i < time )

{

t *= rate ;

i ++ ;

}

amount = principle * t ;

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

ci = amount - principle ;

System . out . println ( "compound interest=" + ci ) ;

}

}

Output ( Using While Loop ) :

enter principle

1000

enter rate

1

enter time

1

amount = 1010.0

compound interest = 10.0

 Using Recursion

  • Another method, using recursion. So basically what is recursion, recursion is a method calls itself repeatedly or a method in java that calls itself is called a recursive method. Check out the following program using recursion .

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

import java . util . Scanner ;

class CompundInterest

{

public static void main ( String args [ ] )

{

double amount = 0 , principle , rate , time , ci , t = 1 ;

Scanner sc = new Scanner ( System . in ) ;

System . out . println ( "enter principle " ) ;

principle = sc . nextDouble ( ) ;

System . out . println ( "enter rate" ) ;

rate = sc . nextDouble ( ) ;

System . out . println ( "enter time" ) ;

time = sc . nextDouble ( ) ;

rate = ( 1 + rate / 100 ) ;

t = ratecal ( rate , time , t ) ;

amount = principle * t ;

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

ci = amount - principle ;

System . out . println ( "compound interest=" + ci ) ;

}

static double ratecal ( double r , double t , double t1 )

{

if ( t < 1 )

return t1 ;

else

{

t1 *= r ;

return ratecal ( r , t - 1 , t1 ) ;

}

}

}

Output ( Using Recursion ) :

enter principle

1000

enter rate

1

enter time

2

amount = 1020.1

compound interest = 20.100000000000023

 

We are working on to embed the online execution tool. The online execution tool will help you to learn and practice more programs. You can even execute the above codes and find out how it works in each different case using static, for loop, while loop, and using recursion.

Here is the quick reference links: 100+ Beginners programs, Java interview programs and interview question and answers.

More Tutorials :

  1. Do while Java Tutorials With Examples
  2. Java Switch Case Statement Tutorials With Examples
  3. Java Operators With Examples
  4. Java Datatypes And Variables

If you want any custom program, do contact us our expert developers will ready to handle any project or source codes for your project or assignment.

Find Interest Value in Java Hackerrank Solution

Source: https://javatutoring.com/compound-interest-java-program/