Ayush 394
Ayush 394
Ayush 394
Practical File
Of
Fundamentals of C
Programming(23CS003)
Submitted
of
BACHELOR OF ENGINEERING
in
COMPUTER SCIENCE & ENGINEERING
1 2310990394
Fundamentals of C Programming (23CS003)
INDEX
Sr. Experiment Name Page No. Date Teacher Sign
No.
2 2310990394
Fundamentals of C Programming (23CS003)
3 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 1
Aim: Write a Program to show the use to input (Scanf)/output (Printf) statements and block structure of
C-program by highlighting the features of "stdio.h".
Program Used: GDB Compiler
Solution:
Implementation:
#include<stdio.h>
void main ()
{
Printf (" My name is Arvin\n");
int age, group, marks, roll_no;
printf (" My age:");
scanf ("%d", &age);
printf (" My group:");
scanf ("%d", &group);
printf (" My marks:");
scanf ("%d", &marks);
printf (" My roll_no:");
scanf ("%d",& roll_no);
printf (" Detail of the student is\n");
printf (" age: %d\n", age);
printf (" group: %d\n", group);
printf (" marks: %d\n", marks);
printf (" roll_no: %d\n", roll_no);
}
Output:
4 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 2
Aim: Write a Program to add the two numbers and display the sum.
Program Used: GDB Compiler
Solution:
Implementation:
#include <stdio.h>
int main ()
{
double num1, num2, sum;
printf ("Enter the first number: ");
scanf ("%lf", &num1);
printf ("Enter the second number: ");
scanf ("%lf", &num2);
sum = num1 + num2;
printf ("Sum: %.2lf\n", sum);
return 0;
}
Output:
5 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 3
Aim: Write a Program to calculate the area and the circumference of a circle by using radius as the input
provided by the user.
Program Used: GDB Compiler
Solution:
Implementation:
#include <stdio.h>
#include <math.h>
int main () {
int radius, area, circumference;
printf (" Enter the radius of the circle: ");
scanf ("%d", &radius);
area =3.14 * (radius * radius);
circumference = 2 * 3.14 * radius;
printf (" Area of the circle: %d\n", area);
printf (" Circumference of the circle: %d\n", circumference);
return 0;
}
Output:
6 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 4
Aim: Write a Program to perform addition, subtraction, division and multiplication of two numbers given
as input by the user.
Program Used: GDB Compiler
Solution:
Implementation:
#include <stdio.h>
int main ()
{
int a, b, add, subtract, division, multiplication;
printf (" Enter the first number: ");
scanf ("%d", &a);
printf (" Enter the second number: ");
scanf ("%d", &b);
add = a + b;
printf (" Sum of two numbers is = %d", add);
subtract = a - b;
printf ("\n Diffirence of two numbers is = %d", subtract);
multiplication = a * b;
printf ("\n Product of two numbers is = %d", multiplication);
division = a / b;
printf ("\n Division of two numbers is = %d", division);
return 0;
}
Output:
7 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 5
Aim: Write a Program to evaluate each of the following equations.
2 2 2
(i) V = u + at. (ii) S = ut+1/2at (iii) T=2*a+√b+9c (iv) H=√b +p
Program Used: GDB Compiler
Solution:
Implementation:
#include <stdio.h>
#include<math.h>
int main ()
{
float v, u, acc, S, t, a, b, c, p, H, T;
printf ("Enter u, acc, t, a, b, c, p = ");
scanf ("%f%f%f%f%f%f%f", &u, &acc, &t, &a, &b, &c, &p);
v=u + acc*t;
S=u*t+1/(2*a*t*t);
T=2*a+ sqrt(b)+9*c;
H=sqrt (b*b + p*p);
printf ("V=%.2f\n S=%.2f\n T=%.2f\n H=%.2f\n", V, S, T, H);
return 0;
}
Output:
8 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 6
Aim: Write a program to swap two variables: a) By using temporary variable. b) Without using
temporary variable
9 2310990394
Fundamentals of C Programming (23CS003)
Solution:
Implementation: (b) Without using temporary variable
#include <stdio.h>
int main () {
int a, b;
printf ("Enter value for a: ");
scanf ("%d", &a);
printf ("Enter value for b: ");
scanf ("%d", &b);
a = a + b;
b = a - b;
a = a - b;
Printf (" After swapping:\n");
printf (" a = %d\n", a);
printf (" b = %d\n", b);
return 0;
}
Output:
10 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 7
Aim: Write a program to find the greatest among three numbers using: (a) Conditional Operator (b) If-
Else statement
return 0;
}
Output:
11 2310990394
Fundamentals of C Programming (23CS003)
Solution:
Implementation: (b) If-Else Statement
#include <stdio.h>
int main ()
{
int a, b, c, Greatest;
printf ("Enter three numbers: ");
scanf ("%d%d%d", &a, &b, &c);
if (a>b&&a>c)
{Greatest=a;}
else if (b>c)
{Greatest=b;}
else
{Greatest=c;}
printf ("The greatest number is %d", Greatest);
return 0;
}
Output:
12 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 8
Aim: Write the following programs using switch case statement: (a) To check that an input alphabet is
vowel or consonant (b) To check whether a number is positive, negative or zero
13 2310990394
Fundamentals of C Programming (23CS003)
Solution:
Implementation: (b) To check whether a number is positive, negative or zero
#include <stdio.h>
int main () {
int num;
printf ("Enter a number: ");
scanf ("%d", &num);
switch (num > 0) {
case 1:
printf ("%d is positive.\n", num);
break;
case 0:
switch (num < 0) {
case 1:
printf ("%d is negative.\n", num);
break;
case 0:
printf ("The number is zero.\n");
break;
}
break;
}return 0;
}
Output:
14 2310990394
Fundamentals of C Programming (23CS003)
15 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 9
Aim: Write a program using while loop to print the sum of first n natural numbers
Program Used: GDB Compiler
Solution:
Implementation:
#include <stdio.h>
int main ()
{
int n, i, sum = 0;
printf ("Enter a positive integer: ");
scanf ("%d", &n);
i = 1;
while (i <= n) {
sum += i;
++i;
}
printf ("Sum = %d", sum);
return 0;
}
Output:
16 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 10
Aim: Write a program to check a number is Armstrong or not using For loop.
Program Used: GDB Compiler
Solution:
Implementation:
#include<stdio.h>
int main ()
{
int n, r, sum=0, temp;
printf (" Enter the number= ");
scanf ("%d", &n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf (" Armstrong Number ");
else
printf (" Not Armstrong Number");
return 0;
}
Output:
17 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 11
Aim: Write the program to count the digits in a number and then print the reverse of the number also.
Program Used: GDB Compiler
Solution:
Implementation:
#include <stdio.h>
int main ()
{
int n, temp, rem, count = 0, revNum = 0;
printf ("\n Enter a Number: ");
scanf ("%d", &n);
temp = n;
while (n!=0)
{
rem=n%10;
count++;
n/=10;
}
n=temp;
while (n!=0)
{
rem=n%10;
revNum=revNum*10+rem;
n/=10;
}
printf (" Number of digits: %d\n", count);
printf (" Reverse of the number: %d\n", revNum);
return 0;
}
Output:
18 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 12
Aim: Write a program to generate the Fibonacci series.
Program Used: GDB Compiler
Solution:
Implementation:
#include <stdio.h>
int main ()
{
int i, n;
int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;
printf ("\n Enter the number of terms: ");
scanf ("%d", &n);
printf (" Fibonacci Series: %d, %d, ", t1, t2);
for (i = 3; i <= n; ++i) {
printf ("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
Output:
19 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 13
Aim: Write a program to print the following patterns:
a) b)
* *
** **
*** ***
**** ****
***** *****
****** ******
20 2310990394
Fundamentals of C Programming (23CS003)
Output:
21 2310990394
Fundamentals of C Programming (23CS003)
Solution:
Implementation: (b)
#include <stdio.h>
int main ()
{
int n, m =1;
printf ("\n Enter the number of rows: ");
scanf ("%d", &n);
for (int i = n; i >= 1; i--)
{
for (int j = 1; j <= i - 1; j++)
{
printf (" ");
}
for (int k = 1; k <= m; k++)
{
printf ("*");
}
printf (" \n");
m++;
}
return 0;
}
Output:
22 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 14
Aim: Write the program to print the following pattern:
1 2 3 4 5 6
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
5 10 15 20 25 30
6 12 18 24 30 36
23 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 15
Aim: Write a program to check that the given number is prime, Armstrong or perfect using the concept of
functions.
24 2310990394
Fundamentals of C Programming (23CS003)
25 2310990394
Fundamentals of C Programming (23CS003)
Output:
26 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 16
Aim: Write a program to calculate the area and circumference of a circle using functions.
Program Used: GDB Compiler
Solution:
Implementation:
#include <stdio.h>
int main()
{
int radius;
float area, circumference;
printf("\n Enter the radius of the circle: ");
scanf("%d", &radius);
area = 3.14 * radius * radius;
circumference = 2 * 3.14 * radius;
printf(" Area of the circle: %.2f\n", area);
printf(" Circumference of the circle: %.2f\n", circumference);
return 0;
}
Output:
27 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 17
Aim: Write a program to swap two variables using the concept of call by value and call by reference.
Program Used: GDB Compiler
Solution:
Implementation: (a) call by value
#include <stdio.h>
void swap(int , int);
int main()
{
int a = 10;
int b = 20;
printf("\n Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b);
printf("\n After swapping values in main a = %d, b = %d\n",a,b);
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("\n After swapping values in function a = %d, b = %d\n",a,b);
}
Output:
28 2310990394
Fundamentals of C Programming (23CS003)
29 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 18
Aim: Write a program to perform the following operations on 1D-Array:
(a)Insert, (b) Update, (c) Delete, (d) Display, (e) Search
30 2310990394
Fundamentals of C Programming (23CS003)
}
arr[pos] = value;
(*size)++;
}
void update(int arr[], int size, int pos, int value) {
if (pos < 0 || pos >= size) {
printf("Invalid position. Update failed.\n");
return;
}
arr[pos] = value;
}
void delete(int arr[], int *size, int pos) {
if (pos < 0 || pos >= *size) {
printf("Invalid position. Deletion failed.\n");
return;
}
for (int i = pos; i < *size - 1; i++) {
arr[i] = arr[i + 1];
}
(*size)--;
}
int main() {
int arr[MAX_SIZE] = {2, 4, 6, 8, 10};
int size = 5;
printf("Initial ");
display(arr, size);
insert(arr, &size, 2, 5);
printf("After inserting 5 at position 2: ");
display(arr, size);
update(arr, size, 3, 7);
printf("After updating value at position 3 to 7: ");
display(arr, size);
delete(arr, &size, 4);
printf("After deleting element at position 4: ");
display(arr, size);
int index = search(arr, size, 10);
if (index != -1) {
31 2310990394
Fundamentals of C Programming (23CS003)
32 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 19
Aim: Write a program to calculate the sum of array elements by passing it to a function.
Program Used: GDB Compiler
Solution:
Implementation:
#include <stdio.h>
int sum_array(int array[], int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += array[i];
}
return sum;
}
int main()
{
int array[] = {1, 2, 3, 4, 5};
int size = sizeof(array) / sizeof(array[0]);
int sum = sum_array(array, size);
printf("\n The sum of the array elements is %d\n", sum);
return 0;
}
Output:
33 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 20
Aim: Write a program to show the use of passing pointer as arguments to the functions.
Program Used: GDB Compiler
Solution:
Implementation:
#include <stdio.h>
void swap(int *a, int *b);
void increment(int *x);
int main() {
int num1 = 5, num2 = 10;
printf("Before swap: num1 = %d, num2 = %d\n", num1, num2);
swap(&num1, &num2);
printf("After swap: num1 = %d, num2 = %d\n", num1, num2);
printf("Before increment: num1 = %d\n", num1);
increment(&num1);
printf("After increment: num1 = %d\n", num1);
return 0;
}
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void increment(int *x) {
(*x)++;
}
Output:
34 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 21
Aim: Write a program matrix multiplication using the concept of 2D array.
Program Used: GDB Compiler
Solution:
Implementation:
#include <stdio.h>
int main()
{
int m1, n1, m2, n2;
printf("\n Enter the number of rows and columns in the first matrix: ");
scanf("%d %d", &m1, &n1);
printf("\n Enter the number of rows and columns in the second matrix: ");
scanf("%d %d", &m2, &n2);
if (n1 != m2)
{
printf("\n The matrices cannot be multiplied.\n");
return 1;
}
int matrix1[m1][n1];
int matrix2[m2][n2];
int result[m1][n2];
printf("\n Enter the elements of the first matrix:\n");
for (int i = 0; i < m1; i++)
{
for (int j = 0; j < n1; j++)
{
scanf("%d", &matrix1[i][j]);
}
}
printf("\n Enter the elements of the second matrix:\n");
for (int i = 0; i < m2; i++)
{
for (int j = 0; j < n2; j++)
{
35 2310990394
Fundamentals of C Programming (23CS003)
scanf("%d", &matrix2[i][j]);
}
}
for (int i = 0; i < m1; i++)
{
for (int j = 0; j < n2; j++)
{
result[i][j] = 0;
for (int k = 0; k < n1; k++)
{
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
printf("\n The result matrix is:\n");
for (int i = 0; i < m1; i++)
{
for (int j = 0; j < n2; j++)
{
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
Output:
36 2310990394
Fundamentals of C Programming (23CS003)
37 2310990394
Fundamentals of C Programming (23CS003)
Experiment No. 22
38 2310990394
Fundamentals of C Programming (23CS003)
transpose[j][i]=a[i][j];
}
}
printf("\n Transpose of a matrix:\n");
for(int i=0;i<c;++i)
{
for(int j=0; j<r; ++j)
{
printf("%d ", transpose[i][j]);
if(j==r-1)
printf("\n");
}
}
return 0;
}
Output:
39 2310990394