1. Write a program that prompts the user to input five decimal numbers. The prog
ID: 668093 • Letter: 1
Question
1. Write a program that prompts the user to input five decimal numbers. The program should then add the five decimal numbers, convert the sum to the nearest integer, and print the result.
Perform an analysis and design for this program. Write the code. Compile and test it
2.Cindy uses the services of a brokerage firm to buy and sell stocks. The firm charges 1.5% service charges on the total amount for each transaction, buy or sell. When Cindy sells stocks, she would like to know if she gained or lost on a particular investment. Write a program that allows Cindy to input the number of shares sold, the purchase price of each share, and the selling price of each share. The program outputs the amount invested, the total service charges, amount gained or lost, and the amount received after selling the stock.
Perform an analysis and design for this problem. Write the code, compile and test it.
Explanation / Answer
import java.io.*;
class integers
{
public static void main(String args[]) throws IOException{
float v, w, x, y, z, sum;
System.out.println("Enter your five decimal numbers: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
v = Float.parseFloat(br.readLine());
w = Float.parseFloat(br.readLine());
x = Float.parseFloat(br.readLine());
y = Float.parseFloat(br.readLine());
z = Float.parseFloat(br.readLine());
sum = v + w + x + y + z;
System.out.println("Sum of entered decimal numbers rounded to nearest integer is = "+Math.round(sum));
}
}
Input :
2.3
2.3
2.3
2.0
2.0
Output:
11
The above program written in Java asks a user to input five floating/real numbers and the calculates their sum and further rounds the value of the sum thus obtained to the nearest integer using the Math.round() function.
import java.io.*;
class Cindy
{
public static void main(String args[]) throws IOException{
float temp;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of shares to be sold : ");
int sno = Integer.parseInt(br.readLine());
System.out.println("Enter the cost price of the share : ");
float cprice = Float.parseFloat(br.readLine());
System.out.println("Enter the selling price of the share : ");
float sprice = Float.parseFloat(br.readLine());
float amount1= cprice*sno;
System.out.println("Total amount invested is : "+amount1);
float amount2= sprice*sno;
float scharge=(float)(1.5/100)*amount2;
System.out.println("Total service charge is : "+scharge);
if (sprice<cprice)
{
temp=amount1-amount2;
System.out.println("Loss incurred is : "+temp);
}
else
{
temp=amount2-amount1;
System.out.println("Your total gain is : "+temp);
}
System.out.println("Total amount recieved after transaction is : "+ (amount2-scharge));
}
}
The above program written in JAVA as the user to input the number of shares to be sold along with the selling and cost price of each share. It the outputs the coressponding values of amount invested, total service charge to be paid, amount recieved after transaction and the gain or loss incurred in the transaction. Sample input and output have been shown below :
Input:
Enter the number of shares to be sold :
10
Enter the cost price of the share :
3
Enter the selling price of the share :
4
Output:
Total amount invested is : 30.0
Total service charge is : 0.59999996
Your total gain is : 10.0
Total amount recieved after transaction is : 39.4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.