Practice Programs

  • Doing Programs gives you the great understanding about what you have learned.
  • Doing Programs gives you the Confidence.
  • So let's Start by doing some programing.

  • Q:Define a structure student with name ,rollno, div, age as it's member. declare variable of structure student and take their value from the user and print them on the screen.

    Solution:Structure Student
    struct student  //structure name
    {
        int  rollno; //member1 ,integer variable which will store,
                     //student roll no.
    
        char  name[20]; //member2 a character array of 20 character
                        //which will store name of student.
    
        char  div;  //member3 a character variable,
                  //which will store division of student.
    
        int age;  //member3 a integer variable,
                  //which will store age of student.
    };
    


    void main()
    {
    
    //declaration of  variable of
    //structure student data type.
    struct student s1;
    
    printf("\nEnter the roll no of student:");
    scanf("%d",&s1.rollno);
    
    printf("\nEnter the name of student:");
    scanf("%s",s1.name);
    
    printf("\nEnter the student div:);
    s1.div = getche();
    
    printf("\nEnter the student age:");
    scanf("%d",&s1.age);
    
    
    
    printf("\nStudent name is %s with rollno %d studies in %c division and student age is %d", s1.name,s1.rollno,s1.div,s1.age);
    }
    

    Output:
    Enter the roll no of student:15
    Enter the name of student:Roy
    Enter the student div:A
    Enter the student age:16

    Student name is Roy with rollno 15 studies in A division and student age is 16

    Exercise:Define a structure Employee with name ,id,designation,age,Salary as it's member, take their value from the user and print them on the screen.


  • Q:Define a structure student with name ,rollno,div,age as it's member.Declare array of student data type with size of 2 and take their value from the user and print data of student whose rollno is 10.

    Solution:Structure Student
    struct student  //structure name
    {
        int  rollno; //member1 ,integer variable which will store,
                     //student roll no.
    
        char  name[20]; //member2 a character array of 20 character
                        //which will store name of student.
    
        char  div;  //member3 a character variable,
                  //which will store division of student.
    
        int age;  //member3 a integer variable,
                  //which will store age of student.
    };
    


    #include<stdio.h>
    void main()
    {
    int i;
    //declaration of  variable of
    //structure student data type.
    struct student s1[4];
    for(i=0;i<2;i++)
    {
    
    
    printf("\nEnter the roll no of student:");
    scanf("%d",&s1[i].rollno);
    
    printf("\nEnter the name of student:");
    scanf("%s",s1[i].name);
    
    printf("\nEnter the student div:");
    s1[i].div = getche();
    
    printf("\nEnter the student age:");
    scanf("%d",&s1[i].age);
    
    }
    for(i=0;i<2;i++)
    {
        if(s1[i].rollno == 10)
        {
            printf("\nName:%s\nAge = %d\nDiv:%c",s1[i].name,s1[i].age,s1[i].div);
            break;
        }
    }
    
    }
    

    Output:
    Enter the roll no of student:15
    Enter the name of student:Roy
    Enter the student div:A
    Enter the student age:16

    Enter the roll no of student:16
    Enter the name of student:James
    Enter the student div:B
    Enter the student age:17

    Student name is Roy with rollno 15 studies in A division and student age is 16
    Student name is James with rollno 16 studies in B division and student age is 17

    Exercise: Define a structure employee with name ,id, designation, age, salary as it's member. declare variable of structure employee and take their value from the user and print the data of employee on the screen whose id is 101.


  • Q:Define a structure student with name ,rollno, div, age as it's member. And define a function give_struct which will take a array of structure student data type as it's argument and it will return structure student with data of student whose rollno is 10.Test that function by calling it.

    Solution:Structure Student
    struct student  //structure name
    {
        int  rollno; //member1 ,integer variable which will store,
                     //student roll no.
    
        char  name[20]; //member2 a character array of 20 character
                        //which will store name of student.
    
        char  div;  //member3 a character variable,
                  //which will store division of student.
    
        int age;  //member3 a integer variable,
                  //which will store age of student.
    };
    


    Function Definition:
    struct student  give_struct(struct student s1[5])
    { int i;
        struct student s2;
        for(i=0;i<2;i++)
        {
            if(s1[i].rollno == 10)
            {   s2.rollno = s1[i].rollno;
                strcpy(s2.name,s1[i].name);
                s2.div = s1[i].div;
                s2.age = s1[i].age;
                break;
            }
        }
        return s2;
    }
    


    Using above define function:
    #include<stdio.h>
    void main()
    {
    int i;
    //declaration of  variable of
    //structure student data type.
    struct student s1[3] = {{10,"Roy",'A',18},{15,"Jason",'B',19}}, s3;
    
    
    s3 = give_struct(s1);   //calling function "give_struct"
    
      printf("\nName:%s\nAge = %d\nDiv:%c",s3.name,s3.age,s3.div);
    }
    

    Output:
    Name:Roy
    Age = 18
    Div:A

    Exercise: Define a structure employee with name ,id,designation,age as it's member. And define a function give_struct which will take a array of structure employee data type as it's argument and it will return structure employee with data of employee whose id is 101.Test that function by calling it.


  • Q:Define a structure student with name , rollno, div, age as it's member. And define a function "take_stuct_point" which will take a pointer of array of structure student data type as it's argument and it will return structure student with data of student whose rollno is 10.Test that function by calling it.

    Solution:Structure Student
    struct student  //structure name
    {
        int  rollno; //member1 ,integer variable which will store,
                     //student roll no.
    
        char  name[20]; //member2 a character array of 20 character
                        //which will store name of student.
    
        char  div;  //member3 a character variable,
                  //which will store division of student.
    
        int age;  //member3 a integer variable,
                  //which will store age of student.
    };
    


    Function Definition:
    struct student  take_struct_point(struct student *s1)
    { int i;
        struct student s2;
        for(i=0;i<2;i++)
        {   
            if((s1->rollno) == 10)
            {   
                s2.rollno = s1->rollno;
                strcpy(s2.name,s1->name);
                s2.div = s1->div;
                s2.age = s1->age;
                return s2;
                break;
            }
            s1++;
        }
    
    }
    


    Using above define function:
    #include<stdio.h>
    void main()
    {
    int i;
    //declaration of  variable of
    //structure student data type.
    struct student s1[2] = {{101,"Royol",'A',13},{10,"Roy",'A',18},{15,"Jason",'A',18}};
    struct  student s4,s3;
    s3 = take_struct_point(&s1);
    
      printf("\nName:%s\nAge = %d\nDiv:%c",s3.name,s3.age,s3.div);
    }
    

    Output:
    Name:Roy
    Age = 18
    Div:A

    Exercise: Define a structure employee with name ,id, designation, age as it's member. And define a function "take_stuct_point" which will take a pointer of array of structure employee data type as it's argument and it will return structure employee with data of employee whose id is 101.Test that function by calling it.




  • Try to solve more problems for understanding the concept more better.
  • Doing Programs is the only way of learning Programming
  • So try to do more programs.







People Also Searched: