Student Record-File Management Program in C

c-techaravind-student-record-img
                                              To compile and execute the program, copy the code into a text file. Save the text file with the extinction .c (ex. program.c). Save your file in turboc2 or borland (it may your othe c-compiler). Now open you IDE (tc.exe) , goto file->pick file. Your source file will be loaded into IDE. Now you can compile and run the file easily.

C program for append,list,search ,modify,delete a student record in a File.


#include<stdio.h>
#include<conio.h>
void append();
void list();
void search();
void modify();
void del();

struct student
{
int no;
char name[20];
};

void main()
{
int a;
char ch;
clrscr();
do{
printf("\nstudent DATABASE\n\n");
printf("1.Append student Record\n2.List student Record\n3.Modify student Record\n4.Delete student Record\n5.Search student Record\n Enter Choice : ");
scanf("%d",&a);
switch(a)
{
case 1:
append();
break;
case 2:
list();
break;
case 3:
modify();
break;
case 4:
del();
break;
case 5:
search();
break;
default :
printf("Invalid Choice!");
}
printf("\n More Actions ? (Y/N) :");
fflush(stdin);
scanf("%c", &ch);
}while(ch=='y'|| ch=='Y');
}

void append()
{
int i,n;
struct student e;
FILE *fp;
fp=fopen("student.dat", "a");
if(fp==NULL)
{
printf("File Creation Failed!");
exit(0);
}
printf("Enter the nos. of students : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("Enter the student Number : ");
scanf("%d", &e.no);
printf("Enter the student Name : ");
fflush(stdin);
gets(e.name);
printf("\n\n");
fwrite((char *)&e, sizeof(e), 1, fp);
}
fclose(fp);
}

void list()
{
int nofrec=0;
struct student e;
FILE *fp;
fp=fopen("student.dat", "rb");
if(fp==NULL)
{
printf("\n\tFile doesn’t exist!!!\TRY AGAIN");
exit(0);
}
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
nofrec++;
printf("\nstudent Number : %d", e.no);
printf("\nstudent Name : %s",e.name);
printf("\n\n");
}
printf("Total number of records present are : %d", nofrec);
fclose(fp);
}

void modify()
{
int recno, nofrec=0;
char ch;
struct student e;
FILE *fp;
fp=fopen("student.dat", "rb+");
printf("Enter the student Number to modify : ");
scanf("%d", &recno);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
nofrec++;
if(e.no==recno)
{
printf("\nstudent Number : %d", e.no);
printf("\nstudent Name : %s",e.name);
printf("\n");
printf("Do you want to modify this record : ? (Y/N)");
fflush(stdin);
scanf("%c", &ch);
fseek(fp, ((nofrec-1)*sizeof(e)), 0);
if(ch=='Y'|| ch=='y')
{
printf("Enter the student No : ");
scanf("%d",&e.no);
printf("Enter the student Name : ");
fflush(stdin);
gets(e.name);
fwrite((char *)&e, sizeof(e), 1, fp);
printf("Record Modified");
}
else
printf("No modifications were made");
fclose(fp);
}
}
}
void del()
{
int recno;
char ch;
struct student e;
FILE *fp, *ft;
fp=fopen("student.dat", "rb");
ft=fopen("Temp.dat", "wb");
printf("Enter the student Number to delete : ");
scanf("%d", &recno);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
if(e.no==recno)
{
printf("\nstudent Number : %d", e.no);
printf("\nstudent Name : %s",e.name);
printf("\n");
printf("Do you want to delete this record : ? (Y/N)");
fflush(stdin);
scanf("%c", &ch);
}
}
if(ch=='y'||ch=='Y')
{
rewind(fp);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
if(recno!=e.no)
{
fwrite((char *)&e, sizeof(e), 1, ft);
}
}
}
else
printf("No Record was deleted");
fclose(fp);
fclose(ft);
remove("student.dat");
rename("Temp.dat", "student.dat");
}


void search()
{
int s,recno;
char sname[20];
struct student e;
FILE *fp;
fp=fopen("student.dat", "rb");
printf("\n1.Search by Name\n2.Search by student No.\n Enter choice : ");
scanf("%d", &s);
switch(s)
{
case 1:
printf("Enter the student Name to Search : ");
fflush(stdin);
gets(sname);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
if(strcmp(sname,e.name)==0)
{
printf("\nstudent Number : %d", e.no);
printf("\nstudent Name : %s",e.name);
printf("\n");
}
}
break;
case 2:
printf("Enter the student Number to Search : ");
scanf("%d", &recno);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
if(e.no==recno)
{
printf("\nstudent Number : %d", e.no);
printf("\nstudent Name : %s",e.name);
printf("\n");
}
}
break;
}
}




Roots of a given quadratic equation in C

c-techaravind


                                              To compile and execute the program, copy the code into a text file. Save the text file with the extinction .c (ex. program.c). Save your file in turboc2 or borland (it may your othe c-compiler). Now open you IDE (tc.exe) , goto file->pick file. Your source file will be loaded into IDE. Now you can compile and run the file easily.







#include<stdio.h>
#include<math.h>
void main()
{
int A, B, C;
float disc, x1, x2;
clrscr();
gotoxy(31,1);
printf("Root of a quadratic equation");
gotoxy(26,3);
printf("http:\\techaravind.blogsopt.com");
gotoxy(1,5);

printf("\n\n\t ENTER THE VALUES OF A,B,C...");
scanf("%d,%d,%d", &A, &B, &C);
disc=(B * B) - (4 * A * C);
if(disc > 0)
{
printf("\n\t THE ROOTS ARE REAL ROOTS");
x1 = (-B/2 * A) + (sqrt((B * B) - (4 * A * C)) /2 * A);
x2 = (-B/2 * A) - (sqrt((B * B) - (4 * A * C)) / 2 * A);
printf("\n\n\t THE ROOTS ARE...: %f and %f\n", x1, x2);
}
else if(disc == 0)
{
printf("\n\t THE ROOTS ARE REPEATED ROOTS");
x1 = -B/2 * A;
printf("\n\n\t THE ROOT IS...: %f\n", x1);
}
else
printf("\n\t THE ROOTS ARE IMAGINARY ROOTS\n");
getch();
}

Print the series upto 'n' terms: 1 4 27 256.......

c-techaravind


                                              To compile and execute the program, copy the code into a text file. Save the text file with the extinction .c (ex. program.c). Save your file in turboc2 or borland (it may your othe c-compiler). Now open you IDE (tc.exe) , goto file->pick file. Your source file will be loaded into IDE. Now you can compile and run the file easily.






#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i,p;
clrscr();
gotoxy(31,1);
printf("1-4-27 series");
gotoxy(26,3);
printf("http:\\techaravind.blogsopt.com");
gotoxy(1,5);
printf("Enter no. of elements:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p=pow(i,i);
printf("%d",p);
printf(" ");

}
getch();
}

Program to print digits triangle in C

c-techaravind


                                              To compile and execute the program, copy the code into a text file. Save the text file with the extinction .c (ex. program.c). Save your file in turboc2 or borland (it may your othe c-compiler). Now open you IDE (tc.exe) , goto file->pick file. Your source file will be loaded into IDE. Now you can compile and run the file easily.








#include<stdio.h>
#include<conio.h>
void main()
{
int p,q,m,n;
clrscr();
gotoxy(31,1);
printf("Digits triangle");
gotoxy(26,3);
printf("www.techaravind.blogspot.com");
gotoxy(1,5);
printf("Enter no.of lines:");
scanf("%d",&n);
 for(p=1;p<=n+1;p++)
{
 for(q=1;q<=n+1-p;q++)
 printf(" ");
m=1;
for(q=1;q<p;q++)
printf("%d",m++);
m=m-1;
for(q=2;q<p;q++)
printf("%d",--m);
printf("\n");
}
     getch();
}

Swaping to two numbers in c

c-techaravind


                                              To compile and execute the program, copy the code into a text file. Save the text file with the extinction .c (ex. program.c). Save your file in turboc2 or borland (it may your othe c-compiler). Now open you IDE (tc.exe) , goto file->pick file. Your source file will be loaded into IDE. Now you can compile and run the file easily.



Swaping of two numbers:

swaping means changing the content of A memory location to B and vice versa. In programing we use a 3rd temporary to change the content.




#include <stdio.h>
#include <conio.h>
main()
{
int a, b, temp;
clrscr();
gotoxy(31,1);
printf("Swaping Of Two Numbers");
gotoxy(26,3);
printf("http:\\techaravind.blogsopt.com");
gotoxy(1,5);
printf("\nEnter any two numbers: ");
scanf("%d %d", &a, &b);
printf("\n\nBefore Swapping:\n");
printf("\na = %d\n", a);
printf("\nb = %d\n", b);
temp = a;
a = b;
b = temp;
printf("\n\nAfter Swapping:\n");
printf("\na = %d\n", a);
printf("\nb = %d\n", b);
getch();
}

Find the nth Fibonacci c series in c



                                              To compile and execute the program, copy the code into a text file. Save the text file with the extinction .c (ex. program.c). Save your file in turboc2 or borland (it may your othe c-compiler). Now open you IDE (tc.exe) , goto file->pick file. Your source file will be loaded into IDE. Now you can compile and run the file easily.



Fibonacci series: Fibonacci series is  series obtained by adding a number with its previous number.


#include<conio.h>
#include<stdio.h>
void main()
{
int f1 = 0, f2 = 1, f3, n;
clrscr();
gotoxy(31,1);
printf("fibbonic series");
gotoxy(26,3);
printf("http:\\techaravind.blogsopt.com");
gotoxy(1,5);
printf("Program for Fibonacci Series\n");
printf("Enter the maximum number for Fibonacci Series: ");
scanf("%d", &n);
printf("\nPrinting Fibonacci Series from 0 - %d\n", n);
printf("%d\n%d\n", f1, f2);
while(1)
{
f3 = f1 + f2;
if(f3 > n)
break;
printf("%d\n", f3);
f1 = f2;
f2 = f3;

}
getch();
}



Menu driven program for simple string operation in c



                                              To compile and execute the program, copy the code into a text file. Save the text file with the extinction .c (ex. program.c). Save your file in turboc2 or borland (it may your othe c-compiler). Now open you IDE (tc.exe) , goto file->pick file. Your source file will be loaded into IDE. Now you can compile and run the file easily.






#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
int
main()
{
int n;
char str1[20],str2[20];
clrscr();
gotoxy(31,1);
printf("String Menu");
gotoxy(26,3);
printf("http:\\techaravind.blogsopt.com");
gotoxy(1,5);
while(1)
{
printf("\n 1)Length of the string
\n2)concadination of the string
\n3)substring\n
4)reversal of string
\n5)exit");
printf("\n Enter your choice:");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\nEnter the string");
gets(str1);
printf("stringlength is:%d",strlen(str1));
break;

case 2:
printf("\nEnter the string1");
gets(str1);
printf("\nEnter the string2");
gets(str2);
printf("\nThe conctinated sting is: %s",strcat(str1,str2));
break;

case 3:
printf("\nEnter the string1");
gets(str1);
printf("\nEnter the string2");
gets(str2);
printf("the substing is:%s",strstr(str1,str2));
break;

case 4:
printf("\nEnter the string1");
gets(str1);
printf("\n The reverce of the strint is");
break;

case 5:
exit(0);
}
}




 
Etutos © 2010-2011