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 16Exercise: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 17Exercise: 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:AExercise: 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:AExercise: 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:
- What are structures?
- Why structures?
- Need of Structure
- Concept of structure
- How to initialize an structure variable?
- How to Access Structure member?
- 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
- 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