You are on page 1of 21

Exp.

Name: Write the code to print its elements in


S.No: 41 Date: 2023-12-28
reverse order

Aim:

Page No: 71
John is learning about dynamic memory allocation in C. He wants to create an array of integers, dynamically
allocate memory, and then reverse the elements of the array. Write a C program to help John perform this
operation using dynamic memory allocation.

Input Format:

ID: 927623BEC081
• A single integer 'n' (1 <= n <= 100) represents the number of integers in the array.
• 'n' integers separated by spaces, providing the initial values of the array.

Output Format:
• A single line containing the reversed array.
Source Code:

reverse1.c

#include<stdio.h>
int main()
{

2023-2027-A4
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);

M.Kumarasamy College of Engineering


}
for(int i=n-1;i>=0;i--)
{
printf("%d ",a[i]);
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
5
11 22 33 44 55
55 44 33 22 11

Test Case - 2

User Output
9
459 964 2225 631 82 3118 5202 9852 235
235 9852 5202 3118 82 631 2225 964 459
Exp. Name: Write the code to find whether the
S.No: 42 Date: 2023-12-27
given string is lucky or not

Aim:

Page No: 72
Write a program to find whether the given string is Lucky or not. A string is said to be lucky if the sum of the
ASCII values of the characters in the string is even. Refer function specifications for the function details. The
function accepts a pointer to a string and returns an int. The return value is 1 if the string is lucky and 0 otherwise.

Input and Output Format: Input consists of a string. Assume that all characters in the string are lowercase letters

ID: 927623BEC081
and the maximum length of the string is 100. Refer sample input and output for formatting specifications.
Source Code:

luckyString.c

#include<stdio.h>
#include<string.h>
int lucky(char str[])
{
int sum=0;
for(int i=0;i<strlen(str);i++){
sum+=str[i];

2023-2027-A4
}
if(sum%2==0){
return 1;
}
else
return 0;

M.Kumarasamy College of Engineering


}
int main()
{
char str[100];
scanf("%s",&str);
if(lucky(str)==1)
printf("%s is lucky",str);
else
printf("%s is not lucky",str);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
anitha
anitha is not lucky

Test Case - 2

User Output
ram
ram is lucky
Exp. Name: Write a program to calculate array sum
S.No: 43 Date: 2023-12-27
using pointers

Aim:

Page No: 73
You are working on a project that requires you to develop a program in C that calculates the sum of elements in a
one-dimensional array using dynamic memory allocation. The array will be provided as input by the user. Your
task is to write a C program that can read the array elements, calculate their sum using pointers, and print the
result.

ID: 927623BEC081
Input format:
The first line contains an integer n, representing the number of elements in that array.
The next line contains an integer array arr[] with n space-separated values.

Output format:
Print the sum of the array of elements using pointers.
Source Code:

Pointerssum.c
#include<stdio.h>
int main()

2023-2027-A4
{
int n,sum=0;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
{

M.Kumarasamy College of Engineering


scanf("%d",&a[i]);
}
int (*p) [n];
p=&a;
for(int i=0;i<n;i++)
{
sum+=(*p)[i];
}
printf("%d\n",sum);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
4
22 22 22 22
88

Test Case - 2

User Output
5
0
0 -22 -44 22 44

M.Kumarasamy College of Engineering 2023-2027-A4 ID: 927623BEC081 Page No: 74


S.No: 44 Exp. Name: Menu driven C program Date: 2024-01-23

Aim:
Write a menu-driven C program that allows a user to enter n numbers and then choose between finding the

Page No: 75
smallest, largest, sum, or average. The menu and all the choices are to be functions. Use a switch statement to
determine what action to take. Display an error message if an invalid choice is entered.

Source Code:

ID: 927623BEC081
ProgramMenu.c

2023-2027-A4
M.Kumarasamy College of Engineering
#include <stdio.h>
void smallest(int[], int);
void largest(int[], int);
void sum(int[], int);
void average(int[], int);

Page No: 76
int main()
{
int n;
printf("Enter number : ");
scanf("%d",&n);

ID: 927623BEC081
int a[n],ch;
printf("Enter %d numbers : ",n);
for(int i = 0; i<n;i++){
scanf("%d",&a[i]);
}
printf("The menu driven is:\n");
printf("1.smallest\n");
printf("2.largest\n");
printf("3.sum\n");
printf("4.average\n");
printf("Enter an option : ");
scanf("%d",&ch);

2023-2027-A4
switch(ch)
{
case 1:
smallest (a,n);
break;
case 2:

M.Kumarasamy College of Engineering


largest (a, n);
break;
case 3:
sum(a,n);
break;
case 4:
average(a, n);
break;
default:
printf("Invalid choice\n");
break;
}
}
void smallest(int arr[], int n) {
int min=arr[0];
for(int i=1;i<n;i++){
if(arr[i]<min){
min=arr[i];
}
}
printf("The smallest element is %d\n",min); }
void largest(int arr[], int n) {
int max=arr[0];
for(int i=1;i<n;i++){
if(arr[i]>max) {
max=arr[i];
}}
void sum(int arr[], int n) {
int sum=0;
for(int i=0;i<n;i++){
sum+=arr[i];
}

Page No: 77
printf("The sum of all elements is %d\n", sum); }
void average(int arr[], int n) {
float sum=0;
for(int i=0;i<n;i++){
sum+=arr[i];

ID: 927623BEC081
}
printf("The average of all elements is %f\n",
(float)sum/n);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output

2023-2027-A4
Enter number :
5
Enter 5 numbers :
6 7 8 9 12
The menu driven is:

M.Kumarasamy College of Engineering


1.smallest
2.largest
3.sum
4.average
Enter an option :
5
Invalid choice

Test Case - 2

User Output
Enter number :
4
Enter 4 numbers :
6897
The menu driven is:
1.smallest
2.largest
3.sum
4.average
Enter an option :
4
The average of all elements is 7.500000
Test Case - 3

User Output
Enter number :
4

Page No: 78
Enter 4 numbers :
10 20 30 40
The menu driven is:
1.smallest

ID: 927623BEC081
2.largest
3.sum
4.average
Enter an option :
2
The largest element is 40

Test Case - 4

User Output

2023-2027-A4
Enter number :
6
Enter 6 numbers :
11 2 33 4 55 6
The menu driven is:

M.Kumarasamy College of Engineering


1.smallest
2.largest
3.sum
4.average
Enter an option :
3
The sum of all elements is 111

Test Case - 5

User Output
Enter number :
4
Enter 4 numbers :
24 25 26 28
The menu driven is:
1.smallest
2.largest
3.sum
4.average
Enter an option :
1
The smallest element is 24
Exp. Name: Write a function to compute mean,
S.No: 45 variance, Standard Deviation, sorting of n Date: 2024-01-23
elements in single dimension array.

Page No: 79
Aim:
Write a C program to compute the mean, variance, Standard Deviation, and sorting of n elements in a single-
dimension array using functions.
Source Code:

ID: 927623BEC081
MeanVariance.c

2023-2027-A4
M.Kumarasamy College of Engineering
#include <stdio.h>
#include <math.h>
void calculateMean(int [], int);
float calculateVariance(int [], int);
float calculateStandardDeviation(int [], int);

Page No: 80
void calculateSort(int [], int);
void main() {
int arr[20], number;
float variance = 0, standardDeviation = 0;
printf("Enter size of the array : ");

ID: 927623BEC081
scanf("%d", &number);
printf("Enter array elements : ");
for (int i = 0; i < number; i++) {
scanf("%d", &arr[i]);
}
calculateMean(arr, number);
variance = calculateVariance(arr, number);
printf("The variance of elements of the array : %f\n", variance);
standardDeviation = calculateStandardDeviation(arr, number);
printf("The Standard Deviation of elements of the array : %f\n", standardDeviation);
calculateSort(arr, number);
printf("The elements in array after sorting :");

2023-2027-A4
for (int i = 0; i < number; i++) {
printf(" %d", arr[i]);
}
printf("\n");
}
void calculateMean(int arr[], int n) {

M.Kumarasamy College of Engineering


float mean = 0;

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


mean += arr[i];
}

mean /= n;

printf("The mean of elements of the array : %f\n", mean);


}

float calculateVariance(int arr[], int n) {


float mean = 0, variance = 0;

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


mean += arr[i];
}

mean /= n;

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


variance += pow(arr[i] - mean, 2);
}

variance /= n;

return variance;
float calculateStandardDeviation(int arr[], int n) {
float variance = calculateVariance(arr, n);
return sqrt(variance);
}

Page No: 81
void calculateSort(int arr[], int n) {
int temp;

for (int i = 0; i < n - 1; i++) {


for (int j = 0; j < n - i - 1; j++) {

ID: 927623BEC081
if (arr[j] > arr[j + 1]) {
// Swap elements if they are in the wrong order
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
//Write your code here...

2023-2027-A4
Execution Results - All test cases have succeeded!
Test Case - 1

User Output

M.Kumarasamy College of Engineering


Enter size of the array :
5
Enter array elements :
24680
The mean of elements of the array : 4.000000
The variance of elements of the array : 8.000000
The Standard Deviation of elements of the array : 2.828427
The elements in array after sorting : 0 2 4 6 8

Test Case - 2

User Output
Enter size of the array :
6
Enter array elements :
357268
The mean of elements of the array : 5.166667
The variance of elements of the array : 4.472222
The Standard Deviation of elements of the array : 2.114763
The elements in array after sorting : 2 3 5 6 7 8
Exp. Name: Student Information Management
S.No: 46 Date: 2024-01-23
System

Aim:

Page No: 82
Write a C program to build a simple Application Software for Student Information Management System which can
perform the following operations:
• Store the First name of the student.
• Store the Last name of the student.
• Store the Unique Roll number for every student.

ID: 927623BEC081
• Store the CGPA of every student.
• Store the Courses Registered by the student.
Source Code:

management_sys.c

2023-2027-A4
M.Kumarasamy College of Engineering
#include <stdio.h>
#include <string.h>
struct Student {
char firstname[50];
char lastname[50];

Page No: 83
int rollnumber;
float cgpa;
char courses[5][50];
int a;
};

ID: 927623BEC081
int main(){
struct Student student;
printf("Student Information Management System\n");
printf("First Name: ");
scanf("%s",student.firstname);
printf("Last Name: ");
scanf("%s",student.lastname);
printf("Roll Number: ");
scanf("%d",&student.rollnumber);
printf("CGPA: ");
scanf("%f",&student.cgpa);

2023-2027-A4
printf("number of courses(up to 5): ");
scanf("%d",&student.a);

if(student.a>5){
printf("Maximum 5 courses can be registered\n");
return 1;

M.Kumarasamy College of Engineering


}
printf("names of courses:\n");
for(int i=0;i<student.a;i++){
scanf("%s",student.courses[i]);
}
printf("Student Information:\n");
printf("First Name: %s\n",student.firstname);
printf("Last Name: %s\n",student.lastname);
printf("Roll Number: %d\n",student.rollnumber);
printf("CGPA: %.2f\n",student.cgpa);
printf("Courses Registered:\n");
for(int i=0;i<student.a;i++){
printf("%s\n",student.courses[i]);
}
return 0;
};

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Student Information Management System
First Name:
Last Name:
Kare
Roll Number:
23
CGPA:

Page No: 84
9.21
number of courses(up to 5):
3
names of courses:

ID: 927623BEC081
Economics
Mathematics
Commerce
Student Information:
First Name: william
Last Name: Kare
Roll Number: 23
CGPA: 9.21
Courses Registered:
Economics
Mathematics

2023-2027-A4
Commerce

Test Case - 2

User Output

M.Kumarasamy College of Engineering


Student Information Management System
First Name:
Peter
Last Name:
Zion
Roll Number:
12
CGPA:
7.9
number of courses(up to 5):
7
Maximum 5 courses can be registered
S.No: 47 Exp. Name: Expense manager Date: 2024-01-22

Aim:
Write a C program to develop an expense manager that reads date, product, price, and product category. The

Page No: 85
program should display the total expense amount based on product category or date as per the user's selection
using structures.

Note: The driver function is provided to you in the editor.


Source Code:

ID: 927623BEC081
expense_manager.c

2023-2027-A4
M.Kumarasamy College of Engineering
#include <stdio.h>
#include <string.h>
#define MAX_EXPENSES 100
// Structure to hold expense details
struct Expense {

Page No: 86
char date[11]; // Store date as string (YYYY-MM-DD)
char product[50];
float price;
char category[20];
};

ID: 927623BEC081
// Function to calculate total expense by category
float totalExpenseByCategory(struct Expense expenses[], int count, char category[])
{
float total = 0;
for (int i = 0; i < count; ++i) {
if (strcmp(expenses[i].category, category) == 0) {
total += expenses[i].price;
}
}
return total;
}
// Function to calculate total expense by date

2023-2027-A4
float totalExpenseByDate(struct Expense expenses[], int count, char date[]) {
float total = 0;
for (int i = 0; i < count; ++i) {
if (strcmp(expenses[i].date, date) == 0) {
total += expenses[i].price;
}

M.Kumarasamy College of Engineering


}
return total;
}

int main() {
int choice;
char category[50];
char date[11];

Page No: 87
printf("Expense Manager\n");

while (1) {
printf("Options:\n");
printf("1. Add Expense\n");

ID: 927623BEC081
printf("2. Calculate Total Expense by Category\n");
printf("3. Calculate Total Expense by Date\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

if (choice == 1) {
if (numExpenses < MAX_EXPENSES) {
printf("Enter date (YYYY-MM-DD): ");
scanf("%s", expenses[numExpenses].date);
printf("Enter product name: ");
scanf("%s", expenses[numExpenses].product);

2023-2027-A4
printf("Enter price: ");
scanf("%f", &expenses[numExpenses].price);
printf("Enter category: ");
scanf("%s", expenses[numExpenses].category);
numExpenses++;
} else {

M.Kumarasamy College of Engineering


printf("Expense list is full!\n");
}
} else if (choice == 2) {
printf("Enter category: ");
scanf("%s", category);
float total = totalExpenseByCategory(expenses, numExpenses, category);
printf("Total expense in category '%s' is %.2f\n", category, total);
} else if (choice == 3) {
printf("Enter date (YYYY-MM-DD): ");
scanf("%s", date);
float total = totalExpenseByDate(expenses, numExpenses, date);
printf("Total expense on date '%s' is %.2f\n", date, total);
} else if (choice == 4) {
break;
} else {
printf("Invalid choice\n");
}
}

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1
User Output
Expense Manager
Options:
1. Add Expense
2. Calculate Total Expense by Category

Page No: 88
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
1

ID: 927623BEC081
Enter date (YYYY-MM-DD):
2023-10-12
Enter product name:
Moisturizer
Enter price:
250
Enter category:
Skincare
Options:
1. Add Expense

2023-2027-A4
2. Calculate Total Expense by Category
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
1
Enter date (YYYY-MM-DD):

M.Kumarasamy College of Engineering


2023-10-12
Enter product name:
Riceflour
Enter price:
100
Enter category:
Food
Options:
1. Add Expense
2. Calculate Total Expense by Category
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
1
Enter date (YYYY-MM-DD):
2023-11-14
Enter product name:
Facewash
Enter price:
300
Enter category:
Skincare
Options:
1. Add Expense
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
2
Enter category:

Page No: 89
Skincare
Total expense in category 'Skincare' is 550.00
Options:
1. Add Expense

ID: 927623BEC081
2. Calculate Total Expense by Category
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
3
Enter date (YYYY-MM-DD):
2023-10-12
Total expense on date '2023-10-12' is 350.00
Options:
1. Add Expense
2. Calculate Total Expense by Category

2023-2027-A4
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
5
Invalid choice

M.Kumarasamy College of Engineering


Options:
1. Add Expense
2. Calculate Total Expense by Category
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
2
Enter category:
Groceries
Total expense in category 'Groceries' is 0.00
Options:
1. Add Expense
2. Calculate Total Expense by Category
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
3
Enter date (YYYY-MM-DD):
2024-07-09
Total expense on date '2024-07-09' is 0.00
Options:
1. Add Expense
2. Calculate Total Expense by Category
3. Calculate Total Expense by Date
4. Exit
4

M.Kumarasamy College of Engineering 2023-2027-A4 ID: 927623BEC081 Page No: 90

You might also like