ayush394

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

Fundamentals of C Programming (23CS003)

Practical File
Of
Fundamentals of C
Programming(23CS003)
Submitted

in partial fulfilment of the award of the degree

of

BACHELOR OF ENGINEERING
in
COMPUTER SCIENCE & ENGINEERING

CHANDIGARH-PATIALA NATIONAL HIGHWAY RAJPURA


(PATIALA) PUNJAB-140401 (INDIA)

Submitted To: Submitted By:


Faculty name: Ms. Neeru Sharma Name – Ayush Singh
Department of Computer Science & Engg. Roll No.- 2310990394
Chitkara University, Punjab Batch/Sem: B.E-CSE/2nd Sem

1 2310990394
Fundamentals of C Programming (23CS003)

INDEX
Sr. Experiment Name Page No. Date Teacher Sign
No.

1 Write a Program to show the use to input 4


(Scanf)/output (Printf) statements and block
structure of C-program by highlighting the
features of "stdio.h".
2 Write a program to add two numbers and 5
display the sum

3 Write a program to calculate the area and 6


the circumference of a circle by using radius
as the input provided by the user.
4 Write a Program to perform addition, 7
subtraction, division and multiplication of
two numbers given as input by the user.
5 Write a program to evaluate each of the 8
following equations. (i) V = u + at. (ii) S =
ut+1/2at 2 (iii) T=2*a+√b+9c
(iv) H=√b 2+p 2
6 Write a program to swap two variable: a) By 9-10
using temporary variable. b) Without using
temporary variable

7 Write a Program to find the greatest among 11-12


three numbers using
: • Conditional Operator
• If-Else statement
8 • Write the following programs using switch 13-15
case statement: • To check that an input
alphabet is vowel or consonant • To check
whether a number is positive, negative or
zero
9 • Write a program using while loop to print 16
the sum of first n natural numbers

10 • : Write a program to check a number is 17


Armstrong or not using For loop.

11 • : Write the program to count the digits in a 18


number and then print the reverse of the
number also.

2 2310990394
Fundamentals of C Programming (23CS003)

12 • Write a program to generate the Fibonacci 19


series

13 • : Write a program to print the following star 20-22


patterns:

14 • : Write the program to print the following 23


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
15 • : Write a program to check that the given 24-26
number is prime, Armstrong or perfect
using the concept of functions

16 • : Write a program to calculate the area and 27


circumference of a circle using functions.

17 • Write a program to swap two variables 28-29


using the concept of call by value and call
by reference

18 • : Write a program to perform the following 30-32


operations on 1D-Array: (a)Insert, (b)
Update, (c) Delete, (d) Display, (e) Search

19 • Write a program to calculate the sum of 33


array elements by passing it to a function

20 • : Write a program to show the use of 34


passing pointer as arguments to the
functions

21 • Write a program matrix multiplication using 35-37


the concept of 2D array

22 • : Write a Program to transpose a given 38-39


matrix.

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

Program Used: GDB Compiler


Solution:
Implementation: (a) By using temporary variable
#include <stdio.h>
int main ()
{
int a, b, temp;
printf ("Enter value for a: ");
scanf ("%d", &a);
printf ("Enter value for b: ");
scanf ("%d", &b);
temp = a;
a = b;
b = temp;
printf ("After swapping:\n");
printf ("a = %d\n", a);
printf ("b = %d\n", b);
return 0;
}
Output:

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

Program Used: GDB Compiler


Solution:
Implementation: (a) Conditional Operator
#include <stdio.h>
int main ()
{
int a, b, c, Greatest;
printf (" Enter three numbers: ");
scanf ("%d%d%d", &a, &b, &c);
Greatest=(a>b&&a>c?a:b>c?b:c);
Printf ("The greatest number is %d", Greatest);

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

Program Used: GDB Compiler


Solution:
Implementation: (a) To check that an input alphabet is vowel or consonant
#include <stdio.h>
int main () {
char c;
printf (" Enter an alphabet: ");
scanf ("%c", &c);
switch(c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
printf (" %c is a vowel.\n", c);
break;
default:
printf (" %c is a consonant.\n", c);
break;
}
return 0;
}
Output:

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)
* *
** **
*** ***
**** ****
***** *****
****** ******

