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
Jesse is trying to find a computer file. he types the name of the file into the computer search bar. the computer is most likely
lord [1]
Jesse is trying to find a computer file. he types the name of the file into the computer search bar. the computer is most likely to use a(n) <span>algorithm</span> to find the file.
5 0
3 years ago
Read 2 more answers
True or false that computers that are joined together are able to share hardware and software, but not data
kozerog [31]
False computers are able to share hardware and software but not data
8 0
3 years ago
Which statement is true about parity in RAM?
mihalych1998 [28]

Answer:

There needs to be an odd number of 1 bits

Explanation:

In memory, parity checking method is used to test the memory. There are two parity conditions. Either the parity is even or the parity is odd.

Odd parity means, number of 1's in eight bits of data. If the number 1's are odd  it means parity is odd. In case of the memory testing result the parity should be odd.

So, In case of Parity of RAM, the parity should be odd, if the data is stored in the RAM.

8 0
3 years ago
Universal Containers has a sales team focused on renewals. They will use many of the same Opportunity fields as other teams, but
ololo11 [35]

Stage Selected Values in the Sales Processes

Explanation:

A system administrator is the individual responsible for maintaining, configuring, and managing computer systems efficiently, particularly multi-user machines like servers.

Defining opportunities which comprise the selling process appropriately is one of the main functions for any company using Sales-force to monitor the performance of its sales process.

The Opportunity Stage Sales-force model off - the-box standards also reflect not the vocabulary or process that sales people use in the company As a result, Sales-force companies frequently make three main mistakes when describing their selling terminology.

6 0
3 years ago
Of the following which would be the best data representation for this puzzle in a puzzle class?
sladkih [1.3K]

please include the puzzle and or questions.

5 0
3 years ago
Other questions:
  • major m,ajorrr points helpppppppppppppppppppppppppppi have a question i hit a few buttons and now my computer is saying everythi
    11·2 answers
  • What is the block of text at the bottom of the page called?
    7·1 answer
  • 12. Noodle Tools is a website that
    8·1 answer
  • Which fingers should you use to type the reach keys?
    12·1 answer
  • What is after Windows 8.1
    11·2 answers
  • An information security ________ is a specification of a model to be followed during the design, selection, and initial and ongo
    11·2 answers
  • Anybody know this question???
    6·1 answer
  • What can be used to help diagnose and fix network connection problems?
    8·1 answer
  • What should you do if an online friend asked to meet you after school? <br>ᵖˡˢˢˢˢ...​
    13·2 answers
  • Assume that the Vehicle class contains a virtual method named CalculateMaxSpeed(). Assume that both the MotorVehicle and Automob
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!