Find the total number of occurrence of digit d in number N

3. Given a number N and a digit d, find the total number of occurrence of digit d in number N

INPUT: Two numbers N and d separated by whitespace.

OUTPUT: Total number of occurrence of digit d in N

SOLUTION IN C

#include<stdio.h>
int main()
{
  int n,d,c=0;
  scanf("%d %d",&n,&d);
  while(n!=0){
    if(d==(n%10))
      c++;
    n=n/10;
  }
  printf("%d",c);
}

SOLUTION IN C++

#include<iostream>
using namespace std;
int main()
{
  int n,d,c=0;
  cin>>n>>d;
  while(n!=0){
    if(d==(n%10))
      c++;
    n=n/10;
  }
  cout<<c;
}

No comments:

Post a Comment

Total Pageviews