CPICP

Embed Size (px)

Citation preview

  • 8/4/2019 CPICP

    1/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Ex. No. 3C PROGRAMMING

    C is a powerful, portable and elegantly structured programming language. It combines

    features of a high-level language with the elements of an assembler and therefore, suitable for

    writing both system software and application packages. It is the most widely used general-

    purpose language today. C has been used for implementing systems such as operating

    systems, compilers, linkers, word processors and utility packages. It is a robust language

    whose rich set of built-in functions and operators can be used to write any complex program.

    Programs written in C are fast and efficient.

    Turbo C IDE

    Turbo C IDE facilitates editing, debugging and execution of applications written in C.

    C programs are saved with .c extension. Some of the shortcut keys are:

    Ctrl + F1 HelpF2 Save

    F3 OpenCopy Ctrl + Ins

    Cut Shift + DelPaste Shift + Ins

    Clear Ctrl + Del

    Alt + F9 CompileCtrl + F9 Execute

    Alt + F5 User ScreenF7 Trace Into

    Alt + F3 CloseAlt + X Quit

  • 8/4/2019 CPICP

    2/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Ex. No. 3.1AREA OF A CIRCLE

    Aim

    To write a program to compute the area and circumference of a circle.

    Algorithm

    Step 1 : Start

    Step 2 : Define constant pi = 3.14

    Step 3 : Read the value ofradius

    Step 4 : Calculate area using formulae pi*radius2

    Step 5 : Calculate circumference using formulae 2*pi*radius

    Step 6 : Print area and circumference

    Step 7 : Stop

    Flowchart

    Read radius

    pi = 3.14

    area = pi * radius2

    circumference = 2 * pi * radius

    Print area, circumference

    Start

    Stop

  • 8/4/2019 CPICP

    3/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Program

    /* Area and circumference of a circle */

    #include #include

    #define pi 3.14

    main()

    {

    int radius;

    float area, circum;

    clrscr();

    printf("\nEnter radius of the circle : ");

    scanf("%d",&radius);

    area = pi * radius * radius;circum = 2 * pi * radius;

    printf("\nArea is %.2f and circumference is

    %.2f\n",area,circum);

    getch();

    }

    Output

    Enter radius of the circle : 8

    Area is 200.96 and circumference is 50.24

  • 8/4/2019 CPICP

    4/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Ex. No. 3.2BIGGEST OF TWO NUMBERS

    Aim

    To write a program to determine the bigger of two numbers using ternary operator.

    Algorithm

    Step 1 : Start

    Step 2 : Read values of a and b

    Step 3 : If a = b then print "Both are equal"

    Step 3.1 : Else ifa > b then print "A is big"

    Step 3.2 : Else print "B is big"

    Step 4 : Stop

    Flowchart

    YN

    Y

    N

    Read a, b

    Start

    Stop

    a=b

    ?

    a>b

    ?

    Print "Both

    are equal"

    Print "A is

    big"

    Print "B is

    big"

  • 8/4/2019 CPICP

    5/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Program

    /* Biggest of 2 Nos using Ternary Operator */

    #include #include

    main()

    {

    int a,b;

    clrscr();

    printf("Enter A value : ");

    scanf("%d",&a);

    printf("Enter B value : ");

    scanf("%d",&b);

    (a==b) ? printf("\nBoth are equal") : a>b ? printf("\nA

    is greater") : printf("\nB is greater");getch();

    }

    Output

    Enter A value : -2

    Enter B value : -5

    A is greater

    Enter A value : 4

    Enter B value : 7

    B is greater

    Enter A value : 11

    Enter B value : 11

    Both are equal

  • 8/4/2019 CPICP

    6/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Ex. No. 3.3LEAP YEAR

    Aim

    To write a program to check whether the given year is a leap year.

    Algorithm

    Step 1 : Start

    Step 2 : Read the value of year

    Step 3 : If yeardivisible by 400 then print "Leap year"

    Step 3.1 : Else ifyeardivisible by 4 and not divisible by 100 then print "Leap year"

    Step 3.2 : Else print "Not a leap year"

    Step 4 : Stop

    Flowchart

    YN

    Y

    N

    Read year

    Start

    Stop

    year%400=0

    year%4 = 0 and

    year%100 0

    ?

    Print

    "Leap"

    Print

    "Leap"

    Print "Not

    Leap"

  • 8/4/2019 CPICP

    7/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Program

    /* Leap Year */

    #include #include

    main()

    {

    int year;

    clrscr();

    printf("\nEnter the year : ");

    scanf("%d",&year);

    if (year%400 == 0)

    printf("\n%d is a Leap year",year);else if (year%100 != 0 && year%4 == 0)

    printf("\n%d is a Leap year",year);

    else

    printf("\n%d is not a Leap year",year);

    getch();

    }

    Output

    Enter the year : 2000

    2000 is a Leap year

    Enter the year : 1800

    1800 is not a Leap year

    Enter the year : 2004

    2004 is a Leap year

  • 8/4/2019 CPICP

    8/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Ex. No. 3.4SIMPLE CALCULATOR

    Aim

    To write a menu driven calculator program using switch statement.

    Algorithm

    Step 1 : Start

    Step 2 : Display calculator menu options

    Step 3 : Read the operatorsymbol and operands n1, n2

    Step 4 : If operator= + then calculate result= n1 + n2

    Step 4.1 : Else ifoperator= then calculate result= n1 n2

    Step 4.2 : Else ifoperator= * then calculate result= n1 * n2

    Step 4.3 : Else ifoperator= / then calculate result= n1 /n2

    Step 4.4 : Else ifoperator= % then calculate result= n1 % n2

    Step 4.2 : Else print "Invalid operator" and go to step 6

    Step 5 : Print result

    Step 6 : Stop

  • 8/4/2019 CPICP

    9/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Flowchart

    Other%/*+

    Display Calculator menu

    Start

    Stop

    operator

    ?

    Print res

    Read operator, n1, n2

    res=n1+n2 res=n1n2 res=n1*n2 res=n1/n2 res=n1%n2

    Print "Invalid

    operator"

  • 8/4/2019 CPICP

    10/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Program

    /* Simple Calculator */

    #include

    #include #include

    main()

    {

    int n1, n2, result;

    char op;

    clrscr();

    printf("\n Simple Calculator\n");

    printf("\n + Summation");

    printf("\n - Difference");

    printf("\n * Product");

    printf("\n / Quotient");printf("\n % Remainder\n");

    printf("\nEnter the operator : ");

    op = getchar();

    printf("Enter operand1 and operand2 : ");

    scanf("%d%d",&n1,&n2);

    switch (op)

    {

    case '+':

    result = n1 +n2;

    break;

    case '-':result = n1 - n2;

    break;

    case '*':

    result = n1 * n2;

    break;

    case '/':

    result = n1 / n2;

    break;

    case '%':

    result = n1 % n2;

    break;

    default:printf("Invalid operator");

    exit(-1);

    }

    printf("\n%d %c %d = %d", n1, op, n2, result);

    getch();

    }

  • 8/4/2019 CPICP

    11/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Output

    Simple Calculator

    + Summation

    - Difference* Product

    / Quotient

    % Remainder

    Enter the operator : -

    Enter operand1 and operand2 : 2 4

    2 - 4 = -2

    Simple Calculator

    + Summation

    - Difference

    * Product

    / Quotient

    % Remainder

    Enter the operator : *

    Enter operand1 and operand2 : 3 2

    3 * 2 = 6

    Simple Calculator

    + Summation

    - Difference

    * Product

    / Quotient

    % Remainder

    Enter the operator : %

    Enter operand1 and operand2 : 5 2

    5 % 2 = 1

  • 8/4/2019 CPICP

    12/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Ex. No. 3.5BINARY TO DECIMAL

    Aim

    To write a program to convert binary number to its decimal equivalent using whileloop.

    Algorithm

    Step 1 : Start

    Step 2 : Read the binary value

    Step 3 : Initialize dec and i to 0

    Repeat steps 47 until binary > 0

    Step 4 : Extract the last digitusing modulo 10

    Step 5 : Calculate decimal = decimal + digit* 2i

    Step 6 : Recompute binary = binary / 10

    Step 7 : Increment i by 1

    Step 8 : Print decimal

    Step 9 : Stop

  • 8/4/2019 CPICP

    13/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Flowchart

    N

    Y

    Read binary

    decimal = i = 0

    binary>0

    digit = binary % 10

    decimal = decimal + digit * 2 i

    binary = binary / 10

    i = i + 1

    Print decimal

    Start

    Stop

  • 8/4/2019 CPICP

    14/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Program

    /* Binary to Decimal */

    #include #include

    #include

    main()

    {

    int dec=0,i=0, d;

    long bin;

    clrscr();

    printf("Enter a binary number : ");

    scanf("%ld",&bin);

    while(bin){

    d = bin % 10;

    dec = dec + pow(2,i) * d;

    bin = bin/10;

    i = i + 1;

    }

    printf("\nDecimal Equivalent is %d", dec);

    getch();

    }

    Output

    Enter a binary number : 1101011

    Decimal Equivalent is 107

  • 8/4/2019 CPICP

    15/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Ex. No. 3.6PRIME NUMBER

    Aim

    To write a program to check the given number is prime or not using for loop.

    Algorithm

    Step 1 : Start

    Step 2 : Read the value ofn

    Step 3 : Initialize i to 2 and flg to 0

    Repeat steps 4 and 5 until i n/2

    Step 4 : Ifn is divisible by i then assign 1 to flg and go to Step 6

    Step 5 : Increment i by 1

    Step 6 : If flg = 0 then print "Prime"

    Step 6.1 : Else print "Not prime"

    Step 7 : Stop

  • 8/4/2019 CPICP

    16/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Flowchart

    YN

    N

    N

    Y

    Read n

    i = 2, flg = 0

    i n/2

    Start

    n%i = 0 flg = 1Y

    i = i + 1

    Print "Prime"

    Stop

    flg = 0Print "Not Prime"

  • 8/4/2019 CPICP

    17/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Program

    /* Prime number */

    #include

    #include

    main()

    {

    int i,n,flg=0;

    clrscr();

    printf("\nEnter a number : ");

    scanf("%d",&n);

    for(i=2; i

  • 8/4/2019 CPICP

    18/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Ex. No. 3.7ARRAY MINIMUM/ MAXIMUM

    Aim

    To write a program to find the largest and smallest of an array

    Algorithm

    Step 1 : Start

    Step 2 : Read the number of array elements as n

    Step 3 : Set up a loop and read array elements Ai, i = 0,1,2,n1

    Step 4 : Assume the first element A0 to be min and max

    Step 5 : Initialize i to 1

    Repeat steps 68 until i < n

    Step 6 : Ifmax < Ai then max = Ai

    Step 7 : Ifmin > Ai then min = Ai

    Step 8 : Increment i by 1

    Step 9 : Print max, min

    Step 10 : Stop

  • 8/4/2019 CPICP

    19/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Flowchart

    N

    Y

    Y

    N

    Read n

    max < Ai

    Start

    Stop

    =

    Read Ai

    i

    min = max = A0

    i = 1 to n1

    max = Ai

    min > Ai min = Ai

    i

    Print min, max

  • 8/4/2019 CPICP

    20/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Program

    /* Maximum and Minimum of an array */

    #include

    #include

    main()

    {

    int a[10];

    int i,min,max,n;

    clrscr();

    printf("Enter number of elements : ");

    scanf("%d",&n);

    printf("\nEnter Array Elements\n");

    for(i=0; i

  • 8/4/2019 CPICP

    21/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Ex. No. 3.8MATRIX MULTIPLICATION

    Aim

    To write a program to perform matrix multiplication based on their dimensions.

    Algorithm

    Step 1 : Start

    Step 2 : Read the order of matrix A as m and n

    Step 3 : Read the order of matrix B as p and q

    Step 4 : If n p then print "Multiplication not possible" and Stop

    Step 5 : Set up a loop and read matrix A elements Aij, i = 0 to m-1 and j = 0 to n-1

    Step 6: Set up a loop and read matrix B elements Bij, i = 0 to p-1 and j = 0 to q-1

    Step 7 : Initialize i to zero

    Step 8 : Initialize j to zero

    Step 9 : Assign 0 to Cij

    Step 10 : Initialize kto zero

    Step 11 : Compute Cij = Cij + Aik * Bkj

    Step 12 : Increment kby 1

    Repeat steps 11 and 12 until k< n

    Step 13 : Increment j by 1

    Repeat steps 913 until j < q

    Step 14 : Increment i by 1

    Repeat steps 814 until i < m

    Step 15 : Set up a loop and print product matrix Cij, i = 0 to m-1 and j = 0 to q-1

    Step 16 : Stop

  • 8/4/2019 CPICP

    22/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Flow Chart

    Y

    N

    Read m, n

    Start

    i = 0 to m1

    Read Aij

    j

    =

    n=p

    X

    Read p, q

    Print min, max

    i

    2

    =

    = 0 to 1

    Read Bij

    j

    i

  • 8/4/2019 CPICP

    23/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    2

    Stop

    =

    j

    =

    i

    Cij = 0

    k = 0 to n1

    Cij = Cij + Aik * Bkj

    k

    i = 0 to m1

    Print Cij

    j

    = 0 to 1

    i

    X

  • 8/4/2019 CPICP

    24/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Program

    /* Matrix Multiplication */

    #include

    #include

    main()

    {

    int a[10][10], b[10][10], c[10][10];

    int r1, c1, r2, c2;

    int i, j, k;

    clrscr();

    printf("Enter order of matrix A : ");

    scanf("%d%d", &r1, &c1);

    printf("Enter order of matrix B : ");

    scanf("%d%d", &r2, &c2);

    if (c1 != r2)

    {

    printf("\nMatrix multiplication not possible");

    getch();

    exit(0);

    }

    printf("\nEnter matrix A elements\n");

    for(i=0; i

  • 8/4/2019 CPICP

    25/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    for(k=0; k

  • 8/4/2019 CPICP

    26/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Ex. No. 3.9ALPHABETICAL ORDER

    Aim

    To write a program to arrange names in their alphabetic order using bubble sortmethod.

    Algorithm

    Step 1 : Start

    Step 2 : Read number of name as n

    Step 3 : Set up a loop and read the name list in name array

    Step 4 : Assign 0 to i

    Step 5 : Assign i+1 to j

    Step 6: If namei > namej then swap the strings namei and namej

    Step 7 : Increment j by 1

    Repeat steps 6 and 7 until j < n

    Step 8 : Increment i by 1

    Repeat steps 58 until i < n-1

    Step 9 : Set up a loop and print the sorted name array

    Step 10 : Stop

  • 8/4/2019 CPICP

    27/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Flow Chart

    Y

    N

    Read n

    namei > namej

    Start

    Stop

    =

    Read namei

    i

    =

    Swap strings

    namei , namej

    i

    i = 0 to n2

    j

    i = 0 to n1

    Print namei

    i

  • 8/4/2019 CPICP

    28/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Program

    /* Sorting strings */

    #include

    #include #include

    main()

    {

    char name[20][15], t[15], srch[15];

    int i,j,n,upper,lower,mid;

    clrscr();

    printf("Enter number of students : ");

    scanf("%d",&n);

    printf("\nEnter student names\n");

    for(i=0; i

  • 8/4/2019 CPICP

    29/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Output

    Enter number of students : 10

    Enter student names

    Raghu

    PrabaGopal

    Anand

    Venkat

    Kumar

    Saravana

    Naresh

    Christo

    Vasanth

    Alphabetical Order

    Anand

    ChristoGopal

    Kumar

    Naresh

    Praba

    Raghu

    Saravana

    Vasanth

    Venkat

  • 8/4/2019 CPICP

    30/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Ex. No. 3.10STRING REVERSE

    Aim

    To write a program to reverse the given string without using library functions.

    Algorithm

    Step 1 : Start

    Step 2 : Read string str

    Step 3 : Assign 0 to len

    Step 4 If lenth character '\0' then go to step 5 else step 6

    Step 5 : Increment len by 1 and go to step 4

    Step 6 : Assign 0 to i and len-1 to j

    Step 7: Swap the ith

    and jth

    character

    Step 8 : Increment i by 1

    Step 9 : Decrement j by 1

    Repeat steps 79 until i < len/2

    Step 10 : Print reversed string str

    Step 10 : Stop

  • 8/4/2019 CPICP

    31/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Flow Chart

    Y

    Y

    N

    Read str

    Start

    Stop

    T = i

    i = j

    j = T

    Print str

    len = 0

    lenth char '\0'

    len = len + 1

    i = 0j = len - 1

    i < len/2

    i = i + 1

    j = j 1

    N

  • 8/4/2019 CPICP

    32/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Program

    /* User-defined String reverse */

    #include

    #include

    main()

    {

    char *str;

    int i,j,len=0;

    char t;

    clrscr();

    printf("Enter a String : ");

    gets(str);

    /* String Length */while (str[len] != '\0')

    len++;

    /* String Reverse */

    for(i=0, j=len-1; i

  • 8/4/2019 CPICP

    33/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Ex. No. 3.11SINE SERIES

    Aim

    To write a program to generate sine series sin(x) =

    !7!5!3

    753 xxxx value using

    function.

    Algorithm

    Step 1 : Start

    Step 2 : Read sine degree as deg

    Step 3 : Convert degree to radians using the formula x = deg * / 180

    Step 4 : Call sinecalc function with parameter x

    Step 5 : Print computed value of sine series

    Step 6: Stop

    sinecalc Function

    Step 1 : Assign x to term and sum

    Repeat steps 3 and 4 until term < 0.00001

    Step 2 : Compute new term as term = term * x2 / (2i (2i+1))

    Step 3 : Alternatively subtract and add term to sum

    Step 4 : Return sum

  • 8/4/2019 CPICP

    34/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Flow Chart

    Y

    N

    Read deg, n

    Start

    sgn = -sgn

    term = term * x2

    / (2i (2i+1))

    sum = sum + sgn * term

    sgn = 1sum = term = x

    Print sinevalue

    Stop

    sinecalc (x)

    x = deg * 3.14 / 180

    Return (sum)

    term> 0.00001

    Call sinevalue = sinecalc(x)

  • 8/4/2019 CPICP

    35/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Program

    /* Sine series */

    #include

    #include

    #define pi 3.14

    float sinecalc(float, int);

    main()

    {

    int deg,n;

    float x,sineval;

    clrscr();

    printf("Enter Sine degree : ");

    scanf("%d",&deg);printf("Enter no. of terms : ");

    scanf("%d",&n);

    x = deg*pi/180;

    sineval = sinecalc(x);

    printf("\nGenerated series value : %.4f", sineval);

    printf("\nBuilt-in function value : %.4f", sin(x));

    printf("\nComputational error\t : %.4f", sin(x)-sineval);

    getch();

    }

    float sinecalc(float x)

    {

    int i=1,sgn=1;

    float sum,term;

    sum = term = x;

    while (1)

    {

    if (term < 0.00001) break;

    printf("\nThe term is %.4lf and sum is %.4lf",

    term, sum);

    sgn = -sgn;term *= x*x / (2*i * (2*i+1));

    sum += sgn *term;

    i++;

    }

    return (sum);

    }

  • 8/4/2019 CPICP

    36/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Output

    Enter Sine degree: 73

    The term is 1.2734 and sum is 1.2734

    The term is 0.3442 and sum is 0.9293

    The term is 0.0279 and sum is 0.9572The term is 0.0011 and sum is 0.9561

    The term is 0.0000 and sum is 0.9561

    The value of Generated Sine series is 0.9561

    Value using built-in function is 0.9561

    Computational error is 0.0000

  • 8/4/2019 CPICP

    37/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Ex. No. 3.12RECURSIVE FUNCTION

    Aim

    To write a program to find factorial value of a given number n! = n * (n-1)! usingrecursion.

    Algorithm

    Step 1 : Start

    Step 2 : Read the value ofn

    Step 3 : Call factorial function with parameter n

    Step 4 : Print factorial value

    Step 5: Stop

    factorial Function

    Step 1 : Ifn = 1 then return 1

    Step 1.1 : Else return n * factorial(n-1)

  • 8/4/2019 CPICP

    38/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Flow Chart

    Read n

    Start

    Return (1)

    Print factvalue

    Stop

    factorial(n)

    n = 1Y N

    Call factvalue = factorial(n)

    Return (n * factorial(n-1))

  • 8/4/2019 CPICP

    39/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Program

    /* Factorial--Recursion */

    #include

    #include

    long factorial(int);

    main()

    {

    int n;

    long int f;

    clrscr();

    printf("Enter a number : ");

    scanf("%d",&n);

    f=factorial(n);

    printf("Factorial value : %ld",f);

    getch();}

    long factorial(int n)

    {

    if (n

  • 8/4/2019 CPICP

    40/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Ex. No. 3.13PASS BY VALUE & REFERENCE

    Aim

    To write a program that demonstrates passing parameters to a function by value andreference.

    Algorithm

    Step 1 : Start

    Step 2 : Assign 10 to a and 20 to b

    Step 3 : Print a and b

    Step 4 : Call swapval function with values ofa and b

    Step 5 : Print a and b

    Step 6 : Call swapreffunction with address ofa and b

    Step 7 : Print a and b

    Step 8 : Stop

    swapval Function

    Step 1 : t = a

    Step 2 : a = b

    Step 3 : b = t

    Step 4 : Return

    swapref Function

    Step 1 : t = *a

    Step 2 : *a = *b

    Step 3 : *b = t

    Step 4 : Return

  • 8/4/2019 CPICP

    41/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Flow Chart

    Start

    Return

    Print a, b

    Stop

    swapval (a, b)

    a = 10b = 20

    Print a, b

    Print a, b

    t = aa = b

    b = t

    Return

    swapref (*a, *b)

    t = *a*a = *b

    *b = t

    Call swapval(a, b)

    Call swapref(&a, &b)

  • 8/4/2019 CPICP

    42/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Program

    /* Pass by value and reference */

    #include

    #include

    void swapval(int, int);

    void swapref(int *,int *);

    main()

    {

    int a = 10, b = 20;

    clrscr();

    printf("\nValues before function calls\n");

    printf("Value of A : %d\n",a);

    printf("Value of B : %d\n",b);

    swapval(a,b);

    printf("\nValues after Pass by Value\n");

    printf("Value of A : %d\n",a);

    printf("Value of B : %d\n",b);

    swapref(&a,&b);

    printf("\nValues after Pass by Reference\n");

    printf("Value of A : %d\n",a);

    printf("Value of B : %d",b);

    getch();

    }

    /* Pass by value */

    void swapval(int a,int b)

    {

    int t;

    t = a;

    a = b;

    b = t;

    }

    /* Pass by Reference*/

    void swapref(int *x,int *y)

    {int t;

    t = *x;

    *x = *y;

    *y = t;

    }

  • 8/4/2019 CPICP

    43/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Output

    Values before function calls

    Value of A : 10

    Value of B : 20

    Values after Pass by Value

    Value of A : 10

    Value of B : 20

    Values after Pass by Reference

    Value of A : 20

    Value of B : 10

  • 8/4/2019 CPICP

    44/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Ex. No. 3.14PAYROLL APPLICATION

    Aim

    To write a program to print employee payroll of a concern using structure.

    Algorithm

    Step 1 : Start

    Step 2 : Define employee structure with fields empid, ename, basic, hra, da, it, gross and

    netpay

    Step 3 : Read number of employees n

    Step 4 : Set up a loop and read empid, ename, and basic for n employees in emp array

    Set up a loop and do steps 59 for n employees

    Step 5 : hra = 2% of basic

    Step 6 : da = 1% of basic

    Step 7 : gross = basic + hra + da

    Step 8 : it = 5% of basic

    Step 9 : netpay = gross - it

    Step 10: Print company and column header

    Step 11 Set up a loop and print empid, ename, basic, hra, da, it, gross and netpay for

    each employee in emp array

    Step 12 : Stop

  • 8/4/2019 CPICP

    45/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Flow Chart

    Start

    Stop

    Define emp structure

    Read n

    = -

    Read empi.empid,empi.name, empi.basic

    i

    i = 0 to n-1

    i

    empi.hra = 0.02 * empi.basic

    empi.da = 0.01 * empi.basicempi.it = 0.05 * empi.basic

    empi.gross = empi.basic + empi.hra + empi.daempi.netpay = empi.gross - empi.it

    = -

    Print empi.empid, empi.name,

    empi.basic, empi.hra, empi.da,empi.gross, empi.it, empi.netpay

    i

  • 8/4/2019 CPICP

    46/47

    C Programming 185151Computer Practice I

    cseannauniv.blogspot.com S.K. Vijai Anand

    Program

    /* Payroll Generation */

    #include

    #include

    struct employee

    {

    int empid;

    char ename[15];

    int basic;

    float hra;

    float da;

    float it;

    float gross;

    float netpay;

    };

    main()

    {

    struct employee emp[50];

    int i, j, n;

    clrscr();

    printf("\nEnter No. of Employees : ");

    scanf("%d", &n);

    for(i=0; i

  • 8/4/2019 CPICP

    47/47

    C Programming 185151Computer Practice I

    printf("\n\n\n\t\t\t\tXYZ & Co. Payroll\n\n");

    for(i=0;i