Program Used: GDB Compiler


Solution:
Implementation: (a)
#include <stdio.h>
int main ()
{
int n;
printf ("\n Enter the number of rows: ");
scanf ("%d", &n);
for (int i=1; i<=n; i++)
{
for (int j=1; j<=i; j++)
{
printf (" * ");
}
printf("\n");
}
return 0;
}

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

Program Used: GDB Compiler


Solution:
Implementation:
#include <stdio.h>
int main ()
{
int i, j, n;
for (i = 1; i <= 6; i++)
{
for (j = 1; j <= 6; j++)
{
printf (" %d ", i * j);
}
printf("\n");
}
return 0;
}
Output:

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.

Program Used: GDB Compiler


Solution:
Implementation:
#include <stdio.h>
#include <math.h>
int is_prime (int num)
{
if (num <= 1)
return 0;
for (int i = 2; i <= sqrt(num); i++)
{
if (num % i == 0)
return 0;
}
return 1;
}
int is_armstrong (int num)
{
int order = 0, temp = num, digit, sum = 0;
while (temp! = 0)
{
temp /= 10;
++order;
}
temp = num;
while (temp! = 0)
{
digit = temp % 10;
sum += pow (digit, order);
temp /= 10;
}

24 2310990394
Fundamentals of C Programming (23CS003)

return (num == sum);


}
int is_perfect (int num)
{
int sum = 0;
for (int i = 1; i < num; i++)
{
if (num % i == 0)
sum += i;
}
return (sum == num);
}
int main ()
{
int num;
printf ("\n Enter a number: ");
scanf ("%d", &num);
if (is_prime(num))
printf (“ %d is a prime number.\n”, num);
else
printf (“ %d is not a prime number.\n”, num);
if (is_armstrong(num))
printf (“ %d is an Armstrong number.\n”, num);
else
printf (“ %d is not an Armstrong number.\n”, num);
if (is_perfect(num))
printf (“ %d is a perfect number.\n”, num);
else
printf (“ %d is not a perfect number.\n”, num);
return 0;
}

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)

Implementation: (b) call by reference


#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:

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

Program Used: GDB Compiler


Solution:
Implementation:
#include <stdio.h>
#define MAX_SIZE 100
void display(int arr[], int size) {
printf("Array elements: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int search(int arr[], int size, int key) {
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
void insert(int arr[], int *size, int pos, int value) {
if (*size >= MAX_SIZE) {
printf("Array is full. Insertion failed.\n");
return;
}
if (pos < 0 || pos > *size) {
printf("Invalid position. Insertion failed.\n");
return;
}
for (int i = *size; i > pos; i--) {
arr[i] = arr[i - 1];

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)

printf("Element 6 found at position %d.\n", index);


} else {
printf("Element 6 not found.\n");
}
return 0;
}
Output:

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

Aim: : Write a Program to transpose a given matrix.


Program Used: GDB Compiler
Solution:
Implementation:
#include <stdio.h>
int main()
{
int a[10][10],transpose[10][10],r,c;
printf("\n Enter the rows and columns: ");
scanf(" %d %d", &r,&c);
printf("\n Enter the matrix element:\n");
for(int i=0; i<r; ++i)
{
for(int j=0; j<c; ++j)
{
printf("\n Enter element a %d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\n Entered matrix: \n");
for(int i=0;i<r;++i)
{
for(int j=0; j<c; ++j)
{
printf("%d ", a[i][j]);
if(j==c-1)
printf("\n");
}
}
for(int i=0;i<r;++i)
{
for(int j=0; j<c; ++j)
{

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

You might also like