Write a program in java using scanner to find the cost price when sp and loss or profit percent are given
Answers
Answer:
import java.util.*;
public class price
{
public static void main()
{
Scanner sc = new Scanner (System.in);
double cp,sp,pp,lp;
int x;
System.out.println("Enter the selling price of the article=");
sp=sc.nextDouble();
System.out.println("1. To enter the profit percent");
System.out.println("2. To enter the loss percent");
System.out.println("Enter your choice=");
x=sc.nextInt();
switch(x)
{
case 1:System.out.println("Enter the profit percent=");
pp=sc.nextDouble();
cp=(sp*100/100+pp);
System.out.println("The cost price ="+cp);
break;
case 2:System.out.println("Enter the loss percent=");
lp=sc.nextDouble();
cp=(sp*100/100-lp);
System.out.println("The cost price ="+lp);
break;
}
}
}
Hope this answer helps you. I compiled and and executed it, it works perfectly... if any problem, send message
Hope it helps ^_^
import java.util.Scanner;
public class Profit_And_Loss_Calculator{
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
double cp, sp, gain, loss;
//Taking input
System.out.print("Enter the Cost Price: ");
cp = sc.nextDouble();
System.out.print("Enter the Selling Price: ");
sp = sc.nextDouble();
//Main part comes here
if (cp > sp && cp >= 0 && sp >= 0){
loss = cp - sp;
double Loss_Percentage = (loss/cp) * 100;
System.out.println("Loss: " + loss);
System.out.println("Loss(%): " + Loss_Percentage);
}
else if (cp < sp && cp >= 0 && sp >= 0){
gain = sp - cp;
double Gain_Percentage = (gain/cp) * 100;
System.out.println("Gain: " + gain);
System.out.println("Gain(%): " + Gain_Percentage);
}
else if (cp == sp && cp >= 0 && sp >= 0){
System.out.println("There is no Gain or Loss");
}
else if (cp < 0 || sp < 0){
System.out.println("Invalid Input *_*");
}
}
}
The underlined values are the inputs
Output:
(i)
Enter the Cost Price: 50
Enter the Selling Price: 40
Loss: 10.0
Loss(%): 20.0
(ii)
Enter the Cost Price: 40
Enter the Selling Price: 50
Gain: 10.0
Gain(%): 25.0
(iii)
Enter the Cost Price: 90
Enter the Selling Price: 90
There is no Gain or Loss
(iv)
Enter the Cost Price: -50
Enter the Selling Price: -40
Invalid Input *_*