PST Lab 2 Ragul Merged
PST Lab 2 Ragul Merged
CAMPUS
RATHINAM TECHZONE
POLLACHIMAIN ROAD, EACHANARI, COIMBATORE-641021.
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 :
Certified that this is the bonafide record of work done by the above student in the
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
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;
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("\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)){
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.
PROGRAM
#include <stdio.h>
int main() {
intnum, reversed = 0, remainder;
num /= 10;
}
printf("\nReversed number: %d\n", reversed);
return 0;
}
OUTPUT
RESULT
Ex. No: PRATICE OF C PROGRAMMING USING
Date: CONDITIONAL STATEMENTS
AIM
ALGORITHM
STEP 1: Start
STEP 2: Declare variables num1, num2, result
STEP 3: Prompt the user to enter two numbers
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
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) {
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;
}
return 0;
}
OUTPUT
RESULT
Ex. No: PRATICE OF C PROGRAMMING USING
Date: CONDITIONAL STATEMENTS
AIM
ALGORITHM
STEP 1: Start
STEP 2: Declare variables num, originalNum, remainder, result
Else,
display "Not an Armstrong number"
STEP 8: End
PROGRAM
#include<stdio.h>
int main() {
int num, originalnum, digits = 0, remainder, result = 0;
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 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);
}
else if(units >=200 && units<=400){
}
else if(units >=401 && units<=600){
}
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
a. Set minIndex = i
b. Repeat for j from i+1 to size- 1
i. If array[j] < array[minIndex], then set minIndex = j
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");
}
intmin_ index;
for (int i = 0; i < size - 1; i++) {
min_index = i;
for (intj = i + 1; j < size; 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;
}
}
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
i. Set found = 1
ii. Display "Element found at index i"
iii. Break
STEP 11: If found is 0,
then display "Element not found"
PROGRAM
#include<stdio.h>
int main() {
int size;
scanf("%d", &size);
int arr[size];
int key;
scanf("%d", &key);
int found = 0;
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
ALGORITHM
STEP 1: Start
STEP 2: Declare function binarySearch with parameters array[], left, right, element
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);
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
b. Exit
STEP 12: Repeat for i from 0 to rows1- 1
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");
scanf("%d", &a[i][j]);
}
}
printf("\nInput elements in the second matrix :\n");
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");
}
printf("\nThe second matrix is:\n");
}
printf("\nThe multiplication of two matrix is :\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
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.
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("%c", str1[i]);
}
printf("\n");
// String to Lowercase
printf("\nOriginal string: %s\n", str1);
printf("%c",tolower(str1[i]));
}
printf("\n");
// String to Uppercase
printf("\nOriginal string: %s\n", str1);
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
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.
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[]) {
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') {
}
// 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') {
}
// Function to concatenate two strings
void stringConcatenate(char str1[], char str2[]) {
int len1 = stringLength(str1);
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);
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);
stringToLower(str2);
printf("\nLowercase string: %s\n", str2);
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 6:End.
PROGRAM
#include <stdio.h>
void swapbyvalue(intx, inty)
{
int temp = x;
x = y;
y = temp;
printf("\nAfter swapping (Call by Value):\n");
{
int temp = *x;
*x = *y;
*y = temp;
printf("\nAfter swapping (Call by Reference):\n");
int a, b;
printf("~Swapping Values~");
printf("\nEnter two integers: ");
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 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;
int arr[a];
sum += arr[i];
}
avg = 0;
for (int i = 0; i < a; i++) {
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
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;
scanf("%s", s.sub[i]);
printf("Enter Marks of Subject %d : ", i + 1);
scanf("%f", &s.marks[i]);
printf("\n");
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) {
fclose(fp2);
return 0;
}
OUTPUT
RESULT