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
Which of the following is NOT a cost typically associated with owning a car?
Arturiano [62]
A. fuel ; when you but a car you aren't buying the gas to go in it 
3 0
4 years ago
Once artwork is inserted into a placeholder, it can be moved around on a slide. True False
inn [45]

Answer:

The answer to this question is true.

Explanation:

The artwork is used on the PowerPoint for making the presentation more effective. The artwork is also known as Clip Art. In the Microsoft PowerPoint, the clip art is a collection of media files like image file, video file, an audio file, and an animation file. If our computer has an Internet connection, then we can add new clip art for free. If we use the artwork or clip art on slide So it can be moved around on the slide.

6 0
3 years ago
A(n)
Yuliya22 [10]

Answer:

A(n)  <u>Data Warehouse</u> is a central repository that contains stored or  archived data gathered from multiple databases.

Explanation:

A data warehouse is also database that is considered as central database of the company, where data of whole company is gathered and stored from multiple databases of the company. It also contains the archived data of the company that could be needed in future.

For Example

A company has several branches and franchises, each of place having own database to store the relevant information about that particular platform. The company has its own database that is linked with all platform's databases of the company. This central database is called data warehouse that gathers data from all sub data bases to analyze the operations and services of the company at different places. This database contains archive data of different databases of the company.

7 0
4 years ago
PLEASE HELP!! How do I bypass the question limit on brainly? They made it to where if you turn off the cookies for the website t
Rina8888 [55]
This makes no sense
7 0
3 years ago
The idea that an object can exist separate from the executing program that creates it is called
vaieri [72.5K]

Answer:

Explanation:

Apply handrub to palm of one hand.

Rub hands together covering all surfaces of hands and fingers.

Rub until handrub is absorbed.

3 0
3 years ago
Other questions:
  • Which of these molecules has the same number of shared and unshared electron pairs?CCl2F2Br2HClPF3H2S
    9·2 answers
  • List two reasons why buying a computer is no easy task?
    7·1 answer
  • What did Francis Ford Coppola and George Lucas create?
    10·1 answer
  • Which of the following is not hardware? Question 7 options: A) Wireless network router B) Printer C) Virus Scanner D) Flat-panel
    11·1 answer
  • With his assembly lines, Henry Ford improved what process? A. computer management B. just-in-time management C. Bessemer process
    5·1 answer
  • Your first job is to create a Java program that repeatedly asks the user whether they wish to calculate another square root. If
    11·1 answer
  • 20 points
    5·1 answer
  • Which of the following is adoptable in Mindanao only?​
    5·1 answer
  • Un comerciante de régimen código para comprar mercancías gravadas a un comerciante de régimen simplificado precio de venta al pú
    5·1 answer
  • Please help I need some one that knows about the article Disney Imagineer because I wasn't in school today so please help me wri
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!