0% found this document useful (0 votes)
4 views84 pages

UCEN R23 Computer Programming Lab Record

Uploaded by

alfredweizmann
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)
4 views84 pages

UCEN R23 Computer Programming Lab Record

Uploaded by

alfredweizmann
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/ 84

University College of Engineering Narasaraopeta

Jawaharlal Nehru Technological University Kakinada


===================================

Program 1: Write an algorithm, draw the flow chart, and write a C program to
calculate the sum and average of 3 numbers
Aim: To write an algorithm, draw the flow chart, and write a C program to calculate
the sum and average of 3 numbers
Algorithm:

Flow Chart:

Lab Record Computer Programming Lab Page 1 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Source Code:
include <stdio.h>
int main()
{
int a, b, c, sum;
float avg;
printf("Enter 3 numbers: \n");
scanf("%d %d %d", &a, &b, &c);
sum = a + b + c;
avg = sum / 3;
printf("Sum = %d \n", sum);
printf("Average = %.2f", avg);
return 0;
}

Lab Record Computer Programming Lab Page 2 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 2: Write an algorithm, draw the flow chart, and write a C program to
convert Fahrenheit to Celsius
Aim: To write an algorithm, draw the flow chart, and write a C program to convert
Fahrenheit to Celsius
Algorithm:

Flow Chart:

Lab Record Computer Programming Lab Page 3 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Source Code:
#include<stdio.h>
int main()
{
float fahrenheit, celsius;
printf("\nEnter Fahrenheit: ");
scanf("%f",&fahrenheit);
celsius = (fahrenheit - 32)*5/9;
printf("\nCelsius: %f ", celsius);
return 0;
}

Lab Record Computer Programming Lab Page 4 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 3: Write an algorithm, draw the flow chart, and write a C program to
calculate the simple interest
Aim: To write an algorithm, draw the flow chart, and write a C program to calculate
simple interest
Algorithm:

Flow Chart:

Lab Record Computer Programming Lab Page 5 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Source Code:
# include <stdio.h>
int main ()
{
int principal, rate, time, interest;
printf("Enter the principal: ");
scanf("%d", &principal);
printf("Enter the rate: ");
scanf("%d", &rate);
printf("Enter the time: ");
scanf("%d", &time);
interest = (principal * rate * time) / 100;
printf("\nThe Simple interest is %d", interest);
return 0;
}

Lab Record Computer Programming Lab Page 6 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 4: Write a C program to find the square root a given number


Aim: To find the square root of a given number
Source Code:
#include <stdio.h>
#include <math.h>
int main()
{
double num, root;
printf("Enter an integer: ");
scanf("%lf", &num);
root = sqrt(num);
printf("\n The Square Root of %.2lf is %.2lf", num, root);
return 0;
}

Lab Record Computer Programming Lab Page 7 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 5: Write a C program to calculate the compound interest


Aim: To calculate Compound Interest
Source Code:
#include <stdio.h>
#include <math.h>
int main()
{
float principle, rate, time, CI;
printf("Enter principle amount: ");
scanf("%f", &principle);
printf("Enter time: ");
scanf("%f", &time);
printf("Enter rate: ");
scanf("%f", &rate);
CI = principle* (pow((1 + rate / 100), time));
printf("Compound Interest = %f", CI);
return 0;
}

