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
What type of encryption is this "1d83ad29cf19ff399445aff3a0da74d9"
bija089 [108]

Due to the length of the hash I'm going to say it's MD5

7 0
3 years ago
ANSWER IN 5 MINUTES TO GET 50 POINTS!!!
Luba_88 [7]
The answer is d. Hope that helps.
7 0
3 years ago
A network that is located in a small area, such as a single building is called a
love history [14]
Networks in small areas are called Local Area Network (LAN). LAN's are in small areas like offices and buildings. Computers close to each other can make up a LAN network with the right equipments. A LAN network is good for doing business.
4 0
3 years ago
Paragraph talking about why i chose bill gates
AlekseyPX

Answer:

Bill Gates

Explanation:

Bill Gates is known as one of the richest entrepreneurs in the world. He is responsible for creating the largest computer and technology business in the world, Microsoft. In the company's early days, no one would have thought that it would change both America and the world from that point on.

When Gates first created Microsoft,

Middle class Americans could now purchase a personal computer that would not put a set back in their bank account.As soon as personal computers started hitting the mainstream market, the demand for improvement began. People wanted their machines to boot up faster, have larger memory, have improved programming, and be easier to use. Luckily Gates already knew the people would have these needs, so development was already underway.

7 0
3 years ago
What cell address indicates the intersection of the first row and the first column in a worksheet?
Roman55 [17]
The answer is A1. 

The columns are arranged alphabetically, and the rows are ordered numerically. The cell address states the column, a letter, followed by the row, a number. The first cell address, the top-left cell of the sheet, is A1
7 0
3 years ago
Read 2 more answers
Other questions:
  • ________ is used to store programs and instructions that are automatically loaded when the computer is turned on.
    14·1 answer
  • I only want someones opinion on this not anything you would find in a book.
    7·1 answer
  • What does prominent hyperintensity mean relating to MRI?
    5·1 answer
  • Black Ops 3 For Ps4 Players Here
    7·1 answer
  • Which process centers the spreadsheet's content on the page?
    15·1 answer
  • Given the statement: int list[25]; The index can go from 0 to 25 inclusive withoutgoing beyond the end of the array.true or fals
    14·1 answer
  • 2. Write a Python regular expression to replace all white space with # in given string “Python is very simple programming langua
    14·1 answer
  • You have been hired by a small company to install a backbone to connect four 100base-T Ethernet LANs (each using one 24-port hub
    12·1 answer
  • Among your selection in different online interfaces, which are your top three favorites?​
    7·2 answers
  • 1. The letters that appear after the dot after a file name are called the:
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!