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 programming.

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

  • Solution:Structure Student
    union student  //union 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.
    union 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 union 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 union 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 15.

    Solution:union Student
    union student  //union 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
    //union student data type.
    union 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:10
    Enter the name of student:James
    Enter the student div:B
    Enter the student age:10


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

    Exercise:Define a union 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 union student with name ,rollno,div,age as it's member. And define a function give_union which will take a array of structure student data type as it's argument and it will return union student with data of student whose rollno is 10.Test that function by calling it.

    Solution:union Student
    union student  //union 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:
    union student  give_union(union student s1[5])
    { int i;
        union 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
    //union student data type.
    union student s3,s1[3]; 
    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);
    
    }
    
    s3 = give_union(s1);   //calling function "give_union"
    
      printf("\nName:%s\nAge = %d\nDiv:%c",s3.name,s3.age,s3.div);
    }
    

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



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


    Name:Roy
    Age = 18
    Div:A

    Exercise: Define a union employee with name ,id,designation,age as it's member. And define a function give_union which will take a array of union employee data type as it's argument and it will return union 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.


Related Concept:



People Also Searched: