Digital Assignment - 4
Digital Assignment - 4
Digital Assignment - 4
1.
ALGORITHM:
1. Start
2. Input: Prompt the user to enter the number of terms, n.
3. Define: A recursive function fibonacci that takes an integer n as input:
- If n is 0 or 1, return n (base cases).
- Otherwise, return the sum of fibonacci(n - 1) and fibonacci(n - 2).
4. Loop: Initialise a loop counter i = 0.
- For each i from 0 to n-1, call fibonacci(i) to compute the i-th term in the Fibonacci
series.
- Print each term.
5. End
PROGRAM:
#include <stdio.h>
int fibonacci(int n) {
if (n == 1||n==0)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("NAZRIN NISSAM 24BBS0111\n");
printf("Enter the number of terms: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
OUTPUT:
RESULT:
Fibonacci series printed to 8 terms using recursion.
2.
AIM: To search a substring in a given string.
ALGORITHM:
1. Start
2. Input: Prompt the user to enter the main string str (up to 100 characters).
3. Input: Prompt the user to enter the substring sub to search (up to 100
characters).
4. Define a function substring_search that takes two pointers, str and sub, and
performs the following:
5. Initialize a pointer p1 to the start of str.
6. Repeat for each character in str:
7. Set p2 to p1 and p3 to sub.
8. While *p3 (current character in sub) is not \0 (end of substring) and *p2 equals
*p3, increment both p2 and p3.
9. If *p3 is \0 (end of sub), return the position (p1 - str) as the starting position of
the substring in str.
10. If the loop completes without finding a match, return -1 (indicating that the
substring was not found).
11. Invoke substring_search in main with str and sub as arguments.
12. Output:
13. If substring_search returns a non-negative value, print "Substring found at
position: " followed by the position.
14. Otherwise, print "Substring not found."
15. End
PROGRAM:
#include <stdio.h>
int substring_search(char *s, char *sub) {
char *ptr1, *ptr2, *ptr3;
for (ptr1 = s; *ptr1 != '\0'; ptr1++) {
ptr2 = ptr1;
ptr3 = sub;
while (*ptr3 != '\0' && *ptr2 == *ptr3) {
ptr2++;
ptr3++;
}
if (*ptr3 == '\0')
return ptr1 - s;
}
return -1;
}
void remove_newline(char *s) {
while (*s != '\0') {
if (*s == '\n') {
*s = '\0';
break;
}
s++;
}
}
int main() {
char s[100], sub[100];
printf("Enter the main string: ");
fgets(s, 100, stdin);
remove_newline(s);
printf("Enter the substring to search: ");
fgets(sub, 100, stdin);
remove_newline(sub);
int pos = substring_search(s, sub);
if (pos != -1)
printf("Substring found at position: %d\n", pos);
else
printf("Substring not found.\n");
return 0;
}
OUTPUT:
RESULT:
The substring has been found.
3.
AIM: To display the frequency of character in a string.
ALGORITHM:
1. Start
2. Input: Prompt the user to enter a string str.
3. Define a function display_frequency that takes str as input:
4. Initialize an integer array freq of size 256 (for each ASCII character)
and set each element to 0.
5. Set a pointer p to the start of str.
6. While *p is not \0 (end of string):
7. Increment the count at freq[(unsigned char)*p].
8. Move p to the next character.
9. Output: Loop through freq from index 0 to 255:
10. If freq[i] is greater than 0, print the character i and its count freq[i].
11.Invoke display_frequency(str) in main.
12. End
PROGRAM:
#include <stdio.h>
void display_frequency(char *s) {
int freq[256] = {0};
char *ptr = s;
while (*ptr != '\0') {
freq[(unsigned char)*ptr]++;
p++;
}
printf("Character frequencies:\n");
for (int i = 0; i < 256; i++) {
if (freq[i] > 0)
printf("'%c' : %d\n", i, freq[i]);
}
}
int main() {
char s[100];
printf("Enter a string: ");
fgets(s, 100, stdin);
display_frequency(s);
return 0;
}
OUTPUT:
RESULT:
The frequency of the characters has been displayed.
4.
AIM:
Create a file to add some string, update the file and print the entire content
of the file.
ALGORITHM:
1. Start
2. Initialize: Declare a file pointer file and a character array str of size
100.
3. Create File:
4. Open a file named "example.txt" in write mode ("w").
5. If the file fails to open, print an error message and terminate the
program.
6. Write Initial Content:
7. Prompt the user to enter a string.
8. Read the string into str.
9. Write str to the file.
10. Close the file.
11.Update File:
12. Reopen the file in append mode ("a").
13. If the file fails to open, print an error message and terminate the
program.
14. Prompt the user to enter another string.
15. Read the string into str.
16. Append str to the file.
17. Close the file.
18. Read and Print File Contents:
19. Open the file in read mode ("r").
20. If the file fails to open, print an error message and terminate the
program.
21. Print a message indicating the content of the file.
22. While there is content in the file:
23. Read a line into str.
24. Print str.
25. Close the file.
26. End
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file;
char str[100];
file = fopen("newfile.txt", "w");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
OUTPUT:
RESULT:
The file has been created and the string has been added and the file
contents have been printed.
5.
AIM: To create a student database using structure that has name, regno,
marks of five subject and calculate the total, average and grade of each
subject and overall.
ALGORITHM:
1. Start
2. Define Structure:
3. Define a structure Student with:
4. A character array name of size 100.
5. An integer regno for registration number.
6. A float array marks of size 5 for storing marks of five subjects.
7. A float total to store the total marks.
8. A float average to store the average marks.
9. A character grade to store the grade.
10.Function Definition: Define a function calculate_grade that takes a pointer
to a Student structure as input:
11. Initialize total to 0.
12.Loop from 0 to 4 (for five subjects):
13.Add each mark from marks to total.
14.Calculate average by dividing total by 5.
15.Determine grade based on average:
16.If average is greater than or equal to 90, set grade to 'A'.
17.If average is greater than or equal to 75, set grade to 'B'.
18.If average is greater than or equal to 50, set grade to 'C'.
19.Otherwise, set grade to 'D'.
20.Main Function:
21.Declare a variable student of type Student.
22.Prompt the user to enter the student's name.
23.Read the name into student.name.
24.Prompt the user to enter the registration number.
25.Read the registration number into student.regno.
26.Prompt the user to enter marks for 5 subjects:
27.For each subject, do the following:
28.Prompt the user to enter the mark for the subject.
29.If the entered mark is less than 0 or greater than 100, display an error
message and prompt again until a valid mark is entered.
30.Call the calculate_grade function with the address of student.
31.Output:
32.Print the student's name.
33.Print the registration number.
34.Print the total marks.
35.Print the average marks.
36.Print the grade.
37.End
PROGRAM:
#include <stdio.h>
struct Student {
char name[100];
char regno[100];
float marks[5];
float total;
float average;
char grade;
};
void calculate_grade(struct Student *s) {
s->total = 0;
for (int i = 0; i < 5; i++) {
s->total += s->marks[i];
}
s->average = s->total / 5;
if (s->average >= 90)
s->grade = 'A';
else if (s->average >= 75)
s->grade = 'B';
else if (s->average >= 50)
s->grade = 'C';
else
s->grade = 'D';
}
int main() {
struct Student student;
printf("Enter student name: ");
fgets(student.name, sizeof(student.name), stdin);
printf("Enter registration number: ");
scanf("%s", &student.regno);
printf("Enter marks for 5 subjects:\n");
for (int j = 0; j < 5; j++) {
printf("Subject %d: ", j + 1);
scanf("%f", &student.marks[j]);
}
calculate_grade(&student);
printf("\nStudent Name: %s", student.name);
printf("Registration Number: %s\n", student.regno);
printf("Total Marks: %.2f\n", student.total);
printf("Average Marks: %.2f\n", student.average);
printf("Grade: %c\n", student.grade);
return 0;
}
OUTPUT:
RESULT:
The marks have been calculated and grade given using structure.
6.
AIM- To Create a library structure and calculate the fine
amount of the book if it is above the due date.
ALGORITHM-
1. Start.
2. Define a structure Book with the following fields:
3. title (string)
4. dueDay (integer)
5. dueMonth (integer)
6. dueYear (integer)
7. Define a function calculate_fine that takes an integer days_overdue:
8. Set fine_per_day to 5.
9. Return fine_per_day * days_overdue.
10.In the main function:
11. Declare a variable of type Book named book.
12.Declare variables returnDay, returnMonth, and returnYear (integers).
13.Print "Enter book title:".
14.Read the book title from input.
15.Print "Enter due date (DD MM YYYY):".
16.Read book.dueDay, book.dueMonth, and book.dueYear from input.
17.Print "Enter return date (DD MM YYYY):".
18.Read returnDay, returnMonth, and returnYear from input.
19.Initialize overdue_days to 0.
20.If returnYear is greater than book.dueYear or (returnYear is equal to
book.dueYear and returnMonth is greater than book.dueMonth) or
(returnYear is equal to book.dueYear and returnMonth is equal to
book.dueMonth and returnDay is greater than book.dueDay):
21.Calculate overdue_days as:
22.(returnYear - book.dueYear) * 365 + (returnMonth - book.dueMonth) * 30 +
(returnDay - book.dueDay).
23.If overdue_days is less than 0, set overdue_days to 0.
24.Print "The book is overdue by X days." (where X is overdue_days).
25.Call calculate_fine(overdue_days) and store the result in fine.
26.Print "Fine amount: Y." (where Y is the value of fine).
27.Else:
28.Print "The book is returned on time or before the due date. No fine."
29.End.
PROGRAM:
#include <stdio.h>
struct Book {
char title[100];
int dueDay;
int dueMonth;
int dueYear;
};
int calculate_fine(int days_overdue) {
int fine_per_day = 5;
return days_overdue * fine_per_day;
}
int main() {
struct Book book;
int returnDay, returnMonth, returnYear;
printf("Enter book title: ");
fgets(book.title, sizeof(book.title), stdin);
printf("Enter due date (DD MM YYYY): ");
scanf("%d %d %d", &book.dueDay, &book.dueMonth, &book.dueYear);
printf("Enter return date (DD MM YYYY): ");
scanf("%d %d %d", &returnDay, &returnMonth, &returnYear);
int overdue_days = 0;
if (returnYear > book.dueYear ||
(returnYear == book.dueYear && returnMonth > book.dueMonth) ||
(returnYear == book.dueYear && returnMonth == book.dueMonth &&
returnDay > book.dueDay)) {
overdue_days = (returnYear - book.dueYear) * 365 + (returnMonth -
book.dueMonth) * 30 + (returnDay - book.dueDay);
if (overdue_days < 0) {
overdue_days = 0;
}
printf("The book is overdue by %d days.\n", overdue_days);
int fine = calculate_fine(overdue_days);
printf("Fine amount: %d\n", fine);
} else {
printf("The book is returned on time or before the due date. No fine.\n");
}
return 0;
}
OUTPUT:
RESULT:
The overdue and the fine calculated are 27 days and 135 rupees
respectively.