In: Computer Science
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
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.
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.
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:-