Question

C++ programming language Create a program that 1) counts from 0 to 10, but doeesn't include...

C++ programming language

Create a program that

1) counts from 0 to 10, but doeesn't include 10

2) counts backwards from 100 to 50 (inclusive) by 2's

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

If you have any doubts please comment below.

Cpp Code:

#include <bits/stdc++.h>

using namespace std;

// Counts the number of '2' digits in a single number

int number0f2s(int n)

{

int count = 0;

while (n > 0)

{

if (n % 10 == 2)

count++;

n = n / 10;

}

return count;

}

// Counts the number of '2' digits between 0 and 10

int numberOf2sinRange(int n)

{

// Initialize result

int count = 0 ;

// Count 2's in every number from 2 to n

for (int i = 2; i < n; i++)

count += number0f2s(i);

return count;

}

int numberOf2sinRangeBackward(int n , int m)

{

int count = 0;

for(int i=n; i>=m; i--)

count += number0f2s(i);

return count;

}

int main()

{

cout << "Counts from 0 to 10 by 2's : "<< numberOf2sinRange(10)<<endl;

cout << "Counts from 100 to 50 backwards : "<< numberOf2sinRangeBackward(100,50);

return 0;

}

Add a comment
Know the answer?
Add Answer to:
C++ programming language Create a program that 1) counts from 0 to 10, but doeesn't include...
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