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
Answer:
HERE IS THE CODE(JAVA)!
Explanation:
int answer(int n,int k)
{
int sum_of_digits=0;
int z=0;
for(int i=0;i<=n;i++)
{
sum_of_digits=0;
int temp=i;
while(temp!=0)
{
int r = temp%10;
sum_of_digits+=r;
temp=temp/10;
}
if(sum_of_digits%k==0)
{
z++;
}
}
return z;
}
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.
To Find:
Program
Solution:
#include <stdio.h>
#include<conio.h>
int result(int n,int k) //function deffinition
{
int sum_of_digits=0;
int z=0;
for(int i=0;i<=n;i++)
{
sum_of_digits=0;
int temp=i;
//code to calculate sum of digits of a number
while(temp!=0)
{
int r = temp%10;
sum_of_digits+=r;
temp=temp/10;
} //to cauclate sum of digits
if(sum_of_digits%k==0) //checking whether sum of digits is exactly divisible by K or not
{
z++; //increasing the result no of possibilities
}
}
printf("%d",z); //printing to screen
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
result(n,k); //calling the function
return 0;
}
OUTPUT:
50 -n
4-k
These are numbers that sum of digits divided by 4 untill 50
4
8
13
17
22
26
31
35
39
40
44
48
Totally count them
So answer is 12