0% found this document useful (0 votes)
26 views

C language programs practice sheets

ITP manual C program codes Pratice C language Practice sheets

Uploaded by

svec24a81a6159
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

C language programs practice sheets

ITP manual C program codes Pratice C language Practice sheets

Uploaded by

svec24a81a6159
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

SRI VASAVI ENGINEERING COLLEGE

(AUTONOMOUS)
PEDATADEPALLI, TADEPALLIGUDEM.

Department of CSE

CERTIFICATE

This is to certify that this is a bonafide record of practical work done in

Computer Programming lab by Mr./Miss.

____________________________ bearing Roll No. ____________

of I semester, Department of CSE during the academic year 2024 – 25.

No. of Programs done:

Examiner – 1 Examiner – 2

Head of the Department


Index

Page
S_NO Lab Program Remarks
No
1 a) C Program to find the sum and average of 3 numbers.
b) C Program to Conversion of Fahrenheit to Celsius and vice versa.
c) C Program to find the area and perimeter of a circle.
2 a) C Program to find the Simple interest and Compound Interest.
b) C Program to find Area of triangle by using “Heron's Formula".
c) C Program to find the square and square root of a number.
3 a) C Program to find the midpoint of the line.
b) C Program to find the distance between two points.
c) C Program to find whether a given integer is odd or even number.
4 a) C Program to find max and min of 3 numbers by using ternary operator
b) C Program to swap the variables values using bitwise XOR operator
c) C Program to take marks of 5 subjects in integers, and find the total, average
in float
5 a) C Program to find the roots of a quadratic equation
b) C Program to find the max and min of four numbers using if-else.
c) C Program to find if the given year is leap year or not.
6 a) C Program to find the grades of a student using else if ladder.
b) C Program to simulate a simple calculator Using switch statement.
7 a) C Program to find the sum of first n numbers
b) C Program to check if the given number is prime or not.
8 a) C Program to find the factorial of a given number.
b) C Program to find the given number is palindrome or not.
c) C Program to find the given number is Armstrong or not.
9 a) C Program to print number pattern #1.
b) C Program to print number pattern #2.
10 a) C Program to print number pattern #3.
b) C Program to print pyramid of stars.
11 a) C Program to print sum of elements of the array.
b) C Program to print sum of even numbers in the array.
c) C Program to print primes in given array.
12 a) C Program to print max and min of the given array.
b) C Program to perform linear search.
13 a) C Program to print elements of the array in reverse order.
b) C Program to perform Bubble sort.
c) C Program to remove duplicate elements in the given array.
14 a) C Program to perform matrix addition.
b) C Program to perform matrix multiplication .
15 a) C Program to perform transpose of a matrix.
b) C Program to print primes in given intervals.
16 a) C Program to find the length of the string using strlen.
b) C Program to find the length of the string without using strlen .
17 a) C program to concatenate 2 strings with using strcat function
b) C program to concatenate 2 strings without using strcat function.
18 a) C program to find the reverse of string without using strrev function
b) C Program to copy one string to another string using pointers.
c) C program to find no of lowercase, uppercase, digits and other characters
using pointers
19 a) C Program to check if the given string is palindrome or not.
b) C Program to remove special characters and numbers in the input string.
c) C Program to find 2’s Compliment of a binary number.
S_NO Lab Program Page Remarks
No
20 a) C Program to find NCR using user defined function.
b) C Program to find length of the string using user defined function.
c) C Program to perform transpose of matrix using user defined function.
21 a) C Program to find the factorial of number using recursion.
b) C Program to print the Fibonacci series using recursion.
c) C Program to find the lcm of 2 numbers using recursion.
22 a) C program to write and read text into a file.
b) C program to copy the contents of one file to another file.
23 a) C program to swap 2 variables using call by reference.
b) C program to count the number of characters in the given file
c) C program to find the sum of elements in array using pointers
24 a) C program to find the total, average of n students using structures
b) C program to Enter n students data(roll,name,marks) and display failed
students list(if marks > = 40 : pass)
25 a) C Program to demonstrate between structures and unions.
b) C program to copy one structure variable to another structure of the same
type
c) c program to find the number of lines characters words in a file
1) a) Develop a c program to find the sum and average of 3 numbers
#include <stdio.h>
int main()
{
int a, b, c, s;
float avg;
printf("Enter three numbers:\n");
scanf("%d%d%d", &a, &b, &c);
s = a + b + c;
avg = s / 3.0;
printf("\nSum = %d, Average = %f\n", s, avg);
return 0;
}
Output:
1) b) Develop a C Program to Conversion of Fahrenheit to Celsius and vice versa
#include <stdio.h>
int main()
{
float c, f;
printf("Enter temperature in Fahrenheit:\n");
scanf("%f", &f);
c = (f - 32) * 5 / 9;
printf("Temperature in Celsius is %.1f\n", c);
printf("Enter temperature in Celsius:\n");
scanf("%f", &c);
f = ((9 * c) / 5) + 32;
printf("Temperature in Fahrenheit is %.1f\n", f);
return 0;
}
Output:
1) c) Develop a C Program to Find Area and Perimeter of Circle
#include <stdio.h>
#define PI 3.14159 // Define the constant for PI
int main()
{
float r, area, perimeter;
printf("Enter radius of circle: ");
scanf("%f", &r);
area = PI * r * r;
perimeter = 2 * PI * r;
// Output the area and perimeter
printf("Area of circle: %.2f\n", area);
printf("Perimeter (Circumference) of circle: %.2f\n", perimeter);
return 0;
}
Output:
2)a) Develop a c program to find the Simple interest and Compound
Interest
#include <stdio.h>
#include <math.h>
int main()
{
float principal, rate, simple_interest, compound_interest;
int years;
printf("Enter the value of principal: ");
scanf("%f", &principal);
printf("Enter the annual interest rate (in percentage): ");
scanf("%f", &rate);
printf("Enter the period in years: ");
scanf("%d", &years);
simple_interest = (principal * rate * years) / 100;
printf("Simple interest is %.2f\n", simple_interest);
compound_interest = principal * (pow(1 + rate / 100, years) - 1);
printf("Compound interest is %.2f\n", compound_interest);
return 0;
}
Output:
2) b) Develop a C program to find Area of triangle from sides can be
calculated by using “Heron's Formula".
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;
float s,area;
printf("enter a b c values:\n");
scanf("%d%d%d",&a,&b,&c);
s=(a+b+c)/2.0;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\nArea : %.2f",area);
return 0;
}
Output:
2)c) Develop a C program to find the square and square root of a number
#include <stdio.h>
#include <math.h>
int main()
{
float a,b,c;
printf("Enter the number: ");
scanf("%f", &a);
b = a * a;
c = sqrt(a);
printf("The square value is %.2f\n", b);
printf("The square root value is %.2f\n", c);
return 0;
}
Output:
3)a) Develop a c program to find the midpoint of the line.
#include<stdio.h>
int main()
{
float x1,x2,y1,y2,x,y;
printf("X1:\n");
scanf("%f",&x1);
printf("Y1:\n");
scanf("%f",&y1);
printf("X2:\n");
scanf("%f",&x2);
printf("Y2:\n");
scanf("%f",&y2);
x=(x1+x2)/2;
y=(y1+y2)/2;
printf("\nBinoy's house is located at (%.1f,%.1f)",x,y);
}
Output:
3)b) Develop a c program to find the distance between two points
#include <stdio.h>
#include <math.h>
int main()
{
float x1, y1, x2, y2,distance;
printf("Input x1: ");
scanf("%f", &x1);
printf("Input y1: ");
scanf("%f", &y1);
printf("Input x2: ");
scanf("%f", &x2);
printf("Input y2: ");
scanf("%f", &y2);
distance = sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
printf("Distance between the said points: %.4f",distance);
printf("\n");
return 0;
}
Output:
3)c) Develop a c program to find whether a given integer is odd or even
number.
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number % 2 == 0)
{
printf("%d is an even number.\n", number);
} else
{
printf("%d is an odd number.\n", number);
}
return 0;
}
Output:
4)a) Find the maximum and minimum of 3 numbers by using ternary
operator
#include<stdio.h>
int main()
{
int a, b, c, max, min;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
min = (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c);
printf("The maximum number is: %d\n", max);
printf("The minimum number is: %d\n", min);
return 0;
}
Output:
4)b) Develop c program to swap the variables values using bitwise XOR
operator
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
// Print original values
printf("Before swapping: a = %d, b = %d\n", a, b);
// Swapping using XOR
a = a ^ b; // Step 1: a now holds a ^ b
b = a ^ b; // Step 2: b now holds (a ^ b) ^ b = a
a = a ^ b; // Step 3: a now holds (a ^ b) ^ a = b
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}

