count how many times a number appears in a sum c

Program Statement:

The program must accept three integers n, p and q as the input. The program must print number of times q occurs at least once in the sum where p is added to n times for p times as the ouput.

Note: This program is also known as Number of Occurrences of q.  

Solution

 #include<stdio.h>

#include <stdlib.h>

int call(long long int x,long long int y,long long int z)

{

    long long int a,d=1;

    while(z>=1)

    {

        d=d*10;

        z--;

    }

    while(x!=0)

    {

        a=x%d;

        if(a==y)

        return 1;

        x=x/10;

    }

    return 0;

}

int main()

{

long long int n,p,q,count=0,i,s,c=0;

scanf("%lld\n%lld\n%lld",&n,&p,&q);

s=q;

while(s!=0)

{

    c++;

    s=s/10;

}

for(i=0;i<p;i++)

{

    n=n+p;

    if(call(n,q,c)==1)

    count++;

}

printf("%lld",count);

}

Input

15

5

5

Output

2

Output Explanation:

Initially, sum is 15 and count is 0,

iteration-p times

checking- q value

Then in 1st iteration, 5 is not present in sum(15+5=20), so the count remains 0

in 2nd, 5 is present in sum(20+5=25), so count becomes 1

in 3rd, 5 is not  present in sum(25+5=30), so count remain 1

in 4th, 5 is present in sum(30+5=35), so count becomes 2

in 5th, 5 is not present in sum(35+5=40), so count remains 2

Input

1111111

10

12

Output

3

Input

10

1

11

Output

1


Rajesh

This Blog will help you to find out the c programs solution easily and also you can able to understand the program.Please follow our blog for more useful updates.

Post a Comment (0)
Previous Post Next Post