0% found this document useful (0 votes)
30 views20 pages

BX 4002

The document contains practical records for a computer science course at PGP College of Engineering and Technology, detailing various programming exercises in C and Python. Each exercise includes the aim, program code, output, and result, covering topics like I/O operations, conditional statements, nested loops, arrays, prime numbers, recursion, string concatenation, structures, and file handling. The document serves as a certification of the practical work done by a student during the academic year 2023-2024.

Uploaded by

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

BX 4002

The document contains practical records for a computer science course at PGP College of Engineering and Technology, detailing various programming exercises in C and Python. Each exercise includes the aim, program code, output, and result, covering topics like I/O operations, conditional statements, nested loops, arrays, prime numbers, recursion, string concatenation, structures, and file handling. The document serves as a certification of the practical work done by a student during the academic year 2023-2024.

Uploaded by

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

lOMoARcPSD|46270988

BX4002

Masters of computer applications (Madras Institute of Technology, Anna University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Judith Petrizia (petri.success@gmail.com)
lOMoARcPSD|46270988

PGP COLLEGE OF ENGINEERING AND TECHNOLOGY


NAMAKKAL – 637 207

NAME : …………………………………………........................

REG. NO : …………………………………………........................

YEAR/SEM : …………………………………………........................

BRANCH : …………………………………………........................

Downloaded by Judith Petrizia (petri.success@gmail.com)


lOMoARcPSD|46270988

PGP COLLEGE OF ENGINEERING AND TECHNOLOGY


NAMAKKAL – 637 207

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CERTIFICATE

This is to certify that bonafide record work done by Mr /Ms…………………………...…………

Register Number ………………………… - BX4002 - PROBLEM SOLVING AND

PROGRAMMING IN C PRACTICAL during the academic year 2023-2024(ODD SEMESTER).

Faculty In-Charge Head of Department

Submitted for University Practical Examination held on

INTERNAL EXAMINER EXTERNAL EXAMINER

Downloaded by Judith Petrizia (petri.success@gmail.com)


lOMoARcPSD|46270988

INDEX
S. PAGE
DATE EXPERIMENTS MARK SIGN
NO NO.

Downloaded by Judith Petrizia (petri.success@gmail.com)


lOMoARcPSD|46270988

Ex.No : IO Operations

Date :

Aim :
To Write programs to get some input , perform some operation and display the output
using I/O statements

Program :
def main():
# Get input from the user
name = input("Enter your name: ")

# Perform operation
greeting = "Hello, " + name + "!"

# Display output
print(greeting)

if __name__ == "__main__":
main()

Output:

5
10
15
20

Result :

Thus the IO Operations are executed and verified successfully.

Downloaded by Judith Petrizia (petri.success@gmail.com)


lOMoARcPSD|46270988

Ex.No : Conditional Statements

Date :

Aim :
To Write a program to execute some specific statements based on the test condition

Program :

def main():
# Test condition
x = 10

# Execute specific statements based on the test condition


if x > 5:
# Statements to execute if condition is true
print("x is greater than 5")
print("Executing specific statements...")

else:
# Statements to execute if condition is false
print("x is not greater than 5")
print("Executing alternative specific statements...")

if __name__ == "__main__":
main ()
Output:
The variable x is assigned the value 10.
The if statement checks if x is greater than 5.
If the condition is true, it executes specific statements inside the if block.
If the condition is false, it executes specific statements inside the else block.

Result :

Thus the Conditional Statements are executed and verified successfully.

Downloaded by Judith Petrizia (petri.success@gmail.com)


lOMoARcPSD|46270988

Ex.No : Nested loop

Date :

Aim :
To Write programs to implement nested loop

Program :

def multiplication_table(n):
# Nested loop to generate multiplication table
for i in range(1, n+1):
for j in range(1, 11):
print(i, "*", j, "=", i*j)
print() # Print an empty line after each row

# Call the function to print multiplication table up to 5


multiplication_table(5)

Output:
This program prints the multiplication table up to a specified number n. It uses a nested loop
with one loop iterating over the numbers from 1 to n (the outer loop), and another loop
iterating over the numbers from 1 to 10 (the inner loop) to generate the multiplication table
for each number.

Result :

Thus the Nested loop are executed and verified successfully.

Downloaded by Judith Petrizia (petri.success@gmail.com)


lOMoARcPSD|46270988

Ex.No : Array

Date :

Aim :
To Write a program in C to get the largest element of an array using the function

Program :

#include <stdio.h>

// Function to find the largest element in an array


int findLargest(int arr[], int size) {
int max = arr[0]; // Assume the first element as the maximum

// Iterate through the array to find the maximum element


for (int i = 1; i < size; i++) {
if (arr[i] > max) {
max = arr[i];
}
}

return max;
}

int main() {
int arr[] = {5, 10, 3, 8, 15};
int size = sizeof(arr) / sizeof(arr[0]);

// Call the function to find the largest element


int largest = findLargest(arr, size);

// Print the result


printf("The largest element in the array is: %d\n", largest);
return 0;
}

Downloaded by Judith Petrizia (petri.success@gmail.com)


lOMoARcPSD|46270988

Output:
 The findLargest function takes an array arr and its size as parameters and returns the largest
element in the array.
 Initially, it assumes the first element of the array as the maximum.
 Then, it iterates through the array starting from the second element and updates the maximum
if it finds a larger element.
 In the main function, an array arr is defined with some elements, and its size is calculated.
 The findLargest function is called with the array and its size as arguments, and the result is
printed.

Result :

Thus the Array Function are executed and verified successfully.

Downloaded by Judith Petrizia (petri.success@gmail.com)


lOMoARcPSD|46270988

Ex.No : Prime Number

Date :

Aim :
To Display all prime numbers between two intervals using functions.

Program :

#include <stdio.h>
#include <stdbool.h>

// Function to check if a number is prime


bool isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}

// Function to display prime numbers between two intervals


void displayPrimeNumbers(int start, int end) {
printf("Prime numbers between %d and %d are:\n", start, end);
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
printf("%d ", i);
}
}
printf("\n");
}

Downloaded by Judith Petrizia (petri.success@gmail.com)


lOMoARcPSD|46270988

int main() {
int start, end;

// Input two intervals


printf("Enter the start and end of the interval: ");
scanf("%d %d", &start, &end);

// Call the function to display prime numbers between the intervals


displayPrimeNumbers(start, end);

return 0;
}

Explanation:

The isPrime function checks if a number is prime. It returns true if the number is prime, and
false otherwise.
The displayPrimeNumbers function takes two integers start and end as input and prints all
prime numbers between these two intervals.
In the main function, it prompts the user to input the start and end of the interval.
Then, it calls the displayPrimeNumbers function with the provided intervals as arguments.
Inside the displayPrimeNumbers function, it iterates through the numbers from start to end
and checks if each number is prime using the isPrime function. If a number is prime, it prints it.
Result :

Thus the Prime Number programs are executed and verified successfully.

Downloaded by Judith Petrizia (petri.success@gmail.com)


lOMoARcPSD|46270988

Ex.No : Recursion

Date :

Aim :
To Reverse a sentence using recursion

Program :

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

// Function to reverse a sentence using recursion


void reverseSentence(char sentence[]) {
// Base case: if the string is empty or contains only one word
if (strlen(sentence) == 0 || strchr(sentence, ' ') == NULL) {
printf("%s ", sentence);
return;
}

// Find the first occurrence of space


char *firstSpace = strchr(sentence, ' ');
if (firstSpace == NULL) { // If there's no space, it's the last word
printf("%s", sentence);
return;
}

// Recursively call reverseSentence with the substring after the space


reverseSentence(firstSpace + 1);

// Replace the space with '\0' to print the word


*firstSpace = '\0';
printf("%s ", sentence);
}

int main() {

Downloaded by Judith Petrizia (petri.success@gmail.com)


lOMoARcPSD|46270988

char sentence[100];

// Input the sentence


printf("Enter a sentence: ");
fgets(sentence, sizeof(sentence), stdin);

// Call the function to reverse the sentence


printf("Reversed sentence: ");
reverseSentence(sentence);

return 0;
}

Explanation:
 The reverseSentence function takes a character array sentence as input and reverses
the sentence using recursion.
 The base case checks if the string is empty or contains only one word (i.e., no space). If
so, it prints the word and returns.
 Otherwise, it finds the first occurrence of a space using strchr function.
 It then recursively calls reverseSentence with the substring after the space.
 After the recursion, it replaces the space with '\0' to print the word and prints the word.
 In the main function, it inputs the sentence using fgets and calls reverseSentence to
reverse the sentence.
Result :

Thus the Reverse a Sentence programs are executed and verified successfully.

Downloaded by Judith Petrizia (petri.success@gmail.com)


lOMoARcPSD|46270988

Ex.No : Concatenate Two Strings

Date :

Aim :
To Write a C program to concatenate two strings

Program :

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

int main() {
char str1[100], str2[100];

// Input the first string


printf("Enter the first string: ");
scanf("%s", str1);

// Input the second string


printf("Enter the second string: ");
scanf("%s", str2);

// Concatenate the two strings


strcat(str1, str2);

// Print the concatenated string


printf("Concatenated string: %s\n", str1);

return 0;
}

Downloaded by Judith Petrizia (petri.success@gmail.com)


lOMoARcPSD|46270988

Explanation:

 This program first defines two character arrays str1 and str2, each with a size of 100
characters.
 It then prompts the user to input two strings, one by one, using scanf.
 After that, it uses the strcat function from the <string.h> library to concatenate str2 to the end
of str1.
 Finally, it prints the concatenated string str1.

Result :

Thus the Concatenation of Two String programs are executed and verified successfully.

Downloaded by Judith Petrizia (petri.success@gmail.com)


lOMoARcPSD|46270988

Ex.No : Structures

Date :

Aim :
To Write a C program to Store Student Information in Structure and Display it.

Program :

#include <stdio.h>

// Define a structure for storing student information


struct Student {
char name[50];
int age;
float marks;
};

int main() {
// Declare a variable of type struct Student
struct Student student;

// Input student information


printf("Enter student name: ");
scanf("%s", student.name);

printf("Enter student age: ");


scanf("%d", &student.age);

printf("Enter student marks: ");


scanf("%f", &student.marks);

// Display student information


printf("\nStudent Information\n");
printf("Name: %s\n", student.name);
printf("Age: %d\n", student.age);
printf("Marks: %.2f\n", student.marks);

Downloaded by Judith Petrizia (petri.success@gmail.com)


lOMoARcPSD|46270988

return 0;
}
Explanation:

This program defines a structure Student with three members: name, age, and marks. It
then declares a variable student of type struct Student to store the information of one student.

The program prompts the user to input the student's name, age, and marks. After
inputting the information, it displays the student's information on the console.

Result :

Thus the above programs are executed and verified successfully.

Downloaded by Judith Petrizia (petri.success@gmail.com)


lOMoARcPSD|46270988

Ex.No : Students Information

Date :

Aim :
To Write a program to read the data from a file and determine the following: Total marks
obtained by each student; Topper of the class.

Program :

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

// Define a structure for storing student information


struct Student {
char name[50];
float marks[3]; // Assuming 3 subjects
float totalMarks;
};

int main() {
FILE *file;
struct Student students[50]; // Assuming maximum 50 students
int numStudents = 0;

// Open the file for reading


file = fopen("student_data.txt", "r");
if (file == NULL) {
printf("Unable to open the file.\n");
return 1;
}

// Read data from the file


while (fscanf(file, "%s %f %f %f", students[numStudents].name,
&students[numStudents].marks[0], &students[numStudents].marks[1],
&students[numStudents].marks[2]) != EOF) {
// Calculate total marks

Downloaded by Judith Petrizia (petri.success@gmail.com)


lOMoARcPSD|46270988

students[numStudents].totalMarks = students[numStudents].marks[0] +
students[numStudents].marks[1] +
students[numStudents].marks[2];
numStudents++;
}

// Close the file


fclose(file);

// Calculate total marks obtained by each student


printf("Total marks obtained by each student:\n");
for (int i = 0; i < numStudents; i++) {
printf("%s: %.2f\n", students[i].name, students[i].totalMarks);
}

// Determine the topper of the class


float maxMarks = -1;
char topper[50];
for (int i = 0; i < numStudents; i++) {
if (students[i].totalMarks > maxMarks) {
maxMarks = students[i].totalMarks;
strcpy(topper, students[i].name);
}
}

// Print the topper of the class


printf("\nTopper of the class: %s\n", topper);

return 0;
}

Downloaded by Judith Petrizia (petri.success@gmail.com)


lOMoARcPSD|46270988

Explanation:

 This program reads student data from a file named student_data.txt.


 It assumes that each line of the file contains the student's name followed by the marks
obtained in three subjects.
 It calculates the total marks obtained by each student and stores them in a structure array
students.
 After reading all the data, it prints the total marks obtained by each student.
 It then determines the topper of the class by finding the student with the highest total
marks and prints their name.

Result :

Thus the above programs are executed and verified successfully.

Downloaded by Judith Petrizia (petri.success@gmail.com)

You might also like