Skip to content

fixing the logic of calculating the sum of even numbers#9

Open
Kneysh wants to merge 1 commit into
hiteshchoudhary:mainfrom
Kneysh:main
Open

fixing the logic of calculating the sum of even numbers#9
Kneysh wants to merge 1 commit into
hiteshchoudhary:mainfrom
Kneysh:main

Conversation

@Kneysh
Copy link
Copy Markdown

@Kneysh Kneysh commented Mar 5, 2025

Fixed the logic of calculating the sum of even numbers

Question : Calculate the sum of even numbers up to a given number n

In the previous solution it was just counting the total even numbers up to a given number n as it was just adding 1 to the variable sum_even. But I changed it and now it adds the current even number to the the variable sum_even which holds the value of the summation of the previous even numbers. Also now the value of 'n' comes from the user.

previous code:

`n = 4
sum_even = 0

for i in range(1, n+1):
if i%2 == 0:
sum_even += 1

print("Sum of the even numbers: , ", sum_even)`

current code:

`n = int(input("Enter the number to add up to: ")) # takes the limit from user
sum_even = 0

for i in range(1, n+1):
if i%2 == 0:
sum_even += i # adds the current even number to the sum of the previous ones

print(f"Sum of the even numbers upto {n} is {sum_even}")`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant