Question

In: Computer Science

Write a Python program which uses a function to calculate the perimeter of a rectangle. a...

  1. Write a Python program which uses a function to calculate the perimeter of a rectangle.
  2. a function named volume to calculate the volume of a cylinder

volume = 3.14 x radius x radius x height

.b function named volume to calculate the volume of a cuboid

volume = Length x width x ht

  1. Write a Python Program to calculate the sum of all odd numbers for 2 to 20 using a for loop.

4.

Write statements that assign random integers to the variable n in the following ranges: Use the random function to generate the numbers. Put the statements in a Java Program and run it.

  1. a) 1 ≤ n ≤ 34
  2. b) 1 ≤ n ≤ 700
  3. c) 0 ≤ n ≤ 299
  4. d) 1000 ≤ n ≤ 5113
  5. e) –14 ≤ n ≤ 25

5.

Write statements that assign random integers to the variable n in the following ranges: Use the random function to generate the numbers. Put the statements in a Java Program and run it.

  1. a) 1 ≤ n ≤ 39
  2. b) 1 ≤ n ≤ 800
  3. c) 0 ≤ n ≤ 291
  4. d) 1000 ≤ n ≤ 5124
  5. e) –17 ≤ n ≤ 34

Solutions

Expert Solution

Below are the four programs as mentioned, out of which two are in Python and other two are in JAVA as mentioned. We have used math module to use the value of "pi" to find the volume of cylinder. The Math module is used in JAVA to generate random integers in the range given in the question.

Program 1:-

import math
def rect_perimeter(length, width):        # calculating perimeter of rectangle.
    return 2*(length+width)
    
def volume_cylinder(radius, height):      # calculating volume of cylinder.
    return math.pi*radius*radius*height
    
def volume_cuboid(length, width, height): # calculating volume of cuboid.
    return length*width*height;
    
print("perimeter of rectangle with length = 3 and width = 5 is:",rect_perimeter(3, 5))
print("volume of cylinder with radius = 3 and height = 9 is:",round(volume_cylinder(3, 9),2))
print("volume of cuboid with length = 3, width = 5 and height = 6 is:",volume_cuboid(3, 5, 6))

Sample Output 1:-

Program 2:-


def sum_odd():                # function to sum all the odd numbers between 2 and 20.
    sum=0.0
    for i in range(2,20):
        if(i%2 == 1):           # odd number when divided by 2 gives a remainder of 1.
            sum = sum + i
    
    return sum
    
    
print("sum of all the odd numbers between 2 and 20 is:",sum_odd()) 

Sample Output 2:-

Program 3:-

import java.lang.Math; 
public class Main
{
    public static int getRandomNumber(int min, int max) {           // function to generate random numbers between range given.
        return (int) ((Math.random() * (max - min + 1)) + min);     // Math.random() gives the double between 0 and 1 where 0 is inclusive and 1 exclusive.
    }
        public static void main(String[] args) {
                System.out.println("random number between 1 and 34 is: "+ getRandomNumber(1, 34));
                System.out.println("random number between 1 and 700 is: "+ getRandomNumber(1, 700));
                System.out.println("random number between 0 and 299 is: "+ getRandomNumber(0, 299));
                System.out.println("random number between 1000 and 5113 is: "+ getRandomNumber(1000, 5113));
                System.out.println("random number between -14 and 25 is: "+ getRandomNumber(-14, 25));
        }
}

Sample Output 3:-

Program 4:-

import java.lang.Math; 
public class Main
{
    public static int getRandomNumber(int min, int max) {           // function to generate random numbers between range given.
        return (int) ((Math.random() * (max - min + 1)) + min);    // Math.random() gives the double between 0 and 1 where 0 is inclusive and 1 exclusive
    }
        public static void main(String[] args) {
                System.out.println("random number between 1 and 39 is: "+ getRandomNumber(1, 39));
                System.out.println("random number between 1 and 800 is: "+ getRandomNumber(1, 800));
                System.out.println("random number between 0 and 291 is: "+ getRandomNumber(0, 291));
                System.out.println("random number between 1000 and 5124 is: "+ getRandomNumber(1000, 5124));
                System.out.println("random number between -17 and 34 is: "+ getRandomNumber(-17, 34));
        }
}

Sample Output 4:-


Related Solutions

rite a program that will calculate the perimeter and area of a rectangle. Prompt the user...
rite a program that will calculate the perimeter and area of a rectangle. Prompt the user to input the length and width. Calculate the area as length * width. Calculate the perimeter as 2* length + 2* width. Display the area and perimeter. Please use a proper heading in a comment block (name, date, description of the program). Please use comments in your code. Run your program twice using different input. Copy the output and place it at the bottom...
Write a Python program that uses function(s) for writing to and reading from a file: a....
Write a Python program that uses function(s) for writing to and reading from a file: a. Random Number File Writer Function Write a function that writes a series of random numbers to a file called "random.txt". Each random number should be in the range of 1 through 500. The function should take an argument that tells it how many random numbers to write to the file. b. Random Number File Reader Function Write another function that reads the random numbers...
Write a Python program that uses function(s) for the following problem: A nutritionist who works for...
Write a Python program that uses function(s) for the following problem: A nutritionist who works for a fitness club helps members by evaluating their diets. As part of her evaluation, she asks members for the number of fat grams and carbohydrate grams that they consumed in a day (two inputs, one for number of fat grams and the other for number of carb grams). Then, she calculates the number of calories that result from the fat, using the following formula:...
1) Write a simple python function program that can calculate the volumes of following shape: cylinder,...
1) Write a simple python function program that can calculate the volumes of following shape: cylinder, circle, cone and sphere. For each of this shape, write a separate function. Let your function take in 2 inputs only. Your program should prompt user to enter these 2 inputs and print out the results of the volumes of the shape separately?
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The program to present the user with a menu where one of the shapes can be selected. Based on the selection made, the user enters the proper input, the program validates the input (i.e all entries must be greater than zero). Once the input is entered and validated, the intended area is calculated and the entered information along with the area are displayed. Area of...
Write a Python module that contains functions to calculate the perimeter and area of at least 4 different geometric shapes.
Modules Assignment pythonWrite a Python module that contains functions to calculate the perimeter and area of at least 4 different geometric shapes.Write a separate Python program (in a separate file from you Python module) that imports your above module and uses several of its functions in your program. Make use of the input() and print() functions in this program file. Try to make your program as a real-world application.
Python 3 Calculate factorial Create a function that uses a loop to calculate the factorial of...
Python 3 Calculate factorial Create a function that uses a loop to calculate the factorial of a number. * function name: get_fact * parameters: num (int) * returns: int * operation: Must use a loop here. Essentially calculate and return the factorial of whatever number is provided. but: - If num is less than 1 or greater than 20, print "num is out of range" and exit the function - If num is not an int, print "invalid num parameter"...
Write a python program for the following question. Complete the symptom similarity function, which measures the...
Write a python program for the following question. Complete the symptom similarity function, which measures the similarity between the symptoms of two patients. See below for an explanation of how the similarity is computed. def symptom_similarity(symptoms_A: Tuple[Set], symptoms_B: Tuple[Set]) -> int: '''Returns the similarity between symptoms_A and symptoms_B. symptoms_A and symptoms_B are tuples of a set of symptoms present and a set of symptoms absent. The similarity measure is computed by the following equations: present_present + absent_absent - present_absent -...
Question 3: getting diagnostics, Write a python program   Write the getting diagnostics function, which reports the...
Question 3: getting diagnostics, Write a python program   Write the getting diagnostics function, which reports the most frequent diagnostic from the patients with the highest symptoms similarity returned by the function similarity to patients. See below for an explanation of exactly what is expected. def getting_diagnostics(patient_set : Set[int], diagnostic_by_patient: Dict[int, str]) -> str: """ Returns a string representing the most frequent diagnostic from the set of diagnostics (stored in the dictionary diagnostic_by_patient) of the patient_set (that is a set of...
13. Minimizing Perimeter: What is the smallest perimeter possible for a rectangle whose area is 36...
13. Minimizing Perimeter: What is the smallest perimeter possible for a rectangle whose area is 36 in2, and what are its dimensions? Also, provide the perimeter and area equations. SHOW WORK. Perimeter Equation: _________________Area Equation: ___________________ Dimensions (include units): ___________Perimeter (include units):___________ 14. Find the linearization L(x) at x = 2 of the function, f(x)=√(x^2+12). SHOW WORK. Linearization L(x): ______________________
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT