Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

a java program to compute savings if rate is 4%&spend14% per month.slary is 50000

0 votes
asked Jul 2, 2019 by anonymous

1 Answer

0 votes
answered Jul 4, 2019 by Vandan Mehta (180 points)

import java.util.Scanner;
public class Savings{
    /**
     * the following program will calculate both monthly savings and total savings for a given number of months using
     * compound interest for any given monthly salary, rate of interest and 
     * percentage of salary that will get spent monthly.
     */ 
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        //Enter the monthly salary (50000 in your case)
        System.out.println("Enter your Salary recieved per month ");
        int salary = in.nextInt();
        //Enter the fraction of the salary that would be spent(14 in your case)
        System.out.println("Enter the percentage of your salary you would be spending");
        int exp = in.nextInt();
        double expenditure = (double)(exp/100.0);
        //Enter the rate of interest (4 in your case)
        System.out.println("Enter the rate of interest per year you would recieve when you store your money in the bank");
        double rateOfInterest = in.nextDouble();
        rateOfInterest /= 100.0;
        //savings per month is just income - expenditure
        double monthlySavings = (double)(salary*(1-expenditure));
        System.out.println("Your Monthly Savings are = "+monthlySavings+" of your respective currency");
        double totalSavings = 0;
        System.out.println("Enter the number of months you wish to follow the same savings plan for");
        int numberOfMonths = in.nextInt();
        //monthly compounding is used
        //to use quarterly compounding use the first comment inside the loop and comment out line number 32
        //to use semi-annual compounding use the second comment inside the loop and comment out line number 32
        for(int i= 1;i<=numberOfMonths;i++){
            totalSavings += monthlySavings;
            double interest = (totalSavings*rateOfInterest)/12.0;            
            /*if(i%3==0){
                double interest = (totalSavings*rateOfInterest)/4.0;
            }*/
            /*if(i%6==0){
                double interest = (totalSavings*rateOfInterest)/2.0;
            }*/
            totalSavings += interest;            
        }
        System.out.println("Your total savings will be "+totalSavings+" of your respective currency");        
        in.close();
    }
}

Copy the above code in any java compiler and compile and run it.

Hope it was Helpful

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...