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
What is the last step to a URL search
Molodets [167]
U paste it the u press enter

4 0
3 years ago
Help pls nnnnnnnnnnnnnnnnnnnn
kiruha [24]

Answer:

3rd choice

Explanation:

i did that yesterday

5 0
3 years ago
Any of my friends can you answer me plz?
Valentin [98]

Answer:

God is always your friend mate

Explanation:

5 0
3 years ago
#include
Ivanshal [37]

Answer:

dang sis did u really have to but all of that

3 0
4 years ago
10(            )  8(532.2 )                         
blagie [28]

Answer:

346.30

Explanation:

7 0
3 years ago
Other questions:
  • Which command should you select to delete a comment?
    13·2 answers
  • You can use _____ to create multiple letters with the same body or message but are addressed to different people.
    14·2 answers
  • MARKETING HELP PLEASE?!?!?
    15·1 answer
  • To use an imported image, simply drag it from the desktop onto the stage. true or false
    15·2 answers
  • Write a python function average_sample_median(n,P), that return the average of 1000 samples of size n sampled from the distribut
    10·1 answer
  • Which U.S. government agency first funded the development of the Internet?
    12·2 answers
  • An integer is said to be a perfect number if the sum of its divisors, including 1 (but not the number itself), is equal to the n
    9·1 answer
  • What programming language does the LMC 'understand'?
    5·1 answer
  • Write the following function without using the C++ string class or any functions in the standard library, including strlen(). Yo
    5·1 answer
  • Look at the slide.
    10·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!