9. Write a Java program to calculate profit or loss.
Static
Solution :-
class profitloss
{
public static void main(String args[])
{
int amount,cp=2000,sp=1500;
if (sp > cp)
{
amount = sp - cp;
System.out.println("Profit :- "+amount);
}
else if(cp > sp)
{
amount = cp - sp;
System.out.println("Loss :- "+amount);
}
else
{
System.out.println("No profit no Loss" );
}
}
}
Output :-
Dynamic
Solution:-
import java.util.*;
class profitloss
{
public static void main(String args[])
{
int amount,sp,cp;
Scanner in = new Scanner(System.in);
System.out.println("Enter the Selling Price :-");
sp = in.nextInt();
System.out.println("Enter the Cost Price :-");
cp = in.nextInt();
if (sp > cp)
{
amount = sp - cp;
System.out.println("Profit :- "+amount);
}
else if(cp > sp)
{
amount = cp - sp;
System.out.println("Loss :- "+amount);
}
else
{
System.out.println("No profit no Loss" );
}
}
}
Output :-