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 16Exercise: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 16Exercise: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:AExercise: 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:
- How to initialize an structure variable?
- How to Access Structure member?
- What are Nested Structure?
- How to built an Nested Structure?
- How to Access members of Nested Structure?
- How to initialize Nested structure?
- Functions and Structure
- How to pass a structure variable to function?
- How to make function which will take structure variable as arguments?
- How to make function which will return structure variable?
- Array and Structure
- How to make array of structure variable?
- How to initialize array structure variable?
- How to Access members of an array of structure?
- How to take members of array structure as input from the user?
- Pointer and Structure
- How to Point a structure variable?
- How to declare a pointer of structure variable?
- How to Initialize an pointer of structure variable?
- How to access member of structure variable which is point by an pointer?
- What is '->' Operator?
- How to allocate memory for structure variable Dynamically
- How to allocate memory of structure variable using malloc() and calloc() function
- Self Referential Block
- What is Referential Structure?
- How to Create self Referential Structure?
- Applications of Self Referential Structures
- Practice Programs
People Also Searched:
- What are structures?
- Why structures?
- Need of Structure
- Concept of structure
- Array and Pointer
- What is array of pointer?
- how to initialize an array of pointers?
- How to Access elements of an Array Of Pointers?
- Functions and Pointers
- How to pass a Pointer To a Function?
- Call By reference
- Functions In C
- Why Functions?
- What are Functions?
- how to write our functions?
- What are programmer define functions?
- How to declare function?
- How to define a function?
- What is function Prototype?
- how to declare a function?
- what are Actual Parameters?
- What are Formal Parameters?
- Function Call?
- how to call a Function?
- What are different types of calling function?
- Call by value
- call by reference
- Recursion
- Array and Functions
- How to pass an array to a function?
- What is Character array
- Functions for Characters
- Functions from #include<ctype.h> Header File
- what are isdigit(), islower() , isupper(),etc
- what are various string handling functions
- Functions from #include<string.h> Header File
- what are strcpy(), strcmp(), strncmp(), strcmpi(),etc
- strcmp() VS strncmp() VS strcmpi()
- Input And Output Functions
- How to take strings As Input
- What is gets() functions
- What is puts() function
0 Comments
Post a Comment