[Link].
3 Implement C programs using Pointers and Structures
Date:
Write a C program
a) To input and display the details of a book using a structure.
b) To input and display the details of a student and calculate the total marks using a
structure.
c) To input and display the sizes of different data types in a structure
d) To demonstrate the usage of a union for storing state information and district
number.
e) To demonstrate and display the sizes of different data types in a union. f) To
generate department store bill. Input details of items, calculate and display the total
bill using a structure.
g) To calculate the sum of two numbers using pointers
OUTPUT:
[Link] a C program to input and display the details of a book using a structure.
AIM:
To write a C program to input and display the details of a book using a structure.
ALGORITHM:
Step 1: Start
Step 2: Define a structure named book with the following fields:
bname (char array for book name)
a_name (char array for author name)
page (integer for number of pages)
price (float for price of the book)
Step 3: Declare a variable b of type struct book.
Step 4: Input book name using scanf.
Step 5: Input author name using scanf.
Step 6: Input number of pages using scanf.
Step 7: Input price of the book using scanf.
Step 8: Display the details of the book using printf.
Step 9: Stop
PROGRAM:
#include<stdio.h>
void main()
{
struct book
{
char bname[70];
char a_name[70];
int page_no;
float price;
}m;
printf("Enter the book name:");
scanf("%s",&[Link]);
printf("Enter the author name:");
scanf("%s",&m.a_name);
printf("Enter number of pages:");
scanf("%d",&m.page_no);
printf("Enter the price of book:");
scanf("%f",&[Link]);
printf("The book %s written by %s has %d pages which costs about %f
rupees",[Link],m.a_name,m.page_no,[Link]);
}
INFERENCE:
RESULT:
OUTPUT:
[Link] a C program to input and display the details of a student and calculate the
total marks using a structure.
AIM:
To write a C program to input and display the details of a student and calculate the total
marks using a structure.
ALGORITHM:
Step 1: Start
Step 2: Define a structure named students with the following fields:
name (char array for student name)
m1 (integer for marks in subject 1)
m2 (integer for marks in subject 2)
m3 (integer for marks in subject 3)
Step 3: Declare a variable a of type struct students.
Step 4: Input student name using scanf.
Step 5: Input marks for subject 1 using scanf.
Step 6: Input marks for subject 2 using scanf.
Step 7: Input marks for subject 3 using scanf.
Step 8: Calculate the total marks as the sum of m1, m2, and m3.
Step 9: Display the total marks using printf.
Step 10: Stop
PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
{
int total;
struct marks
{
int m1, m2,m3;
char name [50];
}m;
printf ("Enter student name:");
scanf("%s", &[Link]);
printf ("Enter marks:");
scanf("%d %d %d",&m.m1,&m.m2,&m.m3);
total=m.m1+m.m2+m.m3;
printf("Total marks scored by %s is %d", [Link],total);
}
INFERENCE:
RESULT:
OUTPUT:
C. Write a C program to input and display the sizes of different data types in a
structure.
AIM:
To write a C program to input and display the sizes of different data types in a structure.
ALGORITHM:
Step 1: Start
Step 2: Define a structure named size with the following fields:
a (integer)
b (character)
c (float)
Step 3: Declare a variable s of type struct size.
Step 4: Input a character value using scanf.
Step 5: Input an integer value using scanf.
Step 6: Input a float value using scanf.
Step 7: Display the size of the integer field using sizeof.
Step 8: Display the size of the character field using sizeof.
Step 9: Display the size of the float field using sizeof.
Step 10: Stop
PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
{
struct size
{
int a;
float b;
char c;
}m;
printf ("Enter integer, float, character:");
scanf("%d %f %c",&m.a,&m.b,&m.c);
printf("The size of integer is %d\nThe size of float is %d\nThe size of character is %d",
sizeof(m.a),sizeof(m.b),sizeof(m.c));
}
INFERENCE:
RESULT:
OUTPUT:
[Link] a C program to demonstrate the usage of a union for storing state
information and district number.
AIM:
To write a C program to demonstrate the usage of a union for storing state information
and district number.
ALGORITHM:
Step 1: Start
Step 2: Define a union named state with the following members:
statename (char array for state name)
districtno (integer for number of districts)
Step 3: Declare a variable s of type union state.
Step 4: Input the state name using scanf.
Step 5: Display the state name using printf.
Step 6: Input the number of districts using scanf.
Step 7: Display the number of districts using printf.
Step 8: Stop
PROGRAM:
#include <stdio.h>
void main()
{
union book
{
char state [50];
char district[50];
}b;
printf("enter state name:");
scanf("%s",&[Link]);
printf("enter no of district:");
scanf("%s",&[Link]);
}