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 which asks a user for a word and performs letters manipulation. If the word is empty, the program should
never [62]

Answer:

The program is as follows:

word = input("Enter a word: ")

if word:

   if len(word) <= 4:

       word = word[::-1]

   else:

       word = word[0]+word[1]+word[-2]+word[-1]

   print(word)

else:

   print('empty!')

Explanation:

This gets input for word from the user

word = input("Enter a word: ")

If input is not empty

if word:

This checks if the length is less than or equal to 4 characters

   if len(word) <= 4:

If yes, this reverses the word

       word = word[::-1]

If otherwise,

   else:

This gets the first, second, second to last two characters

       word = word[0]+word[1]+word[-2]+word[-1]

Print the new string

   print(word)

Print empty, if input is empty

<em>else: </em>

<em>    print('empty!')</em>

8 0
3 years ago
What component uses thermal paste to attach the heat sink?
natka813 [3]

Answer:

Thermal Compound/Thermal Grease

Explanation:

Thermal compound, also known as thermal paste and thermal grease, is a material used to fill the microscopic gaps between a computer's CPU and its heat sink. Thermal compound significantly increases the heat sink's ability to cool the CPU, allowing the CPU to run at a higher speed and improve system performance.

6 0
3 years ago
Make a hierarchical directory structure under /root that consists of one directory containing three subdirectories.
Alika [10]
You can make a hierarchical directory structure under /root that consists of one directory containing subdirectories  by using cd and mkdir
the mkdir command created the directories while the cd command changes which directory you're currently in
5 0
2 years ago
The appropriate length for an e-mail is about half the amount of text that will fit on an 8 1/2' by 11' page.
lys-0071 [83]

Wrong..............................................

7 0
2 years ago
Read 2 more answers
The following is part of a log file taken from the machine on the network with the IP address of 192.168.1.106:__________.
Phantasy [73]

Answer:

Port scan targeting 192.168.1.106.

Explanation:

In the following question, there is some part of the question and options is missing.

In the following statement, when a file log is taken from the computer system which has Internet Protocol address is given in the statement and by the further details of the statement in which time and the port destination by examine those details of the Internet Protocol, the following port scan targeting the IP address 192.168.1.106.

3 0
3 years ago
Other questions:
  • List 3 ways that you can use excel and the features it includes and explain why a spreadsheet is the best choice for this task.
    5·1 answer
  • Who does Potholes effect in South Africa?
    15·1 answer
  • How do I make my own extension for chrome?
    7·1 answer
  • The instructions for a computer program are sometimes referred to as . computer programmers focus on computer programs, but they
    5·2 answers
  • Do You assign the Needs Met rating before assigning the page quality (PQ) rating?
    11·1 answer
  • How is the bootstrap program started?
    6·2 answers
  • Breaking code rules is only a problem once in a while. Group of answer choices True False
    12·1 answer
  • What is Celeste? ( This is for my coding class )
    6·2 answers
  • Which of the following statements are true concerning abstraction and refinement? Select 3 options:
    14·1 answer
  • Jackie is planning a surprise birthday party for her best friend and is researching a location to have the party. Jackie found a
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!