0% found this document useful (0 votes)
44 views9 pages

C Programs for Structures and Data Types

The document contains multiple C programming problems that demonstrate the use of structures to manage and manipulate data. It includes programs for adding distances, storing employee information, handling student records, and performing operations on complex numbers. Each problem is accompanied by code snippets that illustrate the implementation of the respective functionality.

Uploaded by

manab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views9 pages

C Programs for Structures and Data Types

The document contains multiple C programming problems that demonstrate the use of structures to manage and manipulate data. It includes programs for adding distances, storing employee information, handling student records, and performing operations on complex numbers. Each problem is accompanied by code snippets that illustrate the implementation of the respective functionality.

Uploaded by

manab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

/* Write a C program to add two distances (using a user define data type) entered by user.

Measurement of distance should be in inch and feet. (Note: 12 inches = 1 foot)*/


#include <stdio.h>
int main()
{
int a,b,c,d;
printf("Enter 1st value(feet and inch):");
scanf("%d%d",&a,&b);
printf("Enter 1st value(feet and inch):");
scanf("%d%d",&c,&d);
int e=a*12+c*12+b+d;
int f=e/12;
int g=e%12;
printf("\nSum of two distance=%d feet and %d inch",f,g );
return 0;
}
#include <stdio.h>
struct distance
{
int feet;
int inch;
};
int main()
{
struct distance a,b,c,d;
int e,f,g;
printf("Enter 1st value(feet and inch):\n");
scanf("%d",&[Link]);
scanf("%d",&[Link]);
printf("Enter 1st value(feet and inch):\n");
scanf("%d",&[Link]);
scanf("%d",&[Link]);
e=[Link]*12+[Link]*12+[Link]+[Link];
f=e/12;
g=e%12;
printf("\nSum of two distance=%d feet and %d inch",f,g );
return 0;
}
/*problem-2: Write a c program to define a user define data type “employee” (having fields
emp_no, emp_name, salary) using structure. Take input from the user for 3 employees and
display the data. Use separate function to input data and display data. Use case to choose the
option for the user.*/
#include <stdio.h>
# include<string.h>
struct emp
{
int id;
char name[20];
int sal;
};
struct emp obj[3];

void structfun(struct emp *obj)


{
for(int i=1;i<=3;i++)
{
printf("\nRecord of employee:%d",i);
printf("\nID No. is : %d",obj[i].id);
printf("\nName is : %s",obj[i].name);
printf("\nSalary : %d",obj[i].sal);
printf("\n");
}
}
int main()
{
int i;
for(i=1;i<=3;i++)
{
printf("\nEnter Record of employee:%d",i);
printf("\nEnter Emp ID:");
scanf("%d",&obj[i].id);
printf("Enter Emp name:");
scanf("%s",&obj[i].name);
printf("Enter salary:");
scanf("%d",&obj[i].sal);
}
structfun(obj);
return 0;
}
/*problem-3: Write a c program to Store Information (name, roll and marks) of a Student Using
Structure. Use a pointer variable*/
#include <stdio.h>
struct student
{
char name[10];
int roll;
int marks;
};
struct student std1,*ptr1; // define the variables of the Structure with pointers
int main()
{
// store the address of the std1 structure variable
ptr1 = &std1;
printf (" Enter the name of the student-1: ");
scanf (" %s", &ptr1->name);
printf (" Enter the roll of the student-1: ");
scanf (" %d", &ptr1->roll);
printf (" Enter the marks of the student-1: ");
scanf (" %d", &ptr1->marks);

printf ("\n Display the Details of the Student using Structure Pointer");
printf ("\n Details of the Student-1:\n");
printf(" Name: %s\n", ptr1->name);
printf(" Id: %d\n", ptr1->roll);
printf(" Age: %d\n", ptr1->marks);

return 0;
}
/* Problem-4: Write a c program to Store Information about a person (name, phone, age) by
Passing Structure to a Function (Structure call by value)*/
#include <stdio.h>
struct person
{
char name[20];
int phone;
float age;
};
void func(struct person record);
int main()
{
struct person record;
printf("\nEnter Person Details:\n");
printf("Enter Person name:");
scanf("%s",&[Link]);
printf("Enter Person phone Nuber:");
scanf("%d",&[Link]);
printf("Enter person age:");
scanf("%f",&[Link]);

func(record);
return 0;
}
void func(struct person record)
{
printf(" Details record of person\n");
printf(" Name is: %s \n", [Link]);
printf(" Phone number is: %d \n", [Link]);
printf(" Percentage is: %f \n", [Link]);
}
/* Problem-4: Write a c program to Store Information about a person (name, phone, age) by
Passing Structure to a Function (Structure call by reference)*/

