[go: up one dir, main page]

0% found this document useful (0 votes)
39 views63 pages

PST Lab 2 Ragul Merged

Ho

Uploaded by

shaamoffl869
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)
39 views63 pages

PST Lab 2 Ragul Merged

Ho

Uploaded by

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

RATHINAM TECHNICAL

CAMPUS
RATHINAM TECHZONE
POLLACHIMAIN ROAD, EACHANARI, COIMBATORE-641021.

DEPARTMENT OF SCIENCE AND HUMANITIES

RECORD NOTE BOOK

2 2 CS2 0 1 R PROBLEM SOLVING TECHNIQUES


II LABORATORY

NAME :

REGISTER NUMBER :

YEAR/SEMESTER :

ACADEMIC YEAR :
RATHINAM TECHNICAL CAMPUS
RATHINAM TECHZONE
POLLACHIMAIN ROAD, EACHANARI, COIMBATORE-641021.

BONAFIDE CERTIFICATE

NAME :

ACADEMIC YEAR :

YEAR/SEMESTER :

BRANCH :

UNIVERSITY REGISTER NUMBER: ………………………………..

Certified that this is the bonafide record of work done by the above student in the

Laboratory during the year 2023-2024.

Head of the Department Staff-in-Charge

Submitted for the Practical Examination held on

Internal Examiner External Examiner


INDE

S.No Date Experiment Name Marks Staff Sign


INDE
S.No Date Experiment Name Marks Staff Sign
Ex. No: PRACTICE OF C PROGRAMMING
Date: USING INPUT & OUTPUT STATEMENTS

AIM
To write ac program to calculate the area of the rectangle.

ALGORITHM
STEP 1: Start
STEP 2: Declare the variables length, breadth and area as integers.
STEP 3: Get input for the variables length and breadth.
STEP 4: Area = length * breadth

STEP 5: Print the calculated area.


STEP 6: End.

PROGRAM
#include<stdio.h>
int main()
{
int length,breadth,area;
printf("Enter the Length (in cm)= ");

scanf("%d", &length);
printf("Enter the Breadth (in cm) =");

scanf("%d", &breadth);
area= length*breadth;
printf("\nArea of the rectangle (in cm^2) is = %d\n",area);

return 0;
}
OUTPUT

RESULT
Ex. No: PRACTICE OF C PROGRAMMING
Date: USING INPUT & OUTPUT STATEMENTS

AIM
To write ac program to display the personal details.

ALGORITHM
STEP 1: Start
STEP 2: Declare the variables name, dob, gender and address as string.
STEP 3: Declare the variable age as integer & variable ph.no as long long int.
STEP 4: Get input for all the variables from the user.
STEP 5: Print the variables to display the personal information.
STEP 6: End.

PROGRAM
#include<stdio.h>
int main()
{
char name[50],dob[50],gender[10];
char address[100];

int age;
long long intphno;

printf("Enter your Name : ");

scanf("%s",&name);
printf("Enter your Age : ");

scanf("%d",&age);
printf("Enter your DOB : ");

scanf("%s",&dob);
printf("Enter your Gender : ");

scanf("%s",&gender);
printf("Enter your Phone no. : ");

scanf("%lld",&phno);
printf("Enter your Address : ");

scanf("%s",&address);

printf("\nYOUR PERSONAL INFORMATION : \n");

printf("\nName : %s\n",name);

printf("Age : %d\n",age);
printf("DOB : %s\n",dob);
printf("Gender : %s\n",gender);
printf("Phone no. : %lld\n",phno);

printf("Name : %s\n",address);

return 0 ;

}
OUTPUT

RESULT
Ex. No: C PROGRAMMING USING OPERATORS
Date:

AIM
To write ac program that checks the given year is leap year or not.

ALGORITHM
STEP 1: Start
STEP 2: Declare the variables year as an integer.
STEP 3: Get input for the variable year from the user.
STEP 4: Check for the following condition using if condition
• If the year is divisible by 4 and not divisible by 100, or
• If the year is divisible by 400.
STEP 5: If the year satisfies both the conditions print the year is a leap year.
STEP 6: If not print the year is not a leap year

STEP 7: End.

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

int year;
printf("Enter a Year : ");

