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
Develop a C program that calculates the final score and the average score for a student from his/her (1)class participation, (2)
White raven [17]

Answer:

Following are the code to the given question:

#include<iostream>//defining header file

using namespace std;

int main()//defining main method

{

float clspat,test,asst,exam,practice,total,average;//defining floating variable

char name[100];//defining char variable to input value

cout<<"Enter Name of Student: ";//print message

cin>>name; //input value

cout<<"Enter scores in following scoring items between 0 and 100:"<<"\n";//print message

cout<<"Enter class participation score: ";//print message

cin>>clspat;//input value

cout<<"Enter test score: ";//print message

cin>>test;//input value

cout<<"Enter assignment score: ";//print message

cin>>asst;//input value

cout<<"Enter exam score: ";//print message

cin>>exam;//input value

cout<<"Enter practice score: ";//print message

cin>>practice;//input value

total=clspat+test+asst+exam+practice;//calculating total value

average=total/5;//calculating average value

cout<<endl<<"SCORE CARD OF "<<name<<"\n"<<"-----------------------"<<"\n";//print value with message

cout<<"Total score: "<<total<<"\n";//print message with value

cout<<"Average score: "<<average; //print message with value

}

Output:

please find the attached file.

Explanation:

In this code, the floating-point variable is declared, which is used for input value from the user-end and after input the floating-point value it uses the "total, average" variable to calculate its respective values and store its value with init, after storing the value into the variable it uses the print method to print its values.

5 0
3 years ago
____________ approach involves trying every possible key until an intelligible translation of the ciphertext into plaintext is o
Vitek1552 [10]

Answer:

Symmetric encryption or single key encryption

8 0
3 years ago
Complete the sentence. Assigning a data value to a variable in a program is referred to as______.
luda_lava [24]

Answer:

The answer to this question is given below in the explanation section.

Explanation:

The correct answer to this question is initialization.

When you write a variable in a program,  for example int firstNumber. The integer variable firstNumber is declaring in the program.

when you assign a data value to a variable in a program is referred to as initialization. for example:

firstNumber=10;

You can declare and initialize the variable in one statement such as:

int firstNumber=10;

6 0
3 years ago
QUESTION 9
bixtya [17]

Alex if he wants to center the title and his name on his cover page, he has to press centered.

<h3>What are  cover page?</h3>

Cover pages is known to be the page that is made up of the title of sny article, books etc. It also include the name of your school, course name, etc.

Therefore, Alex if he wants to center the title and his name on his cover page, he has to press centered so as to be in the middle top of the document.

Learn more about cover page from

brainly.com/question/3602860

#SPJ1

6 0
2 years ago
QUICK! The commands available from a menu change depending upon what you are doing. True False
Arisa [49]

Answer:

true because it changes depending upon what you were doing so true

7 0
3 years ago
Read 2 more answers
Other questions:
  • Which of the following is generally nota desire today's workers? A. computer ability B willingness to learn C. teamwork skills D
    8·1 answer
  • Search engines enable you to
    12·1 answer
  • What framework provides a simple API for performing web tasks?
    13·1 answer
  • The mode is least appropriate for:___________
    6·1 answer
  • What are some of the ways we can resolve IPv4 address shortages? Check all that apply.
    13·1 answer
  • Question 1 of 10 Chase lives in Oregon but works for a company that is located in Florida. What business trend is this an exampl
    11·2 answers
  • Your friend Amy calls you asking for help with her new LCD monitor. She says the monitor isn't showing the whole picture. What i
    14·1 answer
  • Explain what a datalist element does and is used for.
    12·1 answer
  • Design a for loop that gets 6 integer numbers from a user, accumulates the total of them, then displays the accumulated total to
    9·1 answer
  • A video game controller contains the buttons: A, B, X, Y. When the player presses A their character Jumps. When the player press
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!