Sum of Even Numbers till N
Given a number N, print sum of all even numbers from 1 to N. #include<iostream> using namespace std; int main(){ /* Read input as specified in the question. * Print output as specified in the question. */ int sum,n; cin>>n; sum=0; for (int i=1;i<=n;i++){ if (i%2==0){ sum=sum+i; } } cout<<sum; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include<iostream> using namespace std; int main(){ /* Read input as specified in the question. * Print output as specified in the question. */ int sum,n; cin>>n; sum=0; for (int i=1;i<=n;i++){ ...
Comments
Post a Comment