scanf("%d",&year);
if((year % 4 == 0 && year % 100 !=0 )|| (year % 400 == 0)){

printf("%d is a leap year !",year);


}
else{
printf("%d is not a leap year.",year);
}
return 0;

OUTPUT

RESULT
Ex. No: PRACTICE OF C PROGRAMMING USING
Date: OPERATAORS

AIM
To write ac program to reverse a number.

ALGORITHM
STEP 1: Start
STEP 2: Declare the variables num, reversed and remainder as integers.

STEP 3: Initialize reversed = 0.


STEP 4: Get input for the variable num from the user.
STEP 5: Run a while loop until num is not equal to 0 with the conditions
• Calculate the remainder of num divided by 10 and store it in remainder.
• Multiply reversed by 10 and add remainder to it, then store the result
back in reversed.
• Update num by dividing it by 10.
STEP 6: Print the reversed number stored in reversed.
STEP 7: End.

PROGRAM
#include <stdio.h>
int main() {
intnum, reversed = 0, remainder;

printf("Enter an number: ");


scanf("%d", &num);
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;

num /= 10;
}
printf("\nReversed number: %d\n", reversed);

return 0;
}

OUTPUT

RESULT
Ex. No: PRATICE OF C PROGRAMMING USING
Date: CONDITIONAL STATEMENTS

AIM

To write ac program to perform calculator operations, namely,


addition,subtraction, multiplication, division, modulus and square of a number.

ALGORITHM

STEP 1: Start
STEP 2: Declare variables num1, num2, result
STEP 3: Prompt the user to enter two numbers

STEP 4: Read num1 and num2 from the user


STEP 5: Display the menu of calculator operations

a. Addition
b. Subtraction
c. Multiplication

d. Division
e. Modulus
f. Square of a number
STEP 6: Prompt the user to choose an operation
STEP 7: Perform the chosen operation based on user input

a. For addition, result = num1 + num2


b. For subtraction, result = num1 - num2
c. For multiplication, result = num1 * num2

d. For division, result = num1 / num2


e. For modulus, result = num1 % num2
f. For square of a number, result = num1 * num1
STEP 8: Display the result
STEP 9: End

PROGRAM

#include <stdio.h>
#include <ctype.h>
int main() {
int choice;
char newchoice;

do {
printf("Enter your choice: \n");

printf("1 : Addition\n");
printf("2 : Subtraction\n");
printf("3 : Multiplication\n");

printf("4 : Division\n");
printf("5 : Modulus\n");

printf("6 : Square\n");
printf("\n");
scanf("%d", &choice);
int num1, num2, num, result;
if (choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5) {

printf("Enter num 1 : ");


scanf("%d", &num1);
printf("Enter num 2 : ");

scanf("%d", &num2);
} else if (choice == 6) {
printf("Enter a Number : ");

scanf("%d", &num);
}
switch (choice) {

case 1:
result = num1 + num2;

break;

case 2:
result = num1 - num2;

break;

case 3:
result = num1 * num2;

break;

case 4:
result = num1 / num2;

break;

case 5:
result = num1 % num2;

break;

case 6:
result = num * num;
break;

default:
printf("Invalid choice\n");

return 1;
}

printf("\nResult : %d\n", result);

printf("Do you want to do more calculation? (Y / N) : ");

scanf(" %c", &newchoice);


} while (tolower(newchoice) == 'y');

printf("Thankyou for using the calculator!\n");

return 0;

}
OUTPUT

RESULT
Ex. No: PRATICE OF C PROGRAMMING USING
Date: CONDITIONAL STATEMENTS

AIM

To write a c program to check whether a given number is Armstrong number or not.

ALGORITHM
STEP 1: Start
STEP 2: Declare variables num, originalNum, remainder, result

STEP 3: Initialize result to 0


STEP 4: Prompt the user to enter a number
STEP 5: Read the number from the user and store it in num and originalNum

STEP 6: Repeat until originalNum becomes 0


a. Get the last digit of originalNum: remainder = originalNum % 10

b. Update result: result = result + remainder * remainder * remainder


c. Remove the last digit from originalNum: originalNum = originalNum / 10

STEP 7: If result is equal to num,


then display "Armstrong number"

Else,
display "Not an Armstrong number"

STEP 8: End
PROGRAM
#include<stdio.h>
int main() {
int num, originalnum, digits = 0, remainder, result = 0;

printf("~Armstrong number checker~\n");


printf("Enter the number to be checked : ");

scanf("%d", &num);
originalnum = num;
while (originalnum != 0) {
originalnum = originalnum / 10;

++digits;
}
originalnum = num;
// Check if the number is Armstrong
while (originalnum != 0) {
remainder = originalnum % 10;

int power = 1;
for (int i = 0; i < digits; ++i) {

power *= remainder;
}
result += power;
originalnum = originalnum / 10;

}
if (result == num) {
printf("\n%d is an Armstrong number.\n", num);

} else {
printf("\n%d is not an Armstrong number.\n", num);
}
return 0;

}
OUTPUT

RESULT
Ex. No: PRATICE OF C PROGRAMMING
Date: BILL CALCULATOR

AIM
To write a c program to calculate and print the electricity bill of a given customer

ALGORITHM

STEP 1: Start
STEP 2: Declare variables customerID, name, unitConsumed, charge, totalAmount
STEP 3: Prompt the user to enter customerID, name, and unitConsumed

STEP 4: Read customerID, name, and unitConsumed from the user


STEP 5: Calculate charge based on unitConsumed
a. If unitConsumed <= 199, charge = unitConsumed * 1.20
b. If unitConsumed is between 200 and 399, charge = unitConsumed * 1.50

c. If unitConsumed is between 400 and 599, charge = unitConsumed * 1.80


d. If unitConsumed is 600 or above, charge = unitConsumed * 2.00

STEP 6: If charge > 400,


then apply surcharge: charge = charge + (charge * 0.15)
STEP 7: If charge < 100,
then set charge = 100
STEP 8: Display customer details and totalAmount to be paid

STEP 9: End

PROGRAM
#include<stdio.h>
#include<string.h>
int main()
{
int custid,units;
float amount = 0,surcharge = 0,tamount = 0;

char name[50];
printf("Enter your Name : ");
fgets(name,sizeof(name),stdin);
printf("Enter your Customer ID : ");

scanf("%d",&custid);
printf("Enter Total units consumed : ");

scanf("%d",&units);

printf("\n~EB BILL~\n");

printf("\n");
printf("Customer Name : %s",name);

printf("Customer Id : %d\n",custid);
printf("Total units consumed : %d\n",units);

if (units>83 && units<=199){


amount = units * 1.20;
printf("\nThe total amount to be paid = %.2f",amount);

}
else if(units >=200 && units<=400){

amount = units * 1.50;


printf("\nThe total amount to be paid = %.2f",amount);

}
else if(units >=401 && units<=600){

amount = units * 1.80;


surcharge = amount*0.15;
tamount = amount + surcharge;
printf("\nThe total amount to be paid = %.2f",tamount);

}
else if(units >600){
amount = units * 2.00;
surcharge = amount*0.15;
tamount = amount + surcharge;
printf("\nThe total amount to be paid = %.2f",tamount);

}
else if(units<=83){

amount = 100;
printf("\nThe total amount to be paid (Minimum amount) = %.2f\n",amount);

}
printf("\n");

return 0;
}

OUTPUT

RESULT
Ex. No: SORTING AND SELETION IN
Date: C PROGRAMMING

AIM
To write a c program To sort an Array using Selection Sort Techniques

ALGORITHM

STEP 1: Start
STEP 2: Declare variables array[], size, i,j, minIndex, temp
STEP 3: Prompt the user to enter the size of the array

STEP 4: Read the size of the array from the user


STEP 5: Prompt the user to enter array elements

STEP 6: Read array elements from the user


STEP 7: Repeat for i from 0 to size-2

a. Set minIndex = i
b. Repeat for j from i+1 to size- 1
i. If array[j] < array[minIndex], then set minIndex = j

c. Swap array[minIndex] with array[i]


STEP 8: Display the sorted array

STEP 9: End

PROGRAM

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

int size;
printf("Enter the size of the array: ");

scanf("%d", &size);
int arr[size];
printf("Enter the array elements: \n");

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


scanf("%d", &arr[i]);

}
intmin_ index;
for (int i = 0; i < size - 1; i++) {

min_index = i;
for (intj = i + 1; j < size; j++) {

if (arr[j] < arr[min_index])


min_index = j;

}
// If the minimum index is not the same as i, swap the elements
if (min_index != i) {
int temp = arr[min_index];

arr[min_index] = arr[i];
arr[i] = temp;
}

// Print the sorted array


printf("The sorted array is: ");

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


printf("%d ", arr[i]);

}
printf("\n");

return 0;

}
OUTPUT

RESULT
Ex. No: SORTING AND SELETION IN
Date: C PROGRAMMING

AIM
To write a c program To find a element using Linear Search Techniques

ALGORITHM

STEP 1: Start
STEP 2: Declare variables array[], size, element, found

STEP 3: Prompt the user to enter the size of the array


STEP 4: Read the size of the array from the user

STEP 5: Prompt the user to enter array elements


STEP 6: Read array elements from the user
STEP 7: Prompt the user to enter the element to be searched

STEP 8: Read the element to be searched from the user


STEP 9: Set found = 0
STEP 10: Repeat for i from 0 to size- 1
a. If array[i] is equal to element, then

i. Set found = 1
ii. Display "Element found at index i"

iii. Break
STEP 11: If found is 0,
then display "Element not found"

STEP 12: End

PROGRAM

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

int size;

printf("Enter the size of the array: ");

scanf("%d", &size);

int arr[size];

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

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


scanf("%d", &arr[i]);

int key;

printf("\nEnter the element that needs to be found: ");

scanf("%d", &key);

int found = 0;

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

if (key == arr[i]) {
printf("\nThe element %d is found at index %d\n", key, i);

found = 1;
break;
}
}

if (!found) {
printf("Element not found!\n");
}

return 0;

OUTPUT

RESULT
Ex. No: PRACTICE OF RECURSION IN
Date: C PROGRAMMING

AIM

To write a c program To find an element using Binary Search Techniques


using Recursion.

ALGORITHM

STEP 1: Start
STEP 2: Declare function binarySearch with parameters array[], left, right, element

STEP 3: If left <= right, then


a. Set mid = (left + right) / 2
b. If array[mid] is equal to element, then

i. Return mid
c. If array[mid] < element, then
i. Return binarySearch(array, mid + 1, right, element)

d. Else
i. Return binarySearch(array, left, mid - 1, element)
STEP 4: Return - 1

STEP 5: End

PROGRAM
#include <stdio.h>
int binarySearch(int arr[], int low, inthigh, int target)

