Question

C++ question Using a for loop, count the number of even numbers between 1 and a...

C++ question

Using a for loop, count the number of even numbers between 1 and a number we supply (inclusive). Return 0 if the parameter is less than 1.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;
// function declaration
int count1(int x);
int main()
{
// local variable declaration:
int N,res;
cout<<"Enter the number :";                 //enter the number upto which u want to find even number..
cin>>N;
// calling a function ...........
res=count1(N);
cout<<"Number of even numbers :"<<res;
}

// function returning the number of even numbers...
int count1(int x){
int count=0;       
    if(x<1){                            //if number<0 return 0.....
    return 0;
}
else{
    for(int i=1;i<=x;i++){             //number will be even if it is divisible by 2 .....% means modulus will give remainder 0..
    if (i%2==0){
        count++;                      //inrement count....
}}}
return count;
}

Add a comment
Know the answer?
Add Answer to:
C++ question Using a for loop, count the number of even numbers between 1 and a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT