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