Program To Read File Containing Rollno, Name, Marks of Three Subjects and Calculate Total Marks, Result in Grade and Store in File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Program to read file containing rollno,name,marks of three subjects

and calculate total marks,result in grade and store in file

#include<stdio.h>

#include<conio.h>

int k=0;

struct stud

int rn;

char name[30];

int m1,m2,m3,total;

float avg;

char grade,result;

s[30];

void main()

int no,roll=101,i;

FILE *fptr;

clrscr();

printf("Enter NO of Students:");

scanf("%d",&no);

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

if(fptr==NULL) {

printf("Error!");
exit(1);

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

clrscr();

s[k].rn=roll;

printf("\nEnter the Student Roll Number: %d ",s[k].rn);

printf("\nEnter the Student Name:");

fflush(stdin);

gets(s[k].name);

printf("\nEnter the Three Marks");

scanf("%d",&s[k].m1);

scanf("%d",&s[k].m2);

scanf("%d",&s[k].m3);

if(s[k].m1>=35 && s[k].m2>=35 && s[k].m3>=35)

s[k].result='P';

else

s[k].result = 'F';

s[k].total = s[k].m1+s[k].m2+s[k].m3;

printf("The Total is : %d",s[k].total);

s[k].avg=s[k].total/3;
if(s[k].avg>=60)

if(s[k].result == 'P')

s[k].grade = 'A';

else

s[k].grade = 'N';

else if(s[k].avg>=50)

if(s[k].result == 'P')

s[k].grade = 'B';

else

s[k].grade = 'N';

else if(s[k].avg>=35)

if(s[k].result == 'P')
{

s[k].grade = 'C';

else

s[k].grade = 'N';

getch();

k++;

roll++;

printf("\n*******************************************************");

printf("\n STUDENT MARKLIST ");

printf("\n*******************************************************");

printf("\nROLL \tNAME MARK1 MARK2 MARK3 TOTAL RESULT Average GRADE");

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

printf("\n%d\t%s %d %d %d %d %c %0.1f
%c",s[i].rn,s[i].name,s[i].m1,s[i].m2,s[i].m3,s[i].total,s[i].result,s[i].avg,s[i].grade);

fprintf(fptr,"\nROLL=%d \tNAME=%s MARK1=%d MARK2=%d MARK3=%d TOTAL=%d RESULT=%c


Average=%0.1f GRADE=%c",s[i].rn,s[i].name,s[i].m1,s[i].m2,s[i].m3,s[i].total,s[i].result,s[i].avg,s[i].grade);

fclose(fptr);

getch();

}
Program to read electricity previous meter reading,current meter reading and total units with rate
calculation and writing it to a file

#include <stdio.h>

#include <conio.h>

void main()

int previousreading;

int currentreading;

int unit;

int amount;

FILE *fptr;

clrscr();

printf("Enter the previous meter reading : ");

scanf("%d" ,&previousreading);

printf("Enter the current meter reading : ");

scanf("%d" ,&currentreading);

fptr=(fopen("C:\\reading.txt","w"));

if(fptr==NULL) {

printf("Error!");

exit(1);

unit = currentreading - previousreading;

if(unit<=50)

{
amount = unit * 1;

else if(unit>50 && unit <=100)

amount = unit * 1.5;

else if(unit>100)

amount = unit * 2;

printf("Total amount of Electricity bill is %d",amount);

fprintf(fptr,"\nTotal amount of Electricity bill=%d",amount);

fclose(fptr);

getch();

Program to read no of words in a each sentence and display no of words and sentence and compute
average no of words per sentence

#include<stdio.h>

#include<conio.h>

void main()

FILE *p;

char ch;

int w=1;

clrscr();

p=fopen("c:\\RESULT.txt","r");
if(p==NULL)

printf("file not found");

else

ch=fgetc(p);

while(ch!=EOF)

printf("%c",ch);

if(ch==' '||ch=='\n')

w++;

ch=fgetc(p);

printf("\nWords in a file are=%d",w);

fclose(p);

getch();

Program to read Student Marks, Rollno, Name and grades and store only failed result in a file

#include <stdio.h>
#include <conio.h>

int main(){

int num,rollno;

char name[30];

FILE *fptr;

clrscr();

printf("Enter your student Name: ");

scanf("%s",&name);

printf("Enter your Rollno: ");

scanf("%d",&rollno);

printf("Enter your mark: ");

scanf("%d",&num);

printf(" You entered: %d\n", num); // printing outputs

fptr=(fopen("C:\\failed.txt","w"));

if(fptr==NULL) {

printf("Error!");

exit(1);

if(num >= 80){

printf(" You got A grade"); // printing outputs

else if ( num >=60){ // Note the space between else & if

printf(" You got B grade");

}
else if ( num >=40){

printf(" You got C grade");

else if ( num < 40){

printf(" You Failed in this exam");

fprintf(fptr,"\nName=%s \nRollno=%d \nMark=%d",name,rollno,num);

fclose(fptr);

getch();

return 0;

You might also like