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
________ tv uses a digital signal, or series of 0s and 1s, and is much clearer and less prone to interference than analog tv. re
vagabundo [1.1K]
Digital TV uses a digital signal, or series of 0s and 1s, and is much clearer and less prone to interference than analog TV.<span> It improves reception, offers better picture and sound quality, </span>
In analog television, in which the video and audio are carried by analog signals. In digital they are carried with digital encoding. 
4 0
4 years ago
For angular how can we set up th edatabse.
lesya [120]
You can not communicate directly between Angular and MySQL. You'll need to build a back-end Web service that calls MySql using php or node. Angular can communicate with this back-end Web service via http.
8 0
3 years ago
Limestone deposits indicate that the location was:
jek_recluse [69]
B: formed by an active volcano
8 0
4 years ago
The standing toe touch is most likely to result in
Alecsey [184]
The correct answer is D
4 0
3 years ago
Hardcoding numbers (unnamed, literal constants) in code without explanation is a bad programming practice. What are these hardco
BaLLatris [955]

Answer:

Magic numbers.

Explanation:

In Computer programming, hardcoding can be defined as a software development process which typically involves directly embedding or infusing data into the source code of a computer software application or program rather than generating the data at runtime or sourcing (obtaining) it from an external source.

Hence, hardcoding numbers (unnamed, literal constants) in code without explanation is a bad programming practice. These hardcoded numbers are known as magic numbers.

A magic number refers to a hard-coded value embedded into the source code of a software program without explanation and as such are very difficult to update.

3 0
3 years ago
Other questions:
  • Which component of a computer is its input device and what role does it play in a document?
    6·1 answer
  • Why did the creation of ARPANET help with this critical need?
    9·1 answer
  • Give several reasons why Python is such a great programming language. Explain how Python is related to flowcharts. (students mak
    9·1 answer
  • Assume the existence of an UNSORTED ARRAY of n characters. You are to trace the CS111Sort algorithm (as described here) to reord
    14·1 answer
  • 5.14 Describe how the compare and swap() instruction can be used to provide mutual exclusion that satisfies the bounded-waiting
    13·1 answer
  • How to bold words in microsoft word
    10·1 answer
  • Why wouldn't a game using just run-length encodings be challenging?
    9·1 answer
  • Explain what iteration is and why we need it in code
    11·1 answer
  • It is most commonly used for self-running presentations.
    8·1 answer
  • Which of these are correctly formatted python lists? check all that apply. list1=(race, cars, trucks, bikes) list2=[‘computer sc
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!