0

Write a program that asks the user for a number n and prints the sum of the numbers 1 to n

1- Write a program that asks the user for a number n and prints the sum of the numbers 1 to n 2- Modify the previous program such that only multiples of three or five are considered in the sum, e.g. 3, 5, 6, 9, 10, 12, 15 for n=17 Solution q = 1 n = int(input('Enter number =')) print (n + q) What am I missing? ^ not sure about the second part. Can someone please help me out?

18th Apr 2018, 4:46 PM
Scape Goat
Scape Goat - avatar
3 Respostas
+ 7
You are missing a loop q = 1, sum = 0 n = int(input('Enter a number:')) for i in range(q, n): sum = sum + i print (sum) It is actually line 3 and 4 that make sure that every number between 1, inclusive and n, not inclusive is added ... You're familiar with loops?
18th Apr 2018, 5:03 PM
cyk
cyk - avatar
0
Perhaps a different take on the first part (which may help you on the second part?) function myFunc() { var n=document.getElementById("inputBox").value; var myArr = []; var i = 0; var sum = 0; for (; n > 0; n--) { myArr.push(n); } for (i=0; i < myArr.length; i++) { sum += parseFloat(myArr[i]); } document.getElementById("demo").innerHTML = sum; }
9th Dec 2019, 11:20 PM
Zary Manning
0
Write a program that asks the user for a number n and prints the sum of the numbers 1 to n. (using VB.Net)
23rd Jun 2021, 3:47 AM
Honey Dianne Hiolen Rico
Honey Dianne Hiolen Rico - avatar