PLATE GAME USING FUNCTIONS
Plate Game
In Sachin's birthday party, he organize a game, in which numbered plates are hanged on the wall. We have to hit by a ball on plate, if the plate numbered having exactly three three's in it, you win game.
Write a program, to count the numbers from range(both the numbers inclusive), which have exactly three three's.
Input Format:
A line corresponds to 2 integer which represents range (both are inclusive). Default integer is 200 and 2000,
Output format:
A line corresponds to a integer which represents the count of numbers which is having exactly three three's in it.
#include <iostream>
#include <string>
using namespace std;
int countNumThree3(int n1, int n2){
string s;
int count_digits=0, count_numbers=0;
for(int i=n1;i<=n2;i++){
s=to_string(i);
count_digits=0;
for(int j=0;j<s.length();j++){
if (s[j]=='3'){
count_digits++;
}
}
if (count_digits==3){
count_numbers++;
}
}
return count_numbers;
}
int main()
{
int start=200;
int stop=2000;
cout<<"Enter the starting number: ";
cin>>start;
cout<<"Enter the stopping number: ";
cin>>stop;
int n=countNumThree3(start,stop);
cout<<n;
return 0;
}
Comments
Leave a comment