Output:
4)c) Write a C Program to take marks of 5 subjects in integers, and find the
total, average in float
#include <stdio.h>
int main()
{
int tel, eng, mat, sci, soc, total;
float average;
printf("Enter marks for Telugu (out of 100): ");
scanf("%d", &tel);
printf("Enter marks for English (out of 100): ");
scanf("%d", &eng);
printf("Enter marks for Maths (out of 100): ");
scanf("%d", &mat);
printf("Enter marks for Science (out of 100): ");
scanf("%d", &sci);
printf("Enter marks for Social (out of 100): ");
scanf("%d", &soc);
total = tel + eng + mat + sci + soc;
average = total / 5.0; // Dividing by 5.0 to get float result
printf("Total marks = %d\n", total);
printf("Average marks = %.2f\n", average);
return 0;
}
Output:
5)a)Write a c program to find the roots of a quadratic equation
#include <stdio.h>
#include <math.h>
int main()
{
float a,b,c,discriminant,root1,root2,realPart,imagPart;
printf("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);
discriminant = b*b - 4*a*c;
if (discriminant > 0)
{
root1 = (-b + sqrt(discriminant)) / (2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);
printf("Roots are real and distinct: %.2f and %.2f\n", root1, root2);
}
else if (discriminant == 0)
{
// Two equal real roots
root1 = root2 = -b / (2*a);
printf("Roots are real and equal: %.2f and %.2f\n", root1, root2);
}
else
{
// Complex roots
realPart = -b / (2*a);
imagPart = sqrt(-discriminant) / (2*a);
printf("Roots are complex: %.2f + %.2fi and %.2f - %.2fi\n", realPart,
imagPart,realPart, imagPart);
}
return 0;
}
Output:
5)b)Develop a c program to find the max and min of four numbers using if-
else.
#include <stdio.h>
int main()
{
int a, b, c, d, max, min;
printf("Enter four integers: ");
scanf("%d %d %d %d", &a, &b, &c, &d);
if (a >= b && a >= c && a >= d) {
max = a;
} else if (b >= a && b >= c && b >= d) {
max = b;
} else if (c >= a && c >= b && c >= d) {
max = c;
} else {
max = d;
}
if (a <= b && a <= c && a <= d) {
min = a;
} else if (b <= a && b <= c && b <= d) {
min = b;
} else if (c <= a && c <= b && c <= d) {
min = c;
} else {
min = d;
}
// Output the results
printf("Maximum: %d\n", max);
printf("Minimum: %d\n", min);
return 0;
}
Output:
5)c) Develop a program to check whether a given year is a leap year or not.