Lab Record Computer Programming Lab Page 8 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 6: Write a C program to find the area of the triangle using Heron’s formula
Aim: To find the area of a triangle using Heron's formula
Source Code:
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,s=0,area=0;
printf("\nEnter the length of sides of triangle:);
scanf("%f %f %f",&a,&b,&c);
s = (a+b+c)/2.0;
area = sqrt((s*(s-a)(s-b)(s-c));
printf("Area of triangle =\t %f",area);
getch();
}

Lab Record Computer Programming Lab Page 9 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 7: Write a C program to find the distance travelled by an object


Aim: C program to find the distance travelled by an object
Source Code:
#include<stdio.h>
int main()
{
float u, a, d;
int t;
printf("\nEnter the value of a : ");
scanf("%f", & a);
printf("\nEnter the value of u : ");
scanf("%f", & u);
printf("\nEnter the value of t : ");
scanf("%d", & t);
d = (u * t) + (a * t * t) / 2;
printf("\n The Distance : %.2f", d);
return 0;
}

Lab Record Computer Programming Lab Page 10 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 8: Write a C program to illustrate explicit type conversion


Aim: To illustrate explicit type conversion
Source Code:
#include<stdio.h>
int main()
{
int number = 97;
printf("Integer Value: %d\n", number);
char alphabet = (char) number;
printf("Character Value: %c", alphabet);
return 0;
}

Lab Record Computer Programming Lab Page 11 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 9: Write a C program to find the maximum of three numbers using


conditional statements
Aim: To find the maximum of three numbers without using conditional statements
Source Code:
#include<stdio.h>
int main()
{
double a, b, c;
printf("Enter 3 numbers:\n");
scanf("%lf%lf%lf", &a, &b, &c);
if(a >= b && a >= c)
{
if(b >= c)
{
printf("\n%lf is the 2nd largest number\n", b);
}
else
{
printf("\n%lf is the 2nd largest number\n", c);
}
}
else if(b >= a && b >= c)
{
if(a >= c)
{
printf("\n\%lf is the 2nd largest number\n",a);
}
else

Lab Record Computer Programming Lab Page 12 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

{
printf("\n%lf is the 2nd largest number\n",c);
}
else if(a >= b)
{
printf("\n%lf is the 2nd largest number\n", a);
}
else
{
printf("\n%lf is the 2nd largest number\n", b);
}
return 0;
}

Lab Record Computer Programming Lab Page 13 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 10: Write a C program to find the total and average of 5 subjects
Aim: Tl take the marks of 5 subjects in integers, and find the total, average in float.
#include<stdio.h>
int main()
{
int i;
int arr[5];
double sum=0.0, average=0.0;
printf("\nEnter the marks of 5 subjects: ");
for (i = 0; i< 5; i++)
{
printf(“ Enter subject %d marks”, i+1);
scanf(“%d”, arr[i]);
}
for (i = 0; i< 5; i++)
{
sum += arr[i];
}
average = sum/n;
printf( "\n The Sum of the Elements of the Array is %lf: ", sum);
printf( "\n The Average of the Elements of the Array is %lf: ", average);
return 0;
}

Lab Record Computer Programming Lab Page 14 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 11: Write a C program to find the maximum and minimum of four numbers
using if-else
Aim: Find maximum and minimum of four numbers using if- else
Source Code:
#include <stdio.h>
int main()
{
int num1, num2, num3, num4;
int max, min;
printf("Enter four numbers: ");
scanf("%d %d %d %d", &num1, &num2, &num3, &num4);
if (num1 >= num2 && num1 >= num3 && num1 >= num4)
{
max = num1;
}
else if (num2 >= num1 && num2 >= num3 && num2 >= num4)
{
max = num2;
}
else if (num3 >= num1 && num3 >= num2 && num3 >= num4)
{
max = num3;
}
else
{
max = num4;
}
if (num1 <= num2 && num1 <= num3 && num1 <= num4)

Lab Record Computer Programming Lab Page 15 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

{
min = num1;
}
else if (num2 <= num1 && num2 <= num3 && num2 <= num4)
{
min = num2;
}
else if (num3 <= num1 && num3 <= num2 && num3 <= num4)
{
min = num3;
}
else
{
min = num4;
}
printf("Maximum number: %d\n", max);
printf("Minimum number: %d\n", min);
return 0;
}

Lab Record Computer Programming Lab Page 16 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 12: Write a C program to generate electricity bill


Aim: To generate electricity bill
Source Code:
#include <stdio.h>
int main()
{
int unit;
float amt, total_amt, sur_charge;
printf("Enter total units consumed: ");
scanf("%d", &unit);
if(unit <= 50)
{
amt = unit * 0.50;
}
else if(unit <= 150)
{
amt = 25 + ((unit-50) * 0.75);
}
else if(unit <= 250)
{
amt = 100 + ((unit-150) * 1.20);
}
else
{
amt = 220 + ((unit-250) * 1.50);
}
sur_charge = amt * 0.20;
total_amt = amt + sur_charge;
Lab Record Computer Programming Lab Page 17 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

printf("Electricity Bill = Rs. %.2f", total_amt);


return 0;
}

Lab Record Computer Programming Lab Page 18 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 13: Write a C program to find the roots of a quadratic equation


Aim: To find the roots of the quadratic equation
Source Code:
#include<stdio.h>
#include<math.h>
int main()
{
float a, b, c, determinant, r1, r2, real, imag;
printf("\nEnter coefficients a, b and c: ");
scanf("%f%f%f", &a, &b, &c);
determinant == b*b - 4*a*c;
if(determinant > 0)
{
r1 = (-b + sqrt(determinant))/2*a;
r2 = (-b - sqrt(determinant))/2*a;
printf("\nRoots are: %f and %f ", r1, r2);
}
else if(determinant == 0)
{
r1 = r2 = -b/(2*a);
printf("\nRoots are: %f and %f ", r1, r2);
}
else
{
real = -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("\nRoots are %.2f + i%.2f and %.2f - i%.2f ", real, imag, real,
imag);

Lab Record Computer Programming Lab Page 19 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

}
return 0;}

Lab Record Computer Programming Lab Page 20 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 14: Write a C program to simulate a calculator using switch case


Aim: To simulate a calculator using switch case
Source Code:
#include<stdio.h>
int main()
{
char op;
float num1, num2;
printf("Enter any arithmetic operator: ");
scanf(“%c”, &op);
printf("\nEnter two operands: ");
scanf(“%f %f”, &num1, &num2)
switch(op)
{
case '+':
printf(“\n Sum is: %f ”,num1+num2);
break;

case '-':
printf(“\n Subtraction is: %f ”,num1-num2);
break;

case '*':
printf(“\n Multiplication is: %f ”,num1*num2);
break;

case '/':
printf(“\n Division is: %f ”,num1/num2);
Lab Record Computer Programming Lab Page 21 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

break;
default:
printf("Error! operator is not correct");
break;
}
getch();
return 0;
}

Lab Record Computer Programming Lab Page 22 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 15: Write a C program to find whether the given year is leap year or not
Aim: To find whether the given year is a leap year or not
Source Code:
#include <stdio.h>
int main()
{
int year;
printf("\n Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0)
{
printf("%d is a leap year.", year);
}
else if (year % 100 == 0)
{
printf("%d is not a leap year.", year);
}
else if (year % 4 == 0)
{
printf("%d is a leap year.", year);
}
else
{
printf("%d is not a leap year.", year);
}
return 0;
}

Lab Record Computer Programming Lab Page 23 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 16: Write a C program to find the factorial of a given number using loops
Aim: To find the factorial of a given number using loops
Source Code:
#include<stdio.h>
int main()
{
int num, count, fact = 1;
printf("\nEnter a number to find its factorial\n");
scanf("%d", &num);
for(count = 1; count <= num; count++)
{
fact = fact * count;
}
printf("Factorial of %d is %d\n", num, fact);
return 0;
}

Lab Record Computer Programming Lab Page 24 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 17: Write a C program to check whether a given number is prime or not
Aim: To check whether the given number is prime or not.
Source Code:
#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i<= n / 2; ++i)
{
if (n % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
return 0;
}

Lab Record Computer Programming Lab Page 25 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 18: Write a C program to check whether a given number is palindrome or


not
Aim: To check whether a given number is palindrome or not
Source Code:
#include <stdio.h>
int main()
{
int n, reversed = 0, remainder, original;
printf("\nEnter an integer: ");
scanf("%d", &n);
original = n;
while (n != 0)
{
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);
return 0;
}

Lab Record Computer Programming Lab Page 26 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 19: Write a C program to construct a pyramid of numbers


Aim: To construct a pyramid of numbers
Source Code:
#include <stdio.h>
int main()
{
int i, j, rows;
printf("\nEnter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i<= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
printf("%d ", j);
}
printf("\n");
}
return 0;
}

Lab Record Computer Programming Lab Page 27 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 20: Write a C program to find the minimum of maximum number in a 1-D
array
Aim: To find minimum and maximum in a 1-D array
Source Code:
#include <stdio.h>
int main()
{
int array[10];
int i, min, max, size;
printf(“\n Please enter 10 interger values: “);
for(i=0;i<10;i++)
{
scanf(“%d”, &array[i]);
}
size = sizeof(array) / sizeof(array[0]);
min = array[0];
max = array[0];
for (int i = 1; i< size; i++)
{
if (array[i] < min)
{
min = array[i];
}
if (array[i] > max)
{
max = array[i]
}
}

Lab Record Computer Programming Lab Page 28 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

printf("Minimum value: %d\n", min);


printf("Maximum value: %d\n", max);
return 0;
}

Lab Record Computer Programming Lab Page 29 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 21: Write a C program to find 2’s complemet of a given binary number
Aim: To find 2’s complemt of the given binary number
Source Code:
#include <stdio.h>
#include <string.h>
void findTwosComplement(char binaryNumber[])
{
int length = strlen(binaryNumber);
int i;
for (i = length - 1; i>= 0; i--)
{
if(binaryNumber[i] == '1')
{
break;
}
}
if(i == -1)
{
printf("2's complement: %s\n", binaryNumber);
return;
}
for (int j = i - 1; j >= 0; j--)
{
if(binaryNumber[j] == '0')
{
binaryNumber[j] = '1';
}
else
Lab Record Computer Programming Lab Page 30 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

{
binaryNumber[j] = '0';
}
}
printf("2's complement: %s\n", binaryNumber);
}

int main()
{
char binaryNumber[100];
printf("\nEnter a binary number: ");
scanf("%s", &binaryNumber);
findTwosComplement(binaryNumber);
return 0;
}

Lab Record Computer Programming Lab Page 31 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 22: Write a C program to illustrate the concept of passing arrays to


functions
Aim: Illustrate the concept of passing arrays to functions
Source Code:
#include <stdio.h>
int findSum(int arr[], int size)
{
int i, sum = 0;
for (i = 0; i< size; i++)
{
sum += arr[i];
}
return sum;
}

void displayArray(int arr[], int size)


{
printf("Array elements: ");
for (int i = 0; i< size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}

int main()
{
int i, Array[10];

Lab Record Computer Programming Lab Page 32 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

int sum, size;


for(i=0;i<10;i++)
{
scanf(“%d”, &Array[i]);
}
size = sizeof(Array) / sizeof(Array[0]);
displayArray(Array, size);
sum = findSum(Array, size);
printf("Sum of array elements: %d\n", sum);
return 0;
}

Lab Record Computer Programming Lab Page 33 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 23: Write a C program to illustrate the concept of returning arrays from
function
Aim: Illustrate the concept of returning arrays from function
Source Code:
#include<stdio.h>
int* getEvenNumbers(int N)
{
static int evenNumberArray[100];
int i, even = 2;
for(i=0; i<N; i++)
{
evenNumberArray[i] = even;
even += 2;
}
return evenNumberArray;
}

int main()
{
int *array, counter;
array = getEvenNumbers(10);
printf("Even Numbers");
for(counter=0; counter<5; counter++){
printf("%d", array[counter]);
getch();
return 0;
}

Lab Record Computer Programming Lab Page 34 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 24: Write a C program to calculate the addition of two matrices


Aim: To calculate the addition of two matrices
Source Code:
#include <stdio.h>
void addMatrices(int firstMatrix[10][10], int secondMatrix[10][10], int result[10][10],
int rows, int cols)
{
for (int i = 0; i< rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
result[i][j] = firstMatrix[i][j] + secondMatrix[i][j];
}
}
}

int main()
{
int firstMatrix[10][10], secondMatrix[10][10], resultMatrix[10][10];
int rows, cols;
printf("Enter the number of rows and columns for the matrices: ");
scanf("%d %d", &rows, &cols);
printf("Enter the elements of the first matrix:\n");
for (int i = 0; i< rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
scanf("%d", &firstMatrix[i][j]);

Lab Record Computer Programming Lab Page 35 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

}
}

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


for (int i = 0; i< rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
scanf("%d", &secondMatrix[i][j]);
}
}
addMatrices(firstMatrix, secondMatrix, resultMatrix, rows, cols);
printf("Sum of the matrices:\n");
for (int i = 0; i< rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
printf("%d\t", resultMatrix[i][j]);
}
printf("\n");
}
return 0;
}

Lab Record Computer Programming Lab Page 36 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 25: Write a C program to calculate the multiplication of two matrices


Aim: To calculate the multiplication of two matrices
Source Code:
#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10
void multiplyMatrices(int firstMatrix[MAX_ROWS][MAX_COLS], int
secondMatrix[MAX_ROWS][MAX_COLS], int result[MAX_ROWS][MAX_COLS], int
rowsFirst, int colsFirst, int rowsSecond, int colsSecond)
{
for (int i = 0; i<rowsFirst; ++i)
{
for (int j = 0; j <colsSecond; ++j)
{
result[i][j] = 0
}
}
for (int i = 0; i<rowsFirst; ++i)
{
for (int j = 0; j <colsSecond; ++j)
{
for (int k = 0; k <colsFirst; ++k)
{
result[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
}

Lab Record Computer Programming Lab Page 37 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

void displayMatrix(int matrix[MAX_ROWS][MAX_COLS], int rows, int cols)


{
for (int i = 0; i< rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

int main()
{
int
firstMatrix[MAX_ROWS][MAX_COLS],secondMatrix[MAX_ROWS][MAX_COLS],
result[MAX_ROWS][MAX_COLS];
int rowsFirst, colsFirst, rowsSecond, colsSecond;
int i, j;
printf("Enter rows and columns for the first matrix: ");
scanf("%d %d", &rowsFirst, &colsFirst);
printf("Enter elements of the first matrix:\n");
for (i = 0; i<rowsFirst; ++i)
{
for (j = 0; j <colsFirst; ++j)
{
scanf("%d", &firstMatrix[i][j]);
}
}
Lab Record Computer Programming Lab Page 38 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

printf("Enter rows and columns for the second matrix: ");


scanf("%d %d", &rowsSecond, &colsSecond);
if (colsFirst != rowsSecond)
{
printf("Error! Number of columns in the first matrix should be equal to
the number of rows in the second matrix.\n");
return 1;
}
printf("Enter elements of the second matrix:\n");
for (int i = 0; i<rowsSecond; ++i)
{
for (int j = 0; j <colsSecond; ++j)
{
scanf("%d", &secondMatrix[i][j]);
}
}
multiplyMatrices(firstMatrix, secondMatrix, result, rowsFirst, colsFirst,
rowsSecond, colsSecond);
printf("Resultant matrix after multiplication:\n");
displayMatrix(result, rowsFirst, colsSecond);
return 0;
}

Lab Record Computer Programming Lab Page 39 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 26: Write a C program to find the transpose of a matrix


Aim: To find the transpose of a matrix
Source Code:
#include <stdio.h>
void transposeMatrix(int rows, int cols, int matrix[rows][cols])
{
int transposedMatrix[cols][rows];
int i, j;
for (i = 0; i< rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
transposedMatrix[j][i] = matrix[i][j];
}
}
printf("Transpose of the matrix:\n");
for (int i = 0; i< cols; ++i)
{
for (int j = 0; j < rows; ++j)
{
printf("%d\t", transposedMatrix[i][j]);
}
printf("\n");
}
}

int main()
{
Lab Record Computer Programming Lab Page 40 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

int matrix[50][50];
int rows, cols;
printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &rows, &cols);
printf("Enter the elements of the matrix:\n");
for (int i = 0; i< rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
scanf("%d", &matrix[i][j]);
}
}
transposeMatrix(rows, cols, matrix);
return 0;
}

Lab Record Computer Programming Lab Page 41 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 27: Write a C program to determine the lower triangular matrix of a given
matrix
Aim: To determine the lower triangular matrix of a given matrix
Source Code:
#include <stdio.h>
void lowerTriangular(int mat[10][10], int row, int col)
{
int i, j;
for (i = 0; i< row; i++)
{
for (j = 0; j < col; j++)
{
if (j > i)
{
printf("0 ");
}
else
{
printf("%d ", mat[i][j]);
}
}
printf("\n");
}
}

int main()
{
int matrix[10][10];

Lab Record Computer Programming Lab Page 42 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

int row, col;


printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &row, &col);
printf("Enter the elements of the matrix:\n");
for (int i = 0; i< row; i++)
{
for (int j = 0; j < col; j++)
{
scanf("%d", &matrix[i][j]);
}
}
printf("LowerTriangular Matrix:\n");
lowerTriangular(matrix, row, col);
return 0;
}

Lab Record Computer Programming Lab Page 43 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 28: Write a C program to determine the upper triangular matrix of a given
matrix
Aim: To determine the upper triangular matrix of a given matrix
Source Code:
#include <stdio.h>
void upperTriangular(int mat[10][10], int row, int col)
{
int i, j;
for (i = 0; i< row; i++)
{
for (j = 0; j < col; j++)
{
if (j <i)
{
printf("0 ");
}
else
{
printf("%d ", mat[i][j]);
}
}
printf("\n");
}
}

int main()
{
int matrix[10][10];

Lab Record Computer Programming Lab Page 44 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

int row, col;


printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &row, &col);
printf("Enter the elements of the matrix:\n");
for (int i = 0; i< row; i++)
{
for (int j = 0; j < col; j++)
{
scanf("%d", &matrix[i][j]);
}
}
printf("Upper Triangular Matrix:\n");
upperTriangular(matrix, row, col);
return 0;
}

Lab Record Computer Programming Lab Page 45 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 29: Write a C program to find the sum of all diagonal elements in a matrix
Aim: To find the sum of all the diagonal elements in a matrix
Source Code:
#include <stdio.h>
int main()
{
int matrix[10][10];
int row, col, i, j;
int diagonalSum = 0;
printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &row, &col);
printf("Enter the elements of the matrix:\n");
for (int i = 0; i< row; i++)
{
for (int j = 0; j < col; j++)
{
scanf("%d", &matrix[i][j]);
}
}
for (i = 0; i< row; i++)
{
for (j = 0; j < col; j++)
{
if (i == j)
{
diagonalSum += matrix[i][j];
}
}
Lab Record Computer Programming Lab Page 46 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

}
printf("Sum of diagonal elements: %d\n", diagonalSum);
return 0;
}

Lab Record Computer Programming Lab Page 47 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 30: Write a C program to concatenate two strings without using built-in
functions.
Aim: To concatenate two strings without using built-in functions
Source Code:
#include <stdio.h>
int main()
{
char str1[50],str2[50];
char result[100];
int i, j;
printf("\n Please enter str1:\n");
gets(str1);
printf("\n Please enter str2:\n");
gets(str2);
for (i = 0; str1[i] != '\0'; i++)
{
result[i] = str1[i];
}
for (j = 0; str2[j] != '\0'; j++)
{
result[i + j] = str2[j];
}
result[i + j] = '\0';
printf("Concatenated String: %s\n", result);
return 0;
}

Lab Record Computer Programming Lab Page 48 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 31: Write a C program to compare two strings without using built-in
functions
Aim: To compare two strings without using built-in functions

Source Code:
#include<stdio.h>
int main()
{
int a=0,i;
char s1[20],s2[20];
printf("\n Please enter String1:\n");
gets(s1);
printf("\n Please enter String2:\n");
gets(s2);
for(i=0;s1[i]!='\0'||s2[i]!='\0';i++)
{
if(s1[i]!=s2[i])
{
a=1;
break;
}
}
if(a==0)
{
printf("the two strings are same.\n");
}
else
{

Lab Record Computer Programming Lab Page 49 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

printf("the two strings are not same.\n");


}
}

Lab Record Computer Programming Lab Page 50 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 32: Write a C program to find the length of the string without using built-
in functions.
Aim: To find the length of the string without using built-in functions.
Source Code:
#include<stdio.h>
main()
{
int count=0,i=0;
char name[20];
printf("enter name:\n");
gets(name);
while(name[i]!='\0')
{
count ++;
i++;
}
printf("\n The length of the string is : %d",count);
}

Lab Record Computer Programming Lab Page 51 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 33: Write a C program to copy one string to another string without using
built-in functions.
Aim: To copy one sting to another string without using built-in functions
Source Code:
#include<stdio.h>
main()
{
char str1[20],str2[20];
int i;
printf("\n Please enter String1:");
gets(str1);
for(i=0;str1[i]!='\0';i++)
{
str2[i]=str1[i];
}
str2[i]='\0';
printf("\n Copied String is %s", str2);
getch();
}

Lab Record Computer Programming Lab Page 52 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 34: Write a C program to find the length of a string without using built-in
functions
Aim: To find the length of a string
Source Code:
#include<stdio.h>
int main()
{
char str[100];
int i, length=0;
printf("Enter a string:");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
length++;
}
printf("\n Length of the string is: %d",length);
return 0;
}

Lab Record Computer Programming Lab Page 53 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 35: Write a C program to find whether the string is palindrome or not
without using built-in funtions
Aim: To find whether the string is a palindrome or not without using built-in
functions.
Source Code:
#include <stdio.h>
int main()
{
char str[100];
int i, L, flag = 0;
printf("\n Enter a string: ");
scanf("%s", str);
L = 0;
while (str[L] != '\0')
{
L++;
}
for(i = 0; i < L / 2; i++)
{
if (str[i] != str[L - i - 1])
{
flag = 1;
break;
}
}
if (flag == 0)
{
printf("%s is a palindrome.\n", str);
}
Lab Record Computer Programming Lab Page 54 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

else
{
printf("%s is not a palindrome.\n", str);
}
return 0;
}

Lab Record Computer Programming Lab Page 55 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 36: Write a C program to reverse a string without using built-in functions
Aim: To reverse a string without using built-in functions
Source Code:
#include<stdio.h>
main()
{
char s1[20],s2[20];
int i,l=0;
printf("\n Enter s1:\n");
gets(s1);
while(s1[l]!='\0')
{
l++;
}
for(i=0;s1[i]!='\0';i++)
{
s2[i]=s1[l-i-1];
}
s2[i]!='\0';
printf("\n The reversed string is :%s", s2);
}

Lab Record Computer Programming Lab Page 56 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 37: Write a C program to demonstrate the differences between structures


and unions
Aim: To demonstrate the differences between structures and unions
Source Code:
#include<stdio.h>
union stud
{ int roll;
float marks;
char name[50];
};
struct student
{ int roll;
float marks;
char name[50];
};
main()
{
union stud X;
struct student S;
printf("\n Please enter roll no:");
scanf("%d",&X.roll);
printf("\n Please enter marks:");
scanf("%f",&X.marks);
printf("\n Please enter the name:");
scanf("%s",&X.name);
printf("\n roll no:%d",X.roll);
printf("\n marks:%f",X.marks);
printf("\n name:%s",X.name);

Lab Record Computer Programming Lab Page 57 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

printf("\n Please enter roll no:");


scanf("%d",&S.roll);
printf("\n Please enter marks:");
scanf("%f",&S.marks);
printf("\n Please enter the name:");
scanf("%s",&S.name);
printf("\n roll no:%d",S.roll);
printf("\n marks:%f",S.marks);
printf("\n name:%s",S.name);
}

Lab Record Computer Programming Lab Page 58 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 38: Write a C program to illustrate the concept of passing structures to


functions
Aim: To illustrate the concept of passing structures to functions
Source Code:
#include<stdio.h>
void print_complex(struct complex);
void sum_complex(struct complex, struct complex);
void mul_complex(struct complex,struct complex);
struct complex
{
float real;
float img;
};
main()
{
struct complex c1,c2;
printf("please enter real and imaginary parts of c1 :");
scanf("%f%f",&c1.real,&c1.img);
printf("please enter real and imaginary parts of c2 :");
scanf("%f%f",&c2.real,&c2.img);
printf("\n complex number c1 is :");
print_complex(c1);
printf("\n complex number c2 is:");
print_complex(c2);
printf("\n sum of the two complex numbers is :");
sum_complex(c1,c2);
printf("\n product of the two complex numbers is :");
mul_complex(c1,c2);

Lab Record Computer Programming Lab Page 59 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

return 0;
}
void print_complex(struct complex c3)
{
printf("%f+i%f",c3.real,c3.img);
}
void sum_complex(struct complex c3, struct complex c4)
{
struct complex c5;
c5.real=c3.real+c4.real;
c5.img=c3.img+c4.img;
print_complex(c5);
}
void mul_complex(struct complex c3, struct complex c4)
{
struct complex c6;
c6.real=c3.real*c4.real-c3.img*c4.img;
c6.img=c3.real*c4.img+c4.real*c3.img;
print_complex(c6);
}

Lab Record Computer Programming Lab Page 60 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 39: Write a C program to illustrate the concept of returning structures


from functions
Aim: To illustrate the concept of returning structures from functions
Source Code:
#include<stdio.h>
void print_complex(struct complex);
void sum_complex(struct complex,struct complex);
struct complex mul_complex(struct complex,struct complex);
struct complex
{
float real;
float img;
};
main()
{
struct complex c1,c2,c7;
printf("please enter real and imaginary parts of c1 :");
scanf("%f%f",&c1.real,&c1.img);
printf("please enter real and imaginary parts of c2 :");
scanf("%f%f",&c2.real,&c2.img);
printf("\n complex number c1 is :");
print_complex(c1);
printf("\n complex number c2 is:");
print_complex(c2);
printf("\n sum of the two complex numbers is :");
sum_complex(c1,c2);
printf("\n product of the two complex numbers is :");
mul_complex(c1,c2);

Lab Record Computer Programming Lab Page 61 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

c7=mul_complex(c1,c2);
print_complex(c7);
}
void print_complex(struct complex c3)
{
printf("%f+i%f",c3.real,c3.img);
}
void sum_complex(struct complex c3, struct complex c4)
{
struct complex c5;
c5.real=c3.real+c4.real;
c5.img=c3.img+c4.img;
print_complex(c5);
}
struct complex mul_complex(struct complex c3,struct complex c4)
{
struct complex c6;
c6.real=c3.real*c4.real-c3.img*c4.img;
c6.img=c3.real*c4.img+c4.real*c3.img;
return c6;
}

Lab Record Computer Programming Lab Page 62 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 40: Write a C program to illustrate the concept of a function without return
type and without parameter list
Aim: A function without return type and without parameter list
Source Code:
#include<stdio.h>
void avg( );
main( )
{
avg( );
}
void avg( )
{
int m1,m2,m3;
float avg ;
printf("\n Enter three subject marks :”);
scanf("%d %d %d",&m1,&m2,&m3);
avg=(m1+m2+m3)/3;
printf(“\nAverage = %f”, avg);
}

Lab Record Computer Programming Lab Page 63 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 41: Write a C program to illustrate the concept of function without return
type and with parameter list
Aim: A function without return type and with parameter list
Source Code:
#include<stdio.h>
void avg(int,int,int);
main()
{
int m1,m2,m3;
printf("\nEnter three subject marks:");
scanf("%d %d %d",&m1,&m2,&m3);
avg(m1,m2,m3);
}
void avg (int a,int b,int c)
{
float avg;
avg=(a+b+c)/3;
printf("\nAverage=%f",avg);
}

Lab Record Computer Programming Lab Page 64 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 42: Write a C program to illustrate the concept of function with return type
and without parameter list
Aim: A function with return type and without parameter list
Source Code:
#include<stdio.h>
float avg();
main()
{
float result;
result=avg();
printf(“\n Average = %f”, result);
}
float avg()
{
int m1,m2,m3;
float avg;
printf("Enter three subject marks:");
scanf("%d %d %d",&m1,&m2,&m3);
avg=(m1+m2+m3)/3;
return avg;
}

Lab Record Computer Programming Lab Page 65 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 43: Write a C program to illustrate the concept of function with return type
and with parameter list
Aim: A function with return type and with parameter list.
Source Code:
#include<stdio.h>
float avg(int,int,int);
main()
{
float res;
int m1,m2,m3;
printf("Enter three subject marks:");
scanf("%d %d %d",&m1,&m2,&m3);
res=avg(m1,m2,m3);
printf("\n Average=%f",res);
}
float avg (int a, int b, int c)
{
float x;
x=(a+b+c)/3;
return x;
}

Lab Record Computer Programming Lab Page 66 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 44: Write a C program to illustrate the concept of function passing


parameters using pass by value mechanism
Aim: Illustrate the concept of pass by value
Source Code:
#include<stdio.h>
void sum(int,int);
main()
{
int a,b;
printf("please enter values of a and b:");
scanf("%d %d",&a,&b);
sum(a,b);
}
void sum (int a,int b)
{
int sum=0;
sum=a+b;
printf("\n Sum=%d",sum);
}

Lab Record Computer Programming Lab Page 67 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 45: Write a C program to generate a Fibonacci series using recursion


Aim: Recursive function to generate Fibonacci Series
Source Code:
#include <stdio.h>
int fib(int n)
{
if (n <= 1)
{
return n;
}
else
{
return fib(n - 1) + fib(n - 2);
}
}
int main()
{
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("The first %d terms of the Fibonacci series are: ", n);
for (int i = 0; i < n; i++)
{
printf("%d ", fib(i));
}
printf("\n");
return 0;
}
Lab Record Computer Programming Lab Page 68 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 46: Write a C program to find the LCM of two numbers using recursion
Aim: Find the LCM of two numbers using recursion
Source Code:
#include <stdio.h>
int lcm(int, int);
int main()
{
int a, b, result;
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
result = lcm(a, b); //call the function lcm recursively.
printf("The LCM of %d and %d is %d\n", a, b, result);
return 0;
}
int lcm(int a, int b)
{
static int common = 1;
if (common % a == 0 && common % b == 0)
{
return common;
}
common++;
lcm(a, b);
}

Lab Record Computer Programming Lab Page 69 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 47: Write a C program to find the factorial of a number using recursion
Aim: Recursive function to find the factorial of a given number
Source Code:
#include<stdio.h>
int rec_fact(int);
main()
{
int n,fact;
printf("enter the value of n:\n");
scanf("%d",&n);
fact=rec_fact(n);
printf("\n Factorial of %d is %d",n,fact);
}
int rec_fact(int n)
{
if(n==0)
{
return 1;
}
else
{
return n*rec_fact(n-1);
}
}

Lab Record Computer Programming Lab Page 70 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 48: Write a C pgoram to find the sum of n number using recursion
Aim: Recursive function to find the sum of n numbers
Source Code:
#include<stdio.h>
int sum_rec(int);
main()
{
int num,sum;
printf("enter a number");
scanf("%d",&num);
sum=sum_rec(num);
printf("sum=%d",sum);
}
int sum_rec(int num)
{
if(num!=0)
{
return num+sum_rec(num-1);
}
else
{
return 0;
}
}

Lab Record Computer Programming Lab Page 71 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 49: Write a C program to swap two numbers using call by reference
Aim: To swap two numbers using call by reference
Source Code:
#include <stdio.h>
void swap(int *num1, int *num2)
{
int temp = *num1;
*num1 = *num2;
*num2 = temp;
}
int main()
{
int a, b;
printf("\nEnter two numbers:\n");
scanf("%d %d", &a, &b);
printf("\nBefore swapping: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("\nAfter swapping: a = %d, b = %d\n", a, b);
return 0;
}

Lab Record Computer Programming Lab Page 72 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 50: Write a C program to find number of lowercase, uppercase, digits and
other characters in strings using pointers
Aim: Find number of lowercase, uppercase, digits and other characters in strings
using pointers
Source Code:
#include <stdio.h>
void countCharacters(const char *str, int *lowercase, int *uppercase, int *digits, int
*others)
{
while (*str != '\0')
{
if ((*str >= 'a') && (*str <= 'z'))
{
(*lowercase)++;
}
else if ((*str >= 'A') && (*str <= 'Z'))
{
(*uppercase)++;
}
else if ((*str >= '0') && (*str <= '9'))
{
(*digits)++;
}
else
{
(*others)++;
}
str++;
}

Lab Record Computer Programming Lab Page 73 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

}
int main()
{
char inputString[100];
int lowercase = 0, uppercase = 0, digits = 0, others = 0;
printf("Enter a string: ");
fgets(inputString, sizeof(inputString), stdin);
countCharacters(inputString, &lowercase, &uppercase, &digits, &others);
printf("Lowercase characters: %d\n", lowercase);
printf("Uppercase characters: %d\n", uppercase);
printf("Digits: %d\n", digits);
printf("Other characters: %d\n", others);
return 0;
}

Lab Record Computer Programming Lab Page 74 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 51: Write a C program to manipulate the array values using pointers
Aim: To manipulate the array values using pointers
Source Code:
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("Original array values: ");
for (int i = 0; i < 5; i++)
{
printf("%d ", *(ptr + i));
}
printf("\n");
*(ptr + 0) = 10;
printf("Modified array values: ");
for (int i = 0; i < 5; i++)
{
printf("%d ", *(ptr + i));
}
printf("\n");
return 0;
}

Lab Record Computer Programming Lab Page 75 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 52: Write a C program to write and read text into a file
Aim: To write and read text into a file
Source Code:
#include <stdio.h>
int main()
{
FILE *filePointer;
char data[100];
filePointer = fopen("example.txt", "w");
if (filePointer == NULL)
{
printf("Could not open the file for writing.\n");
return 1;
}
printf("Enter text to write to the file: ");
fgets(data, sizeof(data), stdin);
fprintf(filePointer, "%s", data);
fclose(filePointer);
filePointer = fopen("example.txt", "r");
if (filePointer == NULL)
{
printf("Could not open the file for reading.\n");
return 1;
}
printf("\nReading from the file:\n");
while (fgets(data, sizeof(data), filePointer) != NULL)
{
printf("%s", data);
Lab Record Computer Programming Lab Page 76 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

}
fclose(filePointer);
return 0;
}

Lab Record Computer Programming Lab Page 77 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 53: Write a C program to copy the contents of one file to another file
Aim: To copy the contents of one file to another file
Source Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
c = fgetc(fptr1);
while (c != EOF)
{
Lab Record Computer Programming Lab Page 78 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}

Lab Record Computer Programming Lab Page 79 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 54: Write a C program to find the number of lines, words and characters in
a file
Aim: To write a C program to find no. of lines, words and characters in a file
Source Code:
#include<stdio.h>
int main()
{
FILE *fp;
char ch;
int lines = 0, words = 0, characters = 0;
char filename[50];
printf("Enter the filename: ");
scanf("%s", filename);
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Error opening file!\n");
return 1;
}
while ((ch = fgetc(fp)) != EOF)
{
characters++;
if (ch == '\n')
{
lines++;
}
if (ch == ' ' || ch == '\t' || ch == '\n')
{

Lab Record Computer Programming Lab Page 80 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

words++;
}
}
if (characters > 0 && ch != ' ' && ch != '\t' && ch != '\n')
{
words++;
}
fclose(fp);
printf("Lines: %d\n", lines);
printf("Words: %d\n", words);
printf("Characters: %d\n", characters);
return 0;
}

Lab Record Computer Programming Lab Page 81 of 84


University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

Program 55: Write a C program to read integer values from the user and store them
in even.txt if the number is even and in odd.txt if the number is odd.
Aim: To write a C program to read integer values from the user and store them
ineven.txt if the number is even and in odd.txt if the number is odd.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int num;
char ans[5];
FILE *po, *pe;
clrscr();
po = fopen("odd.txt","w+");
if(po==NULL)
{
printf("Something went wrong!");
exit(1);
}
pe = fopen("even.txt","w+");
if(pe==NULL)
{
printf("Something went wrong!");
exit(1);
}
do
{
Lab Record Computer Programming Lab Page 82 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

printf("Enter number:\n");
scanf("%d",&num);
if(num%2!=0)
{
fprintf(po,"%d\t",num);
}
else
{
fprintf(pe,"%d\t",num);
}
printf("Enter another number? (no to terminate)\n");
fflush(stdin);
scanf("%s",ans);
strlwr(ans);
}while(strcmp(ans,"no")!=0);
rewind(po);
rewind(pe);
printf("\nContent read from even.txt:\n");
while(!feof(pe))
{
fscanf(pe,"%d\t",&num);
printf("%d\t", num);
}
printf("\nContent read from odd.txt:\n");
while(!feof(po))
{
fscanf(po,"%d\t",&num);
printf("%d\t", num);
Lab Record Computer Programming Lab Page 83 of 84
University College of Engineering Narasaraopeta
Jawaharlal Nehru Technological University Kakinada
===================================

fclose(pe);
fclose(po);
}

Lab Record Computer Programming Lab Page 84 of 84

You might also like