0% found this document useful (0 votes)
14 views26 pages

Agil Pps Record 8 9 10 Aligned

The document outlines a series of programming exercises focusing on problem-solving using C, including the use of structures, pointers, and file handling. It details specific tasks such as declaring structures for books and students, implementing pointer operations, and creating a pocket calculator. Additionally, it includes algorithms and code snippets for each exercise, demonstrating the functionality and expected output of the programs.

Uploaded by

mm.nitish76
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views26 pages

Agil Pps Record 8 9 10 Aligned

The document outlines a series of programming exercises focusing on problem-solving using C, including the use of structures, pointers, and file handling. It details specific tasks such as declaring structures for books and students, implementing pointer operations, and creating a pocket calculator. Additionally, it includes algorithms and code snippets for each exercise, demonstrating the functionality and expected output of the programs.

Uploaded by

mm.nitish76
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

Exercise 8 PROGRAM TO ILLUSTRATE STRUCTURES

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: ");

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

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:

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

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: ");

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

fgets(book2.title, 50, stdin);


book2.title[strcspn(book2.title, "\n")] = 0;
printf("Enter price: ");
scanf("%f", &book2.price);
printf("Enter number of pages: ");
scanf("%d", &book2.number_of_pages);
if (book1.price > book2.price) {
printf("\nThe costlier book is:\n");
printf("Title: %s\n", book1.title);
printf("Price: %.2f\n", book1.price);
printf("Number of Pages: %d\n", book1.number_of_pages);
}
else if (book2.price > book1.price) {
printf("\nThe costlier book is:\n");
printf("Title: %s\n", book2.title);
printf("Price: %.2f\n", book2.price);
printf("Number of Pages: %d\n", book2.number_of_pages);
} else {
printf("\nBoth books have the same price.\n");
}
return 0;
}
Output:

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

Question 8(3): Create a structure STUDENT with &lt;Roll_no,Name,Gender,marks[5],


grades[5],GPA&gt;. Calculate the grade of each subject (assuming one-to one
mapping of marks and grades) and GPA as the average of marks scored by a
student.
Algorithm:
Step 1: Start
Step 2: Declare a Book structure with fields title, author, and price.
Step 3: Declare a student structure with fields roll_no, name, gender, marks[5], grades[5], and
GPA.
Step 4: Prompt for book1 detail and store them in book. Similarly for book2.
Step 5: Compare the prices of book1 and book2. If book1.price>book.2, print details of
book2. Otherwise, print details of book2.
Step 6: Prompt for student roll_no, name and gender and store them in the student structure.
Step 7: Prompt for the marks of the subject and store it in student.marks[i].
Step 8: Call calculate_grade() to compute the grade for each subject based on the marks and
store it in student grades[i]. Add the marks to total_marks for GPA calculation.
Step 9: Call calculate_GPA() to compute the GPA by averaging the total_marks from the 5
subjects.
Step 10: Print the subject.roll_no, student.name, and student.gender.
Step 11: Fpr each subject, print the marks and the corresponding grade. Print the calculated
GPA.
Step 12: Stop
Code:
#include <stdio.h>
#include <string.h>
struct Student {
int Roll_no;
char Name[50];
char Gender;
int marks[5];
char grades[5];
float GPA;
};
char calculateGrade(int marks) {
if (marks >= 90) {
Reg No: 2127240701006 Page No:
IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

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

printf("Gender: %c\n", student.Gender);


printf("Marks: ");
for (int i = 0; i < 5; i++) {
printf("%d ", student.marks[i]);
}
printf("\nGrades: ");
for (int i = 0; i < 5; i++) {
printf("%c ", student.grades[i]);
}
printf("\nGPA: %.2f\n", student.GPA);
return 0;
}
Output:

Result:
Thus, the program to illustrate structures were written and executed successfully.

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

Exercise9 PROGRAMS TO ILLUSTRATE POINTERS


Date:
Aim:
To illustrate problems using pointers.
Question9(1):
Demonstrate address of (*) and indirection (*) operators.
Algorithm:
Step 1: Start
Step 2: Declare and initialize a variable.
Step 3: Declare a pointer variable ptr of type int*
Step 4: Use the address_of operator & to obtain the memory address of num and print it.
Step 5: Assign the address of num to the pointer ptr using ptr=& num.
Step 6: Using the indirection operator pront the value of num directly.
Step 7: Modify the value of num through the pointer.
Step 8: Stop.
Code:
#include <stdio.h>
int main()
{
printf("2127240701006\n");
int num = 10;
int *ptr = &num; // ptr now holds the address of num
printf("Value of num: %d\n", num);
printf("Address of num: %p\n", &num);
printf("Value of ptr (address): %p\n", ptr);
printf("Value pointed to by ptr: %d\n", *ptr);
*ptr = 20;
printf("Value of num after modification: %d\n", num);
return 0;
}

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

Output:

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

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:

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

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;

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

}
}
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.

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

Exercise 10 FILE HANDLING IN C


Date:
Aim:
To write a program to handling file in C.
Question 10(1): Read a sentence from console change the case of each character and
write in to a file out.txt
Algorithm:
Step 1: Start.
Step 2: Declare a user-defined function change_case to change the case of characters.
Step 3: Open the file out.txt in write mode to store the input sentence.
Step 4: Prompt the user to enter a sentence and read it using fgets.
Step 5: Write the input sentence to the file using fprintf.
Step 6: Close the file after writing.
Step 7: Open the same file out.txt in read mode to retrieve its contents.
Step 8: Use a loop to read each character from the file.
Step 9: For each character, call the change_case function to modify its case.
Step 10: Display the modified content on the console.
Step 11: Close the file after reading.
Step 12: Stop.
Code:
#include <stdio.h>
#include <ctype.h>
// Function to change the case of a character
char change_case(char ch)
{
if (islower(ch))
{
return toupper(ch); // Convert lowercase to uppercase
}
else if (isupper(ch))
{
return tolower(ch); // Convert uppercase to lowercase
}
return ch; // Return non-alphabetic characters as is
}

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

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:

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

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:

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

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:

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

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;
}

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

// Input data from user


printf("Enter roll number: ");
scanf("%d", &roll);
getchar(); // to consume the newline left by scanf
printf("Enter name: ");
fgets(name, sizeof(name), stdin); // Read the name with spaces
printf("Enter GPA: ");
scanf("%f", &GPA);
// Append the new student record to the file
fprintf(fPtr, "<%d, %s, %.2f>\n", roll, name, GPA);
// Close the file
fclose(fPtr);
printf("New record has been added.\n");
return 0;
}
output

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

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"

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

while (fread(&s, sizeof(struct student), 1, fptr_r)) {


if (strcmp(s.name, "Krishna") == 0) {
// Print details if the name matches
printf("Name: %s\n", s.name);
printf("Roll Number: %d\n", s.roll_no);
printf("Marks: %.2f\n", s.marks);
break; // Stop once we find the student
}
}
// Close the file
fclose(fptr_r);
}
else {
printf("File not found.\n");
}

return 0;
}
Output:

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

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;

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

}
// 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.

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

Reg No: 2127240701006 Page No:


IT22111 – PROGRAM FOR PROBLEM SOLVING LAB

Reg No: 2127240701006 Page No:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy