Ex. No: 7 Generating A Transcript With CGPA and GRADE: Program
Ex. No: 7 Generating A Transcript With CGPA and GRADE: Program
AIM: To write a C program that generating a transcript with CGPA and GRADE
ALGORITHM:
Step 1: Start.
Step 2: Define a structure Student with:name (array of characters to store the
student's name),roll_no (integer to store the roll number),marks[5] (array of 5
floating-point numbers to store marks for 5 subjects),CGPA (floating-point
number to store the calculated CGPA),grade (character to store the calculated
grade)
Step 3: Input Student Details:Prompt the user to enter the student's name.
Use fgets to read the input string, including spaces.Remove any newline
character at the end of the name using strcspn.Prompt the user to enter
the student's roll number.Prompt the user to enter marks for 5 subjects
(assume marks are out of 10).For each subject, read the floating-point
number representing the marks.
Step 4: Calculate CGPA and Grade:Initialize a variable total_marks to 0.
Loop through the marks array (5 subjects) and sum up all the marks.
Calculate the CGPA as total_marks / 50 (since each subject is out of 10 and there are 5
subjects).Determine the grade based on the CGPA:
If CGPA >= 9, assign grade 'A'.
If CGPA >= 8, assign grade 'B'.
If CGPA >= 7, assign grade 'C'.
If CGPA >= 6, assign grade 'D'.
Otherwise, assign grade 'F'.
Step 5: Display Transcript:
Print the student's name.
Print the student's roll number.
Print the marks for each of the 5 subjects.
Print the CGPA.
Print the grade.
Step 6: Stop.
PROGRAM:
#include <stdio.h>
int main() {
struct Student student;
return 0;
}
O/P:
Enter the student's name: John Doe
Enter the student's roll number: 101
Enter marks for 5 subjects (out of 10):
Subject 1: 8
Subject 2: 9
Subject 3: 7
Subject 4: 6
Subject 5: 8
RESULT:
Thus, the C program that generating a transcript with CGPA and GRADE
EX.NO:8 Demonstrate file operations
8a. Count number of characters in the file
AIM:
To write a C program to Count number of characters in the file.
ALGORITHM:
Step 1: Start.
Step 2: Input the filename.
Step 3:Open the file for reading (fopen(filename, "r")).
Step 4:If the file can't be opened, print an error message and End.
Step 5:If the file is opened successfully, read the file character by character using getc.
Step 6:Increment the character count for each character read.
Step 7:When the end of the file (EOF) is reached, close the file (fclose(fp)).
Step 8:Print the total count of characters in the file.
Step 9: Stop.
PROGRAM:
#include<stdio.h>
#define MAX_FILE_NAME 100
int main()
{
FILE* fp;
int count = 0;
char filename[MAX_FILE_NAME];
char c;
printf("Enter file name: ");
scanf("%s", filename);
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Could not open file %s",filename);
return 0;
}
for (c = getc(fp); c != EOF; c = getc(fp))
count = count + 1;
fclose(fp);
printf("The file %s has %d characters\n ",filename, count);
return 0;
}
Output:
Enter file name: Empdetails.txt
The file Empdetails.txt has 26 characters
RESULT:
Thus, the C program for Counting the number of characters in the file has been executed and
verified successfully.
8b. Replace a specific word with the given word in the same file
AIM:
To write a C program to Replace a specific word with the given word in
the same file
ALGORITHM:
Step 1: Start.
Step 2: nput filename, oldWord, newWord.
Step 3: Open the file for reading and the temporary file for writing Step
Step 4: Read the original file line by line.
For each line:
Search for occurrences of oldWord.
Step 5: If oldWord is found, replace it with newWord in the temporary file.
Step 6: If oldWord is not found, copy the character from the original file to the temporary file.
Close the original file and the temporary file.
Step 7: Delete the original file and rename the temporary file to the original filename.
Step 8: Print a success message
PROGRAM:
#include <stdio.h>
#include <string.h>
int main() {
FILE *file;
char filename[100];
char oldWord[100], newWord[100];
return 0;
}
O/P:
Hello world!
This is a test file. Hello again!
Hi world!
This is a test file. Hi again!
RESULT:
Thus, the C program for Replacing a specific word with the given word in the file has been
executed and verified successfully.