ds sem1 record
ds sem1 record
1. Write a c program for electricity bill tacking different categories of users, different slabs in each category.
(using nested if else statement
#include <stdio.h>
int main()
{
int unit;
float amt, total_amt, sur_charge;
return 0;
}
b. x+x^3/3!+x^5/5!+...up to 5 terms
int main()
{
double x = 9;
int n = 10;
cout << Series(x, n);
return 0;
}
#include<stdio.h>
int main()
{
long int x,i,j,k,n,sq,cnt;
double fact,sum=0;
printf("\n ENTER THE VALUE OF N: ");
scanf("%ld",&n);
printf("\n ENTER THE VALUE OF X: ");
scanf("%ld",&x);
for(i=1,cnt=1;i<=n;i=i+2,cnt++)
{
for(j=1,sq=1;j<=i;j++)
sq=sq*x;
for(k=1,fact=1;k<=i;k++)
fact=fact*k;
if(cnt%2==1)
sum=sum+(sq/fact);
else
sum=sum-(sq/fact);
printf("\n THE SUM OF THIS SERIES IS %7.2lf\n",sum);
}
}
#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
return 0;
}
3.2 Write a c program to check whether the given number is PERFECT OR NOT
/**
* C program to check whether a number is Perfect number or not
*/
#include <stdio.h>
int main()
{
int i, num, sum = 0;
{
/* If i is a divisor of num */
if(num%i == 0)
{
sum += i;
}
}
return 0;
}
4. Write a c program to find the mean, mode, median, and variance of list of values by using one dimensional array
/*
* C program to input real numbers and find the mean, variance
* and standard deviation
*/
#include <stdio.h>
#include <math.h>
#define MAXSIZE 10
void main()
{
float x[MAXSIZE];
int i, n;
float average, variance, std_deviation, sum = 0, sum1 = 0;
}
variance = sum1 / (float)n;
std_deviation = sqrt(variance);
printf("Average of all elements = %.2f\n", average);
printf("variance of all elements = %.2f\n", variance);
printf("Standard deviation = %.2f\n", std_deviation);
}
5. Write a menu driven program to read a list of numbers and perform the following operations
a.Print the list
#include <stdio.h>
void print_menu() {
printf("\nMenu:\n");
printf("1. Print the list\n");
printf("2. Exit\n");
}
int main() {
int numbers[100], size, choice;
if (size > 0) {
printf("Enter %d numbers: ", size);
for (int i = 0; i < size; i++) {
scanf("%d", &numbers[i]);
}
}
do {
print_menu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
print_list(numbers, size);
break;
case 2:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice, please try again.\n");
}
} while (choice != 2);
return 0;
}
Menu:
1. Print the list
2. Exit
Enter your choice: 1
The list of numbers is: 1 2 3 4 5
Menu:
1. Print the list
2. Exit
Enter your choice: 2
Exiting the program.
5.Write a menu driven program to read a list of numbers and perform the following operations
b. Delete duplicates from the list
#include <stdio.h>
void print_menu() {
printf("\nMenu:\n");
printf("1. Print the list\n");
printf("2. Delete duplicates from the list\n");
printf("3. Exit\n");
}
return new_size; // Return the new size after duplicates are removed
}
int main() {
int numbers[100], size, choice;
if (size > 0) {
printf("Enter %d numbers: ", size);
for (int i = 0; i < size; i++) {
scanf("%d", &numbers[i]);
}
}
do {
print_menu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
print_list(numbers, size);
break;
case 2:
size = delete_duplicates(numbers, size);
printf("Duplicates removed.\n");
break;
case 3:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice, please try again.\n");
}
} while (choice != 3);
return 0;
}
Enter the number of elements in the list: 6
Enter 6 numbers: 1 2 3 2 4 5
Menu:
1. Print the list
2. Delete duplicates from the list
3. Exit
Enter your choice: 1
The list of numbers is: 1 2 3 2 4 5
Menu:
1. Print the list
2. Delete duplicates from the list
3. Exit
Enter your choice: 2
Duplicates removed.
Menu:
1. Print the list
2. Delete duplicates from the list
3. Exit
Enter your choice: 1
The list of numbers is: 1 2 3 4 5
5. Write a menu driven program to read a list of numbers and perform the following operations
2. Reverse the list
#include <stdio.h>
void print_menu() {
printf("\nMenu:\n");
printf("1. Print the list\n");
printf("2. Reverse the list\n");
printf("3. Exit\n");
}
temp = numbers[i];
numbers[i] = numbers[size - i - 1];
numbers[size - i - 1] = temp;
}
}
int main() {
int numbers[100], size, choice;
if (size > 0) {
printf("Enter %d numbers: ", size);
for (int i = 0; i < size; i++) {
scanf("%d", &numbers[i]);
}
}
do {
print_menu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
print_list(numbers, size);
break;
case 2:
reverse_list(numbers, size);
printf("The list has been reversed.\n");
break;
case 3:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice, please try again.\n");
}
} while (choice != 3);
return 0;
}
output
Enter the number of elements in the list: 5
Enter 5 numbers: 1 2 3 4 5
Menu:
1. Print the list
2. Reverse the list
3. Exit
Enter your choice: 1
Menu:
1. Print the list
2. Reverse the list
3. Exit
Enter your choice: 2
The list has been reversed.
Menu:
1. Print the list
2. Reverse the list
3. Exit
Enter your choice: 1
The list of numbers is: 5 4 3 2 1
6. Write a program to read a list of numbers and search for given number using binary search algorithm and if found
display its index otherwise display the message "element not found in the list" using functions
#include <stdio.h>
if (numbers[middle] == target) {
return middle; // Return index if the target is found
}
else if (numbers[middle] < target) {
left = middle + 1;
}
else {
right = middle - 1;
}
}
int main() {
int numbers[100], size, target, result;
7. Write a menu driven program to read two matrices and compute their sum and product using functions
#include <stdio.h>
void print_menu() {
printf("\nMenu:\n");
printf("1. Print matrices\n");
printf("2. Add matrices\n");
printf("3. Multiply matrices\n");
printf("4. Exit\n");
}
void add_matrices(int matrix1[10][10], int matrix2[10][10], int result[10][10], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
}
void multiply_matrices(int matrix1[10][10], int matrix2[10][10], int result[10][10], int rows1, int cols1, int rows2,
int cols2) {
// Initialize result matrix with 0
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
result[i][j] = 0;
}
}
int main() {
int matrix1[10][10], matrix2[10][10], result[10][10];
int rows1, cols1, rows2, cols2, choice;
printf("For matrix multiplication, the number of columns of the first matrix must be equal to the number of
rows of the second matrix.\n");
}
do {
print_menu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("First Matrix:\n");
print_matrix(matrix1, rows1, cols1);
printf("Second Matrix:\n");
print_matrix(matrix2, rows2, cols2);
break;
case 2:
if (rows1 == rows2 && cols1 == cols2) {
add_matrices(matrix1, matrix2, result, rows1, cols1);
printf("Result of matrix addition:\n");
print_matrix(result, rows1, cols1);
} else {
printf("Matrices must have the same dimensions to add.\n");
}
break;
case 3:
if (cols1 == rows2) {
multiply_matrices(matrix1, matrix2, result, rows1, cols1, rows2, cols2);
printf("Result of matrix multiplication:\n");
print_matrix(result, rows1, cols2);
} else {
printf("Matrix multiplication not possible. The number of columns of the first matrix must be equal to
the number of rows of the second matrix.\n");
}
break;
case 4:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice, please try again.\n");
}
} while (choice != 4);
return 0;
}
Enter the number of rows and columns for the first matrix: 2 2
Enter the elements of the matrix (2 x 2):
12
34
Enter the number of rows and columns for the second matrix: 2
2
Enter the elements of the matrix (2 x 2):
56
78
Menu:
1. Print matrices
2. Add matrices
3. Multiply matrices
4. Exit
Enter your choice: 1
First Matrix:
12
34
Second Matrix:
56
78
Menu:
1. Print matrices
2. Add matrices
3. Multiply matrices
4. Exit
Enter your choice: 2
Result of matrix addition:
68
10 12
Menu:
1. Print matrices
2. Add matrices
3. Multiply matrices
4. Exit
Enter your choice: 3
Result of matrix multiplication:
19 22
43 50
Menu:
1. Print matrices
2. Add matrices
3. Multiply matrices
4. Exit
Enter your choice: 4
Exiting the program.
8. Write a menu driven program to read list of student names and perform the following operations using functions.
1. To print list of names
#include <stdio.h>
#include <string.h>
void print_menu() {
printf("\nMenu:\n");
printf("1. Print list of names\n");
printf("2. Sort names in ascending order\n");
printf("3. Print sorted list of names\n");
printf("4. Exit\n");
}
int main() {
char names[100][100]; // Array to store up to 100 student names, each up to 100 characters long
int n, choice;
void print_menu() {
printf("\nMenu:\n");
printf("1. Print list of names\n");
printf("2. Sort names in ascending order\n");
printf("3. Print sorted list of names\n");
printf("4. Exit\n");
}
int main() {
char names[100][100]; // Array to store up to 100 student names, each up to 100 characters long
int n, choice;
do {
print_menu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
print_names(names, n);
break;
case 2:
sort_names(names, n);
break;
case 3:
print_names(names, n); // After sorting, the list will be printed here
break;
case 4:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice, please try again.\n");
}
} while (choice != 4);
return 0;
}
Menu:
1. Print list of names
2. Sort names in ascending order
3. Print sorted list of names
4. Exit
Enter your choice: 1
List of student names:
John
Alice
Bob
Charlie
Menu:
1. Print list of names
2. Sort names in ascending order
3. Print sorted list of names
4. Exit
Enter your choice: 2
Names have been sorted in ascending
order.
Menu:
1. Print list of names
Menu:
1. Print list of names
2. Sort names in ascending order
3. Print sorted list of names
4. Exit
Enter your choice: 4
Exiting the program.
9. Write a c program that consists of recursive functions to find
1. Factorial of a given number
#include <stdio.h>
int main() {
int num;
return 0;
}
#include <stdio.h>
int main() {
int rows;
printf("Pascal's Triangle:\n");
print_pascals_triangle(rows); // Print the triangle
return 0;
}
Enter the number of rows for Pascal's Triangle: 5
Pascal's Triangle:
1
11
121
1331
14641
Write a menu driven program to read list of student names and perform the following operations using array of
character pointers.
11. a) To insert a student name
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print_menu() {
printf("\nMenu:\n");
printf("1. Insert a student name\n");
printf("2. Print list of student names\n");
printf("3. Exit\n");
}
char name[MAX_NAME_LENGTH];
printf("Enter the student name: ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = '\0'; // Remove newline character
// Allocate memory for the new name and copy it to the students array
students[*count] = (char *)malloc((strlen(name) + 1) * sizeof(char));
if (students[*count] != NULL) {
strcpy(students[*count], name);
(*count)++;
printf("Student name '%s' inserted successfully.\n", name);
} else {
printf("Memory allocation failed. Unable to insert name.\n");
}
}
int main() {
char *students[MAX_STUDENTS]; // Array of character pointers
int count = 0; // Current number of students
int choice;
do {
print_menu();
switch (choice) {
case 1:
insert_student_name(students, &count);
break;
case 2:
print_student_names(students, count);
break;
case 3:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice, please try again.\n");
}
} while (choice != 3);
return 0;
}
Menu:
1. Insert a student name
2. Print list of student names
3. Exit
Enter your choice: 1
Enter the student name: John Doe
Student name 'John Doe' inserted successfully.
Menu:
1. Insert a student name
2. Print list of student names
3. Exit
Enter your choice: 1
Enter the student name: Alice Smith
Student name 'Alice Smith' inserted
successfully.
Menu:
1. Insert a student name
2. Print list of student names
3. Exit
Enter your choice: 2
List of student names:
1. John Doe
2. Alice Smith
Menu:
b) To delete a name
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print_menu() {
printf("\nMenu:\n");
printf("1. Insert a student name\n");
printf("2. Print list of student names\n");
printf("3. Delete a student name\n");
printf("4. Exit\n");
}
char name[MAX_NAME_LENGTH];
printf("Enter the student name: ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = '\0'; // Remove newline character
// Allocate memory for the new name and copy it to the students array
students[*count] = (char *)malloc((strlen(name) + 1) * sizeof(char));
if (students[*count] != NULL) {
strcpy(students[*count], name);
(*count)++;
printf("Student name '%s' inserted successfully.\n", name);
} else {
printf("Memory allocation failed. Unable to insert name.\n");
}
}
int index;
printf("Enter the index of the student name to delete (1 to %d): ", *count);
scanf("%d", &index);
getchar(); // Consume the newline character
// Free the memory allocated for the student name and shift the array
free(students[index - 1]);
for (int i = index - 1; i < *count - 1; i++) {
students[i] = students[i + 1];
}
students[*count - 1] = NULL; // Set the last pointer to NULL
(*count)--; // Decrease the count of student names
int main() {
char *students[MAX_STUDENTS]; // Array of character pointers
int count = 0; // Current number of students
int choice;
do {
print_menu();
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // Consume the newline character after choice input
switch (choice) {
case 1:
insert_student_name(students, &count);
break;
case 2:
print_student_names(students, count);
break;
case 3:
delete_student_name(students, &count);
break;
case 4:
return 0;
}
Menu:
1. Insert a student name
2. Print list of student names
3. Delete a student name
4. Exit
Enter your choice: 1
Enter the student name: John Doe
Student name 'John Doe' inserted successfully.
Menu:
1. Insert a student name
2. Print list of student names
3. Delete a student name
4. Exit
Enter your choice: 1
Enter the student name: Alice Smith
Student name 'Alice Smith' inserted successfully.
Menu:
1. Insert a student name
2. Print list of student names
3. Delete a student name
4. Exit
Enter your choice: 2
List of student names:
1. John Doe
2. Alice Smith
Menu:
1. Insert a student name
2. Print list of student names
3. Delete a student name
4. Exit
Enter your choice: 3
Enter the index of the student name to delete (1 to 2): 1
Student name at index 1 deleted successfully.
Menu:
Menu:
1. Insert a student name
2. Print list of student names
3. Delete a student name
4. Exit
Enter your choice: 4
Exiting the program.
c) To print the names
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print_menu() {
printf("\nMenu:\n");
printf("1. Insert a student name\n");
printf("2. Print list of student names\n");
printf("3. Delete a student name\n");
printf("4. Exit\n");
}
char name[MAX_NAME_LENGTH];
printf("Enter the student name: ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = '\0'; // Remove newline character
// Allocate memory for the new name and copy it to the students array
students[*count] = (char *)malloc((strlen(name) + 1) * sizeof(char));
if (students[*count] != NULL) {
strcpy(students[*count], name);
(*count)++;
printf("Student name '%s' inserted successfully.\n", name);
} else {
printf("Memory allocation failed. Unable to insert name.\n");
}
}
int index;
printf("Enter the index of the student name to delete (1 to %d): ", *count);
scanf("%d", &index);
getchar(); // Consume the newline character
// Free the memory allocated for the student name and shift the array
free(students[index - 1]);
for (int i = index - 1; i < *count - 1; i++) {
students[i] = students[i + 1];
}
students[*count - 1] = NULL; // Set the last pointer to NULL
(*count)--; // Decrease the count of student names
int main() {
char *students[MAX_STUDENTS]; // Array of character pointers
int count = 0; // Current number of students
int choice;
do {
print_menu();
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // Consume the newline character after choice input
switch (choice) {
case 1:
insert_student_name(students, &count);
break;
case 2:
print_student_names(students, count);
break;
case 3:
delete_student_name(students, &count);
break;
case 4:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice, please try again.\n");
}
} while (choice != 4);
return 0;
}
12. Write a program to create an array of structures and display their content.
#include <stdio.h>
#include <string.h>
int main() {
struct Student students[MAX_STUDENTS]; // Array of Student structures
int count = 0; // Number of students
return 0;
}
Enter the number of books (max 5): 2
struct Author {
char name[50];
char country[50];
};
int main() {
struct Book books[5]; // Array of Book structures
int count;
printf("Title: ");
fgets(books[i].title, sizeof(books[i].title), stdin);
books[i].title[strcspn(books[i].title, "\n")] = '\0'; // Remove newline character
return 0;
}
Menu:
1. Insert a student name
2. Print a student name
3. Print list of student names
4. Exit
Enter your choice: 1
Enter the student name: John Doe
Student name 'John Doe' inserted successfully.
Menu:
1. Insert a student name
2. Print a student name
3. Print list of student names
4. Exit
Enter your choice: 1
Enter the student name: Alice Smith
Student name 'Alice Smith' inserted successfully.
Menu:
1. Insert a student name
2. Print a student name
3. Print list of student names
4. Exit
Enter your choice: 3
List of student names:
1. John Doe
2. Alice Smith
Menu:
1. Insert a student name
2. Print a student name
3. Print list of student names
4. Exit
Enter your choice: 2
Enter the index of the student name to print (1 to 2): 1
Student name at index 1: John Doe
Menu:
1. Insert a student name
2. Print a student name
3. Print list of student names
4. Exit
#define MAX_STUDENTS 10
#define MAX_NAME_LENGTH 50
int main() {
struct Student *students; // Pointer to Student structure
int count = MAX_STUDENTS;
return 0;
}
Enter name of student 1: John Doe
Enter age of student 1: 20
Enter name of student 2: Alice Smith
Enter age of student 2: 22
Enter name of student 3: Bob Johnson
Enter age of student 3: 21
Enter name of student 4: Emily Davis
Enter age of student 4: 23
Enter name of student 5: Michael Brown
Enter age of student 5: 24
Enter name of student 6: Sarah Wilson
Enter age of student 6: 19
Enter name of student 7: David Lee
Enter age of student 7: 20
Enter name of student 8: Laura King
Enter age of student 8: 22
Enter name of student 9: Steven Clark
Enter age of student 9: 21
Enter name of student 10: Emma Walker
Enter age of student 10: 23
List of Students:
Student 1: Name: John Doe, Age: 20
Student 2: Name: Alice Smith, Age: 22
Student 3: Name: Bob Johnson, Age: 21
Student 4: Name: Emily Davis, Age: 23
Student 5: Name: Michael Brown, Age: 24
Student 6: Name: Sarah Wilson, Age: 19
Student 7: Name: David Lee, Age: 20
Student 8: Name: Laura King, Age: 22
Student 9: Name: Steven Clark, Age: 21
Student 10: Name: Emma Walker, Age: 23
15. Write a program to display the contents of a file
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file; // Declare a file pointer
char filename[100]; // Buffer to hold the filename
char ch; // Variable to hold each character read
int main() {
FILE *sourceFile, *destinationFile; // Declare file pointers
char sourceFilename[100]; // Buffer for source filename
char destinationFilename[100]; // Buffer for destination filename
char ch; // Variable to hold each character read
int main() {
FILE *sourceFile, *destinationFile; // Declare file pointers
char sourceFilename[100]; // Buffer for source filename
char destinationFilename[100]; // Buffer for destination filename
char ch; // Variable to hold each character read
fclose(destinationFile);