Coding Bihar

EMI Calculator App Jetpack Compose

EMI Calculator App Jetpack Compose - Coding Bihar
 
EMI Calculator App Jetpack Compose
To build an EMI (Equated Monthly Installment) calculator app using Jetpack Compose, you will need a simple user interface (UI) to input the loan amount, interest rate, and loan tenure, and a logic to calculate the EMI based on these inputs.


Here's a basic Jetpack Compose app to calculate EMI:

Steps:

  1. Input fields for loan amount, interest rate, and loan tenure.
  2. Button to calculate EMI.
  3. Display the result (EMI).
\(EMI=\frac{P×r×(1+r)^n}{(1+r)^{n}-1}\)
Where:

  • P = Loan amount (principal)
  • r = Monthly interest rate (annual rate divided by 12 and converted to decimal)
  • n = Loan tenure in months

MainActivity

Copy this code →

package com.codingbihar.composepractice

import ...

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            ComposePracticeTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    EMIApp()
                }
            }
        }
    }
}

EMIApp

Build A game using Jetpack Compose for Beginners
Copy this code →

package com.codingbihar.composepractice

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import kotlin.math.pow

@Composable
fun EMIApp() {
    var principal by remember { mutableStateOf("") }
    var rate by remember { mutableStateOf("") }
    var tenure by remember { mutableStateOf("") }
    var emiResult by remember { mutableStateOf("") }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {

        Text("EMI Calculator", style = MaterialTheme.typography.headlineMedium)
        Spacer(modifier = Modifier.height(20.dp))

        TextField(
            value = principal,
            onValueChange = { principal = it },
            modifier = Modifier
                .fillMaxWidth()
                .padding(10.dp),
            singleLine = true,
            keyboardOptions = KeyboardOptions(
                keyboardType = KeyboardType.Number,
                imeAction = ImeAction.Next
            ),
            label = { Text("Enter Loan Amount") }
        )

        TextField(
            value = rate,
            onValueChange = { rate = it },
            modifier = Modifier
                .fillMaxWidth()
                .padding(10.dp),
            singleLine = true,
            keyboardOptions = KeyboardOptions(
                keyboardType = KeyboardType.Number,
                imeAction = ImeAction.Next
            ),
            label = { Text("Enter Interest Rate (Annual)") }
        )

        TextField(
            value = tenure,
            onValueChange = { tenure = it },
            modifier = Modifier
                .fillMaxWidth()
                .padding(10.dp),
            singleLine = true,
            keyboardOptions = KeyboardOptions(
                keyboardType = KeyboardType.Number,
                imeAction = ImeAction.Done
            ),
            label = { Text("Enter Loan Tenure (Months)") }
        )

        Spacer(modifier = Modifier.height(20.dp))

        Button(onClick = {
            emiResult = calculateEMI(
                principal.toDoubleOrNull() ?: 0.0,
                rate.toDoubleOrNull() ?: 0.0,
                tenure.toIntOrNull() ?: 0
            )
        }) {
            Text("Calculate EMI")
        }

        Spacer(modifier = Modifier.height(20.dp))

        Text(text = "EMI: $emiResult", style = MaterialTheme.typography.headlineSmall)
    }
}

fun calculateEMI(principal: Double, annualRate: Double, months: Int): String {
    if (principal <= 0 || annualRate <= 0 || months <= 0) return "Invalid input"

    val monthlyRate = annualRate / (12 * 100)
    val emi = (principal * monthlyRate * (1 + monthlyRate).pow(months.toDouble())) /
            ((1 + monthlyRate).pow(months.toDouble()) - 1)
    return String.format("%.2f", emi)
}
Build A game using Jetpack Compose
 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!