1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
miv72 [106K]
3 years ago
5

g Write a program that prompts the user for an integer n between 1 and 100. If the number is outside the range, it prints an err

or. The program computes and prints at the end two things: The sum of the numbers from 1 to n. The average of the numbers from 1 to n. Average should only have 2 digits after the decimal.
Computers and Technology
1 answer:
grin007 [14]3 years ago
3 0

Answer:

The cpp program is given below.

#include<iostream>

#include<iomanip>

using namespace std;

int main() {

   

   // variables declared

   int n;

   int sum=0;

   float avg;

   

   do

   {

       // user input taken for number    

       cout<< "Enter a number between 1 and 100 (inclusive): ";

       cin>>n;

       

       if(n<1 || n>100)

           cout<<" Number is out of range. Enter valid number."<<endl;

       

   }while(n<1 || n>100);

   

   cout<<" "<<endl;

   

   // printing even numbers between num and 50  

   for(int num=1; num<=n; num++)

   {

       sum = sum + num;

   }

   

   avg = sum/n;

   

   // displaying sum and average

   cout<<"Sum of numbers between 1 and "<<n<<" is "<<sum<<endl;

   cout<<"Average of numbers between 1 and "<<n<<" is ";

   printf("%.2f", avg);

   

       return 0;

}

OUTPUT

Enter a number between 1 and 100 (inclusive): 123

Number is out of range. Enter valid number.

Enter a number between 1 and 100 (inclusive): 56

 

Sum of numbers between 1 and 56 is 1596

Average of numbers between 1 and 56 is 28.00

Explanation:

The program is explained below.

1. Two integer variables are declared to hold the number, n, and to hold the sum of numbers from 1 to n, sum. The variable sum is initialized to 0.

2. One float variable, avg, is declared to hold average of numbers from 1 to n.

3. User input is taken for n inside do-while loop. The loop executes till user enters value between 1 and 100. Otherwise, error message is printed.

4. The for loop executes over variable num, which runs from 1 to user-entered value of n.

5. Inside for loop, all the values of num are added to sum.

sum = sum + num;

6. Outside for loop, average is computed and stored in avg.

avg = sum/n;

7. The average is printed with two numbers after decimal using the following code.

printf("%.2f", avg);

8. The program ends with return statement.

9. All the code is written inside main() and no classes are involved.

You might be interested in
Please help me asapppp!​
I am Lyosha [343]

Answer:

1.  Formula is A2 : A9 = COUNT( A2: A9 ) = 8

2. Formula is SUM( A2: A9 ) = 36

3. Formula is B2 : B9 = COUNT( B2: B9) = 8

4. Formula is  MAX( C2: C9) = 5

5. Formula is MIN( C4: C8) = 3

6. Formula is SUM( C5 - C6) = 0

7. Formula is AVERAGE( C2: C9) = 4

Explanation: Have a nice day!✌️

6 0
3 years ago
What term is defined as private data placed in a packet with a header containing routing information that allows the data to tra
xeze [42]

Answer:

Encapsulation

Explanation:

Encapsulation is referring to the concept that data buding with some approach or method that is applied to that selected data.  it also restricts the use of some internal data of an object. The motive behind restricting the data is to prevent the misuse of the data.

For example- in java, encapsulation is refer to the wrapping of code with the data in one unit.

7 0
4 years ago
Select the correct answer. Marie uses a browser to visit a blog. How is the blog uniquely identified? A. web page B. website C.
damaskus [11]

Answer:

I belive the answer is E

Explanation:

8 0
3 years ago
Rasha is working in QuickBooks as a heavy storm begins. Her building is notorious for losing power during storms, so she wants t
Anestetic [448]

Answer:

B.

Explanation:

Manually Back Up:

1. File Menu and click Back Up

2. Click Back Up Company.

3. Click Browse if you wanna a specific location o change the name. Then click Save.

5 0
3 years ago
Read 2 more answers
Given a list of numbers, find the correct function to find the sum of the squares of only the positive elements.
vovikov84 [41]

Answer:

sqsum4

Explanation:

So to raise a number to a power in python, you can use the ** operation, which is usually confused with the ^, which is an operation, but it is not for raising numbers to a power. It is the xor bit operation, which if you don't know at the moment, it's fine, since it's not necessary for this. each of these lists uses a generation comprehension which is generally defined as: (x for x in object if condition) with the if condition being optional, but in this case it's necessary. If it's a bit confusing, you can define a generator using a function so it's a bit more spread out:

def generator(object):

   for x in object:

       if condition:

           yield x

Although in this instance were going to be performing some operation on x, which in this case is squaring it. So let's just look at the two functions that use the **, since they should be the only options that will be correct.

sqsum1(nums):

   This function does square x, except it uses incorrect syntax. The condition should come after the for loop. Python likely wont tell you this, because it may think you're trying to do something else. You can do one line if statements like this: a if condition else b, which will return a if the condition is true, and b if it isn't. So it may think that you're trying to do this one line if statement, and say that you're missing an else. The function could even implement in this way: <em>x**2 if x > 0 else 0 for x in nums</em>. This way if the x is negative it counts as 0, or in other words isn't counted towards the sum. But without this fix, the function will raise a syntax error

sqsum4(nums):

   This will square each number in x only if the current element "x" is greater than 0, or in other words positive. And then it returns the sum. So this function returns the expected output

5 0
2 years ago
Other questions:
  • What are the major features of a successful ethics training program and communication systems? Think of an example of a company
    13·1 answer
  • What is the name of the subsystem that transfers data between components inside a pc or between pcs?
    12·1 answer
  • In one to three sentences describe one reason you would import data into a datebase
    13·1 answer
  • Question 1<br> a node is.<br> •a sever<br> A Cell Phone<br> A Laptop<br> a device on a network
    13·1 answer
  • The major difference between a calculator and a computer, when performing calculations, is that a
    12·1 answer
  • In PKI, the CA periodically distributes a(n) _________ to all users that identifies all revoked certificates.
    13·1 answer
  • Does anyone know the answer for question d (questions in the picture btw)
    7·1 answer
  • In query design view you can add .............. to limit the number of records shown in the resulting datasheet
    13·1 answer
  • If you are inviting more than one person to a meeting, you can use a(n) _____ to separate the email addresses.
    5·1 answer
  • which type of virtual machine (vm) takes advantage of unused capacity in data centers at a much lower cost than regular vms of s
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!