Write a C++ program that read a number n and check whether a digit m is present in the number .if so, count how many times it | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Write a C++ program that read a number n and check whether a digit m is present in the number .if so, count how many times it

Cpp problem

25th Apr 2022, 7:00 AM
Vick
Vick - avatar
8 Answers
+ 2
I'm gonna give you a hint Have a variable as frequency counter for digit <m>, set it to zero. To extract last digit of an integer <N>, we modulo <N> by 10 Example: int N = 12345; int digit = N % 10; // <digit> is 5 Now we want to get all the digits one by one so we repeat the above steps to extract last digit, followed by removing the digit that was already extracted by dividing <N> by 10. Keep doing it while <N> is non zero. For this, we're gonna need to use a loop to repeat the steps int N = 12345; int digit = N % 10; // <digit> is 5 N /= 10; // now <N> is 1234, digit 5 is removed Repeat the steps above to get each digit, and when <digit> equals <m>, increment the frequency counter.
25th Apr 2022, 7:34 AM
Ipang
+ 2
Nevermind i found my error it was when i assigned the if statement i used = instead of ==
25th Apr 2022, 9:02 AM
Vick
Vick - avatar
+ 1
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { ll n,m; cin>>n>>m; while(n>0) { if(m==n%10) { cout<<"YES"; return 0; } n/=10; } cout<<"NO"; }
25th Apr 2022, 9:18 AM
Younes Salman
Younes Salman - avatar
0
You are currently taking HTML course, why try C++ assignment when you haven't even started learning it? that's suicidal ...
25th Apr 2022, 7:04 AM
Ipang
0
I have been learning c++ for some time now, i just came across this problem so i came to this app for help Html was a hobby
25th Apr 2022, 7:06 AM
Vick
Vick - avatar
0
Can you help me please
25th Apr 2022, 7:06 AM
Vick
Vick - avatar
0
int m, n, counter=0, digit; cin>>n; cin>>m; while(n!=0){ digit=n%2; n/=10; } I Came this far but how do i make the counter count the number of times m was repeated i tried using if statements within the loop but it doesnt work
25th Apr 2022, 8:51 AM
Vick
Vick - avatar
0
Ok Vick, glad to hear you solved it 👍
25th Apr 2022, 11:42 AM
Ipang