0

How to write a program to find the sum of first n natural numbers using while loop in python??

29th Apr 2022, 3:31 PM
Avinash Tiwari 👨🏻‍💻
Avinash Tiwari 👨🏻‍💻 - avatar
4 Respostas
+ 1
# python program to find the sum of first n natural numbers using while loop :- num = int(input("Enter the number\n")) sum = 0 while num > 0 : sum = sum + num num-=1 print("The sum of first natural number is ", sum)
4th May 2022, 9:19 PM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
+ 5
Show us your code. We can't help if we don't know where the issue is. If you want to add numbers, you need a variable (abc) and increase it. (abc += N) you can check in the lesson how the while loop works. you just need a variable outside that loop and increase it with the number in the loop.
29th Apr 2022, 3:35 PM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
4th May 2022, 5:55 PM
Avinash Tiwari 👨🏻‍💻
Avinash Tiwari 👨🏻‍💻 - avatar
5th May 2022, 1:28 AM
Avinash Tiwari 👨🏻‍💻
Avinash Tiwari 👨🏻‍💻 - avatar