We appreciate your visit to Write a Java program that calculates the number of cows over a period specified by the user The user inputs the initial number of cows. This page offers clear insights and highlights the essential aspects of the topic. Our goal is to provide a helpful and engaging learning experience. Explore the content and find the answers you need!
Answer :
Here is the Java code that calculates the number of cows for a period of time entered by the user. It takes the initial number of cows, the number of years, and the percentage of cows that are in heat, fertile, and serviced by a bull as input from the user.
It then calculates the number of cows for each year and displays the results.
import java.util.Scanner;
public class CowCalculator { public static void main(String[] args)
{ Scanner input = new Scanner(System.in);
int initialCows; int years; double inHeat
; double fertile; double serviced; System.out.print("Enter the initial number of cows: "); initialCows = input.nextInt();
System.out.print("Enter the number of years: "); years = input.nextInt();
System.out.print("Enter the percentage of cows in heat: ");
inHeat = input.nextDouble() / 100.0;
System.out.print("Enter the percentage of fertile cows: ");
fertile = input.nextDouble() / 100.0;
System.out.print("Enter the percentage of serviced cows: "); serviced = input.nextDouble() / 100.0;
double cows = initialCows; for (int i = 1; i <= years; i++) { double heatCows = cows * inHeat; double fertileCows = heatCows * fertile;
double servicedCows = fertileCows * serviced; cows += servicedCows;
System.out.println("Number of cows for year " + i + ": " + cows); } }}
In this code, the Scanner class is used to read input from the user. The initial number of cows, the number of years, and the percentages of cows in heat, fertile, and serviced by a bull are stored in variables. The program then uses a for loop to calculate the number of cows for each year, based on the previous year's results.
To know more about percentage visit :
https://brainly.com/question/32197511
#SPJ11
Thanks for taking the time to read Write a Java program that calculates the number of cows over a period specified by the user The user inputs the initial number of cows. We hope the insights shared have been valuable and enhanced your understanding of the topic. Don�t hesitate to browse our website for more informative and engaging content!
- Why do Businesses Exist Why does Starbucks Exist What Service does Starbucks Provide Really what is their product.
- The pattern of numbers below is an arithmetic sequence tex 14 24 34 44 54 ldots tex Which statement describes the recursive function used to..
- Morgan felt the need to streamline Edison Electric What changes did Morgan make.
Rewritten by : Barada