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
Two electronics technicians are looking at the piece of testing equipment shown in the figure above. Technician A says that this
Ilya [14]
B is your correct answer.
8 0
3 years ago
Read 2 more answers
With working from home now becoming a norm what is an effective network for an organization to secure its internal resources acc
Umnica [9.8K]

Answer:

Always encrypt data never store anything in plain text someone could use wireshark to pull out a data packet and if the data is not encrypted, expect things to happen.

4 0
2 years ago
Suppose as a computer programmer, you have been assigned a task to develop a program to store the sorted data in ascending order
patriot [66]

Answer: i dont know but have an great day

Explanation:

5 0
2 years ago
Where could identity theft access your personal information?
riadik2000 [5.3K]
From hacking into public websites where you pay and stuff, or put in a fake official window to lure out your private info.
6 0
3 years ago
Read 2 more answers
E whether True or False.
Natasha_Volkova [10]

Answer:

Learn vocabulary, terms, and more with flashcards, games, and other study ... covers a large geographical area and is made up of many smaller networks. ... you share the cable infrastructure with your neighbors ... do you need if you want to connect two network segments together such as a ... the twists reduce crosstalk.Explanation:

5 0
2 years ago
Other questions:
  • During an experiment, if you purpose change the temperature to test a hypothesis the temperature is called what??
    6·1 answer
  • Suppose that outfile is an ofstream variable and output is to be stored in the file outputData.out. Which of the following state
    15·1 answer
  • Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Promp
    10·1 answer
  • Briefly explain what are JavaScript librairies​
    12·1 answer
  • Which are types of Internet connections? Choose three answers.
    5·2 answers
  • If you want to wrap text so that it fits a particular cell, which formatting section would you use?
    13·1 answer
  • Pls help computer science I will give brainliest
    8·2 answers
  • Shane is working on a new project for the sales department. The company wants a way to allow the sales force to print orders at
    5·1 answer
  • What is the final gear reduction of a compound gear reduction of 36 to 12 for the first stage and 60 to 12 for the second stage?
    7·1 answer
  • 1. Encrypt this binary string into cipher text: 110000. Include in your answer the formula the
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!