Computer Science, asked by Raagasri, 11 months ago

Given two integers N and K, find Z, the number of non-negative integers that are less than or equal to N and whose sum of digits is divisible by K.

Answers

Answered by madhuvamshi56
2

Answer:

n=int(input())

k=int(input())

s=n

x=0

for i in range(s):

n=i

sum=0

while n!= 0:

rem=n%10

sum = sum+rem

n = int(n//10)

if sum%k==0:

x=x+1

print(x)

Explanation:

Attachments:
Answered by pruthaasl
0

Answer:

#include <iostream>

using namespace std;

int main() {

   int n,k,i,j,d,c=0,sum=0;

   cout<<"\nEnter two non-negative numbers: ";

   cin>>n>>k;

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

       {

           while(i!=0)

           {

       d=i%10;

       i/=10;

       sum+=d;

           }

       if(sum%k==0)

       c=c+1;

       }

       

   cout<<c;

   return 0;

}

Explanation:

  1. Declare the variables needed to store the inputs, the sum of the digits, and keep a count of the non-negative integers.
  2. Take input as two integers from the user and store them.
  3. Separate the digits and calculate their sum for every integer less than or equal to the input.
  4. Check whether the sum of the digits is divisible by the second input using the modulo operator.
  5. If yes, increment the count of the non-negative integers by one.
  6. Repeat steps 3 to 5 for every non-negative number less than or equal to the first input.
  7. Print the count of the non-negative numbers as the output.

#SPJ3

Similar questions