Question: Write a program that asks the user to enter a positive integer n, and calculates the sum of the integers 1+ 3+5+7+...+(2n-1) and prints

Write a program that asks the user to enter a positive integer  

Write a program that asks the user to enter a positive integer n, and calculates the sum of the integers 1+ 3+5+7+...+(2n-1) and prints out this sum to the screen. For example, when n is 5, 2n-1 is 9, thus the sum of integers is 1+3+5+7+9-25. Your program should be able to handle the invalid case where the input is negative. You must use loop to do this task. Sample Run 1 Enter a positive integer: 5 The sum is: 25 Sample Run 2 Enter a positive integer: -5 The input you gave is not valid. It must be positive.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!