+ 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
8 Réponses
+ 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.
+ 2
Nevermind i found my error it was when i assigned the if statement i used = instead of ==
+ 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";
}
0
You are currently taking HTML course, why try C++ assignment when you haven't even started learning it? that's suicidal ...
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
0
Can you help me please
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
0
Ok Vick, glad to hear you solved it 👍