#include <stdio.h>
int main()
{
int year;
printf("Enter year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
printf("%d is a leap year.\n", year);
}
else
{
printf("%d is not a leap year.\n", year);
}
return 0;
}
Output:
6)a) Write a c program to find the grades of a student according below
percentage
if percentage>=90 then O Grade
percentage>=80 then A grade
percentage>=70 then B grade
percentage>=60 then C grade
percentage>=50 then D grade
below 50 then Fail
#include <stdio.h>
int main()
{
int tel, eng, mat, sci, soc, total;
float percentage;
char grade;
// Input marks
printf("Enter marks for Telugu (out of 100): ");
scanf("%d", &tel);
printf("Enter marks for English (out of 100): ");
scanf("%d", &eng);
printf("Enter marks for Maths (out of 100): ");
scanf("%d", &mat);
printf("Enter marks for Science (out of 100): ");
scanf("%d", &sci);
printf("Enter marks for Social (out of 100): ");
scanf("%d", &soc);
// Calculate total and percentage
total = tel + eng + mat + sci + soc;
percentage = (total / 500.0) * 100;
// Determine grade based on percentage
if (percentage >= 90) {
grade = 'O';
} else if (percentage >= 80) {
grade = 'A';
} else if (percentage >= 70) {
grade = 'B';
} else if (percentage >= 60) {
grade = 'C';
} else if (percentage >= 50) {
grade = 'D';
} else {
grade = 'F'; // Fail
}
// Output results
printf("Total marks = %d\n", total);
printf("Percentage = %.2f%%\n", percentage);
printf("Grade = %c\n", grade);

return 0;
}

Output:
6)b) Develop a C program to simulate a simple calculator Using switch
statement
#include<stdio.h>
int main()
{
int a,b;
char c;
printf("Enter 2 number:\n");
scanf("%d %d",&a,&b);
printf("Enter operator:\n");
scanf(" %c",&c);
switch(c)
{
case '+' : printf("The sum is %d",a+b);
break;
case '-' : printf("The difference is %d",a-b);
break;
case '*' : printf("The product is %d",a*b);
break;
case '/' : printf("The quotient is %d",a/b);
break;
case '%' : printf("The remainder is %d",a%b);
break;
default: printf("Invalid Input");
}
}
Output:
7)a) Develop a C program to find the sum of first n numbers
#include <stdio.h>
int main()
{
int n, sum = 0,i;
printf("Enter the value of n: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
sum=sum+i; // Add the current number to sum
}
// Output the result
printf("The sum of the first %d numbers is: %d\n", n, sum);
return 0;
}
Output:
7)b) Write a c program to check if the given number is prime or not
#include <stdio.h>
int main()
{
int num, count = 0,i;
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1) {
printf("%d is not a prime number.\n", num);
return 0;
}
for (i = 1; i <= num; i++) // Check for factors
{
if (num % i == 0)
{
count++;
}
if (count > 2) // If count exceeds 2, break the loop
{
break;
}
}
if (count == 2) // Determine if the number is prime
{
printf("%d is a prime number.\n", num);
}
else
{
printf("%d is not a prime number.\n", num);
}
}
Output:
8)a) Write a c program to find the factorial of a given number
#include <stdio.h>
int main()
{
unsigned long long int f = 1;
int i, n;
printf("enter n value:\n");
scanf("%d", &n);
// Calculate the factorial
for(i = 1; i <= n; i++) {
f = f * i;
}
// Print the result
printf("factorial of %d is %llu",n,f);
return 0;
}
Output:
8)b) Develop a c program to find the given number is palindrome or not

