You are on page 1of 14

# Assignment on structure and file

handling

Name: voore abhilash

Roll no: 17110077

Department : Textile technology

Carry student

Q1: Define a data type for storing complex number and implement

addition, subtraction, multiplication and modulus operations for the

defined data type.

#include<stdio.h>#i

nclude<math.h>

struct complex{ int

r,c;

};

int main()

struct complex a,b,c;


printf("enter the 1st complex no. a+bi=");

scanf("%d%d",&a.r,&a.c);

printf("\nenter the 2nd complex no. c+di="); scanf("%d%d",&b.r,&b.c);

printf("\ncomplex no.1=%d+%di",a.r,a.c);

printf("\ncomplex no.2=%d+%di",b.r,b.c);

c.r=a.r+b.r;

c.c=a.c+b.c;

printf("\nsum = %d+%di ",c.r,c.c);

c.r=a.r-b.r;

c.c=a.c-b.c;
printf("\nsubtraction = %d+%di ",c.r,c.c);
c.r=a.r*b.r-a.c*b.c;

c.c=a.r*b.c+a.c*b.r;

printf("\nmultiplication = %d+%di ",c.r,c.c);

c.r=pow((a.r*a.r+a.c*a.c),0.5);

c.c=pow((b.r*b.r+b.c*b.c),0.5);

printf("\nmodulus of complex no.1 = %d ",c.r);

printf("\nmodulus of complex no.2 = %d ",c.c);

return0;

Output:

Enter the 1st complex no. a+ib = 4 5

Enter the 2nd complex no. c+id = 6 8

Complex no 1. 4+5i
Complex no 2. 6+8I

Sum = 10+13i

Subtraction = -2-3i

Multiplication = -16+62i

Modulus of complex no 1. = 5

Modulus of complex no 2. = 10
Q2: Create a structure to specify data of customers in a bank. The data to

be stored is: Account number, Name, Balance in account.

I. Write a function to print the Account number and name of each

customer with balance below Rs.100

II. If a customer requests for withdrawal or deposit, it is given in the

form: Account Number, Amount, Code (1 for deposit, 0 for

withdrawal) If on withdrawal the balance falls below Rs. 100 then

program give a message ” InsufficientBalance”.

#include<stdio.h>
structdata_of_customers{ intacc;
char name[10];

int bal;

}typedef doc;

void print(doc b[],int n)

{ int i;

printf("\nthe customers whose balance in account is less than 100 are :");

for(i=0;i<n;i++)

if(b[i].bal<100)

printf("\ncustomer %d with acc. no.=%d and name=

%s.",i+1,b[i].acc,b[i].name);

main()

int n,i;

printf("enter the total no. of customers =");

scanf("%d",&n);

doc a[n];

printf("\nenter account no.,name,and balance of %d customers\n",n);

for(i=0;i<n;i++)
{

printf("customer %d =",i+1); scanf("%d%s

%d",&a[i].acc,a[i].name,&a[i].bal);

print(a,n);

int z;

printf("\n\nif you want withdrawal press 0,if you want to deposit press 1 = ");

scanf("%d",&z);

int p,q,r; if(z)

printf("\nenter your accountno.=");

scanf("%d",&p);

printf("\namount you want todeposit=");

scanf("%d",&q);

printf("\nenter your code=");

scanf("%d",&r); for(i=0;i<n;i+

+)

if(p==a[i].acc)

printf("now your total balance is %d ",a[i].bal+q);

else

printf("\nenter your accountno.=");

scanf("%d",&p);

printf("\nenter the amount ofwithdrawal=");

scanf("%d",&q);

printf("\nenter your code=");

scanf("%d",&r); for(i=0;i<n;i+

+)
{

if(p==a[i].acc)

if(a[i].bal<100)
printf("\nINSUFFICIENT BALANCE ,you can't withdraw as your balance is

less than 100");

else

printf("\nnow your balance is %d ",a[i].bal-q);

}} OUTPUT:

Enter the total no. of customers = 3

Enter account no. , name, and balance of 3 customers

Customer 1 = 350265427 Ashok Kumar Customer 2 =

652854692 Dhoni 500

Customer 3 =435789320 Siddhartha 1010

The customer whose balance in account is less than 100 are :

Customer 1 with acc . no = 350265427and name = Ashok Kumar

If you want withdrawal press 0,

If you want to deposit press 1 = 0

Enter your account no. =350265427

Enter the amount of withdrawal =101

Enter the code =6524

INSUFFICIENT BALANCE ,you can't withdraw as your balance is less than

100

Q3: Create a structure Employee1 that holds the information- Id


(integer), Name (character []), Salary (long).Create a union Employee2

that also holds the same information as well as same data type as that

of structure Employee1. Write a program in C to find and display the

size of structure Employee1 and union Employee2.

#include<stdio.h>

structemployee1

int id;

char name[10];

long salary;

}typedef emp1;

union

employee2{ int id;

char name[10];

long salary;

}typedef emp2;

main()

printf("\nsize of structure employee 1 is %d bytes",sizeof(emp1));

printf("\nsize of the union employee 2 is %d bytes",sizeof(emp2));

}return 0;}

Output :

Size of structure employee 1 is 20 bytes

Size of structure employee 2 is 12 bytes

Q4: Create a structure called Library to hold accession number

title of the book, author name, price of the book and flag

indicating whether book is issued or not. Write a menu driven

program that implements the working of a library. The menu options

should be:
I. Add bookinformation

II. Display bookinformation

III. List the count of books in alibrary

IV. List all books of givenauthor

V. Exit

#include<stdio.h>#i

nclude<string.>

structlibrary{

int acc_no; char

title[20];

char author[10];

int flag;}b[10];

int i=0; intmain()

{ int z;

printf("\n1.Add book information");

printf("\n2.Display book information");

printf("\n3.List the count of books in a library");

printf("\n4.List all books of given author");

printf("\n5.Exit ");

printf("\nenter your choice = ");

scanf("%d",&z);

switch(z)

case 1:

printf("\n*enter the accession no. of the book=");

scanf("%d",&b[i].acc_no);

printf("*enter the title of the book=");


getchar();

gets(b[i].title);

printf("*enter the author name of the book=");

gets(b[i].author);
printf("*if book is issued enter 1 else enter 0="); scanf("%d",&b[i].flag);

i++;

goto start;

case 2:int j;

for(j=0;j<i;j++)

printf("\nbook information of %d book are :",j+1);

printf("\n#accession no.=%d",b[j].acc_no);

printf("\n#book title =%s",b[j].title);

printf("\n#author name =%s",b[j].author);

if(b[j].flag)

printf("\n#book is issued\n");

else

printf("\n#book isn't issued\n");

gotostart;

case 3:printf("\ntotal no. of books in the library =%d",i);

gotostart;

case 4:char au[10];

printf("\nenter name of the author whose book you want

="); getchar();

gets(au);

int k;

for(k=0;k<i;k++)

if(strcmp(b[k].author,au)==0)

printf("\nbook title of this author =%s",b[k].title);

goto start;

case 5:printf("\nTHANK YOU");

}
Return 0;

Output :

All three are the outputs of the same program

1.Add book information

2. display bookinformation

3. List the count of the book in alibrary

4.List all books of givenauthor

5.Exist

Enter your choice = 1

*Enter the accession no. of the book = 50

*Enter the title of the book = stage burn

*Enter the author name of the book = kim namjoon

*If book is issued enter 1, else enter 0 = 0

1.Add book information

2. Display bookinformation

3. List the count of the book in alibrary

4.List all books of givenauthor

5.Exist

Enter your choice =2

*Enter the accession no.of the book = 10

*Enter the title of the book = bring the soul

*Enter the author name of the book = kim seokjin

*If book is issued enter1, else enter 0 =1

1.Add book information

2.Display bookinformation

3.List the count of the book in alibrary


4 .List all books of givenauthor
5.Exist
Enter your choice =2
#accesion no. = 2
#book title = kim taehyung
#author name = min yoongi
#book is issued

1.Add book information

2.Display book information

3.List all booksofgivenauthor

4.List all books of givenauthor

5.Exist

Enter your choice = 3

Total no . of book in the library =

10

1.Add book information

2.Display bookinformation

3.List the count of the book in alibrary

4.List all books of givenauthor

5.Exist

Enter your choice = 4

Enter the name of the author whose book you want = Jeon jungkook

Book title of the author = park jimin

1.Add book information

2.Display book information

3.list the count of the book in a library

4.list all books of given author

5.Exist

Enter your choice =5

THANK YOU

5.A program that will read a file and count how manycharacters,

spaces, tabs and newlines are present init.

Solution :

In this example we are opening the assignment of structure


and file handling

#include <stdio.h>

int main()

char in_name[80];

FILE *in_file;

int ch, character = 0, line = 0, space = 0, tab = 0;

printf("Enter file name:\n");

gets(in_name);

in_file = fopen(in_name, "r"); if

(in_file == NULL)

printf("Can't open %s for reading.\n", in_name);

else

while ((ch = fgetc(in_file)) != EOF)

character++;
if (ch == ' ')

space++;

if (ch == '\n')

line++;

if (ch == '\t')

tab++;

fclose(in_file);

printf("\nNumber of characters = %d", character);

printf("\nNumber of spaces = %d", space);

printf("\nNumber of tabs = %d", tab);

printf("\nNumber of lines = %d", line);

}.

Output:
Number of characters = 1376

Number of spaces = 210

Number of tabs = 0

Number of lines = 28

5.Write a program to receive detail (like name, roll no. etc) of

students and write them into file. Also display these recordsof

students onscreen.

Solution :

#include<stdio.h>#

include<stdlib.>

FILE*fptr;

intmain()

char name[50];

int marks,i,n;

printf("Enter number of students: ");

scanf("%d",&n);

fptr=(fopen("C:\\student.txt","a"));

if(fptr==NULL)

Up

{ printf("Error!");

exit(0);

for (i=0; i<n; ++i)

printf("For student%d\nEnter name: ",i+1);

scanf("%s",name);

printf("Enter marks: ");

scanf("%d",&marks);
fprintf(fptr,"\nName: %s \nMarks=%d \n",name,marks);

fclose(fptr); return

0;

Output :

Enter number of students : 3

For student 1

Enter name : Dhoni

Enter marks : 98

For student 2

Enter name : Siddhartha

Enter marks : 95

For student 3

Enter name : Ashok Kumar

Enter marks = 90

#include <stdio.h>

int main()
{

char ch;

FILE *fptr;

fptr=(fopen("C:\\student.txt","r"));

if(fptr == NULL)

printf("Unable to open file.\n");

exit(0);

printf("File opened successfully. Reading file contents character by character.\n\n");

do

ch = fgetc(fptr);
putchar(ch);

while(ch !=EOF);

fclose(fptr);

return0;

Output :

Unable to open file

You might also like