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
Lynna [10]
3 years ago
11

Write a program asks the user for an integer N and then adds up the first N odd integers. For example, if the user asks the sum

of the first 10 odd integers, the program computes: Use a loop for this. Use instructions and pseudo instructions up to chapter 24. Use exception handler services for input and output. How many odd numbers: 10 The sum: 100 If the user enters a negative integer or non-integer, just print a zero. Start your source file with comments that describe it:
Computers and Technology
1 answer:
abruzzese [7]3 years ago
7 0

Answer:

The program in Python is as follows:

while True:

   try:

       num = input("Number of odds: ")

       num = int(num)

       break

   except ValueError:

       print("Invalid integer! ...")

sum = 0

odd = 1

if num > 0:

   for i in range(num):

       sum+=odd

       odd+=2

print("Total:",sum)

Explanation:

This is repeated until a valid integer is inputted

while True:

This uses exception

   try:

This gets the number of odd numbers

       num = input("Number of odds: ")

This converts the input to integer

       num = int(num)

       break

If input is invalid, this catches the exception

<em>    except ValueError:</em>

<em>        print("Invalid integer! ...")</em>

This initializes sum to 0

sum = 0

This initializes odd to 1

odd = 1

If input is positive

if num > 0:

This add the first num odd numbers

<em>    for i in range(num):</em>

<em>        sum+=odd</em>

<em>        odd+=2</em>

This prints the total

print("Total:",sum)

You might be interested in
How was WiFi discovered?
postnew [5]

Answer:

An Australian scientist by the name of John O'Sullivan was inspired by Stephen Hawking's theory of evaporating black holes and their subsequent radio waves. He set out to find them and prove the theory correct. (Wi-Fi was discovered on accident)

7 0
3 years ago
_______ is the lightest chemical element <br> A.oxygen<br> B.hydrogen<br> C.helium
Oksanka [162]
B. Hydrogen because it has an atomic number of 1 and molar mass of 1
6 0
3 years ago
Read 2 more answers
Can you debug the following code using the given test code?public class SavingAccount{ // interest rate for all accounts private
Lyrx [107]

Answer:

Explanation:

The reason the code was not working was due to a couple of errors. First, the SavingAccount class needed to be public so that it can be accessed within the same file. Secondly, the savingsBalance variable was set to final and therefore could not be modified, the final keyword needed to be removed. Lastly, the constructor for the SavingAccount class was incorrect. Constructors do not use the keyword void and instance class variables need to be referenced using the this keyword.

class SavingAccount{

   // interest rate for all accounts

    private static double annualInterestRate = 0;

    private double savingsBalance;

   public SavingAccount(double savingsBalance) {

       this.savingsBalance = savingsBalance;

   }

   public void calculateMonthlyInterest() {

        savingsBalance += savingsBalance * ( annualInterestRate / 12.0 );

    }// end method calculateMonthlyInterest

   // modify interest rate

   public static void modifyInterestRate( double newRate ) {

        annualInterestRate = ( newRate >= 0 && newRate <= 1.0 ) ? newRate : 0.04;

    } // end method modifyInterestRate

   // get string representation of SavingAccount

   public String toString() { return String.format( "$%.2f", savingsBalance );

    } // end method toSavingAccountString

}// end class SavingAccount

// Using this test codepublic

//

class SavingAccountTest{

   public static void main(String[] args) {

       SavingAccount s1 = new SavingAccount(400);

       SavingAccount s2 = new SavingAccount(1000);

       SavingAccount.modifyInterestRate(0.05);

       System.out.printf("SavingAccount 1 is: %s\nSavingAccount 2 is: %s", s1, s2);

       s1.calculateMonthlyInterest(); s2.calculateMonthlyInterest();

       System.out.printf("\nSavingAccount 1 after the interest is: %s\nSavingAccount 2 after the interest is: %s", s1, s2);

       SavingAccount.modifyInterestRate(.025);

       s1.calculateMonthlyInterest();

       s2.calculateMonthlyInterest();

       System.out.printf("\nSavingAccount 1 after the new interest is: %s\nSavingAccount 2 after the new interest is: %s", s1, s2); }}

6 0
3 years ago
2. What are some other reasons why you might need to know the operating system version on your computer or mobile device
mote1985 [20]
1. To check compatibility of an app/ software
2. To know the vulnerabilities
3. To be aware of available system updates
Hope that helps
7 0
2 years ago
What is a program that, when installed on a computer, records every keystroke and mouse click?
Mrac [35]
"Key logger" This could be software or hardware that does this.
6 0
3 years ago
Other questions:
  • Which is better to use for cleaning electronics, Ethyl alcohol vs isopropyl alcohol?
    14·1 answer
  • Online Book Merchants offers premium customers 1 free book with every purchase of 5 or more books and offers 2 free books with e
    5·1 answer
  • What college degrees did Steve Wozniak earn?
    10·1 answer
  • Consider the class ScopeTest defined this way:
    15·1 answer
  • Having friends who cause you stress can decrease your happiness, which can in turn
    13·2 answers
  • A security policy is a?
    11·2 answers
  • personal digital assistants are handheld devices that serve as personql information managers. what is another term used to refer
    11·2 answers
  • This uses the gps methodology to predict the likely location of an offender's base of operation:
    8·1 answer
  • How do I do these? I dont understand.
    12·1 answer
  • Nadeen runs a website on vegan lifestyle. She wants to spread the word and make more people visit her site to read her recipes a
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!