devise an Algorithm find and output the sum of all even numbers up to N
Answers
Answered by
0
c programming algorithm
Answered by
1
Algorithm:
Step 1: START
Step 2: Declare a variable N
Step 3: Take input from the user
Step 4: Declare a variable sum and initialize it with 0
Step 5: Start a loop from 2 upto N and increment the value of i by 2
for(int i = 2 ; i <= N ; i = i + 2)
Step 6: Add all the values in variable sum.
Step 7: Print the value of variable sum
Step 8: END
Program in C++:
#include<iostream>
using namespace std;
int main()
{
int N;
cout<<"Enter a number : ";
cin>>N;
int sum = 0;
for(int i = 2 ; i <= N ; i = i + 2)
{
sum = sum + i;
}
cout<<"Sum of all even number upto "<<N<<" = "<<sum;
return 0;
}
Output:
Enter a number : 15
Sum of all even number upto 15 = 56
Similar questions