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
Which of the following refers to semitransparent text or a faint object that appears
Agata [3.3K]
<h2>Option D: Watermark refers to semitransparent text or a faint object that appears behind the document content.</h2>

Explanation:

Footer: This is a text which appears in the bottom of every page. Mostly page numbers and the name of the publisher or the company name will be in the footer.

Page Color: This represents the color of the page

Page border: This option provides border to the page. The border can be thin or thick according to the requirement.

Watermark: This is the right answer. A watermark is added such that your content cannot be used by anyone else without permission. When you copy the text, the water mark also gets copied, thus avoiding plagiarism.

4 0
3 years ago
What are the three major functions of a game engine?
zzz [600]
Three major functions of a game engine would be Physics, Programming, and an audio engine should be some major functions of a game engine.
4 0
3 years ago
Brainstorm what you want your LED board to do. What would need to happen first? What happens next?
lbvjy [14]
First it will glow then change what color you want
5 0
3 years ago
Read 2 more answers
What is the khan academy game where it is a block having to jump over lava and is an endless runner
saw5 [17]

Answer:https://www.khanacademy.org/computer-programming/lava-the-impossible-game-by-swax97/4638717300965376/embedded?id=1436456095000-0.5&origin=undefined&buttons=yes&embed=yes&editor=no&author=yes

Explanation:

5 0
3 years ago
Setting up network encryption involves
fiasKO [112]
A or B cuz you need one of the two to have WiFi cuz my computer asks if i have an adapter and then SSID
7 0
2 years ago
Other questions:
  • What will be a fundamental aspect of future games??
    12·2 answers
  • Consider the provided C++ code in the main.cpp file: The function func2 has three parameters of type int, int, and double, say a
    8·1 answer
  • How do you measure objects and re-size them in tinkercad? I need it in steps.
    7·1 answer
  • Given two variables , first_place_winner and second_place_winner, write some code that swaps their associated values. use any ad
    12·1 answer
  • What is the purpose of OPPA<br>​
    13·1 answer
  • Which of the following sentences is accurate? a. PDF stands for "Proofreading Direct Files." b. PDF files cannot be edited. c. B
    9·1 answer
  • Susan works for a company that values their employees me and deadlines in finding ways to keep the cost of doing business low wh
    12·1 answer
  • Jackie is planning a surprise birthday party for her best friend and is researching a location to have the party. Jackie found a
    6·1 answer
  • Create a simple JavaScript calculator.
    5·1 answer
  • Which of the following are exclusively associated with copyright protection
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!