{
if (low <= high){
int mid = low + (high - low) / 2;

if (arr[mid] == target){
return mid;
}
if (arr[mid] > target){
return binarySearch(arr, low, mid - 1, target);

}
return binarySearch(arr, mid + 1, high, target);

return - 1;

int main()

{
printf("~Binary search using recursion~");
int arr[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
intn = sizeof(arr) / sizeof(arr[0]);

int target;
printf("\nEnter the target element : ");

scanf("%d",&target);

int result = binarySearch(arr, 0,n - 1, target);

if (result != - 1)
printf("\nElement %d is present at index %d.\n", target, result);

else
printf("\nElement %d is not present in array.\n", target);

return 0;
}
OUTPUT

RESULT
Ex. No: PRACTICE OF RECURSION IN
Date: C PROGRAMMING

AIM
To write a c program find the factorial of a given number using Recursion.

ALGORITHM

STEP 1: Start
STEP 2: Declare function factorial with parameter n

STEP 3: If n is 0 or 1, then
a. Return 1
STEP 4: Else
a. Return n * factorial(n - 1)

STEP 5: End

PROGRAM
#include<stdio.h>
int factorial(intn){
if(n == 1){

return 1;
}
else{
return n * factorial(n - 1);
}
}int main()

{
printf("~Factorial using recursion~\n");

intnum,result ;
printf("\nEnter a number : ");

scanf("%d",&num);
result = factorial(num);
printf("\nFactorial of %d is : %d\n",num,result);

return 0;
}

OUTPUT

RESULT
Ex. No: PRACTICING WITH 2DARRAYS IN
Date: C PROGRAMMING

AIM
To write a c program to read two matrices from the user and to print the product of

two matrices.
ALGORITHM

STEP 1: Start
STEP 2: Declare variables matrix1[10][10], matrix2[10][10], result[10][10],
rows1, cols1, rows2, cols2, i,j, k
STEP 3: Prompt the user to enter the number of rows and columns of matrix1

STEP 4: Read rows1 and cols1 from the user


STEP 5: Prompt the user to enter elements of matrix1

STEP 6: Read elements of matrix1 from the user


STEP 7: Prompt the user to enter the number of rows and columns of matrix2

STEP 8: Read rows2 and cols2 from the user


STEP 9: Prompt the user to enter elements of matrix2

STEP 10: Read elements of matrix2 from the user


STEP 11: If cols1 is not equal to rows2, then
a. Display "Matrix multiplication not possible"

b. Exit
STEP 12: Repeat for i from 0 to rows1- 1

a. Repeat for j from 0 to cols2- 1


i. Set result[i][j] = 0
ii. Repeat fork from 0 to cols1- 1
iii. Set result[i][j] += matrix1[i][k] * matrix2[k][j]

STEP 13: Display the product matrix result

STEP 14: End


PROGRAM
#include <stdio.h>
int main() {
intr1,c1,r2,c2;
printf("Input the rows and columns of first matrix : ");

scanf("%d%d",&r1,&c1);
printf("Input the rows and columns of second matrix : ");

scanf("%d%d",&r2,&c2);
int prod[r1][c2],a[r1][c1],b[r2][c2];
printf("\nInput elements in the first matrix :\n");

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


for (intj = 0; j < c1; j++) {
printf("element - [%d],[%d] : ",i,j);

scanf("%d", &a[i][j]);
}

}
printf("\nInput elements in the second matrix :\n");

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


for (intj = 0; j <c2; j++) {
printf("element - [%d],[%d] : ",i,j);

scanf("%d", &b[i][j]);
}

}
for (int i = 0; i < r1; i++) {
for (intj = 0; j < c2; j++) {

prod[i][j] = 0;
}

}
for (int i = 0; i < r1; i++) {
for (intj = 0; j < c2; j++) {
for (intk = 0; k < c1; k++){
prod[i][j] += a[i][k] * b[k][j];

}
}

}
printf("\nThe first matrix is:\n");

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


for (intj = 0; j < c1; j++) {

printf("%d ", a[i][j]);


}
printf("\n");

}
printf("\nThe second matrix is:\n");

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


for (intj = 0; j < c2; j++) {

printf("%d ", b[i][j]);


}
printf("\n");

}
printf("\nThe multiplication of two matrix is :\n");

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


for (intj = 0; j < c2; j++) {

printf("%d ", prod[i][j]);


}
printf("\n");

return 0;

}
OUTPUT

RESULT
Ex. No: PRACTICING STRING OPERATIONS IN
Date: C PROGRAMMING

AIM
To write a c program to perform string operation namely, String length, String
Copy, String Compare, String Reverse, String lower, String Upper, String Compare, String

Concatenation with pre-defined string functions.

ALGORITHM

STEP 1: Start
STEP 2: Declare character arrays str1 and str2 to store strings.
STEP 3: Display a message indicating predefined string operations.
STEP 4: Input a string into str1.
STEP 5: Calculate and print the length of str1 using the strlen function.
STEP 6: Copy the content of str1 to str2 using the strcpy function and print str2.
STEP 7: Input another string into str2.
STEP 8: Compare str1 and str2 using the strcmp function and print whether they
are equal or not.
STEP 9: Reverse str1 character by character and print the reversed string.
STEP 10: Convert str1 to lowercase character by character using the
tolower function and print the lowercase string.

STEP 11: Convert str1 to uppercase character by character using the


toupper function and print the uppercase string.
STEP 12: Input another string into str2.
STEP 13: Concatenate str2 with str1 using the strcat function and print
the concatenated string.
STEP 14: End.

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

int main()

