0
c++ program Looping question
1. To input two integer numbers and display the sum of even numbers between these two input numbers.
3 Answers
+ 10
>Take a sum variable and initialize it with 0
>Suppose you took two numbers a and b as input
>Then you can use a loop starting from a and ending at b.
>Then check whether the current number is even or odd (you can find the remainder using % modulus operator, if the remainder is 0 then it is even else if is odd)
>If the number is even then add the number to the variable sum.
>Repeat the above steps till the condition of the loop evaluates to be FALSE.
>Then you can display the result i.e. your sum variable
Hope This Helps !!!
Then even if you have doubts, you can ask.
Try to follow the algorithms (or steps) and you can try to code on your own so that it will improve your coding skills, copying and understanding the code slowers down your progress.
+ 5
#include<iostream>
using namespace std;
int main(){
int a,b,k,j,sum=0;
cin>>a>>b;
if (a>b) swap(a,b);
for(j=a; j<=b; j++)
if(j%2==0) sum+=j;
cout<<sum;
return 0;
}
0
let be a and b your numbers.
1. Create three variables min, max, sum=0;
2.1 find the minimum and save to a variable min
2.2 find the maximum and save to a variable max
3. Make a loop :
for(i=min;i<=max;i++){
if(i % 2== 0 ){
sum = sum + i;
}
}