Answer:
The program to this question as follows:
Program:
#include<iostream> //include header file.
using namespace std; //using name space.
int main() //main function.
{
int n,i,key; //define variable.
int a[n]; //define array
cout<<"Enter size of array :"; //message.
cin>>n; //input number from user.
cout<<"Enter array elements :"; //message
for(i = 0;i<n;i++) //loop
{
cin>>a[i]; //input array elements
}
cout<<"Enter a number:"; //message
cin>>key; //input number.
for(i = 0;i<n;i++) //loop
{
if(a[i]<=key) //if block
{
cout<<a[i]<<"\n "; //print array elements
}
}
return 0;
}
Output:
Enter size of array :7
Enter array elements :5
50
50
75
100
200
140
Enter a number:100
5
50
50
75
100
Explanation:
The description of the above program as follows:
- In this program first, we include a header file and define the main method in method we define variables and array that are "n, i, key and a[]". In this function, all variable data type is "integer".
- The variable n is used for the size of array and variable i use in the loop and the key variable is used for comparing array elements. Then we use an array that is "a[]" in the array, we use the for loop to insert elements form user input.
- Then we define a loop that uses a conditional statement in if block we check that array elements is less than and equal to a key variable. If this value is true so, we will print the remaining elements.
Answer:
The algorithm is as follows;
1. Start
2. Input TeddyBears
3. Input Hours
4. WagebyTeddy = 2 * TeddyBears
5. WagebyHour = 5 * Hours
6. If WagebyHour > WagebyTeddy then
6.1 Print WagebyHour
7. Else
7.1. Print WagebyTeddy
8. Stop
Explanation:
The following variables are used;
TeddyBears -> Number of teddy bears made
Hours -> Number of Hours worked
WagebyTeddy -> Wages for the number of teddy bears made
WagebyHour -> Wages for the number of hours worked
The algorithm starts by accepting input for the number of teddy bears and hours worked from the user on line 2 and line 3
The wages for the number of teddy bears made is calculated on line 4
The wages for the number of hours worked is calculated on line 5
Line 6 checks if wages for the number of hours is greated than wages for the number of bears made;
If yes, the calculated wages by hour is displayed
Otherwise
the calculated wages by teddy bears made is displayed
Answer:
Explanation:
The following code is written in Java. It is a very simple three line statement (assuming that the random library was imported) that chooses a random integer value between 5 and 5000 and prints it to the screen using the println statement.
Random rand = new Random();
int randomNum = rand.nextInt((5000 - 5) + 1) + 5;
System.out.println(randomNum);
The random number generator is initialized and given a value between 5 and 5000. Since random number Generator will generate a number between 0 and the given value then subtracting 5 from the initial generated number makes sure that it is not more than 5000 and then adding 1 and 5 after wards makes sure that it is more than 5 always.