Saturday, May 18, 2019

Write a C Program to Find nCr and nPr:

Write a  C program to find ncr and npr

Program :-
    #include <stdio.h>
    
    long factorial(int);
    long find_ncr(int, int);
    long find_npr(int, int);
    
    int main()
    {
       int n, r;
       long ncr, npr;
    
       printf("Enter the value of n and r\n");
       scanf("%d%d",&n,&r);
      
       ncr = find_ncr(n, r);
       npr = find_npr(n, r);
      
       printf("%dC%d = %ld\n", n, r, ncr);
       printf("%dP%d = %ld\n", n, r, npr);
    
       return 0;
    }
    
    long find_ncr(int n, int r) {
       long result;
      
       result = factorial(n)/(factorial(r)*factorial(n-r));
      
       return result;
    }
    
    long find_npr(int n, int r) {
       long result;
      
       result = factorial(n)/factorial(n-r);
      
       return result;
    }
    
    long factorial(int n) {
       int c;
       long result = 1;
    
       for (c = 1; c <= n; c++)
          result = result*c;
    
       return result;
    }
---------------------------------------------------------------------------------------------------

Output:

Enter the value of n and r
5
2

5C2 = 10
5P2 = 20

Swap function in C language (using call by Reference)

Swap function in C language ( using call by reference.)

Program :-

    #include <stdio.h>
    
    void swap(int*, int*);

    int main()
    {
       int x, y;
    
       printf("Enter the value of x and y\n");
       scanf("%d%d",&x,&y);
    
       printf("Before Swapping\nx = %d\ny = %d\n", x, y);
    
       swap(&x, &y);
    
       printf("After Swapping\nx = %d\ny = %d\n", x, y);
    
       return 0;
    }
    //Swap function definition
    void swap(int *a, int *b)
    {
       int t;
    
       t  = *b;
       *b = *a;
       *a = t;
    }

Swap two numbers using pointers in C

Swap two numbers using pointers

Program :

    #include <stdio.h>
    
    int main()
    {
       int x, y, *a, *b, temp;
    
       printf("Enter the value of x and y\n");
       scanf("%d%d", &x, &y);
    
       printf("Before Swapping\nx = %d\ny = %d\n", x, y);
      
       a = &x;
       b = &y;
      
       temp = *b;
       *b   = *a;
       *a   = temp;
    
       printf("After Swapping\nx = %d\ny = %d\n", x, y);
      
return 0;
    }

C programming code to swap using bit-wise XOR

C programming code to swap using bit-wise XOR

Program:-

    #include <stdio.h>
    
    int main()
    {
      int x, y;
    
      scanf("%d%d", &x, &y);
    
      printf("x = %d\ny = %d\n", x, y);
    
      x = x ^ y;
      y = x ^ y;
      x = x ^ y;
    
    
      printf("x = %d\ny = %d\n", x, y);
    
return 0;
    }

Swapping of two numbers in C

Swapping of two numbers in C

Program :-

    #include <stdio.h>
    
    int main()
    {
      int x, y, t;
    
      printf("Enter two integers\n");
      scanf("%d%d", &x, &y);
    
      printf("Before Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
    
      t = x;
      x = y;
      y = t;
    
      printf("After Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
    
      return 0;
    }
----------------------------------------------------------------------------------------------------

Output :-

Enter two integers
76
46

Before Swapping
First integer = 76
Second integer = 46

After Swapping
First integer = 46
Second integer = 76

C program to addition, subtraction, multiplication and division

C program to  addition, subtraction, multiplication and division
program:-
    #include <stdio.h>
    
    int main()
    {
       int first, second, add, subtract, multiply;
       float divide;
    
       printf("Enter two integers\n");
       scanf("%d%d", &first, &second);
    
       add = first + second;
       subtract = first - second;
       multiply = first * second;
       divide = first / (float)second; 
    
       printf("Sum = %d\n", add);
       printf("Difference = %d\n", subtract);
       printf("Multiplication = %d\n", multiply);
       printf("Division = %.2f\n", divide);
    
 return 0;
    }

Output:
Enter two integers
5 3
Sum = 10
Difference = 2
Multiplication = 15
Division = 1.66

Write a C Program To Print Date

Write a C program to print current system date. By using get date function.

Program:-

    #include <stdio.h>
    #include <conio.h>
    #include <dos.h>
    
    int main()
    {
       struct date d;
    
       getdate(&d);
    
       printf("Current system date: %d/%d/%d", d.da_day, d.da_mon, d.da_year);
      return 0;

    }

This code works in Turbo C only because it supports dos.h header file.