Ask Question

Write a program that asks the user to enter the amount s/he has budgeted for a month. The amount should be between 1000 and 2000 (do an input validation). A loop should then prompt the user to enter each of the expenses (show at least 3 expenses) for the month and keep a running total. When the loop finishes, the program should display the amount that the user is over or under budget or exactly on budget. # Hints # Declare variables to store the budget amount, amount spent, difference, and total. # Initialize for while loop # Get the budgeted amount from the user and do input validation # Get the total amount spent from the user (while statement starts, make sure to enter exit criteria) # Determine whether the user is over or under budget, and display the result (if elif else) # print what was the budgeted amount # print what was the amount spent

+4
Answers (1)
  1. 13 May, 10:46
    0
    The program to this question can be given as:

    Program:

    //import package for user input.

    import java. util.*;

    //define class

    public class Budget

    {

    public static void main (String [] a) / /define main method.

    {

    //creating Scanner class object.

    Scanner ob = new Scanner (System. in);

    //define variable.

    double budgetamount=0, amountspent=0, difference=0, total=0, num=0;

    int count = 0;

    System. out. println ("How much have you budgeted for the month? : "); / /print message.

    budgetamount=ob. nextDouble (); / /taking input

    while (budgetamount! = 0) / /checkig number greater then 4 digite.

    {

    budgetamount = budgetamount/10;

    ++count;

    }

    if (count>4) / /condition

    {

    System. out. println ("enter each expense, then type - 999 to quit: ");

    while (num!=-999) / /taking expense

    {

    total=total+num; / /totaling expense

    num=scan. nextDouble ();

    }

    if (total<=budgetamount) / /condtion for over budget.

    {

    System. out. print ("under budget:"); / /print message

    System. out. print (budgetamount-total);

    }

    else

    {

    System. out. print ("over budget:"); / /for under budget

    System. out. print (total-budgetamount);

    }

    }

    else

    {

    System. out. println ("not valid"); / /message for number lessthen 4 digit.

    }

    }

    }

    output:

    How much have you budgeted for the month? : 1200.55.

    enter each expense, then type - 999 to quit: 365.89

    556.90

    339.98

    -999

    over budget:1262.77

    Explanation:

    The explanation of this program can be given as:

    In the above program we import the package in that is used for scanner class. This class is used for the input from the user after input we use the while loop and if-else statement. The while loop is the entry control loop It is used for input validation and if-else is used for the checking condition. Then we insert expense that is inserted by the user. Then we calculate under-budgeted and over-budgeted by conditional statement that is if-else. and at the last we print it.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a program that asks the user to enter the amount s/he has budgeted for a month. The amount should be between 1000 and 2000 (do an ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers