0% found this document useful (0 votes)
12 views35 pages

ds sem1 record

Uploaded by

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

ds sem1 record

Uploaded by

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

DATA SCIENCE I-SEM(C –LANGUAGE)

1. Write a c program for electricity bill tacking different categories of users, different slabs in each category.
(using nested if else statement

/* C program to calculate total electricity bill */

#include <stdio.h>

int main()
{
int unit;
float amt, total_amt, sur_charge;

printf("Enter total units consumed: ");


scanf("%d", &unit);

if(unit <= 50)


{
amt = unit * 0.50;
}
else if(unit <= 150)
{
amt = 25 + ((unit-50) * 0.75);
}
else if(unit <= 250)
{
amt = 100 + ((unit-150) * 1.20);
}
else
{
amt = 220 + ((unit-250) * 1.50);
}

sur_charge = amt * 0.20;


total_amt = amt + sur_charge;

printf("Electricity Bill = Rs. %.2f", total_amt);

return 0;
}

2. Write a c program to evaluate the following using loops


a. 1+x^2/2!+x^4/4!+...up to 5 terms

// C++ program to get the sum of the series


#include <bits/stdc++.h>
using namespace std;

// Function to get the series


double Series(double x, int n)
{
double sum = 1, term = 1, fct, j, y = 2, m;

N.LAXMAN 9963640581 Page 1


DATA SCIENCE I-SEM(C –LANGUAGE)

// Sum of n-1 terms starting from 2nd term


int i;
for (i = 1; i < n; i++) {
fct = 1;
for (j = 1; j <= y; j++) {
fct = fct * j;
}
term = term * (-1);
m = term * pow(x, y) / fct;
sum = sum + m;
y += 2;
}
return sum;
}

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

N.LAXMAN 9963640581 Page 2


DATA SCIENCE I-SEM(C –LANGUAGE)

3.Write a c program to check whether the given number is Prime or not

#include <stdio.h>

int main()
{

int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

// 0 and 1 are not prime numbers


// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;

for (i = 2; i <= n / 2; ++i) {

// if n is divisible by i, then n is not prime


// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}

// flag is 0 for prime numbers


if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", 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;

/* Input a number from user */


printf("Enter any number to check perfect number: ");
scanf("%d", &num);

/* Calculate sum of all proper divisors */


for(i = 1; i <= num / 2; i++)

N.LAXMAN 9963640581 Page 3


DATA SCIENCE I-SEM(C –LANGUAGE)

{
/* If i is a divisor of num */
if(num%i == 0)
{
sum += i;
}
}

/* Check whether the sum of proper divisors is equal to num */


if(sum == num && num > 0)
{
printf("%d is PERFECT NUMBER", num);
}
else
{
printf("%d is NOT PERFECT NUMBER", num);
}

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;

printf("Enter the value of N \n");


scanf("%d", &n);
printf("Enter %d real numbers \n", n);
for (i = 0; i < n; i++)
{
scanf("%f", &x[i]);
}
/* Compute the sum of all elements */
for (i = 0; i < n; i++)
{
sum = sum + x[i];
}
average = sum / (float)n;
/* Compute variance and standard deviation */
for (i = 0; i < n; i++)
{
sum1 = sum1 + pow((x[i] - average), 2);

N.LAXMAN 9963640581 Page 4


DATA SCIENCE I-SEM(C –LANGUAGE)

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

void print_list(int numbers[], int size) {


if (size == 0) {
printf("The list is empty.\n");
} else {
printf("The list of numbers is: ");
for (int i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
}
}

int main() {
int numbers[100], size, choice;

// Reading the list of numbers


printf("Enter the number of elements in the list: ");
scanf("%d", &size);

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

N.LAXMAN 9963640581 Page 5


DATA SCIENCE I-SEM(C –LANGUAGE)

break;
case 2:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice, please try again.\n");
}
} while (choice != 2);

return 0;
}

Enter the number of elements in the list: 5


Enter 5 numbers: 1 2 3 4 5

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

void print_list(int numbers[], int size) {


if (size == 0) {
printf("The list is empty.\n");
} else {
printf("The list of numbers is: ");
for (int i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
}
}

int delete_duplicates(int numbers[], int size) {


if (size == 0) {
return 0;
}

N.LAXMAN 9963640581 Page 6


DATA SCIENCE I-SEM(C –LANGUAGE)

int new_size = size;

for (int i = 0; i < new_size; i++) {


for (int j = i + 1; j < new_size; j++) {
if (numbers[i] == numbers[j]) {
// Shift elements to the left to overwrite the duplicate
for (int k = j; k < new_size - 1; k++) {
numbers[k] = numbers[k + 1];
}
new_size--; // Reduce the size of the list
j--; // Stay at the current index to check the new element
}
}
}

return new_size; // Return the new size after duplicates are removed
}

int main() {
int numbers[100], size, choice;

// Reading the list of numbers


printf("Enter the number of elements in the list: ");
scanf("%d", &size);

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

N.LAXMAN 9963640581 Page 7


DATA SCIENCE I-SEM(C –LANGUAGE)

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

void print_list(int numbers[], int size) {


if (size == 0) {
printf("The list is empty.\n");
} else {
printf("The list of numbers is: ");
for (int i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
}
}

void reverse_list(int numbers[], int size) {


int temp;
for (int i = 0; i < size / 2; i++) {

N.LAXMAN 9963640581 Page 8


DATA SCIENCE I-SEM(C –LANGUAGE)

temp = numbers[i];
numbers[i] = numbers[size - i - 1];
numbers[size - i - 1] = temp;
}
}

int main() {
int numbers[100], size, choice;

// Reading the list of numbers


printf("Enter the number of elements in the list: ");
scanf("%d", &size);

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

N.LAXMAN 9963640581 Page 9


DATA SCIENCE I-SEM(C –LANGUAGE)

The list of numbers is: 1 2 3 4 5

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>

void sort_list(int numbers[], int size) {


int temp;
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (numbers[j] > numbers[j + 1]) {
temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
}

int binary_search(int numbers[], int size, int target) {


int left = 0, right = size - 1;

while (left <= right) {


int middle = (left + right) / 2;

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

return -1; // Return -1 if the target is not found


}

N.LAXMAN 9963640581 Page 10


DATA SCIENCE I-SEM(C –LANGUAGE)

void print_list(int numbers[], int size) {


for (int i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
}

int main() {
int numbers[100], size, target, result;

// Read the list of numbers


printf("Enter the number of elements in the list: ");
scanf("%d", &size);

printf("Enter %d numbers: ", size);


for (int i = 0; i < size; i++) {
scanf("%d", &numbers[i]);
}

// Sort the list for binary search


sort_list(numbers, size);

// Print the sorted list


Enter the number of elements in the
list: 5
Enter 5 numbers: 5 2 9 1 4
Sorted list: 1 2 4 5 9
Enter the number to search: 4
Element found at index 2

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 read_matrix(int matrix[10][10], int rows, int cols) {


printf("Enter the elements of the matrix (%d x %d):\n", rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
}

void print_matrix(int matrix[10][10], int rows, int cols) {

N.LAXMAN 9963640581 Page 11


DATA SCIENCE I-SEM(C –LANGUAGE)

printf("Matrix (%d x %d):\n", rows, cols);


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\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;
}
}

for (int i = 0; i < rows1; i++) {


for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}

int main() {
int matrix1[10][10], matrix2[10][10], result[10][10];
int rows1, cols1, rows2, cols2, choice;

// Read the dimensions of the first matrix


printf("Enter the number of rows and columns for the first matrix: ");
scanf("%d %d", &rows1, &cols1);
read_matrix(matrix1, rows1, cols1);

// Read the dimensions of the second matrix


printf("Enter the number of rows and columns for the second matrix: ");
scanf("%d %d", &rows2, &cols2);
read_matrix(matrix2, rows2, cols2);

if (rows1 != rows2 || cols1 != cols2) {


printf("For matrix addition, both matrices must have the same dimensions.\n");
}
if (cols1 != rows2) {

N.LAXMAN 9963640581 Page 12


DATA SCIENCE I-SEM(C –LANGUAGE)

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

N.LAXMAN 9963640581 Page 13


DATA SCIENCE I-SEM(C –LANGUAGE)

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>

N.LAXMAN 9963640581 Page 14


DATA SCIENCE I-SEM(C –LANGUAGE)

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

void print_names(char names[100][100], int n) {


if (n == 0) {
printf("The list of names is empty.\n");
} else {
printf("List of student names:\n");
for (int i = 0; i < n; i++) {
printf("%s\n", names[i]);
}
}
}

void sort_names(char names[100][100], int n) {


char temp[100];

// Simple bubble sort to sort names


for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (strcmp(names[i], names[j]) > 0) {
strcpy(temp, names[i]);
strcpy(names[i], names[j]);
strcpy(names[j], temp);
}
}
}
printf("Names have been sorted in ascending order.\n");
}

int main() {
char names[100][100]; // Array to store up to 100 student names, each up to 100 characters long
int n, choice;

// Read the list of student names


printf("Enter the number of students: ");
scanf("%d", &n);
getchar(); // To consume the newline character after entering the number of students

printf("Enter the names of %d students:\n", n);


for (int i = 0; i < n; i++) {
fgets(names[i], sizeof(names[i]), stdin);
names[i][strcspn(names[i], "\n")] = '\0'; //
#include <stdio.h>
#include <string.h>

void print_menu() {

N.LAXMAN 9963640581 Page 15


DATA SCIENCE I-SEM(C –LANGUAGE)

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

void print_names(char names[100][100], int n) {


if (n == 0) {
printf("The list of names is empty.\n");
} else {
printf("List of student names:\n");
for (int i = 0; i < n; i++) {
printf("%s\n", names[i]);
}
}
}

void sort_names(char names[100][100], int n) {


char temp[100];

// Simple bubble sort to sort names


for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (strcmp(names[i], names[j]) > 0) {
strcpy(temp, names[i]);
strcpy(names[i], names[j]);
strcpy(names[j], temp);
}
}
}
printf("Names have been sorted in ascending order.\n");
}

int main() {
char names[100][100]; // Array to store up to 100 student names, each up to 100 characters long
int n, choice;

// Read the list of student names


printf("Enter the number of students: ");
scanf("%d", &n);
getchar(); // To consume the newline character after entering the number of students

printf("Enter the names of %d students:\n", n);


for (int i = 0; i < n; i++) {
fgets(names[i], sizeof(names[i]), stdin);
names[i][strcspn(names[i], "\n")] = '\0'; // Remove the newline character
}

do {
print_menu();
printf("Enter your choice: ");
scanf("%d", &choice);

N.LAXMAN 9963640581 Page 16


DATA SCIENCE I-SEM(C –LANGUAGE)

getchar(); // To consume the newline character after choice input

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

Enter the number of students: 4


Enter the names of 4 students:
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: 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

N.LAXMAN 9963640581 Page 17


DATA SCIENCE I-SEM(C –LANGUAGE)

2. Sort names in ascending order


3. Print sorted list of names
4. Exit
Enter your choice: 3
List of student names:
Alice
Bob
Charlie
John

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>

// Function to calculate factorial using recursion


int factorial(int n) {
if (n == 0 || n == 1) { // Base case: factorial of 0 or 1 is 1
return 1;
} else {
return n * factorial(n - 1); // Recursive case: n * factorial(n-1)
}
}

int main() {
int num;

// Read the number from the user


printf("Enter a number to find its factorial: ");
scanf("%d", &num);

// Ensure the number is non-negative


if (num < 0) {
printf("Factorial of a negative number is undefined.\n");
} else {
// Call the recursive factorial function and print the result
printf("Factorial of %d is %d\n", num, factorial(num));
}

return 0;
}

Enter a positive integer: 5


Factorial of 5 is 120
9.2 Print the Pascal triangle using binomial theorem

#include <stdio.h>

N.LAXMAN 9963640581 Page 18


DATA SCIENCE I-SEM(C –LANGUAGE)

// Function to calculate factorial


long long factorial(int n) {
if (n == 0 || n == 1) {
return 1; // Base case
}
long long result = 1;
for (int i = 2; i <= n; i++) {
result *= i; // Calculate factorial
}
return result;
}

// Function to calculate binomial coefficient


long long binomial_coefficient(int n, int k) {
return factorial(n) / (factorial(k) * factorial(n - k));
}

// Function to print Pascal's triangle


void print_pascals_triangle(int rows) {
for (int n = 0; n < rows; n++) {
for (int k = 0; k <= n; k++) {
printf("%lld ", binomial_coefficient(n, k)); // Print binomial coefficient
}
printf("\n"); // New line after each row
}
}

int main() {
int rows;

// Input number of rows from user


printf("Enter the number of rows for Pascal's Triangle: ");
scanf("%d", &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>

N.LAXMAN 9963640581 Page 19


DATA SCIENCE I-SEM(C –LANGUAGE)

#include <string.h>

#define MAX_STUDENTS 100


#define MAX_NAME_LENGTH 100

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

void insert_student_name(char *students[], int *count) {


if (*count >= MAX_STUDENTS) {
printf("Student list is full. Cannot insert more names.\n");
return;
}

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

void print_student_names(char *students[], int count) {


if (count == 0) {
printf("No student names to display.\n");
return;
}

printf("List of student names:\n");


for (int i = 0; i < count; i++) {
printf("%d. %s\n", i + 1, students[i]);
}
}

int main() {
char *students[MAX_STUDENTS]; // Array of character pointers
int count = 0; // Current number of students
int choice;

do {
print_menu();

N.LAXMAN 9963640581 Page 20


DATA SCIENCE I-SEM(C –LANGUAGE)

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:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice, please try again.\n");
}
} while (choice != 3);

// Free allocated memory for student names


for (int i = 0; i < count; i++) {
free(students[i]);
}

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:

N.LAXMAN 9963640581 Page 21


DATA SCIENCE I-SEM(C –LANGUAGE)

1. Insert a student name


2. Print list of student names
3. Exit
Enter your choice: 3
Exiting the program.

b) To delete a name

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STUDENTS 100


#define MAX_NAME_LENGTH 100

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

void insert_student_name(char *students[], int *count) {


if (*count >= MAX_STUDENTS) {
printf("Student list is full. Cannot insert more names.\n");
return;
}

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

void print_student_names(char *students[], int count) {


if (count == 0) {
printf("No student names to display.\n");
return;
}

printf("List of student names:\n");


for (int i = 0; i < count; i++) {

N.LAXMAN 9963640581 Page 22


DATA SCIENCE I-SEM(C –LANGUAGE)

printf("%d. %s\n", i + 1, students[i]);


}
}

void delete_student_name(char *students[], int *count) {


if (*count == 0) {
printf("No student names to delete.\n");
return;
}

int index;
printf("Enter the index of the student name to delete (1 to %d): ", *count);
scanf("%d", &index);
getchar(); // Consume the newline character

if (index < 1 || index > *count) {


printf("Invalid index. Please try again.\n");
return;
}

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

printf("Student name at index %d deleted successfully.\n", index);


}

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:

N.LAXMAN 9963640581 Page 23


DATA SCIENCE I-SEM(C –LANGUAGE)

printf("Exiting the program.\n");


break;
default:
printf("Invalid choice, please try again.\n");
}
} while (choice != 4);

// Free allocated memory for student names


for (int i = 0; i < count; i++) {
free(students[i]);
}

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:

N.LAXMAN 9963640581 Page 24


DATA SCIENCE I-SEM(C –LANGUAGE)

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. Alice Smith

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>

#define MAX_STUDENTS 100


#define MAX_NAME_LENGTH 100

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

void insert_student_name(char *students[], int *count) {


if (*count >= MAX_STUDENTS) {
printf("Student list is full. Cannot insert more names.\n");
return;
}

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

N.LAXMAN 9963640581 Page 25


DATA SCIENCE I-SEM(C –LANGUAGE)

void print_student_names(char *students[], int count) {


if (count == 0) {
printf("No student names to display.\n");
return;
}

printf("List of student names:\n");


for (int i = 0; i < count; i++) {
printf("%d. %s\n", i + 1, students[i]);
}
}

void delete_student_name(char *students[], int *count) {


if (*count == 0) {
printf("No student names to delete.\n");
return;
}

int index;
printf("Enter the index of the student name to delete (1 to %d): ", *count);
scanf("%d", &index);
getchar(); // Consume the newline character

if (index < 1 || index > *count) {


printf("Invalid index. Please try again.\n");
return;
}

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

printf("Student name at index %d deleted successfully.\n", index);


}

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

N.LAXMAN 9963640581 Page 26


DATA SCIENCE I-SEM(C –LANGUAGE)

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

// Free allocated memory for student names


for (int i = 0; i < count; i++) {
free(students[i]);
}

return 0;
}

12. Write a program to create an array of structures and display their content.
#include <stdio.h>
#include <string.h>

#define MAX_STUDENTS 100


#define MAX_NAME_LENGTH 50

// Define a structure to hold student information


struct Student {
char name[MAX_NAME_LENGTH];
int age;
};

// Function to display student information


void display_students(struct Student students[], int count) {
printf("\nList of Students:\n");
for (int i = 0; i < count; i++) {
printf("Student %d: Name: %s, Age: %d\n", i + 1, students[i].name, students[i].age);
}
}

int main() {
struct Student students[MAX_STUDENTS]; // Array of Student structures
int count = 0; // Number of students

printf("Enter the number of students (max %d): ", MAX_STUDENTS);


scanf("%d", &count);
getchar(); // Consume the newline character after the number input

N.LAXMAN 9963640581 Page 27


DATA SCIENCE I-SEM(C –LANGUAGE)

// Ensure the count does not exceed the maximum limit


if (count > MAX_STUDENTS) {
printf("Error: Cannot enter more than %d students.\n", MAX_STUDENTS);
return 1;
}

// Input student information


for (int i = 0; i < count; i++) {
printf("Enter name of student %d: ", i + 1);
fgets(students[i].name, sizeof(students[i].name), stdin);
students[i].name[strcspn(students[i].name, "\n")] = '\0'; // Remove newline character

printf("Enter age of student %d: ", i + 1);


scanf("%d", &students[i].age);
getchar(); // Consume the newline character after age input
}

// Display student information


display_students(students, count);

return 0;
}
Enter the number of books (max 5): 2

Enter details for Book 1:


Title: The Great Gatsby
Publication Year: 1925
Author Name: F. Scott Fitzgerald
Author Country: USA

Enter details for Book 2:


Title: To Kill a Mockingbird
Publication Year: 1960
Author Name: Harper Lee
Author Country: USA

Library Book Information:

Book Title: The Great Gatsby


Publication Year: 1925
Author Name: F. Scott Fitzgerald
Author Country: USA

Book Title: To Kill a Mockingbird


Publication Year: 1960
Author Name: Harper Lee
Author Country: USA

13. Write a program to demonstrate nested structures.


#include <stdio.h>
#include <string.h>

// Define a structure for Author

N.LAXMAN 9963640581 Page 28


DATA SCIENCE I-SEM(C –LANGUAGE)

struct Author {
char name[50];
char country[50];
};

// Define a structure for Book, which contains a nested Author structure


struct Book {
char title[100];
int publication_year;
struct Author author; // Nested structure
};

// Function to display book information


void display_book_info(struct Book book) {
printf("\nBook Title: %s\n", book.title);
printf("Publication Year: %d\n", book.publication_year);
printf("Author Name: %s\n", book.author.name);
printf("Author Country: %s\n", book.author.country);
}

int main() {
struct Book books[5]; // Array of Book structures
int count;

printf("Enter the number of books (max 5): ");


scanf("%d", &count);
getchar(); // Consume the newline character after the number input

// Ensure the count does not exceed the maximum limit


if (count > 5) {
printf("Error: Cannot enter more than 5 books.\n");
return 1;
}

// Input book information


for (int i = 0; i < count; i++) {
printf("\nEnter details for Book %d:\n", i + 1);

printf("Title: ");
fgets(books[i].title, sizeof(books[i].title), stdin);
books[i].title[strcspn(books[i].title, "\n")] = '\0'; // Remove newline character

printf("Publication Year: ");


scanf("%d", &books[i].publication_year);
getchar(); // Consume the newline character after publication year input

printf("Author Name: ");


fgets(books[i].author.name, sizeof(books[i].author.name), stdin);
books[i].author.name[strcspn(books[i].author.name, "\n")] = '\0'; // Remove newline character

printf("Author Country: ");


fgets(books[i].author.country, sizeof(books[i].author.country), stdin);
books[i].author.country[strcspn(books[i].author.country, "\n")] = '\0'; // Remove newline character

N.LAXMAN 9963640581 Page 29


DATA SCIENCE I-SEM(C –LANGUAGE)

// Display book information


printf("\nLibrary Book Information:\n");
for (int i = 0; i < count; i++) {
display_book_info(books[i]);
}

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

N.LAXMAN 9963640581 Page 30


DATA SCIENCE I-SEM(C –LANGUAGE)

Enter your choice: 4


Exiting the program.
14. Write a program to create 10 student records and display those using pointers.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STUDENTS 10
#define MAX_NAME_LENGTH 50

// Define a structure to hold student information


struct Student {
char name[MAX_NAME_LENGTH];
int age;
};

// Function to display student information


void display_students(struct Student *students, int count) {
printf("\nList of Students:\n");
for (int i = 0; i < count; i++) {
printf("Student %d: Name: %s, Age: %d\n", i + 1, (students + i)->name, (students + i)->age);
}
}

int main() {
struct Student *students; // Pointer to Student structure
int count = MAX_STUDENTS;

// Allocate memory for 10 students


students = (struct Student *)malloc(count * sizeof(struct Student));
if (students == NULL) {
printf("Memory allocation failed.\n");
return 1;
}

// Input student information


for (int i = 0; i < count; i++) {
printf("Enter name of student %d: ", i + 1);
fgets((students + i)->name, sizeof((students + i)->name), stdin);
(students + i)->name[strcspn((students + i)->name, "\n")] = '\0'; // Remove newline character

printf("Enter age of student %d: ", i + 1);


scanf("%d", &(students + i)->age);
getchar(); // Consume the newline character after age input
}

// Display student information


display_students(students, count);

// Free allocated memory


free(students);

return 0;

N.LAXMAN 9963640581 Page 31


DATA SCIENCE I-SEM(C –LANGUAGE)

}
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

// Ask the user for the filename


printf("Enter the filename to open: ");
fgets(filename, sizeof(filename), stdin);
filename[strcspn(filename, "\n")] = '\0'; // Remove newline character

// Open the file in read mode


file = fopen(filename, "r");
if (file == NULL) {
printf("Could not open file %s for reading.\n", filename);
return 1; // Exit if the file cannot be opened

N.LAXMAN 9963640581 Page 32


DATA SCIENCE I-SEM(C –LANGUAGE)

// Display the contents of the file


printf("\nContents of the file '%s':\n", filename);
while ((ch = fgetc(file)) != EOF) { // Read character by character until EOF
putchar(ch); // Output the character to the console
}

// Close the file


fclose(file);
return 0;
}
Hello, World!
This is a test file.
It contains multiple lines.

Enter the source filename: source.txt


Enter the destination filename: destination.txt
Contents copied from source.txt to destination.txt successfully.
16. Write a program to copy the contents of one file into another file.
#include <stdio.h>
#include <stdlib.h>

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

// Ask the user for the source and destination filenames


printf("Enter the source filename: ");
fgets(sourceFilename, sizeof(sourceFilename), stdin);
sourceFilename[strcspn(sourceFilename, "\n")] = '\0'; // Remove newline character

printf("Enter the destination filename: ");


fgets(destinationFilename, sizeof(destinationFilename), stdin);
destinationFilename[strcspn(destinationFilename, "\n")] = '\0'; // Remove newline character

// Open the source file in read mode


sourceFile = fopen(sourceFilename, "r");
if (sourceFile == NULL) {
printf("Could not open source file %s for reading.\n", sourceFilename);
return 1; // Exit if the file cannot be opened
}

// Open the destination file in write mode


destinationFile = fopen(destinationFilename, "w");
if (destinationFile == NULL) {
printf("Could not open destination file %s for writing.\n", destinationFilename);
fclose(sourceFile); // Close the source file before exiting
return 1; // Exit if the file cannot be opened
}

N.LAXMAN 9963640581 Page 33


DATA SCIENCE I-SEM(C –LANGUAGE)

// Copy the contents from source file to destination file


while ((ch = fgetc(sourceFile)) != EOF) { // Read character by character until EOF
fputc(ch, destinationFile); // Write the character to the destination file
}

// Close both files


fclose(sourceFile);
fclose(destinationFile);

printf("Contents copied from %s to %s successfully.\n", sourceFilename, destinationFilename);


return 0;
}
17. Write a program to append the contents of one file to another file
#include <stdio.h>
#include <stdlib.h>

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

// Ask the user for the source and destination filenames


printf("Enter the source filename: ");
fgets(sourceFilename, sizeof(sourceFilename), stdin);
sourceFilename[strcspn(sourceFilename, "\n")] = '\0'; // Remove newline character

printf("Enter the destination filename: ");


fgets(destinationFilename, sizeof(destinationFilename), stdin);
destinationFilename[strcspn(destinationFilename, "\n")] = '\0'; // Remove newline character

// Open the source file in read mode


sourceFile = fopen(sourceFilename, "r");
if (sourceFile == NULL) {
printf("Could not open source file %s for reading.\n", sourceFilename);
return 1; // Exit if the file cannot be opened
}

// Open the destination file in append mode


destinationFile = fopen(destinationFilename, "a"); // Use "a" mode to append
if (destinationFile == NULL) {
printf("Could not open destination file %s for writing.\n", destinationFilename);
fclose(sourceFile); // Close the source file before exiting
return 1; // Exit if the file cannot be opened
}

// Append the contents from source file to destination file


while ((ch = fgetc(sourceFile)) != EOF) { // Read character by character until EOF
fputc(ch, destinationFile); // Write the character to the destination file
}

// Close both files


fclose(sourceFile);

N.LAXMAN 9963640581 Page 34


DATA SCIENCE I-SEM(C –LANGUAGE)

fclose(destinationFile);

printf("Contents appended from %s to %s successfully.\n", sourceFilename, destinationFilename);


return 0;
}
This is the content of the source file.
This is the content of the destination file.
Enter the source filename: source.txt
Enter the destination filename: destination.txt
Contents appended from source.txt to destination.txt successfully.
This is the content of the destination file.
This is the content of the source file.
18. Write a program to demonstrate the command line arguments.
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {


if (argc < 2) { // Check if at least one number is provided
printf("Usage: %s <number1> <number2> ... <numberN>\n", argv[0]);
return 1;
}

int sum = 0; // Variable to hold the sum

// Iterate over the command line arguments


for (int i = 1; i < argc; i++) {
sum += atoi(argv[i]); // Convert argument to integer and add to sum
}

// Display the result


printf("The sum of the provided numbers is: %d\n", sum);
return 0;
}

gcc -o sum_numbers sum_numbers.c


./sum_numbers 10 20 30 40
The sum of the provided numbers is: 100

N.LAXMAN 9963640581 Page 35

You might also like