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
Write a Python program to convert the characters to lowercase in a string
wel

Answer:

Following is the program in the python language

st = 'sAN RaN'  #String

print(st.lower()) #display into the lowercase

Output:

san ran

Explanation:

Following are the description of program

  • Declared and initialized the string in the "st" variable .
  • The lower function in python is used for converting the uppercase string into the lower case string .
  • Finally in the print function we used lower function and display the  string into the lower case
8 0
3 years ago
Direct solar power is when:
creativ13 [48]
The answer is c the sun is used to directly do a job 
4 0
3 years ago
Determine the best access modifier for each of the following situations: a. A class Employee records the name, address, salary,
Lorico [155]

Answer:

a. Protected

b. Public

Explanation:

There are four acess modifier in Java.

Default: Acessible only within the same package.

Public: Can be acessed by any class.

Private: Acessible only within the class.

For example, you have a class employee and a private method. This method can only be accessed by an object that is an instance of an employee.

Protected: Used in classes that extend each other. For example, a class of employees would extend employee.

So:

a. A class Employee records the name, address, salary, and phone number.

The best acesses modifier is protected. A class may extended employee but have the same arguments(name, adress, salary, phone number), so it should also have acess to the method.

b. An adding method inside of a class BasicMath.

This method can be used in a variety of packages and projects and classes... and there is no important information regarding security. So the best method is public.

4 0
3 years ago
What should you do if your temperature gauge moves up to just below the red zone?
Alexeev081 [22]
<span>If the temperature gauge moves up to just below the red zone,you should turn off your air conditioner and turn on your vehicle's heater. Then immediately </span>find a mechanic or pull over safely and contact a road service.




4 0
3 years ago
Read 2 more answers
What is the difference between above ground mining and underground mining?
neonofarm [45]
If this is in reference to diamond mining then above ground would be panning while below ground would be the actual mining into the soil. 
6 0
2 years ago
Other questions:
  • What is html?
    9·2 answers
  • What is the inverse function of d(x ) = -2x - 6?
    11·1 answer
  • What element is not a selection in the Interface preferences? UI Character Presets UI Font Size UI Language UI Scaling
    9·1 answer
  • Sarah's Texas location has a server that is starting to give her trouble. It is needing to be restarted frequently and seems to
    15·1 answer
  • An acronym is a word formed by taking the first letters of the words in a phrase and making a word from them. For example, AGH i
    10·1 answer
  • Select the correct answer.
    9·1 answer
  • Help me<br>please answers this questions<br>​
    6·1 answer
  • Which of the following statements is correct? User data cannot be combined and shared among authorized users. In a nondatabase,
    6·1 answer
  • , how do you make this user <br> XtraCrispyIzzy<br> 12 characters or less for microsoft?
    8·1 answer
  • What is System Testing
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!