#include <stdio.h>
int main()
{
int num, reversedNum = 0, remainder, originalNum;
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num; // Store the original number
// Reverse the number
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
if (originalNum == reversedNum) {
printf("%d is a palindrome.\n", originalNum);
} else {
printf("%d is not a palindrome.\n", originalNum);
}
return 0;
}

Output:
8)c) write a c program to check if the given number is armstrong or not
#include <stdio.h>
#include <math.h>
int main()
{
int num,originalNum,remainder,n = 0,result = 0;
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num;
// Calculate the number of digits in the input number
while (originalNum != 0) {
originalNum /= 10;
++n;
}
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
result = result+pow(remainder, n);
originalNum /= 10;
}
if (result == num) {
printf("%d is an Armstrong number.\n", num);
} else {
printf("%d is not an Armstrong number.\n", num);
}
return 0;
}

Output:
9)a) Write a program to print the given pattern.
Sample Input 1: 5
Sample Output 1:
12345
1234
123
12
1
#include <stdio.h>
int main()
{
int n,i,j;
printf("Enter a number: ");
scanf("%d", &n);
// Outer loop for the number of rows
for (i = n; i >= 1; i--)
{
// Inner loop for printing numbers in each row
for (j = 1; j <= i; j++)
{
printf("%d ", j);
}
// Move to the next line after each row
printf("\n");
}
return 0;
}
Output:
9)b) Write a program to print the given pattern.
Enter a number: 5
1
12
123
1234
12345
#include <stdio.h>
int main()
{
int n,i,j;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
// Inner loop for printing numbers in each row
for (j = 1; j <= i; j++)
{
printf("%d ", j);
}
// Move to the next line after each row
printf("\n");
}
return 0;
}
Output:
10)a) Write a c program to print the following pattern
Enter a number: 4
1
23
456
7 8 9 10
#include <stdio.h>
int main()
{
int n, num = 1,i,j;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d ", num);
num++;
}
printf("\n");
}

return 0;
}
Output:
10)b) Write a c program to print the following pattern
enter number: 5
*
**
***
****
*****
#include<stdio.h>
int main()
{
int i,j,n,k;
printf("enter number:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=n;k>=i;k--)
printf(" ");
for(j=1;j<=i;j++)
printf("* ");
printf("\n");
}
}
Output
11)a)Write a c program to sum of the elements in array
#include <stdio.h>
int main()
{
int n,i,sum=0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
sum = sum+arr[i];
}
printf("Sum of the elements: %d\n", sum);
return 0;
}
Output:
11)b) Write a c program to print sum of even numbers in array
#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
{
sum += arr[i];
}
}
printf("Sum of the even elements: %d\n", sum);
return 0;
}
Output:
11)c) Write a c program to print primes in given array
#include <stdio.h>
int main()
{
int n, i, j, isPrime;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Prime numbers in the array: ");
for (i = 0; i < n; i++)
{
if (arr[i] <= 1)
{
continue;
}
isPrime = 1;
for (j = 2; j <= arr[i]/2; j++)
{
if (arr[i] % j == 0)
{
isPrime = 0;
break;
}
}
if (isPrime)
{
printf("%d ", arr[i]);
}
}
}
Output:
12)a) Write a C Program to find the maximum and minimum elements in
array
#include <stdio.h>
int main()
{
int a[15];
int i, n, max, min;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
max = a[0];
min = a[0];
for(i = 0; i < n; i++)
{
if(max < a[i])
{
max = a[i];
}
if(min > a[i])
{
min = a[i];
}
}
printf("%d is the minimum element in the array\n", min);
printf("%d is the maximum element in the array\n", max);
}
Output:
12)b) Develop a C program to perform linear search on 1D array
#include <stdio.h>
int main()
{
int a[100], key, i, n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter %d numbers:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("Enter the number to search: ");
scanf("%d", &key);
for (i = 0; i < n; i++)
{
if (a[i] == key)
{
printf("%d is present at index %d.\n", key, i);
break;
}
}
if (i == n)
{
printf("%d is not present in the array.\n", key);
}
}
Output:
13)a) Write c program to display the elements of array in reverse order
#include <stdio.h>
int main()
{
int a[15];
int i, n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter the elements in the array:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("Array after reversing:\n");
for (i = n - 1; i >= 0; i--)
{
printf("%d ", a[i]);
}
}

