Coding Bihar

Jetpack Compose App for Beginners

Jetpack Compose App for Beginners - Coding Bihar
Jetpack Compose App for calculate compound interest
Compound interest is the interest on a loan or deposit that is calculated based on both the initial principal and the accumulated interest from previous periods. Essentially, it means you earn interest not only on your original investment but also on the interest that has been added over time.
To calculate CI (Compound Interest) and Amount in a Jetpack Compose application, you need to define the formula for compound interest and then integrate it with user inputs (such as principal, rate, time, and number of compounding periods per year). Here's how you can implement this.

Compound Interest Formula:

\(A=P(1+\frac{r}{n})^{nt}\)
  1. A is the total amount.
  2. P is the principal.
  3. r is the annual interest rate (in decimal form).
  4. n is the number of times the interest is compounded per year.
  5. t is the time in years. 
  6. The compound interest is calculated as A−P.
  7. Input Fields: The user enters the principal, rate, time, and the number of times the interest is compounded per year.
  8. Calculate Button: When the button is clicked, it calculates the total amount (A) and compound interest (CI)
  9. Display Results: It shows the total amount and compound interest on the screen.

MainActivity

Copy this code →

package com.codingbihar.jetpackcomposeskill
import ...

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            JetpackComposeSkillTheme {
             CompoundInterestCalculator()
            }
        }
    }
}

CompoundInterestCalculator

Copy this code →

package com.codingbihar.jetpackcomposeskill
import ...

@Composable
fun CompoundInterestCalculator() {
    var principal by remember { mutableStateOf("") }
    var rate by remember { mutableStateOf("") }
    var time by remember { mutableStateOf("") }
    var timePerYear by remember { mutableStateOf("") }
    var amount by remember { mutableDoubleStateOf(0.00) }
    var interest by remember { mutableDoubleStateOf(0.00) }

    Column(
        modifier = Modifier.padding(16.dp)
    ) {
        Text("Compound Interest Calculator",
            style = MaterialTheme.typography.bodyLarge)

        OutlinedTextField(
            value = principal,
            onValueChange = { principal = it },
            label = { Text("Principal (P)") },
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
        )

        OutlinedTextField(
            value = rate,
            onValueChange = { rate = it },
            label = { Text("Annual Interest Rate (r %)") },
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
        )

        OutlinedTextField(
            value = time,
            onValueChange = { time = it },
            label = { Text("Time (t years)") },
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
        )

        OutlinedTextField(
            value = timePerYear,
            onValueChange = { timePerYear = it },
            label = { Text("time Per Year (n)") },
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
        )

        Button(
            onClick = {
                val P = principal.toDoubleOrNull() ?: 0.0
                val r = (rate.toDoubleOrNull() ?: 0.0) / 100
                val t = time.toDoubleOrNull() ?: 0.0
                val n = timePerYear.toDoubleOrNull() ?: 1.0

                // Calculate the amount with compound interest
                val calculatedAmount = P * (1 + r / n).pow(n * t)

                // Calculate the compound interest
                val calculatedInterest = calculatedAmount - P

                // Update the states with the calculated values (no conversion back to Double)
                amount = calculatedAmount
                interest = calculatedInterest
            },
            modifier = Modifier.padding(top = 16.dp)
        ) {
            Text("Calculate")
        }

        // Display amount and interest rounded to 2 decimal places
        Text(text = "Amount: ₹${ amount}", modifier = Modifier.padding(top = 16.dp))
        Text(text = "Compound Interest: ₹${interest}", modifier = Modifier.padding(top = 8.dp))
    }
}

OUTPUT:

Jetpack Compose App for Beginners output


 Sandeep Gupta

Posted by Sandeep Gupta

Please share your feedback us at:sandeep@codingbihar.com. Thank you for being a part of our community!

Special Message

Welcome to coding bihar!