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
A struggle between opposing forces or characters is
tamaranim1 [39]

Answer:

Conflicts

Explanation:

The struggle between two opposing forces or characters in a story. Conflicts can exist between two people, between a person and nature or a machine or between a person a whole society. a conflict can be internal, involving opposing forces within a person's mind.

4 0
3 years ago
How can random numbers in a range be generated in Java?.
Ksju [112]

Answer:

Java oh Na Na my  

Explanation:

5 0
2 years ago
If you are installing separate anti-virus and anti-spyware programs, which should you install first
Vera_Pavlovna [14]
Probably the anti virus, so you don't get a virus from downloading the anti spyware program.
6 0
3 years ago
Write a recursive function called digit_count() that takes a positive integer as a parameter and returns the number of digits in
Gelneren [198K]

Using the computational knowledge in python it is possible to write a code that Write a recursive function called digit_count()

<h3>What is a function in Python?</h3>

In Python, a function is a sequence of commands that performs some task and that has a name. Its main purpose is to help us organize programs into chunks that correspond to how we envision a solution to the problem.

<h3>Writting the code in python:</h3>

<em>def countDigits(n):</em>

<em>   if n< 10:</em>

<em>      return 1</em>

<em>   else:</em>

<em>       return 1 + countDigits(n / 10)</em>

See more about python at brainly.com/question/13437928

#SPJ1

8 0
2 years ago
Types of computer that we use in our daily life​
ozzi

Answer:

all coumputers

Explanation:

i just like coumputers

5 0
3 years ago
Read 2 more answers
Other questions:
  • ________ symbol is used in the "Advanced" section of the time range picker to round down to nearest unit of specified time.
    8·1 answer
  • Why is self-esteem important in self-representation
    7·1 answer
  • What do you do to add a line or circle to your presentation?
    7·2 answers
  • The idea that innovations in transportation and communication technologies has changed the way we think about distance and time
    9·1 answer
  • IOS jail broken or Android unrooted which is better to hack with
    6·1 answer
  • How many pounds must a sand bag weigh to test if a safety net can absorb the proper amount of force? A. 150 lbs. B. 200 lbs. C.
    6·1 answer
  • Who created fortnite
    15·2 answers
  • What is the code i need to do
    12·1 answer
  • My brainly isnt working it isnt showing and answers wats going on? wat do i do
    14·2 answers
  • Hypertext enables you to navigate through pieces of information by clicking the __________, that connect them.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!