Output:
13)b) Write a C Program to Sort the elements of array using Bubble Sort
#include <stdio.h>
int main()
{
int i, n, j, temp, a[25];
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for(i = 0; i < n - 1; i++)
{
for(j = 0; j < n - i - 1; j++)
{
if(a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf("Sorted array:\n");
for(i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
printf("\n"); // Print a new line at the end
return 0;
}
Output:
13)c) Write a C Program to remove duplicates in an array ?
#include <stdio.h>
int main()
{
int arr[] = {4, 5, 6, 4, 5, 7, 8, 9, 6},i,j,k;
int n = sizeof(arr) / sizeof(arr[0]);
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{

if (arr[i] == arr[j])
{
for (k = j; k < n - 1; k++)
{
arr[k] = arr[k + 1];
}
n--;
j--;
}
}
}
printf("Array after removing duplicates: ");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

Output:
14)a) Develop a c program to perform matrix addition?
#include <stdio.h>
int main()
{
int rows, cols, i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int matrix1[rows][cols], matrix2[rows][cols], result[rows][cols];
printf("Enter elements of the first matrix (%dx%d):\n",rows,cols);
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter elements of the second matrix (%dx%d):\n",rows,cols);
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
scanf("%d", &matrix2[i][j]);
}
}
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
printf("Resultant matrix after addition:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("%4d ", result[i][j]);
}
printf("\n");
}
return 0;
}
Output:
14)b) Develop a c program to perform matrix multiplication
#include <stdio.h>
int main()
{
int rows1, cols1, rows2, cols2, i, j, k;
printf("Enter the number of rows and columns for the first matrix: ");
scanf("%d %d", &rows1, &cols1);
printf("Enter the number of rows and columns for the second matrix: ");
scanf("%d %d", &rows2, &cols2);
if (cols1 != rows2)
{
printf("Matrix multiplication is not possible.\n");
}
else
{
int firstMatrix[rows1][cols1],secondMatrix[rows2][cols2],
resultMatrix[rows1][cols2];
printf("Enter elements of the first matrix:\n");
for (i = 0; i < rows1; i++)
{
for (j = 0; j < cols1; j++)
{
printf("Element [%d][%d]: ", i, j);
scanf("%d", &firstMatrix[i][j]);
}
}
printf("Enter elements of the second matrix:\n");
for (i = 0; i < rows2; i++)
{
for (j = 0; j < cols2; j++)
{
printf("Element [%d][%d]: ", i, j);
scanf("%d", &secondMatrix[i][j]);
}
}
for (i = 0; i < rows1; i++)
{
for (j = 0; j < cols2; j++)
{
resultMatrix[i][j] = 0;
for (k = 0; k < cols1; k++)
{
resultMatrix[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
printf("Result Matrix:\n");
for (i = 0; i < rows1; i++)
{
for (j = 0; j < cols2; j++)
{
printf("%4d ", resultMatrix[i][j]);
}
printf("\n");
}
}
}
Output:
15)a) Develop a c Program to perform transpose of a matrix
#include <stdio.h>
int main()
{
int rows, cols;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int matrix[rows][cols], transpose[cols][rows], i, j;
printf("Enter elements of the matrix:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("Element at position [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
transpose[j][i] = matrix[i][j];
}
}
printf("\nOriginal Matrix:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
printf("\nTranspose Matrix:\n");
for (i = 0; i < cols; i++)
{
for (j = 0; j < rows; j++)
{
printf("%4d ", transpose[i][j]);
}
printf("\n");
}
}
Output:
15)b) Develop a C program to print the primes between 2 given intervals
#include <stdio.h>
int main()
{
int lower,upper,num,div;
printf("Enter the lower bound: ");
scanf("%d", &lower);
printf("Enter the upper bound: ");
scanf("%d", &upper);
printf("Prime numbers between %d and %d are: ",lower,upper);
for(num=lower;num<=upper;num++)
{
if (num < 2)
continue;
int is_prime = 1;
for (div=2;div<=num/2;div++)
{
if (num%div == 0)
{
is_prime = 0;
break;
}
}
if (is_prime)
{
printf("%d ", num);
}
}
printf("\n");
return 0;
}
Output:
16)a) Develop a C program to find the length of the string using strlen
function
#include<stdio.h>
#include<string.h>
int main()
{
char s1[25];
printf("Enter a string:\n");
scanf("%[^\n]",s1);
int len=strlen(s1);
printf("Length of the string is %d",len);
return 0;
}
Output:
16)b) Develop a c program to find the length of the string without strlen
function
#include <stdio.h>
int main()
{
char s1[25];
int length = 0;
printf("Enter a string:\n");
scanf("%[^\n]", s1);
while (s1[length] != '\0')
{
length++;
}
printf("Length of the string is %d", length);
return 0;
}
Output:
17)a) C program to concatenate 2 strings with using strcat function
#include <stdio.h>
#include <string.h>
int main()
{
char s1[50], s2[25];
printf("Enter the first string:\n");
scanf("%s", s1);
printf("Enter the second string:\n");
scanf("%s", s2);
strcat(s1, s2); // Concatenate s2 to s1
printf("Concatenated string: %s\n", s1);
return 0;
}
Output:
17)b) Develop a c program to concatenate 2 strings without using strcat
function

#include <stdio.h>
int main()
{
int i = 0, j = 0, k = 0;
char s1[25], s2[25], s3[50];
printf("Enter the first name:\n");
scanf("%s", s1);
printf("Enter the last name:\n");
scanf("%s", s2);
while (s1[i] != '\0')
{
s3[k++] = s1[i++];
}
s3[k++] = ' ';
while (s2[j] != '\0')
{
s3[k++] = s2[j++];
}
s3[k] = '\0';
printf("The concatenated string is:\n");
printf("%s\n", s3);
}

