Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,239 questions

56,142 answers

573 users

How to calculate compound interest in C#

1 Answer

0 votes
using System;

class CompoundInterestCalculator
{
    static double CalculateCompoundInterest(double principal, double rate, double years) {
        if (principal < 0 || rate < 0 || years < 0) {
            Console.WriteLine("Error: Principal, rate, and years must be non-negative values.");
            return -1;
        }
        
        // Calculate total amount using the compound interest formula
        double amount = principal * Math.Pow(1 + rate / 100, years);
        
        // Return compound interest
        return amount - principal;
    }

    static void Main()
    {
        double principal = 100000;
        double rate = 3.5;
        double years = 5;

        double interest = CalculateCompoundInterest(principal, rate, years);

        if (interest >= 0) {
            Console.WriteLine($"Principal Amount: {principal:F2}");
            Console.WriteLine($"Annual Interest Rate: {rate:F2}%");
            Console.WriteLine($"Years: {years:F2}");
            Console.WriteLine($"Compound Interest: {interest:F2}");
            Console.WriteLine($"Total Amount: {(principal + interest):F2}");
        }
    }
}



/*
run:
    
Principal Amount: 100000.00
Annual Interest Rate: 3.50%
Years: 5.00
Compound Interest: 18768.63
Total Amount: 118768.63
    
*/

 



answered Aug 30, 2025 by avibootz

Related questions

1 answer 116 views
1 answer 117 views
1 answer 128 views
1 answer 125 views
1 answer 132 views
1 answer 155 views
...