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
velikii [3]
3 years ago
5

Write a program that reads in an integer value for n and then sums the integers from n to 2 * n if n is nonnegative, or from 2 *

n to n if n is negative. Write the code in two versions: one using only for loops and the other using only while loops.
Computers and Technology
1 answer:
devlian [24]3 years ago
4 0

Answer:

Following are the program in c++

First code when using for loop

#include<iostream> // header file  

using namespace std; // namespace

int main() // main method

{

int n, i, sum1 = 0; // variable declaration

cout<<"Enter the number: "; // taking the value of n  

cin>>n;

if(n > 0) // check if n is nonnegative

{

for(i=n; i<=(2*n); i++)

{

sum1+= i;

}

}

else //  check if n  is negative

{

for(i=(2*n); i<=n; i++)

{

sum1+= i;

}

}

cout<<"The sum is:"<<sum1;

return 0;

Output

Enter the number: 1

The sum is:3

second code when using while loop

#include<iostream> // header file  

using namespace std; // namespace

int main() // main method

{

int n, i, sum1 = 0; // variable declaration

cout<<"Enter the number: "; // taking the value of n  

cin>>n;

if(n > 0) // check if n is nonnegative

{

int i=n;  

while(i<=(2*n))

{

sum1+= i;

i++;

}

}

else //  check if n  is negative

{

int i=n;  

while(i<=(2*n))

{

sum1+= i;

i++;

}

}

cout<<"The sum is:"<<sum1;

return 0;

}

Output

Enter the number: 1

The sum is:3

Explanation:

Here we making 2 program by using different loops but using same logic

Taking a user input in "n" variable.

if n is nonnegative then we iterate the loops  n to 2 * n and storing the sum in "sum1" variable.

Otherwise iterate a loop  2 * n to n and storing the sum in "sum1" variable.  Finally display  sum1 variable .

You might be interested in
If you accidentally put an envelope in the Express Mail box will it still get to its destination?
Gnoma [55]
It depends because if you put a stamp on the envelope. But I think that it will still get to the destination. I think that it will either get sent to the destination or not. But you never know what is going to happen.
8 0
2 years ago
Which visual aid should Emil use to compare and contrast the political systems in three countries?
BaLLatris [955]
A vent diagram. It helps to outline the differences and the similarities between the three countries
5 0
3 years ago
In order to drive safely, you need to ___________. A. possess good vision B. look to the side while keeping your head and eyes s
Elena-2011 [213]
The question is vague, but answer B is absolutely incorrect so by default choice A is correct ...  "good" is hard to define and your vision can be corrected by glasses allowing you to drive, etc.
6 0
3 years ago
¿ Porque la madera presenta mayor resistencia a ser cortada en sentido travesal que en sentido longitudinal
ozzi
A medida que crece un árbol, la mayoría de las células de madera se alinean con el eje del tronco, la rama o la raíz. Estas células están compuestas por haces largos y delgados de fibras, aproximadamente 100 veces más largas que anchas. Esto es lo que le da a la madera su dirección de grano.

La madera es más fuerte en la dirección paralela al grano. Debido a esto, las propiedades de resistencia y rigidez de los paneles estructurales de madera son mayores en la dirección paralela al eje de resistencia que perpendicular a él
5 0
3 years ago
within the data set hrd.temp, payrate is a numeric variable and hours is a character variable. what happens when the following p
Fantom [35]
  • SAS converts the PayRate value to a numeric value form; a message is written in the form of a log

  • SAS can be defined a comprehensive statistical software that provides a wide variety of analytical capabilities, data management, and visualization options

   

  • Compare between SAS and Python

Python is a high-level programming language, more object-oriented, and is known by all programmers.

In addition, python also includes a modern programming language.

Python is a programming language that has evolved from previous programming languages.

But in terms of learning, SAS is a programming language that is easier to follow compared to python.

These are the advantages and disadvantages of each programming language

Learn more about programming language here brainly.com/question/23959041

#SPJ1

6 0
1 year ago
Other questions:
  • Design and implement a class dayType that implements the day of the week in a program. The class dayType should store the day, s
    11·1 answer
  • A plan to budget time for studying and activities is referred to as
    15·1 answer
  • What authentication protocol is ticket-based and is used by windows computers that are members of an active directory domain?
    13·1 answer
  • Which of the following can you do under the fair use exceptions?
    6·1 answer
  • How can people make sure they are using credit cards responsibly
    14·2 answers
  • ______The statement #include &lt; math.h &gt; defines the name of the current program you are writing as "math". (T/F)
    10·2 answers
  • What is a bitmap ???
    13·2 answers
  • What is the awnser ?
    11·1 answer
  • Consider a Huffman’s Algorithm that uses a variable-length encoding scheme to compress the original text: BIRTHDAY to determine
    7·1 answer
  • WHAT ARE SOME PROS AND CONS OF HYDROGEN FUELL CELLS
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!