Output:
18)a) Develop a c program to find the reverse of string without using strrev
function in c
#include <stdio.h>
#include <string.h>
int main()
{
char s1[50];
int i, len;
char temp;
printf("Enter a string:\n");
scanf("%s", s1);
len = strlen(s1);
for (i = 0; i < len / 2; i++)
{
temp = s1[i];
s1[i] = s1[len - i - 1];
s1[len - i - 1] = temp;
}
printf("Reversed string: %s\n", s1);
return 0;
}
Output:
18)b) Develop a C program to copy one string into another using pointers
#include<stdio.h>
#include<stdlib.h>
main()
{
char *s1,*s2;
int i;
s1=(char*)malloc(50);
s2=(char*)malloc(50);
printf("Enter the input string:\n");
scanf("%s",s1);
for(i=0;*(s1+i)!='\0';i++)
*(s2+i)=*(s1+i);
*(s2+i)='\0';
printf("The output string is %s",s2);
return 0;
}
Output:
18)c) Develop a C program to find no of lowercase, uppercase, digits and
other characters using pointers.

#include<stdio.h>
#include<stdlib.h>
main()
{
char *s1;
int i,lc=0,uc=0,dc=0,oc=0;
s1=(char*)malloc(50);
printf("Enter the input string");
scanf("%s",s1);
for(i=0;*(s1+i)!='\0';i++)
if(*(s1+i)>='A' && *(s1+i)<='Z')
uc++;
else if(*(s1+i)>='a' && *(s1+i)<='z')
lc++;
else if(*(s1+i)>='0' && *(s1+i)<='9')
dc++;
else
oc++;
printf("\nTotal No of lower case letters=%d",lc);
printf("\nTotal No of upper case letters=%d",uc);
printf("\nTotal No of digits =%d",dc);
printf("\nTotal No of special characaters=%d",oc);
return 0;
}

Output:
19)a) Develop a C program to find given string is palindrome or not
without using string functions
#include <stdio.h>
#include <string.h>
main()
{
char s1[25];
int i,j,len=0;
printf("Enter a String:\n");
scanf("%s",s1);
for(i=0;s1[i]!='\0';i++)
len++;
for(i=0,j=len-1;i<j;i++,j--)
{
if(s1[i]!=s1[j])
{
printf("Not Palindrome");
break;
}
}
if(i==j)
printf("Palindrome");
}
Output:
19)b) Develop a program to remove special characters and numbers in the
input string
#include <stdio.h>
int main()
{
char s[100];
int i = 0;
printf("Enter any string:\n");
scanf("%[^\n]", s);
while (s[i] != '\0')
{
if ((s[i] >= 'a' && s[i] <= 'z') ||
(s[i] >= 'A' && s[i] <= 'Z') ||
(s[i] == ' '))
{
printf("%c", s[i]);
}
i++;
}
return 0;
}
Output:
19)c) Develop a C Program to find 2’s Compliment of a given number
#include <stdio.h>
#define SIZE 8
int main()
{
char binary[SIZE+1],onesComp[SIZE+1],twosComp[SIZE + 1];
int i, carry=1;
printf("Enter %d bit binary value: ", SIZE);
scanf("%s",binary);
/* Find ones complement of the binary number */
for(i=0; i<SIZE; i++)
{
if(binary[i] == '1')
{
onesComp[i] = '0';
}
else if(binary[i] == '0')
{
onesComp[i] = '1';
}
}
onesComp[SIZE] = '\0';
/* Find twos complement of the binary number */
for(i=SIZE-1; i>=0; i--)
{
if(onesComp[i] == '1' && carry == 1)
{
twosComp[i] = '0';
}
else if(onesComp[i] == '0' && carry == 1)
{
twosComp[i] = '1';
carry = 0;
}
else
{
twosComp[i] = onesComp[i];
}
}
twosComp[SIZE] = '\0';
printf("Original binary = %s\n", binary);
printf("Ones complement = %s\n", onesComp);
printf("Twos complement = %s\n", twosComp);
return 0;
}
Output:
20)a) Write a C Function to calculate NCR Value
#include <stdio.h>
unsigned long long factorial(int num)
{
unsigned long long fact = 1;
int i;
for (i = 1; i <= num; i++)
fact = fact * i;
return fact;
}
unsigned long long nCr(int n, int r)
{
if (r > n)
{
return 0; // nCr is 0 if r > n
}
return factorial(n) / (factorial(r) * factorial(n - r));
}
int main()
{
int n, r;
printf("Enter values for n and r (n >= r):\n");
scanf("%d %d", &n, &r);
printf("The value of %dC%d is: %llu\n", n, r, nCr(n, r));
return 0;
}
Output:
20)b) Write a C Function to find the length of the string