{
char str1[100], str2[100];
printf("~Predefined String Operations\n");

// String Length
printf("Enter a string: ");

scanf("%s", str1);
printf("\nLength of the string: %lu\n", strlen(str1));

// String Copy
strcpy(str2, str1);
printf("\nCopied string: %s\n", str2);

// String Compare
printf("\nEnter another string for comparison: ");

scanf("%s", str2);
if (strcmp(str1, str2) == 0)
printf("\nBoth strings are equal.\n");

else
printf("\nStrings are not equal.\n");

// String Reverse
printf("\nOriginal string: %s\n", str1);

printf("Reversed string: ");


for (int i = strlen(str1) - 1; i >= 0; i--) {

printf("%c", str1[i]);
}
printf("\n");
// String to Lowercase
printf("\nOriginal string: %s\n", str1);

printf("Lowercase string: ");


for (int i = 0; i < strlen(str1); i++) {

printf("%c",tolower(str1[i]));
}
printf("\n");

// String to Uppercase
printf("\nOriginal string: %s\n", str1);

printf("Uppercase string: ");


for (int i = 0; i < strlen(str1); i++) {

printf("%c",toupper(str1[i]));
}
// String Concatenation
printf("\nEnter a string to concatenate with: ");

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

return 0;

}
OUTPUT

RESULT
Ex. No: PRACTICING STRING OPERATIONS IN
Date: C PROGRAMMING

AIM
To write a c program to perform string operation namely String length, String Copy,

String Compare, String Reverse, String lower, String Upper, String Compare, String

Concatenation without in-built function.

ALGORITHM

STEP 1: Start
STEP 2: Declare functions for string operations: stringLength, stringCopy,
stringCompare, stringReverse, stringToLower, stringToUpper, and stringConcatenate.
STEP 3: Declare character arrays original_str, str1, and str2 to store strings.
STEP 4: Display amessage indicating user-defined string operations.
STEP 5: Input a string into original_str.
STEP 6: Copy original_str to str1 for subsequent operations.
STEP 7: Calculate and print the length of str1 using the stringLength function.
STEP 8: Copy str1 to str2 using the stringCopy function and print str2.
STEP 9: Input another string into str2.

STEP 10: Compare str1 and str2 using the stringCompare function and print
whether they are equal or not.
STEP 11: Reverse str1 using the stringReverse function and print the reversed string.

STEP 12: Convert str2 to lowercase using the stringToLower function and print
the lowercase string.

STEP 13: Convert str2 to uppercase using the stringToUpper function and print
the uppercase string.
STEP 14: Input another string into str2.

STEP 15: Concatenate str2 with str1 using the stringConcatenate function and
print the concatenated string.

STEP 16: End.


PROGRAM
#include <stdio.h>
// Function to calculate string length
int stringLength(char str[]) {

int len = 0;
while (str[len] != '\0') {

len++;
}
return len;

}
// Function to copy one string to another
void stringCopy(char source[], char destination[]) {

int i = 0;
while (source[i] != '\0') {
destination[i] = source[i];

i++;
}
destination[i] = '\0';

}
// Function to compare two strings
int stringCompare(char str1[], char str2[]) {

int i = 0;
while (str1[i] != '\0' || str2[i] != '\0') {

if (str1[i] != str2[i]) {
return str1[i] - str2[i];

}
i++;

}
return 0;
}// Function to reverse a string
void stringReverse(char str[]) {

int len = stringLength(str);


for (int i = 0; i < len / 2; i++) {
chartemp = str[i];
str[i] = str[len - i - 1];

str[len - i - 1] = temp;
}

}
// Function to convert a string to lowercase
void stringToLower(char str[]) {

int i = 0;
while (str[i] != '\0') {
if (str[i] >= 'A' && str[i] <= 'Z') {

str[i] = str[i] + 32;


}
i++;
}

}
// Function to convert a string to uppercase
void stringToUpper(char str[]) {

int i = 0;
while (str[i] != '\0') {
if (str[i] >= 'a' && str[i] <= 'z') {

str[i] = str[i] - 32;


}
i++;
}

}
// Function to concatenate two strings
void stringConcatenate(char str1[], char str2[]) {
int len1 = stringLength(str1);

int len2 = stringLength(str2);


int i, j = 0;
for (i = len1; i < len1 + len2; i++) {

str1[i] = str2[j];
j++;

}
str1[i] = '\0';

}
int main(){
char original_str[100], str1[100], str2[100];
printf("~User_Defined String Operations~\n");

// Input string
printf("\nEnter a string: ");

scanf("%s", original_str);

// Copy original string to str1 for subsequent operations

stringCopy(original_str, str1);

// String Length
printf("\nLength of the string: %d\n", stringLength(str1));

// String Copy
stringCopy(str1, str2);
printf("\nCopied string: %s\n", str2);

// String Compare
printf("\nEnter another string for comparison: ");

scanf("%s", str2);
int result = stringCompare(str1, str2);
if (result == 0){
printf("\nBoth strings are equal.\n");
}
else{
printf("\nString 1 is greater than String 2.\n");

}
// Reverse str1 and print reversed string

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

// Convert str2 to lowercase


stringCopy(original_str, str2); // Reset str2 to original input

stringToLower(str2);
printf("\nLowercase string: %s\n", str2);

// Convert str2 to uppercase


stringCopy(original_str, str2); // Reset str2 to original input

stringToUpper(str2);
printf("\nUppercase string: %s\n", str2);

// String Concatenation
printf("\nEnter a string to concatenate with: ");

scanf("%s", str2);
stringCopy(original_str, str1); // Reset str1 to original input

stringConcatenate(str1, str2);
printf("\nConcatenated string: %s\n", str1);

return 0;
}
OUTPUT

