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
dmitriy555 [2]
3 years ago
6

a) Write a program that prompts for an integer—let’s call it X—and then finds the sum of X consecutive integers starting at 1. T

hat is, if X=5, you will find the sum of 1+2+3+4+5=15.b) Modify your program by enclosing your loop in another loop so that you can find consecutive sums. For example , if 5 is entered, you will find five sum of consecutive numbers:1 = 11+2 = 31+2+3 =61+2+3+4 =101+2+3+4+5 =15Print only each sum, not the arithmetic expression.
Computers and Technology
1 answer:
Alex73 [517]3 years ago
7 0

Answer:

<u>Solution a</u>

  1. n = int(input("Enter an integer: "))
  2. sum = 0
  3. for x in range(1, n+1):
  4.    sum += x  
  5. print(sum)

<u>Solution b</u>

  1. n = int(input("Enter an integer: "))
  2. for a in range(1, n + 1):
  3.    sum = 0
  4.    for x in range(1, a+1):
  5.        sum += x
  6.    print(sum)

Explanation:

Solution code is written in Python 3.

<u>Solution a</u>

First get the user input for an integer (Line 1).

Create a variable sum and initialize it with zero value (Line 3).

Create a for loop to traverse through the number from 1 to integer n (inclusive) and sum up the current number x (Line 5-6).

Print the sum to terminal (Line 8).

<u>Solution b</u>

Create an outer for loop that traverse through the number from 1 to integer n (inclusive) (Line 3).

Create an inner loop that traverse through the number from 1 to current a value from the outer loop and sum up the current x value (Line 5-6).

Print the sum to terminal (Line 7) and proceed to next iteration and reset the sum to zero (Line 4).

You might be interested in
A firm would be most likely to establish an enterprise portal if it wanted to Multiple Choice prevent outsiders from using its i
ankoles [38]

Answer:

prevent outsiders from using its information network.

Explanation:

The firm would use a portal to create a network or intranet for all its information because only employees would have access to it.

4 0
3 years ago
a) (20 points) Define the method inDogish recursively such that it returns true if the word is in dog-ish and false if it is not
tia_tia [17]

Answer:

/ Main.java

class Main {

     public static void main(String[] args) {

           //System.out.println(inDogish("aplderogad"));

           //System.out.println(inXish("aplderogad", "dog"));

     }

     // returns true if the word is in dog-ish

     // returns false if word is not in dog-ish

     public static boolean inDogish(String word) {

           /**

           * Note: we can use the inXish() method to complete this method in a

           * single line if you want. just write 'return inXish(word,"dog");' and

           * you are done.

           */

           // if word is null or empty, returning false

           if (word == null || word.length() == 0) {

                 return false;

           }

           // converting word to lower case

           word = word.toLowerCase();

           // if 'd' does not exist on the word, returning false

           if (!dogishHelper(word, 'd')) {

                 return false;

           }

           // removing the characters upto first 'd' in word

           word = word.substring(word.indexOf('d') + 1);

           // if 'o' does not exist on the word, returning false

           if (!dogishHelper(word, 'o')) {

                 return false;

           }

           // removing the characters upto first 'o' in word

           word = word.substring(word.indexOf('o') + 1);

           // if 'g' does not exist on the word, returning false, otherwise

           // returning true

           if (!dogishHelper(word, 'g')) {

                 return false;

           } else {

                 return true;

           }

     }

     // necessary to implement inDogish recursively.

     // returns true if letter is in word, else not.

     public static boolean dogishHelper(String word, char letter) {

           // if word is empty, returning -1

           if (word == null || word.length() == 0) {

                 return false;

           }

           // if first character in word is letter, returning 0

           if (word.charAt(0) == letter) {

                 return true;

           }

           return dogishHelper(word.substring(1), letter);

     }

     // the best thing about the above helper method is that we can use it in

     // inXish method also

     // a generalized version of the inDogish method

     public static boolean inXish(String word, String x) {

           // if x is empty, returning true (base case, which means all the letters

           // in x are in word in that order)

           if (x.length() == 0) {

                 return true;

           }

           // getting first character of x

           char first = x.charAt(0);

           // if first character does not exist on the word, returning false

           if (!dogishHelper(word, first)) {

                 return false;

           }

           // calling and returning inXish recursively, passing the word after

           // removing characters upto first occurrance of char first, and x after

           // removing first character from it

           return inXish(word.substring(word.indexOf(first) + 1), x.substring(1));

     }

}

Explanation:

7 0
4 years ago
Write a function named countEvens that counts and returns the number of even integers in an array. The function must have this h
skelet666 [1.2K]

Answer:

function countEvens(list) {

   // IMPLEMENT THIS FUNCTION!

var even = [];

 

for(var i = 0; i < list.length; i++){

 if (list[i] % 2 == 0){

   even.push(list[i]);

   

 }

}

 return console.log(even)

}

var list = [ 17, 8, 9, 5, 20 ];

var count = countEvens(list);

Explanation:

The programming language used is JavaScript.

An even variable is initialized, to hold  the even variables within list.

A FOR loop loop iterates through all the elements in the list, an IF statement is used to check using the modulo division to check if the elements in the list are even.

The even elements are appended to the even list using .push() method.

Finally, the function returns the even list and outputs the list to the log using console.log(even) .

A list is created and the function is called.

4 0
3 years ago
Write a method that takes a RegularPolygon as a parameter, sets its number of sides to a random integer between 10 and 20 inclus
Sergio [31]

Answer:

import java.lang.Object

import java.lang.Math

public class RegularPolygon  extends java.lang.Object{

  public void randomize(RegularPolygon c){

       int min = 10, max1 = 20;

       double  min1 = 5, max1 = 12;

       double range = (max - min) + 1;

       double range1 = (max1 -min1) + 1

       int side = (Math.random() * range) + min;

       double len = (Math.random() * range1) + min1;

       c.setNumSides(side);

       c.setSideLength( len);

 }

 public static void main(String[] args) {

    RegularPolygon r = new RegularPloygon();

    randomize(r);

 }

}

Explanation:

The randomize method accepts a regular polygon class as its only parameter and assigns a random number of sides and the length of these sides with the 'Math.random' function.

5 0
3 years ago
"the ________ attribute of the anchor tag can cause the new web page to open in its own browser window."
zheka24 [161]
Target





-------------------------------
8 0
3 years ago
Other questions:
  • If memory accesses take 100 times more than register accesses, then a LOAD instruction will take ___ machine cycles than an ADD
    7·1 answer
  • Which button is used to open the Start menu in Windows Vista?
    14·1 answer
  • Which one of the secondary storage types below would be best if you wanted to edit files and re-save them to secondary storage?
    10·1 answer
  • A compound document contains _______ from different applications.
    12·2 answers
  • The class shown below called is called CSVReader. Use this class exactly as it is written (specifically don’t add any instance v
    8·1 answer
  • which of the following is the full path and filename of the file that contains information about which interrupt request (IRQ) c
    10·1 answer
  • What function does an extranet perform?
    5·1 answer
  • How are PivotCharts different from regular charts?
    12·1 answer
  • HELPPPPP Which tag used to add a new line:<br> A. ol<br> B. P<br> C. h1<br> D li
    7·1 answer
  • A(n) _____ is an organization's management support system that combines many databases to improve management decision making.
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!