Question

In: Computer Science

a function named area to calculate the area of a rectangle area = Length x width...

  1. a function named area to calculate the area of a rectangle

area = Length x width

2.a function named volume to calculate the volume of a sphere

volume = 4/3 x 3.14 x radius x radius x radius

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

volume = Length x width x ht

4. Use a loop structure to calculate the sum of all odd numbers from 1 to 17

5) Use a loop structure to calculate the product of all even numbers from 1 to 13.

6.Write a program, which uses a selection structure (if/else) to calculate and print the student grades.

83 and above is an A

63 and above is a B

53 and above is a C

40 and above is a D

below 40 is fail.

7. 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 ≤ 51
  2. b) 1 ≤ n ≤ 900
  3. c) 0 ≤ n ≤ 391
  4. d) 1000 ≤ n ≤ 5127
  5. e) –17 ≤ n ≤ 22
  1. Stimulate a coin tossing environment using random functions and using a for loop - toss the coin 100 times and print the number of heads and tails.
  2. Write a program to convert US dollars to Pakistani rupees

Taka = 164*US;

Solutions

Expert Solution

# 1. Calculation of area of rectangle

#include<bits/stdc++.h>
using namespace std;
int area(int length, int width) // function for calculating the area
{
    return length*width;
}
int main()
{
    int length;
    int width;
    cout<<"Enter the length of rectangle: ";
    cin>>length;
    cout<<"Enter the width of rectangle: ";
    cin>>width;
    cout<<"the area of rectangle is "<<area(length, width);
}

#2. Program for calculating the volume of sphere

#include<bits/stdc++.h>
using namespace std;

double volume(int r) // function for calculating the volume of sphere
{
    double vol = (4/3)*3.14*r*r*r;
    return vol;
}
int main()
{
    int rad;
    cout<<"Enter the radius of the sphere: ";
    cin>>rad;
    cout<<"the area of rectangle is "<<volume(rad);
}

#3 Program to find the volume of the cuboid

#include<bits/stdc++.h>
using namespace std;
int volume(int l, int w, int h)
{
    return l*w*h;
}
int main()
{
    int length, width, ht;
    cout<<"Enter the length of the cuboid: ";
    cin>>length;
    cout<<"Ether the width of cuboid: ";
    cin>>width;
    cout<<"Enter the height of cuboid: ";
    cin>>ht;
    cout<<"the area of rectangle is "<<volume(length, width, ht);
}

#4. Sum of odd number from 1 to 17

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int sum =0;
    for(int i=1; i<=17; i++){
        if(i%2==1){ // it will check whether i is odd or not
            sum += i; // adding the odd values
        }
    }
    cout<<sum;
}

#5. Product of all even number from 1 to 13

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int product = 1;
    for(int i=1; i<=13; i++)
    {
        if(i%2==0){   // it will check whether i is even or not
            product *= i;
        }
    }
    cout<<product;
}

#6 Calculation and printing the grades:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int marks;
    cout<<"Enter your marks: ";
    cin>>marks;
    if(marks>=83){
        cout<<"Your grade is : A";
    }
    else if(marks>=63){
        cout<<"Your grade is : B";
    }
    else if(marks>=53){
        cout<<"Your grade is : C";
    }
    else if(marks>=40){
        cout<<"Your grade is : D";
    }
    else{
        cout<<"You are Fail";
    }
}

#7. Random Number in given range


import java.io.*;
import java.util.Random; // for Random() function

class GFG {
        public static void main (String[] args) {
                Random rand = new Random(); 
  
        // Generate random integers in range 1 to 51 
        int randInt1 = rand.nextInt(51)+1; 
        System.out.println("Random Integers range 1 to 51: "+randInt1); 
        
        // Generate random integers in range 1 to 900
        int randInt2 = rand.nextInt(900)+1;
        System.out.println("Random Integers range 1 to 900: "+randInt2);
        
        //Generate random integers in range 0 to 391
        int randInt3 = rand.nextInt(392);
        System.out.println("Random Integers range 0 to 391: "+randInt3);
        
        //Generate random integers in range 1000 to 5128
        int randInt4 = 1000 + rand.nextInt(4128) + 1000;
        System.out.println("Random Integers range 1000 to 5127: "+randInt4);
        
        //Generate random integers in range -17 to 22
        int randInt5 = rand.nextInt(40)-17;
        System.out.println("Random Integers range -17 to 22: "+randInt5);
        }
}

#8. coin Tossing


import java.io.*;
import java.util.Random;

