Discrete Structure Lab Questions

PROGRAMS

1. C program to demonstrate example of floor and ceil functions.

Program:-

#include <stdio.h>
#include <math.h>
int main()
{
    float val;
    float fVal, cVal;
    printf("Enter a float value: ");
    scanf("%f", &val);
    fVal = floor(val);
    cVal = ceil(val);
    printf("floor value:%f \nceil value:%f\n", fVal, cVal);
    return 0;
}

Output

    Enter a float value: 123.45

    floor value:123.000000 

    ceil value:124.000000


2.What is cartesian product of two set:-
if set A={1,2} and set B={4,5,6,7}  then cartesian product of set A and B is :-
A*B={ (1,4),(1,5),(1,6),(1,7),(2,4),(2,5),(2,6),(2,7) }

Program:-

#include <stdio.h>

int main()
{
    int a[10], b[10], n1, n2;
    printf("Enter size of set A\n");
    scanf("%d", &n1);
    printf("Enter element of set A\n");
    for (int i = 0; i < n1; i++)
        scanf("%d", &a[i]);
    printf("Enter size of set B\n");
    scanf("%d", &n2);
    printf("Enter element of set B\n");
    for (int i = 0; i < n2; i++)
        scanf("%d", &b[i]);
    // logic for cartesian product
    printf("{");
    for (int i = 0; i < n1; i++)
    {
        for (int j = 0; j < n2; j++)
        {
            printf(" (%d %d) ", a[i], b[j]);
        }
    }
    printf("}");
    return 0;
}


Output:-

Enter size of set A

2

Enter element of set A

1 2 

Enter size of set B

Enter element of set B

4 5 6 7

{ (1,4),(1,5),(1,6),(1,7),(2,4),(2,5),(2,6),(2,7) }



3.Perform addition, subtraction, multiplication and  division of two numbers

#include <stdio.h>
int main()
{
    int num1, num2;
    int sum, sub, mult, mod;
    float div;
    /*
     * Read two numbers from user separated by comma
     */
    printf("Input any two numbers separated by comma : ");
    scanf("%d,%d", &num1, &num2);
    /* Performs all arithmetic operations   */
    sum = num1 + num2;
    sub = num1 - num2;
    mult = num1 * num2;
    div = (float)num1 / num2;
    mod = num1 % num2;
    /* Prints the result of all arithmetic operations */
    printf("The sum of the given numbers : %d\n", sum);
    printf("The difference of the given numbers : %d\n", sub);
    printf("The product of the given numbers : %d\n", mult);
    printf("The quotient of the given numbers : %f\n", div);
    printf("MODULUS = %d\n", mod);
    return 0;
}

 

Sample Output:


Input any two numbers separated by comma : 10,5                                                               

The sum of the given numbers : 15                                                                             

The difference of the given numbers : 5                                                                       

The product of the given numbers : 50                                                                         

The quotient of the given numbers : 2.000000

MODULUS = 0 



4.The C program for the permutation of numbers is given below.


#include <stdio.h>

#include <conio.h>

int fact(int);

int main()

{

    int NPR,N,R,M,i;

    NPR=0;

    M=0;

    M= N-R;

    printf("Enter value for N and R:");

    scanf("%d %d",&N,&R);

    NPR = fact(N)/fact(M);

    for(i=0;i<45;i++)

    printf("_");printf("\n\n");

    printf("Number of %d-permutation of %d object 

    = %dn",R,N,NPR);

    for(i=0;i<45;i++)

    printf("_");printf("\n\n");

    system("PAUSE");

    return 0;

}


int fact(m)

{

    int i,fact=1;

    for(i=1;i<=m;i++)

    {

        fact=fact*i;

    }

    return(fact);

}

Output

The output of the first program is given below.


Enter value for N and R: 10 2

------------------------------------------------

Number of 2-permutation of 10 objects = 3628800






5.C program to demonstrate Basic Euclidean Algorithm


#include <stdio.h>

int EuclideanExtended(int a, int b, int * x, int * y) {
  if (a == 0) {
    * x = 0;
    * y = 1;
    return b;
  }
  int xtemp, ytemp; // To store results of recursive call
  int res = EuclideanExtended(b % a, a, & xtemp, & ytemp);
  * x = ytemp - (b / a) * xtemp;
  * y = xtemp;
  return res;
}

int main() {
  int x, y;
  int a = 60, b = 25;
  int res = EuclideanExtended(a, b, & x, & y);
  printf("gcd(%d, %d) = %d", a, b, res);
}

Output

gcd(60, 25) = 5