Aryan Assignment
Aryan Assignment
C PROGRAMMING
COURSE CODE – CSMC101
Assignment On-
“C PROGRAMS”
SUBMITTED BY:
SUBMITTTED TO:
DR. SHATRUGHAN MODI
2|Page
INDEX
S no. Title Pg. No.
1 Lab Assignment-1 4-20
1 Program 1 4
2 Program 2 5
3 Program 3 6
4 Program 4 7
5 Program 5 8-9
6 Program 6 10-11
7 Program 7 12-13
8 Program 8 14-15
9 Program 9 16
10 Program 10 17
11 Program 11 18
12 Program 12 19
13 Program 13 20
2 Lab Assignment - 2 21-41
1 Program 1 21
2 Program 2 22
3 Program 3 23
4 Program 4 24-25
5 Program 5 26-28
6 Program 6 29-31
7 Program 7 32-33
8 Program 8 34-37
9 Program 9 38-41
3 Lab Assignment - 3 42-63
1 Program 1 42-43
2 Program 2 44-45
3 Program 3 46
4 Program 4 47-48
3|Page
5 Program 5 49-50
6 Program 6 51-52
7 Program 7 53
8 Program 8 54
9 Program 9 55
10 Program 10 56-57
11 Program 11a 58-59
12 Program 11b 60-61
13 Program 12 62-63
4 Lab Assignment - 4 64-92
1 Program 1 64-65
2 Program 2 66-67
3 Program 3 68
4 Program 4 69-73
5 Program 5 74-75
6 Program 6 76-77
7 Program 7 78
8 Program 8 79-80
9 Program 9 81-87
10 Program 10 88-92
5 Lab Assignment - 5 94-111
1 Program 1 94-95
2 Program 2 96-98
3 Program 3 99-102
4 Program 4 103-105
5 Program 5 106-111
5 Lab Assignment - 6 112-127
1 Program 1 112-113
2 Program 2 114-115
3 Program 3 116-117
4 Program 4 118-119
5 Program 5 120-121
6 Program 6 122-123
4|Page
7 Program 7 124-125
8 Program 8 126-127
5 Lab Assignment - 7 128-140
1 Program 1 128-129
2 Program 2 130-131
3 Program 3 132-133
4 Program 4 134-135
5 Program 5 136
6 Program 6 137-138
7 Program 7 139-140
5|Page
LAB ASSIGNMENT - 1
PROGRAM – 1
Create a program to check the number entered from the console is even
or odd.
#include <stdio.h>
int main()
{
int n;
printf("Enter a number\n");
scanf("%d",&n);
if(n%2==0)
printf("The number is even");
else
printf("The number is odd");
}
OUTPUT :
6|Page
PROGRAM – 2
Create a program to print “Welcome to C world”.
#include <stdio.h>
int main()
{
printf("Welcome to C world.");
return 0;
}
OUTPUT:
7|Page
PROGRAM – 3
Create a program to convert KM into miles and vice-versa.
#include <stdio.h>
int main()
{
int n;
char ch;
printf("Enter distance\n");
scanf("%d", &n);
printf("Is the distance in KM ?Y|N\n");
scanf(" %c",&ch);
if(ch=='Y')
printf("Miles = %lf", 0.62137 * n);
else
printf("KM = %lf", 1.609344 * n);
return 0;
}
OUTPUT:
8|Page
PROGRAM – 4
Create a program to swap two numbers without using third variable.
#include <stdio.h>
int main()
{
int x,y;
printf("Enter two numbers\n");
scanf("%d%d", &x,&y);
x=x+y;
y=x-y;
x=x-y;
printf("The numbers are %d %d", x,y);
return 0;
}
OUTPUT:
9|Page
PROGRAM – 5
Create a program to calculate area of
a. Circle
b. Rectangle
c. Square
#include <stdio.h>
int main()
{
float r,s,l,b;
printf("Enter radius of circle\n");
scanf("%f",&r);
printf("Enter the length and breadth of the rectangle\n");
scanf("%f%f",&l,&b);
printf("Enter the side of square\n");
scanf("%f",&s);
printf("The area of circle is %lf\n",3.14*r*r);
printf("The area of rectangle is %lf\n", l*b);
printf("The area of the square is %lf\n", s*s);
}
10 | P a g e
OUTPUT:
11 | P a g e
PROGRAM – 6
Write a program to calculate Simple interest and compound interest for
the principal amount entered through console for the given rate of
interest and time entered through console.
#include <stdio.h>
#include <math.h>
int main()
{
float p,r,t;int n;
printf("Enter the principal amount\n");
scanf("%f",&p);
printf("Enter the rate of interest\n");
scanf("%f",&r);
printf("Enter the time in years\n");
scanf("%f",&t);
printf("Enter numbe of of times rate appiled per year\n");
scanf("%d",&n);
printf("SIMPLE INTEREST = %f\n",((p*r*t)/100));
printf("COMPOUND INTEREST = %f\n",
p*(pow((1+(r/(100*n))),n*t)));
}
12 | P a g e
OUTPUT:
13 | P a g e
PROGRAM – 7
Write a program to find the nature of roots of quadratic equation. Also
find the roots if possible.
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, D, root1, root2;
printf("Enter coefficients a, b, c: ");
scanf("%lf %lf %lf", &a, &b, &c);
if (a == 0) {
printf("Not a quadratic equation.\n");
return 0;
}
D = b * b - 4 * a * c;
if (D > 0) {
printf("Roots are real and distinct.\n");
root1 = (-b + sqrt(D)) / (2 * a);
root2 = (-b - sqrt(D)) / (2 * a);
printf("Roots: %.2lf, %.2lf\n", root1, root2);
} else if (D == 0) {
printf("Roots are real and equal.\n");
root1 = -b / (2 * a);
printf("Root: %.2lf\n", root1);
14 | P a g e
} else {
printf("Roots are complex.\n");
double realPart = -b / (2 * a);
double imagPart = sqrt(-D) / (2 * a);
printf("Roots: %.2lf + %.2lfi, %.2lf - %.2lfi\n", realPart, imagPart,
realPart, imagPart);
}
return 0;
}
OUTPUT :
15 | P a g e
PROGRAM – 8
W.A.P that will display Grade of student according to his/her marks
using if
else ladder. (Write same program using switch case also)
Marks > 80 then Grade = A
Marks between 61 & 80 then Grade = B
Marks between 51 & 60 then Grade = C Marks between 41 & 50 then
Grade = D
Marks between 35 & 40 then Grade = E Marks < 35 then Grade = F
#include <stdio.h>
int main()
{
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
char grade;
grade = 'C';
} else if (marks >= 41 && marks <= 50) {
grade = 'D';
} else if (marks >= 35 && marks <= 40) {
grade = 'E';
} else {
grade = 'F';
}
OUTPUT:
17 | P a g e
PROGRAM – 9
Create a C program in which converts the temperature taken by user in
Fahrenheit to Celsius. And for conversion use the formula which is
given.
#include <stdio.h>
int main() {
float f, c;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &f);
c = (f - 32) * 5 / 9;
printf("Temperature in Celsius: %.2f\n", c);
return 0;
}
OUTPUT:
18 | P a g e
PROGRAM – 10
Write a program in which the total numbers of days are given and you
have to convert that in perfect no of days, year and month.
#include <stdio.h>
int main()
{
int t, y, m, d;
printf("Enter the total number of days: ");
scanf("%d", &t);
y=t/365;
m=(t-(y*365))/12;
d=(t- (m*30))/30;
printf("Years:%d Months:%d Days:%d",y,m,d);
}
OUTPUT
19 | P a g e
PROGRAM – 11
Same as the above program but here instead of days the seconds are
given and you need to convert it into hour, min and seconds format.
#include <stdio.h>
int main() {
int sec, h, m, s;
printf("Enter the total number of seconds: ");
scanf("%d", &sec);
h = sec / 3600;
sec %= 3600;
m = sec / 60;
s = sec % 60;
printf("Hours: %d, Minutes: %d, Seconds: %d\n", h, m, s);
return 0;
}
OUTPUT:
20 | P a g e
PROGRAM – 12
Write a program to check the type of triangle (isosceles, equilateral,
scalene) when the side lengths are entered through console.
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter the lengths of the three sides of the triangle: ");
scanf("%d %d %d", &a, &b, &c);
if (a == b && b == c)
printf("The triangle is Equilateral.\n");
else if (a == b || b == c || a == c)
printf("The triangle is Isosceles.\n");
else
printf("The triangle is Scalene.\n");
return 0;
}
OUTPUT:
21 | P a g e
PROGRAM – 13
Write a program to find largest as well as smallest of four numbers
entered through console by user.
#include <stdio.h>
int main()
{
int a,b,c,d;
printf("Enter 4 numbers :\n");
scanf("%d%d%d%d",&a,&b,&c,&d);
int l = ((a>b?a:b)>(c>d?c:d))?(a>b?a:b):(c>d?c:d);
printf("Largest :%d\n",l);
int s= ((a<b?a:b)<(c<d?c:d))?(a<b?a:b):(c<d?c:d);
printf("Smallest :%d\n",s);
return 0;
}
OUTPUT:
22 | P a g e
LAB ASSIGNMENT - 2
PROGRAM – 1
Write a program to find largest as well as smallest of four numbers
entered through console by user.
#include <stdio.h>
#include <math.h>
int main() {
double r, v;
printf("Enter radius: ");
scanf("%lf", &r);
v = (4.0 / 3.0) * 3.14 * pow(r, 3);
printf("Volume: %.f\n", v);
return 0;
}
OUTPUT:
23 | P a g e
PROGRAM – 2
Create a program to calculate factorial of a number entered from the
console.
#include <stdio.h>
int f(int n)
{
if (n == 0 || n == 1)
return 1;
return n * f(n - 1);
}
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial: %d\n", f(n));
return 0;
}
OUTPUT:
24 | P a g e
PROGRAM – 3
Create a program to display the reverse of number entered through
keyboard ,also display the sum of digits of the same number.
#include <stdio.h>
int main() {
int n, r = 0, s = 0;
printf("Enter a number: ");
scanf("%d", &n);
while (n != 0) {
r = r * 10 + (n % 10);
s += n % 10;
n /= 10;
}
printf("Reverse: %d\n", r);
printf("Sum: %d\n", s);
return 0;
}
OUTPUT:
25 | P a g e
PROGRAM – 4
Create a program to display the mathematical tables for the first n
natural numbers (n entered through console).
#include <stdio.h>
int main() {
int n, i, j;
printf("Enter the number of tables to display: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printf("\nMultiplication Table for %d:\n", i);
for (j = 1; j <= 10; j++)
{
printf("%d x %d = %d\n", i, j, i * j);
}
}
return 0;
}
26 | P a g e
OUTPUT:
27 | P a g e
PROGRAM – 5
Calculate the value of e^x , sin(x) using series.
#include <stdio.h>
double exp_taylor(double x, int n) {
double s = 1.0, t = 1.0;
for (int i = 1; i < n; i++) {
t *= x / i;
s += t;
}
return s;
}
double sin_taylor(double x, int n) {
double s = x, t = x;
int sign = -1;
for (int i = 1; i < n; i++) {
t *= x * x / ((2 * i) * (2 * i + 1));
s += sign * t;
sign = -sign;
}
return s;
}
int main() {
double x;
28 | P a g e
int n, choice;
printf("Enter x: ");
scanf("%lf", &x);
printf("Enter terms: ");
scanf("%d", &n);
printf("Choose 1 for e^x or 2 for sin(x): ");
scanf("%d", &choice);
if (choice == 1) {
printf("e^%.2f ≈ %.6f\n", x, exp_taylor(x, n));
} else if (choice == 2) {
printf("sin(%.2f) ≈ %.6f\n", x, sin_taylor(x, n));
} else {
printf("Invalid choice.\n");
}
return 0;
}
29 | P a g e
OUTPUT:
30 | P a g e
PROGRAM – 6
Create a program to check whether the number entered through console
is
a. Palindrome or not
b. Prime or not
c. Armstrong or not
#include <stdio.h>
#include <math.h>
int is_palindrome(int n) {
int r = 0, t = n;
while (t > 0) {
r = r * 10 + t % 10;
t /= 10;
}
return n == r;
}
int is_prime(int n) {
if (n <= 1) return 0;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return 0;
}
return 1;
31 | P a g e
int is_armstrong(int n) {
int s = 0, t = n, d = 0;
while (t > 0) {
d++;
t /= 10;
}
t = n;
while (t > 0) {
s += pow(t % 10, d);
t /= 10;
}
return n == s;
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (is_palindrome(n)) {
printf("%d is a Palindrome\n", n);
} else {
32 | P a g e
OUTPUT:
33 | P a g e
PROGRAM – 7
Write a program to generate following series up to n no. of terms (n is
entered through console).
a. Fibonacci
b. Geometric terms where first term is 1 and common ratio is 1⁄2.
#include <stdio.h>
void fib(int n) {
int a = 0, b = 1, nx;
printf("Fibonacci Series: ");
for (int i = 1; i <= n; i++) {
printf("%d ", a);
nx = a + b;
a = b;
b = nx;
}
printf("\n");
}
void geo(int n) {
double t = 1.0;
printf("Geometric Series: ");
for (int i = 1; i <= n; i++) {
printf("%.2f ", t);
34 | P a g e
t *= 0.5;
}
printf("\n");
}
int main() {
int n;
printf("Enter number of terms: ");
scanf("%d", &n);
fib(n);
geo(n);
return 0;
}
OUTPUT:
35 | P a g e
PROGRAM – 8
Write a program to generate following output for n number of rows (n is
entered through
console).
a. Upper Right Triangle for character ‘&’
b. Lower Right Triangle for character ‘*’
c. Pyramid for any character entered through keyboard.
d. Pascal Triangle
#include <stdio.h>
void upper_right(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("& ");
}
printf("\n");
}
}
void lower_right(int n) {
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
36 | P a g e
printf("\n");
}
}
void pascal(int n) {
for (int i = 0; i < n; i++) {
for (int k = 0; k < n - i - 1; k++) {
printf(" ");
}
int val = 1;
for (int j = 0; j <= i; j++) {
printf("%d ", val);
val = val * (i - j) / (j + 1);
}
printf("\n");
37 | P a g e
}
}
int main() {
int n;
char ch;
printf("Enter number of rows: ");
scanf("%d", &n);
printf("Enter character for pyramid: ");
scanf(" %c", &ch);
printf("\nPyramid:\n");
pyramid(n, ch);
printf("\nPascal Triangle:\n");
pascal(n);
return 0;
38 | P a g e
}
OUTPUT:
39 | P a g e
PROGRAM – 9
Write a program to find
#include <stdio.h>
int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
void sum_series_a(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += 2 * i - 1;
}
printf("Sum of Series 1: %d\n", sum);
}
void sum_series_b(int n) {
double sum = 0.0;
for (int i = 1; i <= n; i++) {
40 | P a g e
if (i % 2 == 0) {
sum -= 1.0 / i;
} else {
sum += 1.0 / i;
}
}
printf("Sum of Series 2: %.6f\n", sum);
}
void sum_series_c(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum += factorial(i);
} printf("Sum of Series 3: %d\n", sum);
void sum_series_d(int n) {
int sum = 0;
for (int i = 1; i <= n; i++)
{
41 | P a g e
sum += i * i;
}
printf("Sum of Series 4: %d\n", sum);
}
void sum_series_e(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += (i + 1) * i;
}
printf("Sum of Series 5: %d\n", sum);
}
int main() {
int n;
printf("Enter number of terms (n): ");
scanf("%d", &n);
sum_series_a(n);
sum_series_b(n);
sum_series_c(n);
sum_series_d(n);
sum_series_e(n);
42 | P a g e
return 0;
}
OUTPUT:
43 | P a g e
LAB ASSIGNMENT - 3
PROGRAM – 1
Create a program to determine whether the number entered is a valid
year and if so, whether it is a leap year.
#include <stdio.h>
int main()
{
int y;
printf("Enter year: ");
scanf("%d", &y);
if (y > 0)
{
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)) {
printf("%d is a leap year.\n", y);
} else {
printf("%d is not a leap year.\n", y);
}
} else
{
printf("Invalid year.\n");
}
44 | P a g e
return 0;
}
OUTPUT:
45 | P a g e
PROGRAM – 2
The librarian in a library wants an application that will calculate the due
date for a book given the issue date. The no. of days in which the book is
due can be decided by the librarian at the time of issuing a book. For e.g.
If librarian enters the current date as 14-01-99 and the no of days in
which the book is due as 15, then your program should calculate the due
date and give the output as 29-01-99.
#include <stdio.h>
int main()
{
int d, m, y, due;
int dim[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
printf("Enter issue date (DD-MM-YY): ");
scanf("%d%d%d", &d, &m, &y);
printf("Enter number of days till due: ");
scanf("%d", &due);
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
dim[1] = 29;
d += due;
while (d > dim[m - 1])
{
d -= dim[m - 1];
m++;
if (m > 12) {
46 | P a g e
m = 1;
y++;
}
OUTPUT:
47 | P a g e
PROGRAM – 3
Write a program that accepts input of a number of seconds, validates it
and outputs the equivalent number of hours, minutes and seconds.
#include <stdio.h>
int main() {
int sec, hrs, min;
printf("Enter the number of seconds: ");
scanf("%d", &sec);
hrs = sec / 3600;
sec = sec % 3600;
min = sec / 60;
sec = sec % 60;
printf("Time: %d hours, %d minutes, %d seconds\n", hrs, min, sec);
return 0;
}
OUTPUT:
48 | P a g e
PROGRAM – 4
Write a program which to print the multiplication table from 1 to m for n
where m, n is entered by the user.
#include <stdio.h>
int main()
{
int n, m;
printf("Enter the number for which you want the multiplication table:
");
scanf("%d", &n);
printf("Enter the number of terms : ");
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
printf("%d x %d = %d\n", n, i, n * i);
}
return 0;
}
49 | P a g e
OUTPUT:
50 | P a g e
PROGRAM – 5
Implement the question 6 of lab assignment #2 using concept of
functions.
#include <stdio.h>
void tab(int n, int m)
{
for (int i = 1; i <= m; i++)
printf("%d x %d = %d\n", n, i, n * i);
}
int main() {
int n, m;
printf("Enter the number for which you want the multiplication table:
");
scanf("%d", &n);
printf("Enter the number of terms (m): ");
scanf("%d", &m);
tab(n, m);
return 0;
}
51 | P a g e
OUTPUT:
52 | P a g e
PROGRAM – 6
Write a recursive function to calculate g.c.d of two numbers.
#include <stdio.h>
int gcd(int n,int m)
{
if(n%m==0)
return m;
else
return gcd(n,m-1);
}
int main()
{
int n,m;
printf("Enter two numbers\n");
scanf("%d%d",&n,&m);
if(n>m)
printf("The G.C.D of the number is %d",gcd(n,m));
else
printf("The G.C.D of the number is %d",gcd(m,n));
}
53 | P a g e
OUTPUT:
54 | P a g e
PROGRAM – 7
Write a recursive function to calculate sum of n natural numbers. (n is
entered through keyboard).
#include <stdio.h>
int sum(int n)
{
if(n==1)
return 1;
else
return n+ sum(n-1);
}
int main()
{
int n;
printf("Enter A Number");
scanf("%d",&n);
printf("The sum is %d",sum(n));
}
OUTPUT:
55 | P a g e
PROGRAM – 8
Write a program to find
#include <stdio.h>
int fact(int n)
{
if(n==1)
return 1;
else
return n*fact(n-1);
}
int main()
{
int n;
printf("Enter A Number\n");
scanf("%d",&n);
printf("The factorial is %d",fact(n));
}
OUTPUT:
56 | P a g e
PROGRAM – 9
Write a recursive function to print binary representation of integer n
(entered through console).
#include <stdio.h>
int bin(int n)
{
if(n==0)
return 0;
else
return (n%2)+ 10*bin(n/2);
}
int main()
{
int n;
printf("Enter A Number\n");
scanf("%d",&n);
printf("The binary representation of integer is %d",bin(n));
}
OUTPUT:
57 | P a g e
PROGRAM – 10
Write a recursive function to calculate x^n (both x and n entered through
console).
#include <stdio.h>
int pwr(int x, int n)
{
if (n == 0)
return 1;
else
return x * pwr(x, n - 1);
}
int main()
{
int x, n;
printf("Enter base: ");
scanf("%d", &x);
printf("Enter exponent: ");
scanf("%d", &n);
printf("%d^%d = %d\n", x, n, pwr(x, n));
return 0;
}
58 | P a g e
OUTPUT:
PROGRAM – 11a
59 | P a g e
while (num != 0) {
int rem = num % 16;
hex[i++] = (rem < 10) ? (rem + '0') : (rem - 10 + 'A');
num /= 16;
}
int main() {
int num;
printf("Enter a decimal number: ");
60 | P a g e
scanf("%d", &num);
OUTPUT:
PROGRAM – 11b
Write a program to find
61 | P a g e
#include <stdio.h>
int main() {
int num;
printf("Enter a decimal number: ");
scanf("%d", &num);
if (num == 0)
printf("0");
else
{
printf("Hexadecimal equivalent: ");
decToHex(num);
}
printf("\n");
return 0;
}
62 | P a g e
OUTPUT:
PROGRAM – 12
Write a recursive function to search an element in the given list using
binary search technique.
63 | P a g e
#include <stdio.h>
int binSearch(int a[], int l, int r, int x) {
if (l > r) return -1;
int m = l + (r - l) / 2;
if (a[m] == x) return m;
if (a[m] > x) return binSearch(a, l, m - 1, x);
return binSearch(a, m + 1, r, x);
}
int main() {
int n, x;
printf("Enter number of elements: ");
scanf("%d", &n);
int a[n];
printf("Enter sorted elements:\n");
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
printf("Enter element to search: ");
scanf("%d", &x);
int res = binSearch(a, 0, n - 1, x);
if (res != -1) printf("Element found at index %d\n", res);
else printf("Element not found\n");
return 0;
}
64 | P a g e
OUTPUT:
65 | P a g e
LAB ASSIGNMENT - 4
PROGRAM – 1
Create a program to compute g.c.d. of two numbers using Euclidean
algorithm.
#include <stdio.h>
int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a,&b);
printf("GCD of %d and %d is %d\n", a,b,gcd(a,b));
return 0;
}
66 | P a g e
OUTPUT:
67 | P a g e
PROGRAM – 2
Create a program to find the sum of numbers having odd index and
multiplication of numbers having even index in one dimensional array.
#include <stdio.h>
int main() {
int n, sum = 0, mul = 1;
printf("Enter number of elements: ");
scanf("%d", &n);
int a[n];
printf("Enter elements: ");
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
OUTPUT:
69 | P a g e
PROGRAM – 3
Write a program to print numbers divisible by 3 and 7 between 1 to 500.
#include <stdio.h>
int main()
{
for (int i = 1; i <= 500; i++)
if (i % 3 == 0 && i % 7 == 0)
printf("%d\n", i);
return 0;
}
OUTPUT:
70 | P a g e
PROGRAM – 4
Create a program to read one dimensional array from the keyboard and
apply the
following operations on it:
a. To search an element entered through console.
b. To display the reverse list with and without using second array.
c. To sort the list with and without using second array.
d. To find the mean of the list.
#include <stdio.h>
void searchElement(int arr[], int n, int element) {
int found = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == element) {
printf("Element %d found at index %d\n", element, i);
found = 1;
break;
}
}
if (!found) {
printf("Element %d not found in the array.\n", element);
}
}
void reverseWithSecondArray(int arr[], int n) {
71 | P a g e
int revArr[n];
for (int i = 0; i < n; i++) {
revArr[i] = arr[n - i - 1];
}
printf("Reversed array (using second array): ");
for (int i = 0; i < n; i++) {
printf("%d ", revArr[i]);
}
printf("\n");
}
void reverseWithoutSecondArray(int arr[], int n) {
printf("Reversed array (without using second array): ");
for (int i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
}
void sortWithSecondArray(int arr[], int n) {
int sortedArr[n];
for (int i = 0; i < n; i++) {
sortedArr[i] = arr[i];
}
for (int i = 0; i < n - 1; i++) {
72 | P a g e
}
printf("Sorted array (without using second array): ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
void calculateMean(int arr[], int n) {
float sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
float mean = sum / n;
printf("Mean of the array: %.2f\n", mean);
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
74 | P a g e
}
int element;
printf("Enter element to search: ");
scanf("%d", &element);
searchElement(arr, n, element);
reverseWithSecondArray(arr, n);
reverseWithoutSecondArray(arr, n);
sortWithSecondArray(arr, n);
sortWithoutSecondArray(arr, n);
calculateMean(arr, n);
return 0;
}
OUTPUT:
75 | P a g e
PROGRAM – 5
Create a program to read one dimensional character type array and
display all the vowels present in it with their indices.
#include <stdio.h>
int main()
{
int s,i,j;
printf("Enter size of array \n");
scanf("%d",&s);
char ch[s];
printf("Enter characters in array");
for(i=0;i<s;i++)
scanf(" %c",&ch[i]);
char v[5]={'a','e','i','o','u'};
for(i=0;i<s;i++)
for(j=0;j<5;j++)
if(ch[i]==v[j])
{
printf("Vowel '%c' found at index : %d\n",ch[i],i);
break;
}
return 0;
}
76 | P a g e
OUTPUT:
77 | P a g e
PROGRAM – 6
Create a program to read one dimensional character type array and
reprint after changing the case of each character.
#include <stdio.h>
int main()
{
int s,i,j;
printf("Enter size of array \n");
scanf("%d",&s);
char ch[s];
printf("Enter characters in array\n");
for(i=0;i<s;i++)
scanf(" %c",&ch[i]);
for(i=0;i<s;i++)
if(ch[i]>='a'&& ch[i]<='z')
printf("%c ",(ch[i]-32));
else
printf("%c ",(ch[i]+32));
return 0;
}
78 | P a g e
OUTPUT:
79 | P a g e
PROGRAM – 7
Create a program to print Floyd’s triangle for n number of rows.
#include <stdio.h>
int main()
{
int n,i,j;
printf("Enter number of rows\n");
scanf("%d",&n);
int d=1;
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
printf("%d ",d++);
printf("\n");
}
}
OUTPUT:
80 | P a g e
PROGRAM – 8
Create a program to check whether two strings are anagrams or not.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) {
return (*(char *)a - *(char *)b);
}
int main() {
char s1[100], s2[100];
printf("Enter first string: ");
fgets(s1, sizeof(s1), stdin);
printf("Enter second string: ");
fgets(s2, sizeof(s2), stdin);
s1[strcspn(s1, "\n")] = '\0';
s2[strcspn(s2, "\n")] = '\0';
if (strlen(s1) != strlen(s2)) {
printf("Not anagrams\n");
return 0;
}
qsort(s1, strlen(s1), sizeof(char), cmp);
qsort(s2, strlen(s2), sizeof(char), cmp);
if (strcmp(s1, s2) == 0)
81 | P a g e
printf("Anagrams\n");
else
printf("Not anagrams\n");
return 0;
}
OUTPUT:
82 | P a g e
PROGRAM – 9
Write a program to perform the following operations on one-
dimensional array (entered through console).
a. Delete an element from the list (if found).
b. Delete an element from the specified index.
c. Insert an element at specified index.
d. Insert an element in ascending/descending order.
#include <stdio.h>
void deleteByValue(int arr[], int *size, int value) {
int found = 0;
for (int i = 0; i < *size; i++) {
if (arr[i] == value) {
found = 1;
for (int j = i; j < *size - 1; j++) {
arr[j] = arr[j + 1];
}
(*size)--;
break;
}
}
if (!found) {
printf("Element not found\n");
83 | P a g e
} else {
printf("Element deleted\n");
}
}
int main() {
int arr[100], size, choice, value, index, order;
do {
printf("\nMenu:\n");
printf("1. Delete an element by value\n");
printf("2. Delete an element by index\n");
printf("3. Insert an element at a specified index\n");
printf("4. Insert an element in ascending/descending order\n");
printf("5. Exit\n");
printf("Enter your choice: ");
86 | P a g e
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to delete: ");
scanf("%d", &value);
deleteByValue(arr, &size, value);
break;
case 2:
printf("Enter the index to delete from: ");
scanf("%d", &index);
deleteByIndex(arr, &size, index);
break;
case 3:
printf("Enter the element to insert: ");
scanf("%d", &value);
printf("Enter the index to insert at: ");
scanf("%d", &index);
insertAtIndex(arr, &size, value, index);
break;
case 4:
printf("Enter the element to insert: ");
scanf("%d", &value);
87 | P a g e
return 0;
}
88 | P a g e
OUTPUT:
89 | P a g e
PROGRAM – 10
Write a program to perform the following operations:
a. Transpose of matrix.
b. Addition of two matrices (if possible)
c. Subtraction of two matrices (if possible).
d. Multiplication of two matrices (if possible)
#include <stdio.h>
int r1,r2,c1,c2;
void transpose(int a[r1][c1])
{
int i,j;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d", a[j][i]);
printf("\n");
}
}
void addition(int a[r1][c1],int b[r2][c2])
{
int i,j;
for(i=0;i<r1;i++)
90 | P a g e
{
for(j=0;j<c1;j++)
printf("%d ",a[i][j]+b[i][j]);
printf("\n");
}
}
void subtraction(int a[r1][c1],int b[r2][c2])
{
int i,j;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d ",a[i][j]-b[i][j]);
printf("\n");
}
}
void multiplication(int a[r1][c1],int b[r2][c2])
{
int i,j,s,k;
for(i=0;i<r1;i++)
{
for(k=0;k<c2;k++)
{
91 | P a g e
s=0;
for(j=0;j<r2;j++)
s+= a[i][j]*b[j][k];
printf("%d ",s);
}
printf("\n");
}
}
int main()
{
int i,j;
printf("Enter rows of array \n");
scanf("%d",&r1);
printf("Enter columns of array \n");
scanf("%d",&c1);
int a[r1][c1];
printf("Enter elements in array\n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
else
printf("Multiplication not possible");
}
OUTPUT:
94 | P a g e
LAB ASSIGNMENT - 5
PROGRAM – 1
Create a program to sort a list of strings into alphabetical order (as in
dictionary) using an array of pointers.
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <string.h>
int main() {
char *arr[] = {"banana", "apple", "orange", "grape", "kiwi"};
int n = sizeof(arr) / sizeof(arr[0]);
char **temp;
}
}
}
return 0;
}
OUTPUT
96 | P a g e
PROGRAM – 2
2. Write a program that analysis the line of text to check the following:
a. Number of characters.
b. Digits.
c. White spaces.
d. Vowels.
e. Consonants.
f. Special Characters.
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int chars = 0, digits = 0, spaces = 0, vowels = 0, consonants = 0,
specials = 0;
chars++;
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
vowels++;
} else {
consonants++;
}
} else if (isdigit(c)) {
digits++;
chars++;
} else if (isspace(c)) {
spaces++;
chars++;
} else if (!isalnum(c)) {
specials++;
chars++;
}
}
return 0;
}
OUTPUT
99 | P a g e
PROGRAM – 3
Write a menu driven program to convert a number entered through
console into words.
#include <stdio.h>
#include <string.h>
if (num == 0) {
printf("Zero");
return;
}
num %= 1000;
}
if (num > 0) {
printf("%s ", units[num]);
}
}
int main() {
int choice, number;
switch (choice) {
case 1:
convertDigitByDigit(number);
break;
case 2:
convertToWords(number);
break;
default:
printf("Invalid choice.\n");
break;
}
return 0;
}
OUTPUT
103 | P a g e
PROGRAM – 4
Write a program to check the validity of the string entered through
console as:
a. Email ID
b. Date (entered in any valid format)
#include <stdio.h>
#include <string.h>
#include <ctype.h>
return 0;
}
}
return 1;
}
int main() {
char input[100];
105 | P a g e
return 0;
}
106 | P a g e
int main() {
char str1[100], str2[100];
int choice;
printf("\nMenu:\n");
printf("1. Reverse String\n");
printf("2. Calculate Length of String\n");
printf("3. Change Case\n");
printf("4. Change to Upper Case\n");
printf("5. Change to Lower Case\n");
printf("6. Append to Strings\n");
printf("7. Sort the Characters of String\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar();
switch (choice) {
case 1:
reverseString(str1);
printf("Reversed String: %s\n", str1);
break;
case 2:
printf("Length of String: %d\n", stringLength(str1));
110 | P a g e
break;
case 3:
changeCase(str1);
printf("Changed Case String: %s\n", str1);
break;
case 4:
toUpperCase(str1);
printf("Upper Case String: %s\n", str1);
break;
case 5:
toLowerCase(str1);
printf("Lower Case String: %s\n", str1);
break;
case 6:
printf("Enter the second string to append: ");
fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = '\0'; // Remove newline character
appendStrings(str1, str2);
printf("Appended String: %s\n", str1);
break;
case 7:
sortString(str1);
printf("Sorted String: %s\n", str1);
111 | P a g e
break;
case 8:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
return 0;
}
OUTPUT
112 | P a g e
LAB ASSIGNMENT - 6
PROGRAM – 1
1. Use arrays to input and output 3 names, roll numbers and marks.
#include <stdio.h>
#include <string.h>
int main() {
int m[3], r[3];
char n[3][100];
scanf("%d", &m[i]);
}
printf("\nStudent details:\n");
for (int i = 0; i < 3; i++) {
printf("Name: %s\n", n[i]);
printf("Roll Number: %d\n", r[i]);
printf("Marks: %d\n\n", m[i]);
}
return 0;
}
OUTPUT:
114 | P a g e
PROGRAM – 2
Use arrays to input and output 3 names, roll numbers and marks.(Using
structures)
#include <stdio.h>
struct student {
char name[100];
int roll;
int marks;
};
int main() {
struct student s[3];
for (int i = 0; i < 3; i++) {
printf("Enter name for student %d: ", i + 1);
scanf(" %[^\n]", s[i].name);
printf("Enter roll number for student %d: ", i + 1);
scanf("%d", &s[i].roll);
printf("Enter marks for student %d: ", i + 1);
scanf("%d", &s[i].marks);
}
printf("\nStudent details:\n");
for (int i = 0; i < 3; i++) {
printf("Name: %s, Roll Number: %d, Marks: %d\n", s[i].name,
s[i].roll, s[i].marks);
115 | P a g e
}
return 0;
}
OUTPUT:
116 | P a g e
PROGRAM – 3
WAP to initialize structures, copy one structure whole into another and
also just a few variables of one structure into another.
#include <stdio.h>
struct student {
char name[100];
int roll;
int marks;
};
int main() {
struct student s1 = {"John", 101, 90};
struct student s2;
s2 = s1;
struct student s3;
s3.roll = s1.roll;
s3.marks = s1.marks;
printf("Student 2 (Copy of s1): %s, Roll Number: %d, Marks: %d\n",
s2.name, s2.roll, s2.marks);
printf("Student 3 (Partial copy of s1): Roll Number: %d, Marks: %d\
n", s3.roll, s3.marks);
return 0;
}
117 | P a g e
OUTPUT:
118 | P a g e
PROGRAM – 4
Implement nested structures. Think about any real-world application of
nested structures.
#include <stdio.h>
struct address {
char city[50];
char state[50];
};
struct student {
char name[100];
int roll;
struct address addr;
};
int main() {
struct student s1;
printf("Enter name for student: ");
scanf(" %[^\n]", s1.name);
printf("Enter roll number for student: ");
scanf("%d", &s1.roll);
printf("Enter city for student: ");
scanf(" %[^\n]", s1.addr.city);
printf("Enter state for student: ");
scanf(" %[^\n]", s1.addr.state);
119 | P a g e
printf("\nStudent details:\n");
printf("Name: %s, Roll Number: %d\n", s1.name, s1.roll);
printf("City: %s, State: %s\n", s1.addr.city, s1.addr.state);
return 0;
}
OUTPUT:
120 | P a g e
PROGRAM – 5
Implement dot operator and arrow operator for structures.
#include <stdio.h>
struct student {
char name[100];
int roll;
int marks;
};
int main() {
struct student s1;
struct student *ptr = &s1;
printf("Enter name for student: ");
scanf(" %[^\n]", s1.name);
printf("Enter roll number for student: ");
scanf("%d", &s1.roll);
printf("Enter marks for student: ");
scanf("%d", &s1.marks);
printf("\nUsing dot operator: Name: %s, Roll Number: %d, Marks:
%d\n", s1.name, s1.roll, s1.marks);
printf("Using arrow operator: Name: %s, Roll Number: %d, Marks:
%d\n", ptr->name, ptr->roll, ptr->marks);
return 0;
}
121 | P a g e
OUTPUT:
122 | P a g e
PROGRAM – 6
Pass and return structure into other functions besides main().
#include <stdio.h>
struct student {
char name[100];
int roll;
int marks;
};
struct student input() {
struct student s;
printf("Enter name for student: ");
scanf(" %[^\n]", s.name);
printf("Enter roll number for student: ");
scanf("%d", &s.roll);
printf("Enter marks for student: ");
scanf("%d", &s.marks);
return s;
}
void output(struct student s) {
printf("\nStudent details:\n");
printf("Name: %s, Roll Number: %d, Marks: %d\n", s.name, s.roll,
s.marks);
}
123 | P a g e
int main() {
struct student s = input();
output(s);
return 0;
}
OUTPUT:
124 | P a g e
PROGRAM – 7
How would you swap two structures?
#include <stdio
.h>
struct student {
char name[100];
int roll;
int marks;
};
void swap(struct student *s1, struct student *s2) {
struct student temp = *s1;
*s1 = *s2;
*s2 = temp;
}
int main() {
struct student s1, s2;
printf("Enter name for student 1: ");
scanf(" %[^\n]", s1.name);
printf("Enter roll number for student 1: ");
scanf("%d", &s1.roll);
printf("Enter marks for student 1: ");
scanf("%d", &s1.marks);
printf("Enter name for student 2: ");
125 | P a g e
OUTPUT:
126 | P a g e
PROGRAM – 8
Implement a simple union using a C program which contains an int, float
and char array of 4 elements.
#include <stdio.h>
union data {
int i;
float f;
char str[4];
};
int main() {
union data d;
printf("Enter an integer: ");
scanf("%d", &d.i);
printf("Integer: %d\n", d.i);
printf("Enter a float: ");
scanf("%f", &d.f);
printf("Float: %f\n", d.f);
printf("Enter a string (3 characters max): ");
scanf("%3s", d.str);
printf("String: %s\n", d.str);
return 0;
}
127 | P a g e
OUTPUT:
128 | P a g e
LAB ASSIGNMENT - 7
: PROGRAM – 1
“Story.txt” file contains the following story. Read the content and
display it on the screen using a C program
#include <stdio.h>
int main() {
FILE *file = fopen("Story.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
char ch;
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
fclose(file);
return 0;
}
129 | P a g e
OUTPUT
130 | P a g e
PROGRAM – 2
Read the file Story.txt (in Q1) and display the contents on screen line by
line using C program.
#include <stdio.h>
int main() {
FILE *file = fopen("Story.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
char line[256];
while (fgets(line, sizeof(line), file)) {
printf("%s", line);
}
fclose(file);
return 0;
}
131 | P a g e
OUTPUT
132 | P a g e
PROGRAM – 3
Write a C program to count:
a) Number of characters in a file
b) Number of words in a file
c) Number of lines in a file
#include <stdio.h>
#include <ctype.h>
int main() {
FILE *file = fopen("Story.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
int ch, characters = 0, words = 0, lines = 0;
int in_word = 0; // flag to check if inside a word
while ((ch = fgetc(file)) != EOF) {
characters++;
if (ch == '\n') {
lines++;
}
if (isspace(ch)) {
in_word = 0; // outside a word
} else if (in_word == 0) {
133 | P a g e
words++;
in_word = 1; // inside a word
}
}
if (ch == EOF && ch != '\n') {
lines++;
}
fclose(file);
printf("Characters: %d\n", characters);
printf("Words: %d\n", words);
printf("Lines: %d\n", lines);
return 0;
}
OUTPUT
134 | P a g e
PROGRAM – 4
How would you copy the contents of a file into another file using a C
program?
#include <stdio.h>
int main() {
FILE *source = fopen("Story.txt", "r");
if (source == NULL) {
printf("Error opening source file.\n");
return 1;
}
char ch;
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination);
}
135 | P a g e
fclose(source);
fclose(destination);
OUTPUT
136 | P a g e
PROGRAM – 5
Read marks500.txt to find min and max of the marks.
#include <stdio.h>
int main() {
FILE *file = fopen("marks500.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
int mark, min = 101, max = -1;
while (fscanf(file, "%d", &mark) != EOF) {
if (mark < min) min = mark;
if (mark > max) max = mark;
}
fclose(file);
printf("Minimum Mark: %d\n", min);
printf("Maximum Mark: %d\n", max);
return 0;
}
OUTPUT
137 | P a g e
PROGRAM – 6
Read marks500.txt. Which roll number is the topper and print the roll
numbers who
failed (marks below 33)
#include <stdio.h>
int main() {
FILE *file = fopen("marks500.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
int roll_no, mark;
int topper_roll_no = 0, topper_mark = -1;
printf("Roll numbers who failed (marks below 33):\n");
while (fscanf(file, "%d %d", &roll_no, &mark) != EOF) {
if (mark < 33) {
printf("%d\n", roll_no);
}
}
}
fclose(file);
printf("\nTopper: Roll Number: %d, Marks: %d\n", topper_roll_no,
topper_mark);
return 0;
}
OUTPUT
139 | P a g e
PROGRAM – 7
Read marks500.txt. Find average and standard deviation of these 500
marks and save
this into a new file MarksStats.txt
#include <stdio.h>
#include <math.h>
int main() {
FILE *file = fopen("marks500.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
int marks[500];
int sum = 0, count = 0;
while (fscanf(file, "%d", &marks[count]) != EOF) {
sum += marks[count];
count++;
}
fclose(file);
double average = sum / (double)count;
double sum_squared_diff = 0;
140 | P a g e