structures using c by balagurusamy

Upload: priyadarshini212007

Post on 04-Jun-2018

248 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/13/2019 Structures using C by Balagurusamy

    1/128

    Structure

    A Structureis a collection of different dataitems, that are stored under a common name.

    Syntax:

    struct structure_name{

    structure element1;

    structure element2;

    .

    };

  • 8/13/2019 Structures using C by Balagurusamy

    2/128

  • 8/13/2019 Structures using C by Balagurusamy

    3/128

    Example

    #include

    #includestruct stud

    {

    int regno;

    char name[10];

    int m1;

    int m2;

    int m3;

    };

    struct stud s;void main()

    {

    float tot,avg;

  • 8/13/2019 Structures using C by Balagurusamy

    4/128

    printf("\nEnter the student regno,name,m1,m2,m3:");

    scanf("%d%s%d%d%d",&s.regno,&s.name,&s.m1,&s.m2,

    &s.m3);

    tot=s.m1+s.m2+s.m3;

    avg=tot/3;

    printf("\nThe student Details are:");

    printf("\n%d\t%s\t%f\t%f",s.regno,s.name,tot,avg);

    }

  • 8/13/2019 Structures using C by Balagurusamy

    5/128

    Output

    Enter the student regno,name,m1,m2,m3:100

    aaa

    87

    98

    78

    The student Details are:

    100 aaa 263.000000 87.666664

  • 8/13/2019 Structures using C by Balagurusamy

    6/128

    Structure assignment

    It is possible to assign one structure

    information to another structure of same type

    using simple assignment statement.

  • 8/13/2019 Structures using C by Balagurusamy

    7/128

    Example#include

    #include

    void main()

    {

    struct x

    {int a;

    }x,y;

    clrscr();

    x.a=10;y=x;

    printf("The value of y.a is%d",y.a);

    getch();

    }

  • 8/13/2019 Structures using C by Balagurusamy

    8/128

    Output

    The value of y.a is10

  • 8/13/2019 Structures using C by Balagurusamy

    9/128

    Example#include

    #include

    struct stud

    {

    int regno;

    char name[10],grade;

    int m1,m2,m3;float avg,tot;

    } s[10];

    void main()

    {int i,n;

    printf("\nEnter the no.of students:");

    scanf("%d",&n);

  • 8/13/2019 Structures using C by Balagurusamy

    10/128

    for(i=0;i

  • 8/13/2019 Structures using C by Balagurusamy

    11/128

    s[i].grade='A';

    else if(s[i].avg>=50)

    s[i].grade='B';

    else if(s[i].avg>=35)

    s[i].grade='C';

    }

    }

    printf("\nSTUDENT MARK LIST\n");

    printf("\nREGNO\tNAME\tTOTAL\tAvg\tGRADE");

    for(i=0;i

  • 8/13/2019 Structures using C by Balagurusamy

    12/128

    Enter the no.of students:2

    Enter the student regno,name,m1,m2,m3:101

    aaa

    8998

    78

    Enter the student regno,name,m1,m2,m3:102

    bbb

    5968

    76

    STUDENT MARK LIST

    REGNO NAME TOTAL Avg GRADE

    101 aaa 265.000000 88.333336 d

    102 bbb 203.000000 67.666664 A

  • 8/13/2019 Structures using C by Balagurusamy

    13/128

    Union

    An Unionis a collection of different data items,that are stored under a common name. Heresame memory is shared by its members.

    Syntax:

    union union _name{

    union element1;

    union element2;

    };

  • 8/13/2019 Structures using C by Balagurusamy

    14/128

    Example:

    unionresult

    {

    int mark;float avg;

    char grade;

    };

    unionresult s;

  • 8/13/2019 Structures using C by Balagurusamy

    15/128

    Example

    #include

    #includeunion stud

    {

    int a;

    char b[2];

    };

    void main()

    {union stud c;

  • 8/13/2019 Structures using C by Balagurusamy

    16/128

    c.a=256;

    printf("\nc.a value is%d",c.a);

    printf("\nc.b[0] value is%d",c.b[0]);

    printf("\nc.b[1] value is%d",c.b[1]);

    }

    Output :

    c.a value is256

    c.b[0] value is0

    c.b[1] value is1

  • 8/13/2019 Structures using C by Balagurusamy

    17/128

    256 = 00000010 00000000

    Higher bit Lower bit

    00000000 00000010

    c.a - 2 Byte

    c.b[0] 1 Byte c.b[0] 1 Byte

    c.b[0] c.b[1]

  • 8/13/2019 Structures using C by Balagurusamy

    18/128

    Example#include

    #include

    struct student{

    int a;

    int b;

    char c;

    }s;union student1

    {

    int a;

    int b;char c;

    }s1;

  • 8/13/2019 Structures using C by Balagurusamy

    19/128

    void main()

    {

    printf("\nThe size of struct is %d",sizeof(s));

    printf("\nThe size of union is %d",sizeof(s1));

    getch();

    }

    Output :

    The size of struct is 5

    The size of union is 2

  • 8/13/2019 Structures using C by Balagurusamy

    20/128

    Structure & Union

    int int char

    2 Byte 2Byte 1Byte

    2 Byte

    structure

    int ,int, char

    un ion

  • 8/13/2019 Structures using C by Balagurusamy

    21/128

    Preprocessor

    It is a program that processes the source

    program before compilation.

    It operates under the following directives

    File Inclusion

    Macro substitution

    Conditional inclusion

  • 8/13/2019 Structures using C by Balagurusamy

    22/128

    File Inclusion

    It is used to include some file that contains

    functions or some definitions.

    Syntax:

    #include (or)

    #includefilename

    Eg: #include#include ex.c

  • 8/13/2019 Structures using C by Balagurusamy

    23/128

    Example

    #include

    #include

    #include "addition.txt"

    void main()

    {int a,b;

    printf("\nEnter the numbers:");

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

    printf("The Value is %d",add(a,b));getch();

    }

  • 8/13/2019 Structures using C by Balagurusamy

    24/128

    addition.txt

    int add(int a,int b)

    {

    return(a+b);

    }

  • 8/13/2019 Structures using C by Balagurusamy

    25/128

    Output

    Enter the numbers:7

    4

    The Value is 11

  • 8/13/2019 Structures using C by Balagurusamy

    26/128

    Example

    #include

    #include

    #include "fact.c"

    void main()

    {int a;

    printf("\nEnter the number:");

    scanf("%d",&a);

    printf("The factorial of %d! is %d",a,rec(a));

    getch();

    }

  • 8/13/2019 Structures using C by Balagurusamy

    27/128

    fact.c

    int rec(int x)

    {

    int f;

    if(x==1)return(1);

    else

    f=x*rec(x-1);

    return(f);

    }

  • 8/13/2019 Structures using C by Balagurusamy

    28/128

    Output

    Enter the number:5

    The factorial of 5! is 120

  • 8/13/2019 Structures using C by Balagurusamy

    29/128

    Macro Substitution

    It is used to define and use integer, string, or

    identifier in the source program

    The three forms of macros are

    Simple Macro

    Argumented Macro

    Nested Macro

  • 8/13/2019 Structures using C by Balagurusamy

    30/128

    Simp le Macro

    It is used to define some constants

    Syntax

    # define identifier string/integer Eg:

    #define pi 3.14

    #define CITY chennai

  • 8/13/2019 Structures using C by Balagurusamy

    31/128

    Example

    #include

    #include#define pi 3.14

    #define CITY "chennai"

    void main()

    {

    printf("The Value is %f",2*pi);printf("\nThe Value CITY is %s",CITY);

    getch();

    }

    Output :

    The Value is 6.280000

    The Value CITY is chennai

  • 8/13/2019 Structures using C by Balagurusamy

    32/128

    Argumented Macro

    It is used to define some complex forms in the

    source program.

    Syntax:

    #define identifier (v1,v2,.) string/integer

    Eg:#define cube(n) (n*n*n)

  • 8/13/2019 Structures using C by Balagurusamy

    33/128

    Example

    #include

    #include

    #define cube(n) (n*n*n)

    void main()

    {

    printf("The Value of 3 cube is %d",cube(3));

    getch();

    }

    Output :

    The Value of 3 cube is 27

  • 8/13/2019 Structures using C by Balagurusamy

    34/128

    Nested Macro

    Here one macro is used by another macro.

    Eg:

    #define a 3

    #define sq a*a

  • 8/13/2019 Structures using C by Balagurusamy

    35/128

    Example

    #include

    #include#define a 3

    #define sq a*a

    void main()

    {printf("The Value is %d",sq);

    getch();

    }

    Output :

    The Value is 9

  • 8/13/2019 Structures using C by Balagurusamy

    36/128

    Conditional Inclusion

    It is used to include some conditional

    statements.

  • 8/13/2019 Structures using C by Balagurusamy

    37/128

    Example

    #include

    #include#define a 3

    #ifdef a

    #define c a+5

    #endifvoid main()

    {

    printf("\nThe value C is %d",c);

    getch();

    }

    Output :

    The value C is 8

  • 8/13/2019 Structures using C by Balagurusamy

    38/128

    Pointers

    Pointer is a variable that contains the memory

    address of another variable.

  • 8/13/2019 Structures using C by Balagurusamy

    39/128

    Example:

    x=5

    x Variable

    1002 Address

    5 Value

  • 8/13/2019 Structures using C by Balagurusamy

    40/128

    Example#include

    #includevoid main()

    {

    int x=5;

    printf("\n The Address of x = %u",&x);printf("\n The Value of x = %d",x);

    }

    OutputThe Address of x = 8714

    The Value of x = 5

  • 8/13/2019 Structures using C by Balagurusamy

    41/128

    Pointer Declaration

    Syntax

    data-type *pointer-name;

    data-type - Type of the data towhich the pointer points.

    pointer-name - Name of the pointer

    Example: int *a;

  • 8/13/2019 Structures using C by Balagurusamy

    42/128

    Accessing Variable through Pointer

    If a pointer is declared and assigned to a

    variable, then the variable can be accessed

    through the pointer.

    Example:

    int *a;

    x=5;

    a=&x;

    E l

  • 8/13/2019 Structures using C by Balagurusamy

    43/128

    Example

    #include

    #includevoid main()

    {

    int x=5;

    int *a;

    a=&x;

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

    printf("\n The Address of x = %u",&x);printf("\n The Value of a = %d",a);

    printf("\n The Value of x = %d",*a);

    }

  • 8/13/2019 Structures using C by Balagurusamy

    44/128

    Output

    The Value of x = 5

    The Address of x = 8758

    The Value of a = 8758

    The Value of x = 5

  • 8/13/2019 Structures using C by Balagurusamy

    45/128

    Example:

    #include

    #includevoid main()

    {

    int y=10;

    int *a;a=&y;

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

    printf("\n The Address of y = %u",&y);

    printf("\n The Value of a = %d",a);printf("\n The Address of a = %u",&a);

    }

  • 8/13/2019 Structures using C by Balagurusamy

    46/128

    5001 10

    8000

    a y

    5001

    Variable

    Value

    Address

  • 8/13/2019 Structures using C by Balagurusamy

    47/128

    Output

    The Value of y = 10

    The Address of y = 5001

    The Value of a = 5001

    The Address of a = 8000

  • 8/13/2019 Structures using C by Balagurusamy

    48/128

    Null Pointer

    A pointer is said to be null pointer if zero is

    assigned to the pointer.

    Example

    int *a,*b;

    a=b=0;

  • 8/13/2019 Structures using C by Balagurusamy

    49/128

    Pointer to Pointer

    Here one pointer stores the address of

    another pointer variable.

    Example:

    int x=10,*a,**b;

    a=&x;b=&a;

  • 8/13/2019 Structures using C by Balagurusamy

    50/128

    5001 10

    8000

    a x

    5001

    Variable

    Value

    Address

    8000

    9000

    b

    Example

  • 8/13/2019 Structures using C by Balagurusamy

    51/128

    p#include

    #include

    void main()

    {

    int a=10;

    int *b,**c;

    b=&a;c=&b;

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

    printf("\n The Address of a = %u",&a);

    printf("\n The Value of b = %d",b);printf("\n The Address of b = %u",&b);

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

    printf("\n The Address of c = %u",&c);

    }

  • 8/13/2019 Structures using C by Balagurusamy

    52/128

    Output

    The Value of a = 10

    The Address of a = 5001

    The Value of b = 5001

    The Address of b = 8000

    The Value of c = 8000

    The Address of c = 9000

  • 8/13/2019 Structures using C by Balagurusamy

    53/128

    Pointers and Functions

    Call by Value

    Call by Reference

  • 8/13/2019 Structures using C by Balagurusamy

    54/128

    Call by value

    Actual argument passed to the formal

    argument.

    Any changes to the formal argument does not

    affect the actual argument.

  • 8/13/2019 Structures using C by Balagurusamy

    55/128

    Example

    #include #include

    void main()

    {

    int x,y,swap(int,int);

    printf("\nEnter value of x:");

    scanf("%d",&x);

    printf("\nEnter value of y:");scanf("%d",&y);

  • 8/13/2019 Structures using C by Balagurusamy

    56/128

    change(x,y);

    printf("\n\nValues in the Main()-->x=%d,y=%d",x,y);

    }

    int swap(int a,int b)

    {

    int c;

    c=a;

    a=b;

    b=c;

    printf("\nValues in the Function -->x=%d,y=%d",a,b);

    }

  • 8/13/2019 Structures using C by Balagurusamy

    57/128

    Output

    Enter value of x:5

    Enter value of y:6

    Values in the Function -->x=6,y=5

    Values in the Main()-->x=5,y=6

  • 8/13/2019 Structures using C by Balagurusamy

    58/128

  • 8/13/2019 Structures using C by Balagurusamy

    59/128

    Call by reference

    Instead of passing value, the address of the

    argument will be passed.

    Any changes to the formal argument will

    affect the actual argument.

  • 8/13/2019 Structures using C by Balagurusamy

    60/128

    Example

    #include #include

    void main()

    {

    int x,y,change(int*,int*);

    printf("\nEnter value of x:");

    scanf("%d",&x);

    printf("\nEnter value of y:");scanf("%d",&y);

  • 8/13/2019 Structures using C by Balagurusamy

    61/128

    change(&x,&y);

    printf("\n\nValues in the Main()-->x=%d,y=%d",x,y);

    }

    int change(int *a,int *b)

    {

    int c;

    c=*a;

    *a=*b;

    *b=c;printf("\nValues in the Function -->x=%d,y=%d",*a,*b);

    }

  • 8/13/2019 Structures using C by Balagurusamy

    62/128

    Output

    Enter value of x:5

    Enter value of y:6

    Values in the Function -->x=6,y=5

    Values in the Main()-->x=6,y=5

  • 8/13/2019 Structures using C by Balagurusamy

    63/128

    Pointer to Array

    The elements of the array can also be

    accessed through a pointer.

    Example

    int a[3]={2,3,7};

    int *b;

    b=a;

  • 8/13/2019 Structures using C by Balagurusamy

    64/128

    Example:

    #include

    #includevoid main()

    {

    int a[3]={2,3,7};

    int *b;b=a;

    printf("\n The Value of a[0] = %d",a[0]);

    printf("\n The Address of a[0] = %u",&a[0]);

    printf("\n The Value of b = %d",b);}

  • 8/13/2019 Structures using C by Balagurusamy

    65/128

    8744 2

    9000

    b a[0]

    8744

    Variable

    Value

    Address

  • 8/13/2019 Structures using C by Balagurusamy

    66/128

  • 8/13/2019 Structures using C by Balagurusamy

    67/128

    Example

    #include#include

    void main()

    {

    int a[5]={2,3,7,9,10};int i;

    for(i=0;i

  • 8/13/2019 Structures using C by Balagurusamy

    68/128

    2 3 7 9 10

    a[0] a[1] a[2] a[3] a[4]

    8724 8726 8728 8730 8732

    Array

    ValueAddress

  • 8/13/2019 Structures using C by Balagurusamy

    69/128

    Output

    The Value of a[0] = 2The Address of a[0] = 8724

    The Value of a[1] = 3

    The Address of a[1] = 8726

    The Value of a[2] = 7

    The Address of a[2] = 8728

    The Value of a[3] = 9

    The Address of a[3] = 8730The Value of a[4] = 10

    The Address of a[4] = 8732

    Example

  • 8/13/2019 Structures using C by Balagurusamy

    70/128

    Example#include

    #include

    void main(){

    int a[5]={1,2,3,4,5};

    int i,sum=0;

    int *b;

    b=a;

    for(i=0;i

  • 8/13/2019 Structures using C by Balagurusamy

    71/128

    Output

    The Sum is 15

  • 8/13/2019 Structures using C by Balagurusamy

    72/128

    Pointer and Structures

    Syntax:

    struct structure_name

    {

    structure element1;

    structure element2;

    .

    }variable,*ptr;

  • 8/13/2019 Structures using C by Balagurusamy

    73/128

    Example:struct stud

    {

    int sno;char name[10];

    int mark;

    };

    struct stud *s;

    Example

  • 8/13/2019 Structures using C by Balagurusamy

    74/128

    Example#include

    #includestruct stud

    {

    int regno;

    char name[10];int m1;

    int m2;

    int m3;

    };struct stud s;

    struct stud *t;

    void main()

  • 8/13/2019 Structures using C by Balagurusamy

    75/128

    void main()

    {

    float tot,avg;

    t=&s;

    printf("\nEnter the student regno,name,m1,m2,m3:");

    scanf("%d%s%d%d%d",&s.regno,&s.name,&s.m1,&s.m2,&s.m3);

    tot=s.m1+s.m2+s.m3;avg=tot/3;

    printf("\nThe student Details are:");

    printf("\n%d\t%s\t%f\t%f",s.regno,s.name,tot,avg);

    printf("\n%d\t%s\t%f\t%f",t->regno,t->name,tot,avg);}

  • 8/13/2019 Structures using C by Balagurusamy

    76/128

    Output

    Enter the student regno,name,m1,m2,m3:1

    aaa

    76

    89

    76

    The student Details are:

    1 aaa 241.000000 80.333336

    1 aaa 241.000000 80.333336

  • 8/13/2019 Structures using C by Balagurusamy

    77/128

    Command Line Argument

    It allows the user to pass some information to

    the program while running the program.

    Example

  • 8/13/2019 Structures using C by Balagurusamy

    78/128

    Example#include

    #includevoid main(int argc,char argv[])

    {

    printf("\n The Argument is %s",argv[0]);

    getch();

    }

  • 8/13/2019 Structures using C by Balagurusamy

    79/128

    Output

    C:\tc>a

    The Argument is C:\TC\A.EXE

    l d

  • 8/13/2019 Structures using C by Balagurusamy

    80/128

    String Palindrome

    #include#include

    #include

    void main()

    {char s1[15],s2[15];

    printf("\nenter the string:");

    scanf("%s",s1);

    strcpy(s2,s1);

    strrev(s1);

    if(strcmp(s1 s2)==0)

  • 8/13/2019 Structures using C by Balagurusamy

    81/128

    if(strcmp(s1,s2)==0)

    printf("\n The string is palindrome");

    elseprintf("\n The string is not a palindrome");

    getch();

    }

    Output :

    enter the string: aba

    The string is palindrome

    D l i C P

  • 8/13/2019 Structures using C by Balagurusamy

    82/128

    Developing a C Program

    The Program development life cycle is

    considered as a sequence of events by the

    programmer to develop the program.

    The Program development life cycle containsthe following phase

    Program Design

    Program Coding Program Testing

    P D i

  • 8/13/2019 Structures using C by Balagurusamy

    83/128

    Program Design

    Analysing the problem

    Algorithm development

    Selection of conditional and control structure

    etc,.

    P C di

  • 8/13/2019 Structures using C by Balagurusamy

    84/128

    Program Coding

    Documentation

    Statement construction

    Input and output format etc,.

    P T ti

  • 8/13/2019 Structures using C by Balagurusamy

    85/128

    Program Testing

    It is the process of executing the program withsample data

  • 8/13/2019 Structures using C by Balagurusamy

    86/128

    Example#include

  • 8/13/2019 Structures using C by Balagurusamy

    87/128

    # c ude std o

    #include

    #define p 3.14

    void main(){

    #ifdef p

    printf("\nPentium");

    #else

    printf("\n Celeron");

    #endif

    printf("\nthe value is %f",2*p);

    getch();

    }

    Output :

    Pentium

    the value is 6.280000

  • 8/13/2019 Structures using C by Balagurusamy

    88/128

    Example

  • 8/13/2019 Structures using C by Balagurusamy

    89/128

    p#include

    #include

    struct stud{

    int regno;

    char name[10];

    int m1;int m2;

    int m3;

    };

    struct stud s;

    void main()

    {

    float tot,avg;

    int i;

    printf("\nEnter the student regno,name,m1,m2,m3:");

  • 8/13/2019 Structures using C by Balagurusamy

    90/128

    p ( g , , , , );

    for(i=0;i

  • 8/13/2019 Structures using C by Balagurusamy

    91/128

    Enter the student regno,name,m1,m2,m3:100

    aaa78

    67

    98

    101

    bbb

    80

    90

    75

    101bbb 245.000000 81.666664101bbb 245.000000 81.666664

  • 8/13/2019 Structures using C by Balagurusamy

    92/128

    #include

    #include

    int main(void)

    {

    char string[15];

    char *ptr, c = 'r';

    strcpy(string, "This is a string");ptr = strchr(string, c);

    if (ptr)

    printf("The character %c is at position: %d\n", c, ptr-string);

    else

    printf("The character was not found\n");

    return 0;

    }

    Example

  • 8/13/2019 Structures using C by Balagurusamy

    93/128

    Example#include

    #include

    #include

    void main()

    {

    char a[]="Dept";

    int i=0;clrscr();

    while(a[i]!='\0')

    {

    printf("\nThe character is %c",a[i]);i++;

    }

    getch();

    }

  • 8/13/2019 Structures using C by Balagurusamy

    94/128

    The character is D

    The character is e

    The character is p

    The character is t

  • 8/13/2019 Structures using C by Balagurusamy

    95/128

    Structure

  • 8/13/2019 Structures using C by Balagurusamy

    96/128

    Structure

  • 8/13/2019 Structures using C by Balagurusamy

    97/128

  • 8/13/2019 Structures using C by Balagurusamy

    98/128

  • 8/13/2019 Structures using C by Balagurusamy

    99/128

  • 8/13/2019 Structures using C by Balagurusamy

    100/128

    Lab Exercise

    C Programs

    Function-with arg & return#include

  • 8/13/2019 Structures using C by Balagurusamy

    101/128

    #include

    void main()

    {int a,b,c;

    int add(int,int);

    printf("\nEnter two number:");

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

    c=add(a,b);

    printf("\nSum is:%d",c);

    }

    int add(int x,int y)

    {int z;

    z=x+y;

    return(z);

    }

    Output

  • 8/13/2019 Structures using C by Balagurusamy

    102/128

    Output

    Enter two number:6

    7

    Sum is:13

    Example

  • 8/13/2019 Structures using C by Balagurusamy

    103/128

    Example

    #include #include

    void main()

    {

    int x,y,change(int*,int*);printf("\nEnter value of x:");

    scanf("%d",&x);

    printf("\nEnter value of y:");

    scanf("%d",&y);

  • 8/13/2019 Structures using C by Balagurusamy

    104/128

    change(&x,&y);

    printf("\n\nValues in the Main()-->x=%d,y=%d",x,y);

    }

    int change(int *a,int *b)

    {

    int c;c=*a;

    *a=*b;

    *b=c;

    printf("\nValues in the Function -->x=%d,y=%d",*a,*b);

    }

    Output

  • 8/13/2019 Structures using C by Balagurusamy

    105/128

    Output

    Enter value of x:5

    Enter value of y:6

    Values in the Function -->x=6,y=5

    Values in the Main()-->x=6,y=5

    Factorial-Recursive Fn

  • 8/13/2019 Structures using C by Balagurusamy

    106/128

    Factorial-Recursive Fn

    #include#include

    void main()

    {

    int a;int rec(int);

    printf("\nEnter the number:");

    scanf("%d",&a);

    printf("The factorial of %d! is %d",a,rec(a));

    }

    int rec(int x)

    {

  • 8/13/2019 Structures using C by Balagurusamy

    107/128

    {

    int f;

    if(x==1)return(1);

    else

    f=x*rec(x-1);

    return(f);

    }

    Output:

    Enter the number:5

    The factorial of 5! is 120

    Example: Working of 3!

  • 8/13/2019 Structures using C by Balagurusamy

    108/128

    Example: Working of 3!

    Matrix Multiplication

  • 8/13/2019 Structures using C by Balagurusamy

    109/128

    #include

    #include

    void main(){

    int i,j,k,r1,r2,c1,c2;

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

    clrscr();step1:

    printf("\n Enter the size of matrix A \n");

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

    printf("\n Enter the size of matrix B \n");

    scanf("%d%d",&r2,&c2);if(c1==r2)

    goto step2;

    else

    goto step1;

    step2:

    printf("\n Enter the elements of matrix A \n");

  • 8/13/2019 Structures using C by Balagurusamy

    110/128

    for(i=0;i

  • 8/13/2019 Structures using C by Balagurusamy

    111/128

    {

    for(j=0;j

  • 8/13/2019 Structures using C by Balagurusamy

    112/128

    2

    Enter the size of matrix B:2

    2Enter the elements of matrix A

    4

    4

    4

    4Enter the elements of matrix B

    4

    4

    44

    The resultant matrix is

    32 32

    32 32

  • 8/13/2019 Structures using C by Balagurusamy

    113/128

    Lab Ex:9,Finding area, circumference

  • 8/13/2019 Structures using C by Balagurusamy

    114/128

    of circle

    #include#include

    void main ( )

    {

    int r;

    float area,c;

    clrscr( );

    printf(" \nEnter the value of r:");scanf("%d",&r);

    area=3.14*r*r;

  • 8/13/2019 Structures using C by Balagurusamy

    115/128

    ;

    c=2*3.14*r;

    printf(" \nThe area is :%f",area);printf(" \nThe circumference is :%f",c);

    getch( );

    }

    Output :

    Enter the value of r:7

    The area is :153.860001

    The circumference is :43.959999

    Lab Ex:9,Conversion of Celsius to

    h h

  • 8/13/2019 Structures using C by Balagurusamy

    116/128

    Fahrenheit

    #include#include

    void main ( )

    {

    float c,f;clrscr( );

    printf(" \nEnter the value of c:");

    scanf("%f",&c);

    f=(c*1.8)+32;

    printf(" \nThe Fahrenheit is :%f",f);

    getch( );

    }

  • 8/13/2019 Structures using C by Balagurusamy

    117/128

    Enter the value of c:35

    The fahrenheit is :95.000000

    Lab Ex:11,Arithmetic operations

  • 8/13/2019 Structures using C by Balagurusamy

    118/128

    #include

    #include

    void main()

    {

    int a,b,c,d,e,f;clrscr();

    printf("\nEnter the values of A and B:");

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

    c=a+b;d=a-b;

    e=a*b;

    f=a/b;

  • 8/13/2019 Structures using C by Balagurusamy

    119/128

    printf("\nThe values of A + B:%d",c);printf("\nThe values of A - B:%d",d);

    printf("\nThe values of A * B:%d",e);

    printf("\nThe values of A / B:%d",f);getch();

    }

    Output

  • 8/13/2019 Structures using C by Balagurusamy

    120/128

    Output

    Enter the values of A and B:6

    3

    The values of A + B:9

    The values of A - B:3

    The values of A * B:18

    The values of A / B:2

    Lab.Ex13,Largest among 3 nos#incl de

  • 8/13/2019 Structures using C by Balagurusamy

    121/128

    #include

    #include

    void main ( ){

    int a,b,c;

    clrscr( );

    printf(" \nEnter the value of a:");

    scanf("%d",&a);

    printf(" \nEnter the value of b:");

    scanf("%d",&b);

    printf(" \nEnter the value of c:");

    scanf("%d",&c);

  • 8/13/2019 Structures using C by Balagurusamy

    122/128

    if((a>b)&&(a>c)){

    printf(" \nA is Big");

    }

    else

    {

    if(b>c)

    printf(" \nB is Big");

    else

    printf(" \nC is Big");}

    getch( );

    }

    Output

  • 8/13/2019 Structures using C by Balagurusamy

    123/128

    p

    Enter the value of a:5

    Enter the value of b:7

    Enter the value of c:3

    B is Big

    String Palindrome

  • 8/13/2019 Structures using C by Balagurusamy

    124/128

    g

    #include#include

    #include

    void main()

    {

    int len,i,j;

    char str[15];

    clrscr();

    printf("\n Enter the string:");

    scanf("%s",str);len=strlen(str);

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

  • 8/13/2019 Structures using C by Balagurusamy

    125/128

    {

    if(str[i]!=str[j])

    {

    printf("\nThe String is not a palindrome");

    getch();

    exit(0);

    }

    }printf("\nThe String is a palindrome");

    getch();

    }

    Output :

    Enter the string:abcba

    The String is a palindrome

    Lab.Ex:14,Quadratic Equation#i l d di h

  • 8/13/2019 Structures using C by Balagurusamy

    126/128

    #include

    #include

    #includevoid main ( )

    {

    int a,b,c,d,r1,r2;

    clrscr( );printf(" \nEnter the value of a:");

    scanf("%d",&a);

    printf(" \nEnter the value of b:");

    scanf("%d",&b);

    printf(" \nEnter the value of c:");

    scanf("%d",&c);

    d=b*b-4*a*c;

    if(d 0)

  • 8/13/2019 Structures using C by Balagurusamy

    127/128

    if(d>=0)

    {

    r1=(-b+sqrt(d))/(2*a);r2=(-b-sqrt(d))/(2*a);

    printf(" \nThe roots are %d,%d",r1,r2);

    }else

    {

    printf(" \nThe roots are imaginary");

    }

    getch( );

    }

    Output

  • 8/13/2019 Structures using C by Balagurusamy

    128/128

    p

    Enter the value of a:1

    Enter the value of b:4

    Enter the value of c:4

    The roots are -2,-2