c++ program Looping question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

c++ program Looping question

1. To input two integer numbers and display the sum of even numbers between these two input numbers.

19th Mar 2019, 10:05 AM
yumna Albalushi
yumna Albalushi - avatar
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.
19th Mar 2019, 12:15 PM
Nova
Nova - avatar
+ 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; }
19th Mar 2019, 12:35 PM
Zuleyha_O
Zuleyha_O - avatar
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; } }
19th Mar 2019, 12:15 PM
Prokopios Poulimenos
Prokopios Poulimenos - avatar