class GFG {
        public static void main (String[] args) {
                Random rand = new Random(); 
        int res = 0;
        int headCount = 0;
        int tailCount = 0;
        for(int i=0; i<100; i++){
            res = rand.nextInt(2); // it will generate only 0 and 1
            if(res==0){
                tailCount++;
            }
            else{
                headCount++;
            }
        }
        System.out.println("Head Count is: "+headCount);
        System.out.println("Tail count is: "+tailCount);
        }
}

#9. Dollar to Pakistani rupees

#include <iostream>
using namespace std;

int main() {
        int dollar;
        cout<<"Enter your Dollar value: ";
        cin>>dollar;
        int taka = 164*dollar;
        cout<<dollar<<" Dollar is "<<taka<<" Pakistani taka.";
        return 0;
}

Related Solutions

Create a program to input the length and width of a rectangle and calculate and print...
Create a program to input the length and width of a rectangle and calculate and print the perimeter and area of the rectangle. To do this you will need to write a Rectangle class and a separate runner class. Your program should include instance variables, constructors, an area method, a perimeter method, a toString method, accessor and mutator methods, and user input. Your runner class should include 3 Rectangle objects. One default rectangle, one coded rectangle, and one user input...
Find the length and width of a rectangle whose perimeter is 21 meters and whose area...
Find the length and width of a rectangle whose perimeter is 21 meters and whose area is 20 square meters. Assign variables to the unknown(s)... Form a system of equations... Solve the system and state your answer in the context of the problem, show all steps clearly, be sure to check your answers:
A rectangle has a length of 10 inches and a width of 6 inches. If the length is increased by x inches and the width increased
For the following exercises, use the written statements to construct a polynomial function that represents the required information.A rectangle has a length of 10 inches and a width of 6 inches. If the length is increased by x inches and the width increased by twice that amount, express the area of the rectangle as a function of x.  
Given an integer named area which represents the area of a rectangle, write a function minPerimeter...
Given an integer named area which represents the area of a rectangle, write a function minPerimeter that calculates the minimum possible perimeter of the given rectangle, and prints the perimeter and the side lengths needed to achieve that minimum. The length of the sides of the rectangle are integer numbers. For example, given the integer area = 30, possible perimeters and side-lengths are: (1, 30), with a perimeter of 62 (2, 15), with a perimeter of 34 (3, 10), with...
Given an integer named area which represents the area of a rectangle, write a function minPerimeter...
Given an integer named area which represents the area of a rectangle, write a function minPerimeter that calculates the minimum possible perimeter of the given rectangle, and prints the perimeter and the side lengths needed to achieve that minimum. The length of the sides of the rectangle are integer numbers. For example, given the integer area = 30, possible perimeters and side-lengths are: (1, 30), with a perimeter of 62 (2, 15), with a perimeter of 34 (3, 10), with...
double maxArea(Rectangle a, Rectangle b, Rectangle c) {     double width;     double length;     double...
double maxArea(Rectangle a, Rectangle b, Rectangle c) {     double width;     double length;     double area = 0;     area = width * length;     cout << "\n***maxArea called" << endl;          cout << "***       rectangleCount = " << Rectangle::rectangleCount << endl << endl;    } Compete this code to find the maximum area of rectangle between a,b,c
Write a program 'Rectangle-Area.c' that inputs the length and width of a rectangle and outputs its...
Write a program 'Rectangle-Area.c' that inputs the length and width of a rectangle and outputs its area. The program consists of two functions: the main function and a function that computes and returns the area of a rectangle. The output of the program should be in the main program, not in the function.      Sample Input: 5 8   Sample Output: The area of a 5 by 8 rectangle is 40.
4. Define the width of a rectangle as the longest length of its sides. Given a...
4. Define the width of a rectangle as the longest length of its sides. Given a closed rectangle A in Rn and a partition P of A, define the mesh of P as the maximum width of its subrectangles. Prove that a bounded function f : A → R is integrable on A if and only if, for every > 0, there is a δ > 0 such that U(f, P) − L(f, P) < for every partition P of...
Q11.a. Calculate the cost of a foundation with length x width x thickness dimensions of 1000...
Q11.a. Calculate the cost of a foundation with length x width x thickness dimensions of 1000 x 1200 x 600 mm. Concrete of ratios 1:4:8 will be made and mixed at site with a mixer machine. Assume the following: [8.5] • The manpower service rate for casting the concrete OMR 13 / cubic meter. • The formwork rate is OMR 5 OMR / square meter. • The diesel consumption for the mixer machine is 3 liters / hour. • The...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults to 1. It has read-only properties that calculate the Perimeter and the Area of the rectangle. It has properties for both length and width. The set accessors should verify that length and width are each floating-point numbers greater than 0.0 and less than 20.0. Write an app to test class Rectangle. this is c sharp program please type the whole program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT