Assignment On Structure and File Handling
Assignment On Structure and File Handling
handling
Carry student
Q1: Define a data type for storing complex number and implement
#include<stdio.h>#i
nclude<math.h>
r,c;
};
int main()
scanf("%d%d",&a.r,&a.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;
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;
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);
return0;
Output:
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
#include<stdio.h>
structdata_of_customers{ intacc;
char name[10];
int bal;
}typedef doc;
{ 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)
%s.",i+1,b[i].acc,b[i].name);
main()
int n,i;
scanf("%d",&n);
doc a[n];
for(i=0;i<n;i++)
{
%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);
scanf("%d",&p);
scanf("%d",&q);
scanf("%d",&r); for(i=0;i<n;i+
+)
if(p==a[i].acc)
else
scanf("%d",&p);
scanf("%d",&q);
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
else
}} OUTPUT:
100
that also holds the same information as well as same data type as that
#include<stdio.h>
structemployee1
int id;
char name[10];
long salary;
}typedef emp1;
union
char name[10];
long salary;
}typedef emp2;
main()
}return 0;}
Output :
title of the book, author name, price of the book and flag
should be:
I. Add bookinformation
V. Exit
#include<stdio.h>#i
nclude<string.>
structlibrary{
title[20];
char author[10];
int flag;}b[10];
{ int z;
printf("\n5.Exit ");
scanf("%d",&z);
switch(z)
case 1:
scanf("%d",&b[i].acc_no);
gets(b[i].title);
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("\n#accession no.=%d",b[j].acc_no);
if(b[j].flag)
printf("\n#book is issued\n");
else
gotostart;
gotostart;
="); getchar();
gets(au);
int k;
for(k=0;k<i;k++)
if(strcmp(b[k].author,au)==0)
goto start;
}
Return 0;
Output :
2. display bookinformation
5.Exist
2. Display bookinformation
5.Exist
2.Display bookinformation
5.Exist
10
2.Display bookinformation
5.Exist
Enter the name of the author whose book you want = Jeon jungkook
5.Exist
THANK YOU
5.A program that will read a file and count how manycharacters,
Solution :
#include <stdio.h>
int main()
char in_name[80];
FILE *in_file;
gets(in_name);
(in_file == NULL)
else
character++;
if (ch == ' ')
space++;
if (ch == '\n')
line++;
if (ch == '\t')
tab++;
fclose(in_file);
}.
Output:
Number of characters = 1376
Number of tabs = 0
Number of lines = 28
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;
scanf("%d",&n);
fptr=(fopen("C:\\student.txt","a"));
if(fptr==NULL)
Up
{ printf("Error!");
exit(0);
scanf("%s",name);
scanf("%d",&marks);
fprintf(fptr,"\nName: %s \nMarks=%d \n",name,marks);
fclose(fptr); return
0;
Output :
For student 1
Enter marks : 98
For student 2
Enter marks : 95
For student 3
Enter marks = 90
#include <stdio.h>
int main()
{
char ch;
FILE *fptr;
fptr=(fopen("C:\\student.txt","r"));
if(fptr == NULL)
exit(0);
do
ch = fgetc(fptr);
putchar(ch);
while(ch !=EOF);
fclose(fptr);
return0;
Output :