C Programs - Unix

download C Programs - Unix

of 16

Transcript of C Programs - Unix

  • 7/27/2019 C Programs - Unix

    1/16

    FUNCTION WITH NO ARGUMENTS AND NO RETURN VALUES

    #include

    void sum(void);

    main()

    {

    sum();

    }

    void sum(void)

    {

    int num1, num2, num3;

    printf("Enter two numbers: \n");

    scanf("%d%d", &num1, &num2);

    num3=num1+num2;

    printf("Summation of %d and %d is %d \n", num1, num2, num3);

    }

    OUTPUT

    Enter two numbers:

    10

    20

    Summation of 10 and 20 is 30

  • 7/27/2019 C Programs - Unix

    2/16

    FUNCTION WITH ARGUMENTS AND RETURN VALUES

    #include

    #include

    int largest(int, int);

    void main()

    {

    int a,b,big;

    printf("Enter two numbers : \n");

    scanf("%d%d", &a, &b);

    big=largest(a,b);

    printf("Largest Element = %d", big);

    }

    int largest(int a1, int b1)

    {

    if(a1>b1)

    return a1;

    elsereturn b1;

    }

    OUTPUT

    Enter two numbers

    15

    25

    Largest Element = 25

  • 7/27/2019 C Programs - Unix

    3/16

    DYNAMIC ALLOCATION OF MEMORY

    #include

    main()

    {

    int *p;

    p=(int *)malloc(sizeof(int));

    if(p==0)

    {

    printf(" ERROR : Out of Memory \n");

    return 1;

    }

    *p=5;

    printf("Value of P = %d \n", *p);

    printf("Address of P = %u \n", p);

    free(p);

    return 0;

    }

    OUTPUT

    Value of P = 5

    Address of P =134518200

  • 7/27/2019 C Programs - Unix

    4/16

    ALLOCATE SPACE FOR A STRING DYNAMICALLY AND

    PRINT THE STRING BACKWARDS

    #include

    #include

    int main()

    {

    char *s;

    register int i;

    s=malloc(80);

    if(!s)

    {

    printf("Memory request failed................. \n");

    exit(1);

    }

    scanf("%s",s);

    for(i=strlen(s)-1; i>=0; i--)

    printf("%c",s[i]);free(s);

    return 0;

    }

    OUTPUT

    Hai

    iaH

  • 7/27/2019 C Programs - Unix

    5/16

    BINARY SEARCH USING FUNCTION

    #include

    #include

    int main()

    {

    int bsearch(int x[],int n,int s);

    int x[20],i,n,s;

    printf("How many numbers?");

    scanf("%d",&n);

    printf("Enter all numbers in the list");

    for(i=0;is)

  • 7/27/2019 C Programs - Unix

    6/16

    end = mid;

    else

    if(x[mid]

  • 7/27/2019 C Programs - Unix

    7/16

    FACTORIAL IN FINDING ncr USING FUNCTION

    #include

    #include

    int main()

    {

    int fact(int k);

    int n,r,ncr;

    printf("\n Enter value to n and r:");

    scanf("%d %d", &n,&r);

    ncr=fact(n)/(fact(r)*fact(n-r));

    printf("\n Value of ncr = %d",ncr);

    }

    int fact(int k)

    {

    int i, p=1;

    for(i=1;i

  • 7/27/2019 C Programs - Unix

    8/16

    COSINE SERIES USING FUNCTION

    #include

    #include

    int main()

    {

    float cosine(float x);

    float x=0;

    printf("\n x in degrees cos(x) ");

    while (x

  • 7/27/2019 C Programs - Unix

    9/16

    return(s);

    }

    OUTPUT

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

    x in degrees cos(x)

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

    0 1.0

    30 0.87

    60 0.50

    90 0.00

    120 -0.50

    150 -0.87

    180 -1.00

  • 7/27/2019 C Programs - Unix

    10/16

    PRODUCT OF MATRIX USING ARRAYS

    #include

    #include

    int main()

    {

    int *a[10],*b[10], *c[10], m,n,l,i,k,j;

    for(i=0;i

  • 7/27/2019 C Programs - Unix

    11/16

    for(j=0;j

  • 7/27/2019 C Programs - Unix

    12/16

    Resultant Matrix is

    26 9 5

    4 -28 18

    18 19 -3

  • 7/27/2019 C Programs - Unix

    13/16

    CREATION & READING THE CONTENT OF A FILE

    #include

    #include

    struct emp

    {

    int eno;

    char ename[20];

    float bpay;

    };

    FILE *efile;

    int main()

    {

    struct emp erec;

    int n,i;

    efile = fopen("EMPLOY.DAT", "w");

    printf("\nEnter the number of employees:");

    scanf("%d",&n);for(i=1;i

  • 7/27/2019 C Programs - Unix

    14/16

    printf("\nAfter adding content to the file");

    efile=fopen("EMPLOY.DAT","r");

    printf("\n--------------------------------------------------");

    printf("\n Emp.no Employee name Basic Pay");

    printf("\n --------------------------------------------------\n");

    fread(&erec,sizeof(erec),1,efile);

    while (!feof(efile))

    {

    printf("\n %d \t %-20s %0.2f",erec.eno,erec.ename,erec.bpay);

    fread(&erec, sizeof(erec),1,efile);

    }

    printf("\n --------------------------------------------------");

    fclose(efile);

    return 0;

    }

    OUTPUT

    Enter the number of Employees: 2

    Enter the 1 employee details :

    Enter the Employee number: 101Employee name: xxxx

    Basic Pay: 65433.4

    Enter the 2 employee details :

    Enter the Employee number: 102

    Employee name: yyyy

    Basic Pay: 7000

    After adding content to the file

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

    Emp.no Employee name Basic Pay

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

    101 xxxx 65433.4

    102 yyyy 7000.0

  • 7/27/2019 C Programs - Unix

    15/16

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

    APPENDING A RECORD TO THE FILE

    #include

    #include

    struct emp

    {

    int eno;

    char ename[20];

    float bpay;

    };

    FILE *efile;

    int main()

    {

    struct emp erec;

    efile = fopen("EMPLOY.DAT", "a");

    printf("\nEnter the Employee number");

    scanf("%d",&erec.eno);printf("Employee Name");

    scanf("%s",erec.ename);

    printf("Basicpay");

    scanf("%f",&erec.bpay);

    fwrite(&erec,sizeof(erec),1,efile);

    fclose(efile);

    printf(" After Appending the content of file");

    efile=fopen("EMPLOY.DAT","r");

    printf("\n--------------------------------------------------");

    printf("\n Emp.no Employee name Basic Pay");

    printf("\n --------------------------------------------------");

    fread(&erec,sizeof(erec),1,efile);

  • 7/27/2019 C Programs - Unix

    16/16

    while (!feof(efile))

    {

    printf("\n %d \t %-20s %0.2f",erec.eno,erec.ename,erec.bpay);

    fread(&erec, sizeof(erec),1,efile);

    }

    printf("\n --------------------------------------------------\n");

    fclose(efile);

    return 0;

    }

    OUTPUT

    Enter the Employee number: 103

    Employee name: zzzzBasic Pay: 12000

    After Appending the content of file

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

    Emp.no Employee name Basic Pay

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

    101 xxxx 65433.4

    102 yyyy 7000.0

    103 zzzz 12000.0