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?
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?
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;
}
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)