0% found this document useful (0 votes)
6 views15 pages

CPDSweek 3 Final

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)
6 views15 pages

CPDSweek 3 Final

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
You are on page 1/ 15

Ex.No.

3
Implement C programs using Pointers and Structures
Date:10-9-24

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:
Enter the book name:CPDS
Enter the book author name:Rutherford
Enter the book price:999
Enter the book page no.:569
The Name of the Book is CPDS
The name of the author is Rutherford
The price of the book is 999
The no. of pages is 569

Flowchart
3.a write 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)
auth (char array for author name)
page (integer for number of pages)
price (int 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>
int main() {
struct book{
char bname[100], auth[100];
int page, price;
}b;
printf("Enter the book name:");
scanf("%s",b.bname);
printf("Enter the book author name:");
scanf("%s",b.auth);
printf("Enter the book price:");
scanf("%d",&b.price);
printf("Enter the book page no.:");
scanf("%d",&b.page);
printf("The Name of the Book is %s\nThe name of the author is %s\nThe price of the
book is %d\nThe no. of pages is %d",b.bname,b.auth,b.price,b.page);
return 0;
}
OUTPUT:
Enter the student name:Srivishnu
Enter the Marks:32 45 67
Name : Srivishnu
Total: 144

Flowchart
3.b.Write 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 s 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>
int main(){
struct student{
int m1,m2,m3;
char name[100];

}s;
printf("Enter the student name:");
scanf("%s",s.name);
printf("Enter the Marks:");
scanf("%d%d%d",&s.m1,&s.m2,&s.m3);
printf("Name : %s\nTotal: %d\n",s.name,(s.m1+s.m2+s.m3));
}
OUTPUT:
3 4.0 s
The Size of Integer is 4
The Size of Float is 4
The Size of String is 1

Flowchart
3.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>
int main(){
struct size{
int a;
float b;
char c;
}s;
scanf("%d%f%c",&s.a,&s.b,&s.c);
printf("The Size of Integer is %d\n",sizeof(s.a));
printf("The Size of Float is %d\n",sizeof(s.b));
printf("The Size of String is %d",sizeof(s.c));
}
OUTPUT:
Enter the state name:Tamilnadu
The name of the state is Tamilnadu
Enter the no. of districts:38
No. of districts is 38

Flowchart
3.d.Write 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:
state (char array for state name)
dist (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>
int main(){
union states{
char state[100];
int dist;
}s;
printf("Enter the state name:");
scanf("%s",&s.state);
printf("The name of the state is %s\n",s.state);
printf("Enter the no. of districts:");
scanf("%d",&s.dist);
printf("No. of districts is %d",s.dist);
}
OUTPUT:
Enter the integer : 23
The Size of Integer is 4
Enter the float value : 23.4567
The Size of Float is 4
Enter the character : h
The Size of Character is 1
Flowchart
3.e.Write a C program to demonstrate and display the sizes of different data types
in a union.

AIM:

To write a C program to demonstrate and display the sizes of different data types in a
union.

ALGORITHM:

Step 1: Start
Step 2: Define a union named size with the following members: a
(integer)
b (float)
c (character)
Step 3: Declare a variable s of type union size.
Step 4: Display the size of the integer member using sizeof.
Step 5: Display the size of the float member using sizeof.
Step 6: Display the size of the character member using sizeof.
Step 7: Display the size of the string member using sizeof.
Step 8: Stop

PROGRAM:
#include<stdio.h>
int main(){
union size{
int a;
float b;
char c;
}s;
printf("Enter the integer : ");
scanf("%d",&s.a);
printf("The Size of Integer is %d\n",sizeof(s.a));
printf("Enter the float value : ");
scanf("%f",&s.b);
printf("The Size of Float is %d\n",sizeof(s.b));
printf("Enter the character : ");
scanf("%s",&s.c);
printf("The Size of Character is %d",sizeof(s.c));
}
OUTPUT:
Enter the number of items: 2
Item 1
Enter the Item name: Brush
Enter the quantity: 5
Enter the price: 20
Item 2
Enter the Item name: ToothPaste
Enter the quantity: 2
Enter the price: 45
Item name: Brush
Quantity: 5
Item name: ToothPaste
Quantity: 2
Total: 190
Flowchart
3.f.Write a C program to generate department store bill. Input details of items,
calculate and display the total bill using a structure.
AIM:
To write a C program to input details of items, calculate and display the total bill using a
structure.
ALGORITHM:
Step 1: Start
Step 2: Define a structure named item with the following fields: itname , qua, price
Step 3: Declare an array a of type struct item to hold details for multiple items.
Step 4: Input the number of items n from the user.
Step 5: Use a loop to input details (itname, qua & price) for each item & display the bill
header.
Step 6: Use a loop to display details of each item.
Step 7: Calculate the total cost by summing up the product of quantity and price for each
item.
Step 8: Display the total bill amount.
Step 9: Stop
PROGRAM:
#include <stdio.h>
void main() {
int n;
struct item {
char itname[50];
int qua;
int price;
} a[10];
printf("Enter the number of items: ");
scanf("%d", &n);
int i = 0;
for (i = 0; i < n; i++) {
printf("Item %d\n", i+1);
printf("Enter the Item name: ");
scanf("%s", a[i].itname);
printf("Enter the quantity: ");
scanf("%d", &a[i].qua);
printf("Enter the price: ");
scanf("%d", &a[i].price);}
int total = 0;
for (i = 0; i < n; i++) {
printf("Item name: %s\n", a[i].item_name);
printf("Quantity: %d\n", a[i].qty);
total += (c[i].price * a[i].qty) }
printf("Total: %d\n", total);
}
OUTPUT:
Enter the numbers : 80 90
The sum is 170
Flowchart
3.g.Write a C program that uses pointers to calculate the sum of two numbers.

AIM:

To write a C program that uses pointers to calculate the sum of two numbers.

ALGORITHM:

Step 1: Start
Step 2: Declare integer variables a, b, and sum.
Step 3: Declare pointer variables x and y.
Step 4: Assign the addresses of a and b to the pointers x and y, respectively.
Step 5: Input two numbers from the user and store them in a and b.
Step 6: Calculate the sum of the numbers using the dereferenced pointers x and y.
Step 7: Display the sum.
Step 8: Stop

PROGRAM:
#include <stdio.h>
void main() {
int a, b, sum;
int *x,*y;
printf("Enter the numbers : ");
scanf("%d%d", &a,&b);
x = &a;
y = &b;
sum = *x + *y;
printf("The sum is %d\n", sum);
}

RESULT:

Therefore, programs to Implement C programs using Pointers and Structures is written


and executed.

You might also like