Home > Back-end >  Zakat of cows calculation
Zakat of cows calculation

Time:05-01

For every 30 cows, zakat is cow of one years old, and for every 40 cows zakat is female cow of two years old.

EXAMPLE:

30-39 cows -> zakat: 1 cow of one years old

40-49 cows -> zakat: 1 female cow of two years old

50-59 cows -> zakat: 1 female cow of two years old

60-69 cows -> zakat: 2 cows of one years old

120-129 cows -> zakat: 3 female cows of two years old OR 4 cows of one years old

EXPLANATION:

30-39 category: 30 combination: 30

30-49 category: 40 combination: 40

50-59 category: 50 combination: 40

60-69 category: 60 combination: 30 30

120-129 category: 120 combination: 40 40 40 OR 30 30 30 30

Category is clear. Combination could be made only using numbers 30 or 40. Based on combination zakat is calculated.

#include <iostream>
#include <cmath>
int main()
{
    int cows=67;
    int category=cows-cows;
    if(30==category)
    std::cout<<"1 cow of one years old";
    if(40==category)
    std::cout<<"1 female cow of two years old";
    if(50==category)
    std::cout<<"1 female cow of two years old";
    if(30 30==category)
    std::cout<<"2 cows of one years old";
    if(30 30 30 30==category||40 40 40==category)
    std::cout<<"4 cows of one years old OR 3 female cows of two years old";
    return 0;
}

Do you have any idea how to make algorithm for making combinations that would work for huge numbers? Program should print zakat based on combinations.

CodePudding user response:

Consider a number of cows, N.

The maximum number of two-years female cows (V2 for short) is floor(N/40). So, your zakat may have a number of V2 ranging from 0 to floor(N/40), which leaves N-40*V2 cows untithed. These obviously require floor((N-40*V2)/30) V1 to complete zakat.

Pseudo-code:

N := number of cows
MV2 := floor(N/40)
out 'Possible zakat due:'
loop i from 0 to MV2:
    V1 := floor((N-40*i)/30)
    out i, ' female cows of 2 years, ', V1, ' cows of 1 year'
end

You can complicate the algorithm to ensure that when there are multiple combinations, only that with the minimum untithed cows is accepted.

For example if you have 60 cows, the basic algorithm will yield from 0 to 1 cows of two years:

V2=0, 60 cows remain, V1=2, untithed = 0
V2=1, 20 cows remain, V1=0, untithed = 20

In this case, the (1, 0) solution is a cheat and has to be discarded. With 112 cows:

V2=0, 112 cows remain, V1=3, untithed = 22
V2=1,  72 cows remain, V1=2, untithed = 12
V2=2,  32 cows remain, V1=1, untithed = 2

Here, the closest solution is 2 cows of 2 years, 1 cow of 1 year.

In C:

#include <math.h>
#include <stdio.h>
int main() {
  int cows;
  printf("Enter number of cows: ");
  scanf("%d", &cows);
  int category = cows - cows % 10;
  int N, v2, mx, b1, b2, un, i;
  for (N = 0; N <= cows; N  = 10) {
    v2 = floor(N / 40);
    mx = 40;
    b2 = 0;
    b1 = 0;
    for (i = 0; i <= v2; i  ) {
      int j = floor((N - 40 * i) / 30);
      un = N - 40 * i - 30 * j;
      if (un <= mx) {
        b2 = i;
        b1 = j;
        mx = un;
      }
    }
    un = N - 40 * b2 - 30 * b1;
    if (N == category) {
      printf("For %d cows, the zakat is:\n", cows);
      if (cows < 30)
        printf("0 cows");
      if (b1)
        printf("%d one years old cows\n", b1);
      if (b2)
        printf("%d two years old female cows\n", b2);
    }
  }
}

OUTPUT:

Enter number of cows: 140
For 140 cows, the zakat is:
2 one years old cows
2 two years old female cows
  • Related