Find Profit or Loss Program in java

Question:

Sam started a new business with an investment of Rs.1, 00,000. During the first year he got a profit of x%, whereas in the second year he lost a certain amount(say Rs. Y). Help him to find out the profit/loss (in terms of initial investment) in percentage at the end of the second year.

Sample input 1:

Enter the profit percentage

20

Enter the amount lost in Rs.

50000

Sample output 1:

After two years he gets a loss of 30%.

Explanation :

investment = 100000

profit = 20% of 100000 = 20000

Loss=50000

If loss > profit, there is a loss

If loss < profit, there is a profit

If loss = profit, no gain no loss

Here loss > profit So loss

To calculate  loss% –  

loss amount = loss – profit = 30000

Loss % =( loss amount  / investment)    * 100             

That is 30000 * 100 / 100000 = 30%

Sample input 2:

Enter the profit percentage

20

Enter the amount lost in Rs.

20000

Sample output 2:

After two years he gets no loss or no gain.

CODE:

import java.util.*;
public class  Main
{
    public static void main (String[] args) {
        int investment=100000, amount=0;
        Scanner sc =new Scanner(System.in);
        
        System.out.println("Enter the profit percentage");
            int profit_percentage_1sty=sc.nextInt();
        
        System.out.println("Enter the amount lost in Rs.");
            int lost_amount_2ndy=sc.nextInt();
        
            int profit_amount_1sty=(profit_percentage_1sty*investment)/100;//BADMAS
            amount=(investment+profit_amount_1sty-lost_amount_2ndy);
        
        if(amount>investment)
        {   
            int profit=((amount-investment)*100)/investment;//BADMAS
            System.out.println("After two years he gets a profit of "+profit+"%");    
        }
        
        else if(amount==investment)
        {
            System.out.println("After two years he gets no loss or no gain");
        }
        
        else if(amount<investment)
        {
            int loss=((investment-amount)*100)/investment;//remember BADMAS while writing arithmetic expressions
            System.out.println("After two years he gets a loss of "+loss+"%");
        }
    }
}

Leave a Comment

Scroll to Top