0% found this document useful (0 votes)
34 views28 pages

2nd Sem POP Lab Programs

The document contains a series of C programming exercises that cover various topics such as leap year determination, finding the tallest person, calculating GCD and LCM, checking for palindromes, sorting arrays, searching for elements, matrix multiplication, swapping values, prime number checking, and managing student records. Each program includes code snippets and explanations on how to implement the respective functionalities. The exercises are designed to help students practice and understand fundamental programming concepts in C.

Uploaded by

nithyavais09
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)
34 views28 pages

2nd Sem POP Lab Programs

The document contains a series of C programming exercises that cover various topics such as leap year determination, finding the tallest person, calculating GCD and LCM, checking for palindromes, sorting arrays, searching for elements, matrix multiplication, swapping values, prime number checking, and managing student records. Each program includes code snippets and explanations on how to implement the respective functionalities. The exercises are designed to help students practice and understand fundamental programming concepts in C.

Uploaded by

nithyavais09
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

/

LAB PROGRAMS
Program Statement 1: Secure life, an insurance company issues special returns to its
customers on leap years. Given a year, write a C program to find whether the customer
is due for special returns are not. (use ternary operator)

//Program to find whether the year is a Leap year or not


#include <stdio.h>
int main()
{
int year;
int result;
printf("Ënter the year\n");
scanf("%d", &year);
// a year is a leap year if it is divisible by 4 but not by 100, except those years
divisible by 400
result = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? printf("%d
is a leap year\n", year) : printf("%d is not a leap year\n", year);
return 0;
}

Result:
Program Statement 2: The tallest of the three pupils are to be selected for the basketball
tournament held in the university next year. Write a C program using branching
statements to find the candidate selected for the tournament. (if else)
//Program to find the tallest of three people
#include <stdio.h>
int main()
{
int tallest;
int ht_p1, ht_p2, ht_p3;
printf("Enter the height of three persons in cm\n");
scanf ("%d %d %d", &ht_p1, & ht_p2, &ht_p3);
if (ht_p1 < ht_p2)
{
if (ht_p2 < ht_p3)
tallest = ht_p3;
else
tallest = ht_p2;
}
Else
{
if (ht_p1 < ht_p3)
tallest = ht_p3;
else
tallest = ht_p1;
}
printf ("Tallest is %d\n", tallest);
return 0;
}
Result:
`

Program Statement 3: A person is making identical balloon arrangements for a party.


He has X maroon balloons and Y white balloons. He wants each arrangement to have
the same number of each colour. Design a C program to find greatest (GCD) and Least
(LCD) number of arrangements using Euclid’s algorithm to make use of every balloon.

//To find GCD and LCM of any two numbers


#include <stdio.h>
void main()
{
int num1, num2, gcd, lcm, remainder, numerator, denominator;

printf("Enter two numbers:\n");


scanf("%d %d", &num1, &num2);

//To find numerator and denominator


numerator = (num1>num2)? num1:num2;
denominator = (num1<num2)? num1:num2;
remainder = numerator % denominator;

while (remainder != 0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}
gcd = denominator;
lcm = num1 * num2 / gcd;
printf("GCD of %d and %d = %d\n", num1, num2, gcd);
printf("LCM of %d and %d = %d\n", num1, num2, lcm);
}

Result:
Program Statement 4: A Criminal leaves a four digit number as his calling card, Design
an algorithm and develop a C program to test the four digit number left by the criminal
is palindrome or not.

//Program to find whether the number is Palindrome or not


#include<stdio.h>
int main()
{
int original, n, reversed, remainder;
printf(“Enter a number \n”);
scanf(“%d”, &n);
if(n > 9999 || n < 1000)
{
printf(“Error: Invalid entry of the number \n”);
return 0;
}
original = n;
reversed = 0;
while(n != 0)
{
remainder = n % 10;
n = n / 10;
reversed = reversed * 10 + remainder;
}
printf(“The reverse of %d is %d \n”, original, reversed);
if(original == reversed)
printf(“It is a Palindrome \n”);
else
printf(“It is not a Palindrome \n”);
return 0;

Result:
Program Statement 5: The books in the library are randomly placed on the shelves.
Design a C program that sorts the book based on the ISBN, use bubble sort to
implement the program.

// Program to sort an array in ascending order using Bubble Sort method


#include<stdio.h>
main()
{
int a[10], n, i, j, temp;
printf("Enter the number of elements \n");
scanf("%d", &n);
if(n > 100 || n <= 0)
{
printf("Error in the range \n");
return;
}
printf("Enter the array elements \n");
for(i=0 ; i<n ; i++)
scanf("%d", &a[i]);
for(i=0 ; i<n-1 ; i++)
{
for(j=0 ; j<n-i-1 ; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}

}
printf("Sorted Elements are \n");
for(i=0 ; i<n ; i++)
printf("%d \t", a[i]);
}

Result:
Program Statement 6: Given an ISBN, design a C program to search and display the
book if present in the library. Use binary search to design the program.

//Program to search an element in an array


#include<stdio.h>
main()
{
int a[10], n, i, key, low = 0, high, mid, pos=0, c=0;
printf("Enter the number of elements \n");
scanf("%d", &n);
printf("Enter the array elements \n");
for(i=0 ; i<n ; i++)
scanf("%d", &a[i]);
printf("Enter the element to be searched \n");
scanf("%d", &key);
high = n-1;
while(low <= high)
{
mid = (low + high) / 2;
if(a[mid] == key)
{ pos = mid;
c = 1;
break;
}
else if(a[mid] > key)
high = mid - 1;
else
low = mid + 1;
}
if(c == 0)

printf("Element not found \n");


else
printf("Element found at position %d \n", pos);
return 0;
}

Result:
Program Statement 7: In a Google web search engine, the user types a string. Design a
C program to check if a sub string is present in the given string.
#include <stdio.h>
#include <string.h>

int main() {
char inputString[1000];
char subString[100];

// Input the main string


printf("Enter the main string: ");
gets(inputString);

// Input the substring to search for


printf("Enter the substring to search for: ");
gets(subString);

// Check if the substring is present in the main string


if (strstr(inputString, subString) != NULL) {
printf("Substring '%s' is present in the main string.\n", subString);
} else {
printf("Substring '%s' is not present in the main string.\n", subString);
}

return 0;
}

Enter the main string: hello


Enter the substring to search for: llo
Substring 'llo' is present in the main string.

Program Statement 8: Design, develop and execute a program in C to read two matrices
A(M × N) and B(p × q) and compute the product of A and B.
//Program to find Matrix Multiplication
#include <stdio.h>
int main()
{
int a[10][10], b[10][10], c[10][10], i, j, m, n, p, q, k;
printf("Enter the order Matrix A: \n");
scanf("%d %d", &m, &n);
if(m<1 || n<1)
{
printf("Error: Kindly enter valid order \n");
return -1;
}
printf("Enter the order Matrix B: \n");
scanf("%d %d", &p, &q);
if(p<1 || q<1)
{
printf("Error: Kindly enter valid order \n");
return -1;
}
if(n != p)
{
printf("Error: Matrix Multiplication is not possible \n");
return -1;
}
printf("Enter the elements of Matrix A :\n");
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
scanf("%d", &a[i][j]);

printf("Enter the elements of Matrix B :\n");


for (i = 0; i < p; ++i)
for (j = 0; j < q; ++j)
scanf("%d", &b[i][j]);
for (i = 0; i < m; ++i)
{ for (j = 0; j < q; ++j)
{ c[i][j] = 0;
for (k = 0; k < p; ++k)
c[i][j] = c[i][j] + (a[i][k] * b[k][j]);
}
}
printf("Multiplication of the two Matrices is \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < q; ++j)
{ printf("%d \t", c[i][j]);
}
printf("\n");
}
}

Result:
Program Statement 9: You are given two envelopes, each containing money. Design,
develop and execute a program in C to swap two envelopes using following methods.
a. Call by value
b. Call by reference

9a. Program to demonstrate the use of Call by Value to swap two numbers
#include<stdio.h>
swap(int x, int y);
main()
{
int a,b;
printf("Enter any two values \n");
scanf("%d %d", &a, &b);
printf("The values before function call a is %d and b is %d \n", a, b);
swap(a,b);
printf("The values after function call a is %d and b is %d \n", a, b);
}
swap(int x, int y)
{ int t;
t = x;
x = y;
y = t;
printf("The values after swapping a is %d and b is %d inside the function \n", x,
y);
}

Result:
9b. Program to demonstrate the use of Call by Reference to swap two numbers
#include<stdio.h>
swap(int *a, int *b);
main()
{
int a,b;
printf("Enter any two values \n");
scanf("%d %d", &a, &b);
printf("The values before swapping a is %d and b is %d \n", a, b);
swap(&a,&b);
printf("The values after swapping outside the function a is %d and b is %d \n",
a, b);
}
swap(int *a, int *b)
{
int t;
t = *a;
*a = *b;
*b = t;
printf("The values after swapping inside the function a is %d and b is %d \n",
*a, *b);
}

Result:
Program Statement 10: You have been asked to arrange lucky draw in college and the
person who picks a prime number always wins the prize. Design a C program to test if
the participant has won or lost.
//Program to find whether given number is prime
#include <stdio.h>

int main() {
int pickedNumber;
// Input the number picked by the participant
printf("Enter the picked number: ");
scanf("%d", &pickedNumber);

int isPrime = 1; // 1 indicates prime, 0 indicates not prime

if (pickedNumber <= 1) {
isPrime = 0;
} else {
for (int i = 2; i * i <= pickedNumber; i++) {
if (pickedNumber % i == 0) {
isPrime = 0;
break;
}
}
}

if (isPrime) {
printf("Congratulations! You've won the prize. The picked number %d is prime.\
n", pickedNumber);
} else {
printf("Sorry, you haven't won the prize. The picked number %d is not prime.\n",
pickedNumber);
}

return 0;
}

output:
case1:
Enter the picked number: 21
Sorry, you haven't won the prize. The picked number 21 is not prime.
case2:Enter the picked number: 7
Congratulations! You've won the prize. The picked number 7 is prime.
Program Statement 11: In the university to maintain student database for result
calculation, using structures design a C program that accepts the details such as Roll
no, student name and marks of three subjects of ‘n’ students and print their details
along with their total marks.
#include <stdio.h>
// Define a structure to store student details
struct Student {
int rollNumber;
char name[50];
int marks[3]; // Array to store marks for three subjects
int totalMarks;
};

int main() {
int n; // Number of students
printf("Enter the number of students: ");
scanf("%d", &n);

// Declare an array of structures to store student information


struct Student students[n];
// Input student details
for (int i = 0; i < n; i++) {
printf("Enter details for Student %d:\n", i + 1);
printf("Roll Number: ");
scanf("%d", &students[i].rollNumber);
printf("Name: ");
scanf(" %[^\n]s", students[i].name); // Read the name with spaces
printf("Enter marks for three subjects:\n");
for (int j = 0; j < 3; j++) {
printf("Subject %d: ", j + 1);
scanf("%d", &students[i].marks[j]);
}
}

// Calculate total marks and print student details


printf("\nStudent Details and Total Marks:\n");
for (int i = 0; i < n; i++) {
students[i].totalMarks = 0; // Initialize total marks
printf("Student %d:\n", i + 1);
printf("Roll Number: %d\n", students[i].rollNumber);
printf("Name: %s\n", students[i].name);
printf("Marks for three subjects: ");
for (int j = 0; j < 3; j++) {
students[i].totalMarks += students[i].marks[j]; // Calculate total marks
printf("%d ", students[i].marks[j]);
}
printf("\nTotal Marks: %d\n", students[i].totalMarks);
}

return 0;
}

Enter the number of students: 2


Enter details for Student 1:
Roll Number: 1
Name: a
Enter marks for three subjects:
Subject 1: 35
Subject 2: 67
Subject 3: 45
Enter details for Student 2:
Roll Number: 2
Name: b
Enter marks for three subjects:
Subject 1: 78
Subject 2: 45
Subject 3: 23

Student Details and Total Marks:


Student 1:
Roll Number: 1
Name: a
Marks for three subjects: 35 67 45
Total Marks: 147
Student 2:
Roll Number: 2
Name: b
Marks for three subjects: 78 45 23
Total Marks: 146

Program Statement 12 & 13: The local government school needs simple recording
software which accepts names of the students in random order prepares the nominal
roll in alphabetical order. Help the school by designing a simple c program to sort the
given names and display using sorted names using file handling functions.

Solution:
In this program, we assume that you have a text file named "student_names.txt" in the
same directory as the program, containing one student name per line. The program reads the
names from the file, sorts them using the qsort function with the compare function for
comparison, and then writes the sorted names to a new file named "sorted_names.txt".
Finally, it displays the sorted names on the console.

To run this program, save it with a ".c" extension (e.g., "name_sorter.c"), compile it
using a C compiler, and then execute the resulting binary. Make sure you have the
"student_names.txt" file in the same directory as the program. After running the program, you
will find the sorted names in the "sorted_names.txt" file and see the sorted names displayed
on the console.

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

#define MAX_NAME_LENGTH 100

// Function to compare two strings for sorting in alphabetical order


int compare(const void *a, const void *b) {
return strcmp((const char *)a, (const char *)b);
}

int main() {
char names[MAX_NAME_LENGTH][MAX_NAME_LENGTH];
int numNames = 0;

// Open the file for reading


FILE *file = fopen("student_names.txt", "r");
if (file == NULL) {
printf("Failed to open the file.\n");

return 1;
}
// Read the names from the file
char name[MAX_NAME_LENGTH];
while (fgets(name, sizeof(name), file) != NULL) {
name[strcspn(name, "\n")] = '\0'; // Remove newline character
strcpy(names[numNames], name);
numNames++;
}

// Close the file


fclose(file);

// Sort the names in alphabetical order


qsort(names, numNames, sizeof(names[0]), compare);

// Open a new file for writing the sorted names


file = fopen("sorted_names.txt", "w");
if (file == NULL) {
printf("Failed to create the file.\n");
return 1;
}

// Write the sorted names to the file and display them


printf("Sorted names:\n");
for (int i = 0; i < numNames; i++) {
printf("%s\n", names[i]);
fprintf(file, "%s\n", names[i]);
}

// Close the file


fclose(file);
return 0;
}

Program Statement 14: Design a C program to demonstrate to pass or return a


structure to/from a Function using call by value and call by reference method. Use
Employee details (EMPID, EMPNAME, EMPSAL, DEPARTMENT) for the structure.

Solution:
In this program, we define a structure called Employee with fields EMPID, EMPNAME,
EMPSAL, and DEPARTMENT. We then have two functions: modify Employee CallBy
Value, which takes an Employee structure as a parameter by value and modifies its contents,
and modify Employee CallBy Reference, which takes an Employee structure as a parameter
by reference (using a pointer) and modifies its contents.

The display Employee function is used to display the details of an Employee structure. In the
main function, we create two Employee structures, emp1 and emp2. We pass emp1 to modify
Employee CallBy Value to modify its contents and then display the modified details.
Similarly, we pass the address of emp2 to modify Employee CallBy Reference to modify its
contents and display the modified details.

When you run the program, you will see the employee details before and after modification,
demonstrating both call by value and call by reference methods.
#include <stdio.h>
#include <string.h>

// Structure definition for Employee


struct Employee {
int EMPID;
char EMPNAME[100];
float EMPSAL;
char DEPARTMENT[100];
};

// Function to modify the Employee structure using call by value


void modifyEmployeeCallByValue(struct Employee emp) {
[Link] = 1001;
strcpy([Link], "John");
[Link] = 5000.0;
strcpy([Link], "IT");

}
// Function to modify the Employee structure using call by reference
void modifyEmployeeCallByReference(struct Employee *emp) {
emp->EMPID = 1002;
strcpy(emp->EMPNAME, "Jane");
emp->EMPSAL = 6000.0;
strcpy(emp->DEPARTMENT, "HR");
}
// Function to display the Employee structure
void displayEmployee(struct Employee emp) {
printf("EMPID: %d\n", [Link]);
printf("EMPNAME: %s\n", [Link]);
printf("EMPSAL: %.2f\n", [Link]);
printf("DEPARTMENT: %s\n", [Link]);
}
int main() {
struct Employee emp1, emp2;
// Pass the structure by value and modify its contents
modifyEmployeeCallByValue(emp1);
printf("Employee details after modification using call by value:\n");
displayEmployee(emp1);
printf("\n");
// Pass the structure by reference and modify its contents
modifyEmployeeCallByReference(&emp2);
printf("Employee details after modification using call by reference:\n");
displayEmployee(emp2);
printf("\n");

return 0;
}

Program Statement 15: Nidhi a second semester engineering students wants to learn
difference between a structure and unions in C programming, help her to distinguish
the same with a program to read and print her details using structure and unions.
Solution:
In this program, we define a structure named Student Structure and a union named Student
Union to represent Nidhi's details. The structure has separate fields for roll number, name,
and CGPA, while the union shares the same memory space for these fields.

In the main function, we create an instance of the structure student Structure and an instance
of the union student Union to store Nidhi's details. We use scanf to read Nidhi's roll number,
name, and CGPA separately for both the structure and the union.

After reading the details, we print Nidhi's information using both the structure and the union.
In the structure case, each field is accessed individually using dot notation (student Structure.
Roll Number, [Link], student Structure. cgpa). In the union case, since all the
fields share the same memory space, we can access any field and get the value that was most
recently assigned.

When you run the program, it will prompt you to enter Nidhi's roll number, name, and CGPA
separately for both the structure and the union. After that, it will display the entered details
using both the structure and the union. This will help Nidhi understand the difference
between structures and unions in C programming.

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

// Structure definition for Nidhi using structure


struct Student_Structure {
int rollNumber;
char name[100];
float cgpa;
};

// Union definition for Nidhi using union


union Student_Union {
int rollNumber;
char name[100];
float cgpa;
};

int main() {
// Using Structure
struct Student_Structure studentStructure;
printf("Enter Nidhi's roll number: ");
scanf("%d", &[Link]);
printf("Enter Nidhi's name: ");
scanf(" %[^\n]s", [Link]);

printf("Enter Nidhi's CGPA: ");


scanf("%f", &[Link]);

printf("\nStudent details using Structure:\n");


printf("Roll Number: %d\n", [Link]);
printf("Name: %s\n", [Link]);
printf("CGPA: %.2f\n", [Link]);
// Using Union
union Student_Union studentUnion;
printf("\nEnter Nidhi's roll number: ");
scanf("%d", &[Link]);
printf("Enter Nidhi's name: ");
scanf(" %[^\n]s", [Link]);
printf("Enter Nidhi's CGPA: ");
scanf("%f", &[Link]);

printf("\nStudent details using Union:\n");


printf("Roll Number: %d\n", [Link]);
printf("Name: %s\n", [Link]);
printf("CGPA: %.2f\n", [Link]);

return 0;
}

You might also like