This blog is under construction
Showing posts with label Basic C Programs. Show all posts
Showing posts with label Basic C Programs. Show all posts

Tuesday, 9 July 2013

C program to calculate correlation coefficient

How to find correlation coefficient?
Please check the below link to get better understanding on correlation coefficient.

Write a C program to calculate correlation coefficient.


  #include <stdio.h>
  #include <math.h>

  int main() {
        int x[100], y[100], xy[100], xsquare[100], ysquare[100];
        int i, n, xsum, ysum, xysum, xsqr_sum, ysqr_sum;
        float coeff, num, deno;

        xsum = ysum = xysum = xsqr_sum = ysqr_sum = 0;

        /* get the number of entries from the user */
        printf("Enter the value for n:");
        scanf("%d", &n);

        /* get the values for x and y  from the user */
        printf("Enter the value for x and y:\n");
        for (i = 0; i < n; i++) {
                printf("x[%d] and y[%d]: ", i, i);
                scanf("%d%d", &x[i], &y[i]);
        }

        /* find the needed data to manipulate correlation coeff */
        for (i = 0; i < n; i++) {
                xy[i] = x[i] * y[i];
                xsquare[i] = x[i] * x[i];
                ysquare[i] = y[i] * y[i];
                xsum = xsum + x[i];
                ysum = ysum + y[i];
                xysum = xysum + xy[i];
                xsqr_sum = xsqr_sum + xsquare[i];
                ysqr_sum = ysqr_sum + ysquare[i];
        }

        num = 1.0 * ((n * xysum) - (xsum * ysum));
        deno = 1.0 * ((n * xsqr_sum - xsum * xsum)* (n * ysqr_sum - ysum * ysum));

        /* calculate correlation coefficient */
        coeff = num / sqrt(deno);

        /* print the result */
        printf("Correlation Coefficient : %.4f\n", coeff);
        return 0;
  }


Note:
gcc corr_coeff.c -lm => linked math library since we have used sqrt() math function.

  Output:
  jp@jp-VirtualBox:~/$ gcc corr_coeff.c -lm
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the value for n:6
  Enter the value for x and y:
  x[0] and y[0]: 43 99
  x[1] and y[1]: 21 65
  x[2] and y[2]: 25 79
  x[3] and y[3]: 42 75
  x[4] and y[4]: 57 87
  x[5] and y[5]: 59 81
  Correlation Coefficient : 0.5298




See Also:

C program to check whether a given number is magic number or not

What is a magic number?
Example: 1729

  • Find the sum of digits of the given number.(1 + 7 + 2 + 9 => 19)
  • Reverse of digit sum output.  Reverse of 19 is 91
  • Find the product of digit sum and the reverse of digit sum.(19 X 91 = 1729)
  • If the product value and the given input are same, then the given number is a magic number.(19 X 91 <=> 1729)
  • So, 1729 is a magic number.


Write a C program to check whether a given number is magic number or not.


  #include <stdio.h>

  /* sum of digits of a number */
  int sumOfDigits(int num) {
        int sum = 0;
        while (num > 0) {
                sum = sum + (num % 10);
                num = num / 10;
        }
        return sum;
  }

  /* returns reverse of a given number */
  int reverse(int num) {
        int rev = 0;
        while (num > 0) {
                rev = (rev * 10) + (num % 10);
                num = num / 10;
        }
        return rev;
  }

  int main () {
        int num, sum, rev;

        /* get the input value from the user */
        printf("Enter the value for num:");
        scanf("%d", &num);

        /* find sum of digits */
        sum = sumOfDigits(num);

        /*
         * if the value is single digit, then
         * the value and its reverse are same
         */
        if (sum < 10) {
                if ((sum * sum) == num) {
                        printf("%d is a magic number\n", num);
                } else {
                        printf("%d is not a magic number\n", num);
                }
                return 0;
        }

        /* reverse of the given number */
        rev = reverse(sum);

        /* print the outputs */
        if ((sum * rev) == num) {
                printf("%d is a magic number\n", num);
        } else {
                printf("%d is not a magic number\n", num);
        }

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the value for num:1729
  1729 is a magic number



See Also:

C program to print the four digit numbers whose sum of squares of first half and second half of the number equals the same number

Write a C program to print the four digit numbers whose sum of squares of first half and second half of the number equals the same number.


  #include <stdio.h>
  int main() {
        int i = 1000, fhalf, shalf, fsquare, ssquare;

        /* print if square of 1st and 2nd half are same */
        while (i < 10000) {
                shalf = i % 100;
                fhalf = i / 100;
                ssquare = shalf * shalf;
                fsquare = fhalf * fhalf;
                if (fsquare == ssquare) {
                        printf("%d => (%d)^2 equals to (%d)^2\n",
                                i, fhalf, shalf);
                }
                i++;
        }
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  1010 => (10)^2 equals to (10)^2
  1111 => (11)^2 equals to (11)^2
  1212 => (12)^2 equals to (12)^2
  1313 => (13)^2 equals to (13)^2
  1414 => (14)^2 equals to (14)^2
  1515 => (15)^2 equals to (15)^2
  1616 => (16)^2 equals to (16)^2
  1717 => (17)^2 equals to (17)^2
  1818 => (18)^2 equals to (18)^2
  1919 => (19)^2 equals to (19)^2
  2020 => (20)^2 equals to (20)^2
  2121 => (21)^2 equals to (21)^2
  2222 => (22)^2 equals to (22)^2
  2323 => (23)^2 equals to (23)^2
  2424 => (24)^2 equals to (24)^2
  2525 => (25)^2 equals to (25)^2
  2626 => (26)^2 equals to (26)^2
  2727 => (27)^2 equals to (27)^2
  2828 => (28)^2 equals to (28)^2
  2929 => (29)^2 equals to (29)^2
  3030 => (30)^2 equals to (30)^2
  3131 => (31)^2 equals to (31)^2
  3232 => (32)^2 equals to (32)^2
  3333 => (33)^2 equals to (33)^2
  3434 => (34)^2 equals to (34)^2
  3535 => (35)^2 equals to (35)^2
  3636 => (36)^2 equals to (36)^2
  3737 => (37)^2 equals to (37)^2
  3838 => (38)^2 equals to (38)^2
  3939 => (39)^2 equals to (39)^2
  4040 => (40)^2 equals to (40)^2
  4141 => (41)^2 equals to (41)^2
  4242 => (42)^2 equals to (42)^2
  4343 => (43)^2 equals to (43)^2
  4444 => (44)^2 equals to (44)^2
  4545 => (45)^2 equals to (45)^2
  4646 => (46)^2 equals to (46)^2
  4747 => (47)^2 equals to (47)^2
  4848 => (48)^2 equals to (48)^2
  4949 => (49)^2 equals to (49)^2
  5050 => (50)^2 equals to (50)^2
  5151 => (51)^2 equals to (51)^2
  5252 => (52)^2 equals to (52)^2
  5353 => (53)^2 equals to (53)^2
  5454 => (54)^2 equals to (54)^2
  5555 => (55)^2 equals to (55)^2
  5656 => (56)^2 equals to (56)^2
  5757 => (57)^2 equals to (57)^2
  5858 => (58)^2 equals to (58)^2
  5959 => (59)^2 equals to (59)^2
  6060 => (60)^2 equals to (60)^2
  6161 => (61)^2 equals to (61)^2
  6262 => (62)^2 equals to (62)^2
  6363 => (63)^2 equals to (63)^2
  6464 => (64)^2 equals to (64)^2
  6565 => (65)^2 equals to (65)^2
  6666 => (66)^2 equals to (66)^2
  6767 => (67)^2 equals to (67)^2
  6868 => (68)^2 equals to (68)^2
  6969 => (69)^2 equals to (69)^2
  7070 => (70)^2 equals to (70)^2
  7171 => (71)^2 equals to (71)^2
  7272 => (72)^2 equals to (72)^2
  7373 => (73)^2 equals to (73)^2
  7474 => (74)^2 equals to (74)^2
  7575 => (75)^2 equals to (75)^2
  7676 => (76)^2 equals to (76)^2
  7777 => (77)^2 equals to (77)^2
  7878 => (78)^2 equals to (78)^2
  7979 => (79)^2 equals to (79)^2
  8080 => (80)^2 equals to (80)^2
  8181 => (81)^2 equals to (81)^2
  8282 => (82)^2 equals to (82)^2
  8383 => (83)^2 equals to (83)^2
  8484 => (84)^2 equals to (84)^2
  8585 => (85)^2 equals to (85)^2
  8686 => (86)^2 equals to (86)^2
  8787 => (87)^2 equals to (87)^2
  8888 => (88)^2 equals to (88)^2
  8989 => (89)^2 equals to (89)^2
  9090 => (90)^2 equals to (90)^2
  9191 => (91)^2 equals to (91)^2
  9292 => (92)^2 equals to (92)^2
  9393 => (93)^2 equals to (93)^2
  9494 => (94)^2 equals to (94)^2
  9595 => (95)^2 equals to (95)^2
  9696 => (96)^2 equals to (96)^2
  9797 => (97)^2 equals to (97)^2
  9898 => (98)^2 equals to (98)^2
  9999 => (99)^2 equals to (99)^2




See Also:

C program to print even, odd and prime factors of a given number

Write a C program to print even, odd and prime factors of a given number.


  #include <stdio.h>
  #include <string.h>

  /* tell whether the given number is even, odd or prime */
  void numType(int data, char *type) {
        int i, flag = 1;

        /* checking whether the given no is prime */
        for (i = 2; i <= data - 1; i++) {
                if (data % i == 0) {
                        flag = 0;
                        break;
                }
        }

        if (data != 1 && flag) {
                strcpy(type, "a prime");
                return;
        }

        /* check for even or odd */
        if (data % 2 == 0) {
                strcpy(type, "an even");
        } else {
                strcpy(type, "an odd");
        }
        return;
  }

  int main() {
        int num, i;
        char type[32];

        /* get the input value from the user */
        printf("Enter the value for n:");
        scanf("%d", &num);

        /* prints factors and its type */
        for (i = 1; i <= num; i++) {
                if (num % i == 0) {
                        numType(i, type);
                        printf("Factor %d is %s number\n", i, type);
                }
        }

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the value for n:100
  Factor 1 is an odd number
  Factor 2 is a prime number
  Factor 4 is an even number
  Factor 5 is a prime number
  Factor 10 is an even number
  Factor 20 is an even number
  Factor 25 is an odd number
  Factor 50 is an even number
  Factor 100 is an even number




See Also:

C program to calculate ncr and npr


Permutation and Combination Formulas:
nCr = n! / r!(n-r)!
nPr = n! / (n-r)! 



Write a C program to calculate ncr and npr.


  #include <stdio.h>

  /* returns factorial of the given number */
  int fact(int num) {
        int res = 1;
        if (num == 0 || num == 1) {
                return res;
        }

        while (num > 0) {
                res = res * num;
                num--;
        }
        return res;
  }

  int main() {
        int n, r;
        float nPr, nCr;

        /* get the value for n and r from the user */
        printf("Enter the value for n and r:");
        scanf("%d%d", &n, &r);

        /* conbination calculation */
        nCr = (1.0 * fact(n)) / (fact(r) * fact(n-r));

        /* permutation calculation */
        nPr = (1.0 * fact(n)) / fact(n-r);

        /* print the permutation result */
        printf("nPr = %.2f\n", nPr);

        /* print the combination result */
        printf("nCr = %.2f\n", nCr);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/cpgms/lab_pgms/01_cbasics$ ./a.out
  Enter the value for n and r:10 5
  nPr = 30240.00
  nCr = 252.00




See Also: