-1

I'm trying to make a program that calculates the sum of the first N odd numbers. where N is the number of the first odd numbers (e.g. N=4, then the first odds are 1,3,5,7)

it should output the odd numbers.

is the code correct? (given that it needs to be in a loop) or is there a better code for calculating the sum? Also, how to input validate negative numbers? I only know how to do it for integers. (the input needs to be only positive integers)

numberN=input("Please enter a positive number N: ")
sumofodd = 0
try:
    digit = int(numberN)
    if digit > 0:
        for n in range (0,digit+1,1):
            sumofodd = n*n
        print("the sum of", digit, " is ", sumofodd)
except ValueError: 
    print("you did not enter a positive integer")
    exit(1)

Thank you very much

MSS98
  • 15
  • 7
  • 1
    [This post](https://stackoverflow.com/q/65018676/6340496) can provide some guidance and optimisations. Including [short code](https://stackoverflow.com/a/65018781/6340496) and [pure math](https://stackoverflow.com/a/65019035/6340496). – S3DEV Mar 01 '21 at 18:02
  • Thank you very much – MSS98 Mar 01 '21 at 18:03
  • 1
    "is the code correct?" - you are the one who should tell us. Did you test it? Does it produce the right output for your test cases? – Thierry Lathuille Mar 01 '21 at 18:07
  • it does produce the correct output, but the ''' sumofodd = n*n ''' part of the code also works without the for loop. so I was wondering if its wrong is that sense. I'm new to coding – MSS98 Mar 01 '21 at 18:11
  • ofcouse the loop here is not necessary. here is a program to calculate sum of n non-negative odd numbers `print(int(input("Enter a number : ")))**2)` – Rishabh Kumar Mar 01 '21 at 18:15
  • Yes, you can calculcate that without looping. Answer is n². see https://math.stackexchange.com/questions/2415565/sum-of-the-first-n-odd-numbers-is-n2 – ex4 Mar 01 '21 at 18:16

4 Answers4

1

Your code doesn't seem right. You are note calculating sum of numbers. Instead you are calculating n² for last digit it comes across. And your range doesn't loop through odd numbers.

@S3DEV give you a great reference to totally different (and better) approach, but here is your approach fixed:

numberN=input("Please enter a positive number N: ")
sumofodd = 0
try:
    digit = int(numberN)
except:
    print("you did not enter a integer")
    exit(0)
if x < 0:
    pring("give positive number")
    exit(0)
for n in range(1,digit*2,2):
    sumofodd += n
print ("the sum of", digit, " is ", sumofodd)
    
ex4
  • 2,289
  • 1
  • 14
  • 21
  • omg I see the error now, I was trying to fix the wrong part of the code. It makes sense now. Thank you very very much. . I was trying to fix the code according to the reference provided by @S3DEV – MSS98 Mar 01 '21 at 18:20
0

Following @ex4 answers, you could also do this to simplify code and validate negative inputs:

numberN = abs(int(input("Please enter a positive number N: ")))
sumofodd = 0
for n in range(1,numberN*2,2):
      sumofodd += n
print ("the sum of", numberN, " is ", sumofodd)

abs() returns the absolute value and because: abs(int(input("..."))) The input gets turned into an integer that turns positive.

You could also use this:

 numberN = abs(int(input("Please enter a positive number N: ")))
 print ("the sum of", numberN, " is ", numberN*numberN)

Same result.

  • Yes, just adding input validation for non-integers inputs. and it works great. Thank you!! – MSS98 Mar 01 '21 at 18:37
-1
try:
    n = int(input("Please enter a positive number N: "))
    if n > 0:
        print("the sum of", n, "is", n * n)
except ValueError:
    print("you did not enter a positive integer")
    exit(1)
  • The sum of odd numbers from 1 to infinity can be found easily, using Arithmetic Progression. So, just by printing it's squared value would give you the sum of n odd numbers – George Emmanuel Mar 01 '21 at 19:35
-2
#include <stdio.h>
int main(void) {
    int odd;
    printf("Enter n: ");
    scanf("%d", &odd);
          
    if (odd % 2 == 1) 
        odd = odd + 1;
          
    while (odd <= 1) {
        printf("%d\n", odd);
        odd = odd + 1;
    }
    return 0;
}
Jakob Liskow
  • 1,323
  • 14
  • 22