write an algorithm: that will ask the user for a positive whole number Use a whi
ID: 3686340 • Letter: W
Question
write an algorithm:
that will ask the user for a positive whole number
Use a while loop to add up all even numbers less than or equal to the user input (so if the user enters 100, add all the even numbers from 2 up to 100)
-First, make a list of the variables you need. I suggest:
The user input number (call it bound)
A total to help you add together the numbers
The current number you wish to add (start it at 2)
You can get just the even numbers by starting your current number at 2 and adding 2 to it each time inside the loop.
Explanation / Answer
Begin
Input positive number n
initialize sum=0
initialize i=2 // Since 2 is the first even numbe
while(i<=2) //if true it goes to in-loop
sum=sum+i //it sums
i=i+2 //Adding 2 to current even number will give next even number
output "sum"
End
C program is
#include <stdio.h>
int main()
{
int sum,i, n;
printf("Print all even numbers till: ");
scanf("%d", &n);
sum=0;
i=2;
while(i<=n)
{
sum=sum+i;
i += 2;
}
printf("sum of even numbers of that range is :%d ",sum);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.