RESULT
Ex. No: PRACTICE OF CALL BY VALUE & CALL BY
Date: REFERSENCE IN C PROGRAMMING

AIM

To write a c program to swap two integer values using call by value & call
by reference
ALGORITHM

STEP 1:Start

STEP 2:Define two functions: swapbyvalue and swapbyreference, to swap


values using call by value and call by reference methods respectively.
STEP 3:In the swapbyvalue function:
a. Accept two integers x andy as parameters.
b. Swap the values of x andy using a temporary variable.

c. Print the swapped values.


STEP 4:In the swapbyreference function:
a. Accept two integer pointers x andy as parameters.
b. Swap the values pointed to by x andy using a temporary variable.

c. Print the swapped values.

STEP 5:In the main function:


a. Declare variables a and b to store integers.
b. Display a message indicating swapping values.

c. Prompt the user to input two integers a and b.


d. Print the original values of a and b.
e. Call the swapbyvalue function with arguments a and b.
f. Call the swapbyreference function with the addresses of a and b.

STEP 6:End.
PROGRAM
#include <stdio.h>
void swapbyvalue(intx, inty)

{
int temp = x;

x = y;
y = temp;
printf("\nAfter swapping (Call by Value):\n");

printf("x = %d, y = %d\n",x, y);


}

void swapbyreference(int *x, int *y)

{
int temp = *x;

*x = *y;
*y = temp;
printf("\nAfter swapping (Call by Reference):\n");

printf("x = %d, y = %d\n", *x, *y);


}
int main() {

int a, b;
printf("~Swapping Values~");
printf("\nEnter two integers: ");

scanf("%d %d", &a, &b);


printf("\nBefore swapping:\n");

printf("a = %d, b = %d\n", a, b);


swapbyvalue(a, b);
swapbyreference(&a, &b);

return 0;
}
OUTPUT

RESULT
Ex. No: PRACTICING POINTERS IN

Date: C PROGRAMMING

AIM

To write a c program to swap two integer values using call by value & call
by reference

ALGORITHM
STEP 1: Start

STEP 2: Declare variables: `a` to store the size of the array, `sum` to store the
sum of the array elements, and `avg` to store the average.

STEP 3: Display a message indicating the process of finding the average of an


array using pointers.
STEP 4: Prompt the user to input the size of the array (`a`).
STEP 5: Declare an integer array `arr` of size `a`.
STEP 6: Prompt the user to input the elements of the array.
STEP 7: Calculate the sum of the array elements while inputting the elements.
STEP 8: Create a pointer `ptr` pointing to the first element of the array (`arr`).
STEP 9: Initialize `avg` to 0.

STEP 10: Using a loop, iterate through the array elements pointed by `ptr`,
calculate the sum using pointer arithmetic, and store it in `avg`.
STEP 11: Divide the sum by the size of the array to find the average.
STEP 12: Print the calculated average of the array.
STEP 13: End.

PROGRAM
#include <stdio.h>
int main()
{
int a, sum = 0;
float avg;

printf("~Average of array using Pointers~\n");

printf("Enter the size of the array: ");


scanf("%d", &a);

int arr[a];

printf("\nEnter the array elements:\n", a);

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


scanf("%d", &arr[i]);

sum += arr[i];
}

// Pointer to the first element of the array

int *ptr = arr;

// Calculate average using pointers

avg = 0;
for (int i = 0; i < a; i++) {

avg = avg + *(ptr + i);


}
avg = avg/a;

printf("Average of the array : %.2f\n", avg);

return 0;

}
OUTPUT

RESULT
Ex. No: PRACTICING STRUCT IN

Date: C PROGRAMMING

AIM

To write a c program to create a structure named student with the fields roll
no, name,
marks in three subjects. Initialize the structure with the values entered by user and

display the details.

ALGORITHM

STEP 1: Start
STEP 2: Define structure named student with fields rollNumber, name, and marks
STEP 3: Declare a variable s of type student

STEP 4: Prompt the user to enter roll number, name, and marks of three subjects
for the student
STEP 5: Read roll number, name, and marks from the user and store them in
the respective fields of s
STEP 6: Display the details of the student
STEP 7: End

PROGRAM
#include<stdio.h>

struct var {
char name[50], sub[3][50];

introll;
float marks[3];

};
int main() {
struct var s;

printf("Enter name : ");


fgets(s.name,sizeof(s.name),stdin);

printf("Enter Roll No. : ");


scanf("%d", &s.roll);

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


printf("Enter Subject %d : ", i + 1);

scanf("%s", s.sub[i]);
printf("Enter Marks of Subject %d : ", i + 1);

scanf("%f", &s.marks[i]);
printf("\n");

printf("Your Information : \n");

printf("\nName : %s\n", s.name);


printf("Roll No. : %d\n", s.roll);
printf("Subjects and Marks:\n");
for (int i = 0; i < 3; i++) {
printf("\nSubject %d : %s\n", i + 1, s.sub[i]);

printf("Marks: %.2f\n",s.marks[i]);
}

return 0;

}
OUTPUT

RESULT
Ex. No: PRACTICE OF FILE HANDLING IN

Date: C PROGRAMMING

AIM
To write a c program to read data from a text file and display it on the screen.

ALGORITHM

STEP 1: Start
STEP 2: Include the necessary header files.
STEP 3: Declare a file pointer fp.
STEP 4: Declare a character array c to store the contents of the file.
STEP 5: Open the file "index.txt" in read mode using fopen(), store the file
pointer in fp.

STEP 6: Use fgets() to read the first 14 characters (plus the null terminator)
from the file into the character array c.
STEP 7: Print the content of the character array c.
STEP 8: Close the file using fclose(fp).
STEP 9: End.

PROGRAM

#include <stdio.h>
int main()
{
FILE *fp;
char c[100];
fp = fopen("C:\\Users\\ragul\\OneDrive\\Desktop\\index.txt","r");

fgets(c,15,fp);
printf("%s", c);

printf("\n");
return 0;

OUTPUT

RESULT
Ex. No: PRACTICE OF FILE HANDLING IN

Date: C PROGRAMMING

AIM
To write a c program to read data from a text file and append it to another file.

ALGORITHM

STEP 1: Start
STEP 2: Include the necessary header files.
STEP 3: Declare two file pointers fp1 and fp2.
STEP 4: Declare a character array str to store lines read from the source file.

STEP 5: Open the source file "index.txt" in read mode using fopen(), and store the
file pointer in fp1.

STEP 6: Open the destination file "append.txt" in append mode using fopen(),
and store the file pointer in fp2.

STEP 7: Read lines from the source file using fgets() into the character array str
until fgets() returns NULL, indicating the end of the file.
STEP 8: Write each line from str to the destination file using fprintf().
STEP 9: Close both files using fclose(fp1) and fclose(fp2).
STEP 10: End.

PROGRAM

#include <stdio.h>
int main() {
FILE *fp1, *fp2;

char str[50];
fp1 = fopen("C:\\Users\\ragul\\OneDrive\\Desktop\\index.txt", "r");
fp2 = fopen("C:\\Users\\ragul\\OneDrive\\Desktop\\append.txt", "a");
while (fgets(str, 50, fp1) != NULL) {

fprintf(fp2, "%s", str);


}
fclose(fp1);

fclose(fp2);
return 0;
}

OUTPUT
RESULT

You might also like