#include <stdio.h>
int stringLength(char *str)
{
int length = 0;
while (str[length] != '\0')
{
length++;
}
return length;
}
int main()
{
char str[100];
printf("Enter a string: ");
scanf("%[^\n]", str);
printf("The length of the string is: %d\n", stringLength(str));
return 0;
}
Output:
20)c) Write C Function to Transpose of a matrix

#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10
void transposeMatrix(int matrix[MAX_ROWS][MAX_COLS],int
result[MAX_COLS][MAX_ROWS], int rows, int cols)
{
int i,j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
result[j][i] = matrix[i][j];
}
}
}
void printMatrix(int matrix[MAX_ROWS][MAX_COLS],int rows,int cols)
{
int i,j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("%4d ", matrix[i][j]);
}
printf("\n");
}
}
int main()
{
int matrix[MAX_ROWS][MAX_COLS];
int transpose[MAX_COLS][MAX_ROWS];
int rows, cols,i,j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
printf("Enter the elements of the matrix:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("Enter element [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
transposeMatrix(matrix, transpose, rows, cols);
printf("\nOriginal Matrix:\n");
printMatrix(matrix, rows, cols);
printf("\nTransposed Matrix:\n");
printMatrix(transpose, cols, rows);
return 0;
}
Output:
21)a) Write a C recursive function to find the factorial of a number
#include <stdio.h>
int factorial(int n)
{
if (n<=1)
return 1;
else
return n * factorial(n - 1);
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0)
printf("Factorial is not defined for negative numbers.\n");
else
{
printf("Factorial of %d is %d\n", num, factorial(num));
}
return 0;
}
Output:
21)b) Write a c recursive function to generate Fibonacci series
#include <stdio.h>
int fibonacci(int n)
{
if (n<=1)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}

int main()
{
int n, i;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 0; i < n; i++)
{
printf("%d ", fibonacci(i));
}
return 0;
}

Output:
21)c) Write a c recursive function to find the lcm of 2 numbers
#include <stdio.h>
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int lcm(int a, int b)
{
return (a * b) / gcd(a, b);
}
int main()
{
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("LCM of %d and %d is %d\n", num1, num2, lcm(num1, num2));
return 0;
}
Output:
22)a) Write a C program to write and read text into a file
#include<stdio.h>
int main()
{
FILE *fp1;
char ch;
fp1=fopen("input.txt","w");
printf("\nEnter Text into file\n");
while((ch=getchar())!=EOF)
{
putc(ch,fp1);
}
fclose(fp1);
printf("Contents in a file is\n");
fp1=fopen("input.txt","r");
while((ch=getc(fp1))!=EOF)
{
putchar(ch);
}
fclose(fp1);
return 0;
}
Output:
Enter Text into file
C Programming is invented by Dennis Ritchie in the year 1972
at AT&T Bell labs
^Z(to stop the input ctrl+z)
Contents in a file is
C Programming is invented by dennis ritchie in the year 1972
at AT&T Bell labs

Note: a text file named input.txt will be created in current working directory.
22)b) Write a c program to copy the contents of one file to another file.
#include<stdio.h>
int main()
{
FILE *fp1,*fp2;
char ch;
fp1=fopen("input.txt","r");
fp2=fopen("output.txt","w");
while((ch=getc(fp1))!=EOF)
putc(ch,fp2);
fclose(fp1);
fclose(fp2);
return 0;
}
Output:
A file called output.txt will be created in the working directory in which all
contents of input.txt will be copied to output.txt
22)c) Write a C program to merge two files into the third file
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *file1, *file2, *outputFile;
char ch;
file1 = fopen("file1.txt", "r");
if (file1 == NULL){
printf("Error opening file1.txt\n");
return 1;
}
file2 = fopen("file2.txt", "r");
if (file2 == NULL) {
printf("Error opening file2.txt\n");
fclose(file1); // Close file1 if file2 can't be opened
return 1;
}
outputFile = fopen("output.txt", "w");
if (outputFile == NULL) {
printf("Error opening output.txt\n");
fclose(file1); // Close file1
fclose(file2); // Close file2
return 1;
}
while ((ch = fgetc(file1)) != EOF) {
fputc(ch, outputFile);
}
// Copy contents of the second file to the output file
while ((ch = fgetc(file2)) != EOF) {
fputc(ch, outputFile);
}
fclose(file1);
fclose(file2);
fclose(outputFile);
printf("Files merged successfully into output.txt\n");
}
Output:
23)a) Write a c program to swap 2 variables using call by reference.
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("\nBefore swapping:\n");
printf("Number 1 = %d\n", num1);
printf("Number 2 = %d\n", num2);
swap(&num1, &num2);
printf("\nAfter swapping:\n");
printf("Number 1 = %d\n", num1);
printf("Number 2 = %d\n", num2);
return 0;
}
Output:
23)b) Develop a program to count the number of characters in the given file
#include <stdio.h>
int main()
{
FILE *fp;
int n;
fp = fopen("sample.txt", "r");
if (fp == NULL)
{
printf("Error opening file.\n");
return 1; // Return error code
}
fseek(fp, 0, SEEK_END);
n = ftell(fp);
fclose(fp);
printf("The number of characters present in file is: %d\n", n - 1);
return 0;
}
Output:
The number of characters present in file is: 18
23)c) Write a c program to find the sum of elements in array using pointers
#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
int *ptr = arr;
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++)
{
scanf("%d", (ptr + i));
sum += *(ptr + i);
}
printf("The sum of the array elements is: %d\n", sum);
return 0;
}
Output:
24)a) Develop a C program to find the total, average of n students using
structures
#include <stdio.h>
struct student {
char s1[50];
int sub[10];
int tot;
float avg;
};
struct student st[10];
int main()
{
int i, j, n;
printf("How many students? ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\nEnter name of student %d: ", i + 1);
scanf("%s", st[i].s1);
st[i].tot = 0;
printf("Enter marks for 6 subjects: ");
for (j = 0; j < 6; j++)
{
scanf("%d", &st[i].sub[j]);
st[i].tot += st[i].sub[j];
}
st[i].avg = st[i].tot / 6.0;
}
printf("\nStudent details:\n");
for (i = 0; i < n; i++)
{
printf("Name: %s, Total Marks: %d, Average: %.2f\n",
st[i].s1, st[i].tot, st[i].avg);
}
}
Output:
24)b) Write a c program to Enter n students data(roll,name,marks) and
display failed students list(if marks > = 40 : pass)
#include <stdio.h>
struct Student {
int roll;
char name[50];
float marks;
};
int main()
{
int n;
printf("Enter the number of students: ");
scanf("%d", &n);
struct Student students[n],i;
for (i = 0; i < n; i++) {
printf("\nEnter details for student %d:\n", i + 1);
printf("Roll number: ");
scanf("%d", &students[i].roll);
printf("Name: ");
scanf(" %[^\n]", students[i].name); // To read a string with spaces
printf("Marks: ");
scanf("%f", &students[i].marks);
}

// Display the list of students who failed


printf("\nList of failed students:\n");
int hasFailed = 0;
for (i = 0; i < n; i++) {
if (students[i].marks < 40) {
printf("Roll: %d, Name: %s, Marks: %.2f\n", students[i].roll,
students[i].name,
students[i].marks);
hasFailed = 1;
}
}

if (!hasFailed)
{
printf("No students have failed.\n");
}

return 0;
}
Output:
25)a) demonstrate between structures and unions by using a simple
program
#include <stdio.h>
struct MyStruct {
int intVal; // 4 bytes
float floatVal; // 4 bytes
char charVal; // 1 byte
};
union MyUnion {
int intVal; // 4 bytes
float floatVal; // 4 bytes
char charVal; // 1 byte
};
int main()
{
struct MyStruct s;
union MyUnion u;
printf("Size of structure: %u bytes\n", sizeof(s));
printf("Size of union: %u bytes\n\n", sizeof(u));
s.intVal = 10;
s.floatVal = 3.14f;
s.charVal = 'A';
printf("Structure values:\n");
printf("intVal: %d\n", s.intVal);
printf("floatVal: %.2f\n", s.floatVal);
printf("charVal: %c\n\n", s.charVal);
u.intVal = 10;
printf("Union values after setting intVal:\n");
printf("intVal: %d\n", u.intVal);
u.floatVal = 3.14f; // Overwrites intVal
printf("Union values after setting floatVal:\n");
printf("floatVal: %.2f\n", u.floatVal);
printf("intVal (corrupted): %d\n", u.intVal);
return 0;
}
Output:
25)b) Write a C program to copy one structure variable to another
structure of the same type
#include <stdio.h>
struct student {
char s1[50];
int tot;
float avg;
};
struct student st1, st2;
int main() {
printf("\nEnter student name: ");
scanf("%s", st1.s1);
printf("\nEnter Total marks: ");
scanf("%d", &st1.tot);
printf("\nEnter Average marks: ");
scanf("%f", &st1.avg);
// Copy st1 to st2
st2 = st1;
printf("\nStudent details:");
printf("\nName = %s, Total = %d, Average = %.2f\n",st2.s1,st2.tot,st2.avg);
}
Output:
25)c) Develop a c program to find the number of lines characters words in
a file
#include <stdio.h>
int main() {
int lc = 0, wc = 0, cc = 0;
char ch;
FILE *fp;
fp = fopen("input.txt", "r");
if (fp == NULL) {
printf("Error: File cannot be opened.\n");
return 1;
}
// Read characters from the file
while (fscanf(fp, "%c", &ch) != EOF) {
if (ch == '\n') {
lc++;
wc++;
} else if (ch == ' ' || ch == '\t') {
wc++;
}
cc++;
}
// Adjust line count for the last line if the file does not end with '\n'
if (cc > 0 && ch != '\n') {
lc++;
}
fclose(fp);
printf("Lines: %d, Words: %d, Characters: %d\n", lc, wc, cc);
return 0;
}

Output:
Lines: 2, Words: 15, Characters: 79

You might also like