#include <stdio.h>
struct person
{
char name[20];
int phone;
float age;
};
void func(struct person *record);
int main()
{
struct person record;
printf("\nEnter Person Details:\n");
printf("Enter Person name:");
scanf("%s",&[Link]);
printf("Enter Person phone Nuber:");
scanf("%d",&[Link]);
printf("Enter person age:");
scanf("%f",&[Link]);

func(&record);
return 0;
}
void func(struct person *record)
{
printf(" Details record of person\n");
printf(" Name is: %s \n", record->name);
printf(" Phone number is: %d \n", record->phone);
printf(" Percentage is: %f \n", record->age);
}
/* Problem-5:Write a C Program to input your birth day, month and year in numeric
format using structure and display total days as an output.*/
#include <stdio.h>
struct ctd
{
int year;
int month;
int day;
};
struct ctd obj;
int main()
{
int td;
printf("Enter no of Year:\n");
scanf("%d",&[Link]);
printf("Enter no of Month:\n");
scanf("%d",&[Link]);
printf("Enter no of Day:\n");
scanf("%d",&[Link]);
td=365*[Link]+30*[Link]+[Link];
printf("Total day:%d",td);
return 0;
}
/* Prblem-6:Write a program to enter to Cartesian coordinate points and display the distance
between them.*/
#include <stdio.h>
#include<math.h>
float two_dis(float x1, float y1, float x2, float y2){
printf("Enter the value of x1 and y1:\n");
scanf("%f%f",&x1,&y1);
printf("Enter the value of x2 and y2:\n");
scanf("%f%f",&x2,&y2);
float dis = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0);
printf("Distance between 2 points are : %f", dis);
return 0;
}
int main()
{
float x1,x2,y1,y2;
two_dis(x1, y1, x2, y2);
return 0;
}
/*Problem-7: Define a structure “complex” (typedef) to read two complex numbers and
perform addition, subtraction of these two complex numbers and display the result.*/
#include <stdio.h>
struct complex
{
int r, i;
};
int main()
{
struct complex a, b, c,d;
printf("Enter the value a and b of the first complex number (a + ib): ");
scanf("%d%d", &a.r, &a.i);//2,3
printf("Enter the value c and d of the second complex number (c + id): ");
scanf("%d%d", &b.r, &b.i);//4,5
c.r = a.r - b.r;
c.i = a.i - b.i;
d.r=a.r+b.r;
d.i=a.i+b.i;
if (c.i >= 0)
printf("Difference of the complex numbers = %d + %di\n", c.r, c.i);
else
printf("Difference of the complex numbers = %d %di\n", c.r, c.i);
printf("addtion of the complex numbers = %d + %di", d.r, d.i);
return 0;
}

/*Problem-8: Write a program to read Roll No, Name, Address, Age & average marks of 12
students in the BCT class and display the details from function.*/
#include <stdio.h>
struct std
{
int roll;
char name[20];
int age;
int marks;
};
struct std obj[3];
void structfun(struct std *obj)
{
for(int i=1;i<=3;i++)
{
printf("\nRecord of student:%d",i);
printf("\nStd roll No. is : %d",obj[i].roll);
printf("\nStd Name is : %s",obj[i].name);
printf("\nAge is : %d",obj[i].age);
printf("\nStd Marks is : %d",obj[i].marks);
printf("\n");
}
}
int main()
{
int i;
int sum=0;
float avg;
for(i=1;i<=3;i++)
{
printf("\nEnter Record of student:%d",i);
printf("\nEnter std roll:");
scanf("%d",&obj[i].roll);
printf("Enter std name:");
scanf("%s",&obj[i].name);
printf("Enter std Age:");
scanf("%d",&obj[i].age);
printf("Enter std marks:");
scanf("%d",&obj[i].marks);
sum=sum+obj[i].marks;
}
avg=sum/--i;
structfun(obj);
printf("All std average marks is=%f",avg);
return 0;
}

You might also like