Ask Question
7 October, 02:21

Write a program that asks the user to enter the amount that he or she has budgeted for a month. A loop should then prompt the user to enter each of his or her 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.

+2
Answers (1)
  1. 7 October, 05:19
    0
    import java. util.*;

    public class Main {

    public static void main (String[] args)

    {

    Scanner scan = new Scanner ();

    double budget=0, num=0, total=0;

    System. out. println ("Your budget for the month? ");

    budget=scan. nextDouble ();

    System. out. println ("enter all expense, and after that type - 9999 to quit: ");

    while (num! = - 9999)

    {

    total+=num;

    num=scan. nextDouble ();

    }

    if (total<=budget)

    {

    System. out. println ("under budget by ");

    System. out. println (budget-total);

    }

    else

    {

    System. out. println ("over budget by ");

    System. out. println (total-budget);

    }

    }

    }

    Explanation:

    Take the budget as an input from user and store it to the budget variable. Loop until user has entered all his expenses and keep on adding them to the total variable. Check If the total is less than or equal to budget or otherwise, and then print the relevant message accordingly.
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 that he or she has budgeted for a month. A loop should then prompt the user to enter ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers