Agil Pps Record 8 9 10 Aligned
Agil Pps Record 8 9 10 Aligned
Date:
Aim:
To write a program to illustrate Structures.
Question8(1): Declare a book with <title, float price, int number_of_pages>. Read the
details of a book and print the same.
Algorithm:
Step 1: Start
Step 2: Define the structure book with three fields:
char title[100] for the title, float_price for the price int number_of_pages for the
number of pages.
Step 3: Declare the structure variable book of type struct book.
Step 4: Read user input: Ask the user to enter the title of the book, the price of the book, and
number of pages.
Step 5: Display the book details.
Step 6: End the program by returning 0.
Code:
#include <stdio.h>
#include <string.h>
struct Book {
char title[50];
float price;
int number_of_pages;
};
int main()
{
printf("2127240701006\n");
struct Book book;
printf("Enter book title: ");
fgets(book.title, 50, stdin);
book.title[strcspn(book.title, "\n")] = 0;
printf("Enter book price: ");
scanf("%f", &book.price);
printf("Enter number of pages: ");
scanf("%d", &book.number_of_pages);
printf("\nBook Details:\n");
printf("Title: %s\n", book.title);
printf("Price: %.2f\n", book.price);
printf("Number of Pages: %d\n", book.number_of_pages);
return 0;
}
Output:
Question8(2): Read 2 book details and print the details of the costlier book.
Algorithm:
Step 1: Start
Step 2: Define a structure Book with three numbers:
title(a string to store the title of the book), author(a string to store the author of the
book), price(a float to store the price of the book).
Step 3:Prompt the user to enter details for the first book.
Step 4: Prompt the user to enter details for the second book.
Step 5: Compare the prices of the two books. If the price of the first book is greater print the
details of the first book else print the second one. If the prices are same print both the
books
have same price.
Step 6: Stop
Code:
#include <stdio.h>
#include <string.h>
struct Book {
char title[50];
float price;
int number_of_pages;
};
int main() {printf("2127240701006\n");
struct Book book1, book2;
// Read details of the first book
printf("Enter details of the first book:\n");
printf("Enter title: ");
fgets(book1.title, 50, stdin);
book1.title[strcspn(book1.title, "\n")] = 0;
printf("Enter price: ");
scanf("%f", &book1.price);
printf("Enter number of pages: ");
scanf("%d", &book1.number_of_pages);
getchar(); // Consume the newline character left by scanf()
printf("\nEnter details of the second book:\n");
printf("Enter title: ");
return 'A';
} else if (marks >= 80) {
return 'B';
} else if (marks >= 70) {
return 'C';
} else if (marks >= 60) {
return 'D';
} else {
return 'F';
}
}
int main()
{
printf("2127240701006\n");
struct Student student;
printf("Enter Roll No: ");
scanf("%d", &student.Roll_no);
printf("Enter Name: ");
scanf(" %[^\n]", student.Name); // Read the entire name, including spaces
printf("Enter Gender ((M/F): ");
scanf(" %c", &student.Gender);
printf("Enter marks in 5 subjects:\n");
for (int i = 0; i < 5; i++) {
printf("Subject %d: ", i + 1);
scanf("%d", &student.marks[i]);
student.grades[i] = calculateGrade(student.marks[i]);
}
float sum = 0;
for (int i = 0; i < 5; i++) {
sum += student.marks[i];
}
student.GPA = sum / 5.0;
printf("\nStudent Details:\n");
printf("Roll No: %d\n", student.Roll_no);
printf("Name: %s\n", student.Name);
Reg No: 2127240701006 Page No:
IT22111 – PROGRAM FOR PROBLEM SOLVING LAB
Result:
Thus, the program to illustrate structures were written and executed successfully.
Output:
Question 9(2):
Write a C program, to swap two number using pointers.
Algorithm:
Step 1: Start
Step 2: Declare two integers variables: num1 and num2.
Step 3: Input two integers from the user.
Step 4: Display the values of num1 and num2 before swapping.
Step 5: Call the swap function. Pass the address of num1 and num2. Inside swap, store *a in a
temporary variable temp. Assign the value at b to a. Assign the value in temp to b.
Step 6: Display the values of num1 and num2 after swapping.
Step 7: Stop.
Code:
#include <stdio.h>
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main() {printf("2127240701006\n");
int x = 10, y = 20;
printf("Before swapping:\n");
printf("x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swapping:\n");
printf("x = %d, y = %d\n", x, y);
return 0;
}
Output:
Question 9(3):
Implement a Pocket calculator with arithmetic operators (include
increment/decrement also).
Algorithm:
Step 1: Start
Step 2: Declare a user defined function add, subtract, multiply, divide, increment, decrement
with pointers(*)
Step 3: Create a menu_based system to do the operations.
Step 4: Get the necessary input from the user.
Step 5: Call the functions to do the operations with ampersand(&).
Step 6: Display the output.
Step 7: Stop.
Code:
#include <stdio.h>
#include <math.h>
int main()
{
printf("2127240701006\n");
char operator;
double num1, num2, result;
printf("Enter an operator (+, -, *, /, ^, ++, --): ");
scanf(" %c", &operator);
if (operator == '+' || operator == '-' || operator == '*' || operator == '/' || operator == '^') {
printf("Enter two operands: ");
scanf("%lf %lf", &num1, &num2);
}
else if (operator == '+' || operator == '-') {
printf("Enter an operand: ");
scanf("%lf", &num1);
}
else
{
printf("Invalid operator\n");
return 1;
}
}
switch (operator) {
case '+':
if (operator == '+') {
result = num1 + num2;
} else {
result = num1 + 1; // Increment
}
break;
case '-':
if (operator == '-') {
result = num1 - num2;
} else {
result = num1 - 1; // Decrement
}
}
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 == 0) {
printf("Error: Division by zero\n");
return 1;
}
result = num1 / num2;
break;
case '^':
result = pow(num1, num2);
break;
default:
printf("Invalid operator\n");
return 1;
}
Reg No: 2127240701006 Page No:
IT22111 – PROGRAM FOR PROBLEM SOLVING LAB
printf("%.2lf\n", result);
return 0;
}
Output:
Result:
Thus, the c program to illustrate pointers were written and executed successfully.
int main()
{
printf("2127240701006\n");
FILE *fptr_w, *fptr_r;
char sentence[200], ch;
printf("Enter a sentence: ");
fgets(sentence, sizeof(sentence), stdin);
fptr_w = fopen("out.txt", "w");
if (fptr_w == NULL)
{
printf("Error opening file for writing.\n");
return 1;
}
fprintf(fptr_w, "%s", sentence);
fclose(fptr_w);
fptr_r = fopen("out.txt", "r");
if (fptr_r == NULL)
{
printf("Error opening file for reading.\n");
return 1;
}
printf("Modified content: ");
while ((ch = fgetc(fptr_r)) != EOF)
{
putchar(change_case(ch)); // Change case and print
}
fclose(fptr_r);
return 0;
}
Output:
Question 10(2):
10(2) Read the contents of the file out.txt and write in the console
Algorithm:
Step 1: Start.
Step 2: Declare a file pointer fptr_r and a character variable ch.
Step 3: Open the file out.txt in read mode using fopen.
Step 4: Check if the file was opened successfully:
• If fptr_r == NULL, display an error message and terminate the program.
Step 5: Use a loop to read characters from the file one by one using fgetc.
Step 6: Print each character to the console using putchar.
Step 7: Continue the loop until the end of the file (EOF) is reached.
Step 8: Close the file using fclose.
Step 9: Stop.
Code:
#include <stdio.h>
int main() {
printf("2127240701006\n");
FILE *fptr_r;
char ch;
fptr_r = fopen("out.txt", "r");
if (fptr_r == NULL) {
printf("Error opening file for reading.\n");
return 1;
}
while ((ch = fgetc(fptr_r)) != EOF) {
putchar(ch); // Print each character to the console
}
fclose(fptr_r);
return 0;
}
Output:
10(3) Read <roll, Name,GPA> of a student from console and write into a file student.txt
Algorithm:
Step 1: Start.
Step 2: Declare variables:
• int roll to store the student's roll number.
• char name[50] to store the student's name.
• float GPA to store the student's GPA.
Step 3: Open the file student.txt in write mode using fopen.
Step 4: Check if the file was opened successfully:
• If file == NULL, display an error message and terminate the program.
Step 5: Prompt the user to input the student's details:
• Roll number using scanf.
• Name using fgets (handle leftover newline with getchar).
• GPA using scanf.
Step 6: Write the input data to the file in the format <roll, name, GPA> using fprintf.
Step 7: Close the file using fclose.
Step 8: Display a confirmation message indicating successful file write operation.
Step 9: Stop
.
Code:
#include <stdio.h>
int main()
{
printf("2127240701006\n");
FILE *file;
int roll;
char name[50];
float GPA;
// Open the file in write mode
file = fopen("student.txt", "w");
// Check if the file was opened successfully
if (file == NULL)
{
printf("Error opening file\n");
return 1;
Reg No: 2127240701006 Page No:
IT22111 – PROGRAM FOR PROBLEM SOLVING LAB
}
// Get input from the user
printf("Enter Roll Number: ");
scanf("%d", &roll);
getchar(); // to consume the leftover newline character
printf("Enter Name: ");
fgets(name, sizeof(name), stdin); // Read the name including spaces
printf("Enter GPA: ");
scanf("%f", &GPA);
// Write to the file
fprintf(file, "<%d, %s, %.2f>", roll, name, GPA);
// Close the file
fclose(file);
printf("The content has been written onto the file student.txt\n");
return 0;
}
Output:
10(4) Add a new record-Read from console and append to the file student.txt
Algorithm:
Step 1: Start.
Step 2: Declare variables:
• int roll to store the student's roll number.
• char name[50] to store the student's name.
• float GPA to store the student's GPA.
Step 3: Open the file student.txt in append mode using fopen.
Step 4: Check if the file was opened successfully:
• If fPtr == NULL, display an error message and terminate the program.
Step 5: Prompt the user to input the student's details:
• Roll number using scanf.
• Name using fgets (handle leftover newline with getchar).
• GPA using scanf.
Step 6: Append the new student record to the file in the format <roll, name, GPA> using
fprintf.
Step 7: Close the file using fclose.
Step 8: Display a confirmation message indicating that the new record has been added
successfully.
Step 9: Stop.
Code:
#include <stdio.h>
int main() {
printf("2127240701006\n");
FILE *fPtr;
int roll;
char name[50];
float GPA;
// Open file in append mode
fPtr = fopen("student.txt", "a");
if (fPtr == NULL) {
printf("Error opening file.\n");
return 1;
}
10(5) Retrieve the contents of a student named krishna and print in console
Algorithm:
Step 1: Start.
Step 2: Define a structure student with the following fields:
• char name[100] to store the student's name.
• int roll_no to store the roll number.
• float marks to store the marks.
Step 3: Declare a file pointer fptr_r and a variable s of type struct student.
Step 4: Open the binary file student.bin in read mode using fopen.
Step 5: Check if the file was opened successfully:
• If fptr_r == NULL, display an error message and terminate the program.
Step 6: Use a loop to read each student record from the file using fread.
Step 7: For each record, check if the name matches "Krishna" using strcmp.
• If the name matches:
o Print the student's details: name, roll number, and marks.
o Break the loop as the required student is found.
Step 8: Close the file using fclose.
Step 9: If no matching record is found, print a message indicating that the student was not
found.
Step 10: Stop.
Code:
#include <stdio.h>
#include <string.h>
struct student { printf("2127240701006\n");
char name[100]; // Student name
int roll_no; // Roll number
float marks; // Marks
};
int main() {
FILE *fptr_r;
struct student s;
// Open the binary file for reading
fptr_r = fopen("student.bin", "rb");
if (fptr_r != NULL) {
// Read student record and check if the name is "Krishna"
return 0;
}
Output:
10(6): Write a C program to copy the contents of one file (student.txt) to another file
(new_student.txt).
Algorithm:
Step 1: Start.
Step 2: Declare file pointers fptr1 and fptr2.
Step 3: Open the source file (student.txt) in read mode using fopen.
• If the file cannot be opened, display an error message and terminate the program.
Step 4: Open the destination file (new_student.txt) in write mode using fopen.
• If the file cannot be opened, display an error message, close fptr1, and terminate the
program.
Step 5: Use a loop to read each character from student.txt using fgetc.
Step 6: Inside the loop, write the character to new_student.txt using fputc.
• Continue reading characters until the end of the file (EOF) is reached.
Step 7: Close both files using fclose.
Step 8: Display a success message indicating that the contents were copied successfully.
Step 9: Stop.
Code:
#include <stdio.h>
int main()
{
printf("2127240701006\n");
FILE *fptr1, *fptr2;
char c;
// Open student.txt for reading
fptr1 = fopen("student.txt", "r");
if (fptr1 == NULL) {
printf("Could not open student.txt for reading.\n");
return 1;
}
// Open new_student.txt for writing
fptr2 = fopen("new_student.txt", "w");
if (fptr2 == NULL) {
printf("Could not open new_student.txt for writing.\n");
fclose(fptr1); // Close fptr1 before returning
return 1;
}
// Read each character from student.txt and write it to new_student.txt
c = fgetc(fptr1);
while (c != EOF) {
fputc(c, fptr2);
c = fgetc(fptr1); // Get the next character
}
// Close both files
fclose(fptr1);
fclose(fptr2);
printf("Contents copied successfully.\n");
return 0;
}
Output:
Result:
Thus, the file handling in C program were written and executed successfully.