0% found this document useful (0 votes)
30 views41 pages

C Lab Programs 2024 Final Print

Uploaded by

Akshay Roddam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views41 pages

C Lab Programs 2024 Final Print

Uploaded by

Akshay Roddam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

S.G.T DEGREE COLLEGE, BALLARI.

Part A

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 1


S.G.T DEGREE COLLEGE, BALLARI.
/* 1. Write a C program to Display “Hello, World!” & to Add Two Numbers, Explaining
the Structure of a C Program */

#include <stdio.h>
#include<onio.h>

void main()
{
int num1, num2, sum;
clrscr();
printf("Hello, World!\n");
num1 = 5;
num2 = 10;
sum = num1 + num2;
printf("The sum of %d and %d is: %d\n", num1, num2, sum);
getch();
}

OUTPUT:

Hello World
The sum of 5 and 10 is: 15

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 2


S.G.T DEGREE COLLEGE, BALLARI.
/* 2. Write a C program to Demonstrate the Use of Keywords, Identifiers, Constants,
Variables, Declare and Initialize Different Datatypes, and Define Symbolic Constants
*/

#include <stdio.h>
#include<conio.h>
#define PI 3.14159
void main()
{

int radius = 5;
float circumference;
char grade = 'A';
double area;
clrscr();
circumference = 2 * PI * radius;
area = PI * radius * radius;
printf("Radius of the circle: %d\n", radius);
printf("Circumference of the circle: %.2f\n", circumference);
printf("Area of the circle: %.2f\n", area);
printf("Grade: %c\n", grade);
getch();
}

OUTPUT:

Radius of the circle: 5


Circumference of the circle: 31.42
Area of the circle: 78.54
Grade: A

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 3


S.G.T DEGREE COLLEGE, BALLARI.
/* 3. Write a C Program to Read and Display an Integer, a Float, and a Character Using
printf and scanf, and to Demonstrate the Use of Different Control Strings and Escape
Sequences */

#include <stdio.h>
#include<conio.h>
void main()
{
int iVar;
float flVar;
char cVar;
char sVar[50]; // Array of characters (string)
clrscr();
printf("Enter an integer: ");
scanf("%d", &iVar);

printf("Enter a float: ");


scanf("%f", &fVar);

printf("Enter a character: ");


scanf(" %c", &cVar);

printf("Enter a string (without spaces): ");


scanf("%s", sVar);

printf("\nYou entered the integer: %d\n", iVar); // %d for integers


printf("You entered the float: %.2f\n", fVar); // %.2f for two decimal places in float
printf("You entered the character: %c\n", cVar); // %c for characters
printf("You entered the string: %s\n", sVar); // %s for strings

// Escape sequences demonstration


printf("\n--- Escape Sequences ---\n");

printf("New line: This is one line,\n and this is the next line.\n");

// Tab (\t)
printf("Tab space: Here is\ta tab space.\n");

// Backslash (\\)
printf("Backslash: Displaying a backslash: \\\n");

// Double quotes (\")


printf("Double quotes: Displaying \"double quotes\" inside a string.\n");

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 4


S.G.T DEGREE COLLEGE, BALLARI.
// Backspace (\b)
printf("Backspace: Text\b\b\bThis removes the last few characters.\n");

// Carriage return (\r)


printf("Carriage return: This will overwrite text.\rOVERWRITE\n");

// Alert (Bell) (\a)


printf("Alert/Bell: You will hear a sound\a (if the terminal supports it).\n");
getch(); //to display the output on command prompt
}

OUTPUT:

Enter an integer: 42
Enter a float: 3.14
Enter a character: A
Enter a string (without spaces): Hello

You entered the integer: 42


You entered the float: 3.14
You entered the character: A
You entered the string: Hello

--- Escape Sequences ---


New line: This is one line,
and this is the next line.
Tab space: Here is a tab space.
Backslash: Displaying a backslash: \
Double quotes: Displaying "double quotes" inside a string.
Backspace: Text This removes the last few characters.
Carriage return: This will overwrite text.
OVERWRITE
Alert/Bell: You will hear a sound (if the terminal supports it).

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 5


S.G.T DEGREE COLLEGE, BALLARI.
/* 4. Write a C Program to Read and Display a Single Character Using getchar and
putchar, and Read and Display a String Using gets and puts */

#include <stdio.h>
#include<conio.h>
void main()
{
char ch;
char str[100];
clrscr();

printf("Enter a single character: ");


ch = getchar();
printf("You entered: ");
putchar(ch);
printf("\n");

// Clearing the input buffer after getchar


getchar(); // This is necessary to consume the newline left by getchar()

printf("Enter a string: ");


gets(str);

printf("You entered: ");


puts(str);
getch();
}

OUTPUT:

Enter a single character: A


You entered: A
Enter a string: SGT Degree College, Ballari.
You entered: SGT Degree College, Ballari.

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 6


S.G.T DEGREE COLLEGE, BALLARI.
/*5. Write a C program to perform basic arithmetic operations (+, -, *, /, %) and
demonstrate the use of assignment, increment, and decrement operators. */

#include <stdio.h>
#include<conio.h>
void main()
{
int a, b;
clrscr();
printf("Enter two integers (a and b): ");
scanf("%d %d", &a, &b);

// Basic arithmetic operations


printf("\n--- Arithmetic Operations ---\n");
printf("Sum (a + b) = %d\n", a + b);
printf("Difference (a - b) = %d\n", a - b);
printf("Product (a * b) = %d\n", a * b);
printf("Quotient (a / b) = %d\n", a / b);
printf("Remainder (a %% b) = %d\n", a % b); // Note: %% prints %
// Assignment, increment, and decrement operators
printf("\n--- Increment and Decrement Operations ---\n");
a++;
printf("a after increment (a++): %d\n", a);
b--;
printf("b after decrement (b--): %d\n", b);
getch();
}

OUTPUT:

Enter two integers (a and b): 10 3

--- Arithmetic Operations ---


Sum (a + b) = 13
Difference (a - b) = 7
Product (a * b) = 30
Quotient (a / b) = 3
Remainder (a % b) = 1

--- Increment and Decrement Operations ---


a after increment (a++): 11
b after decrement (b--): 2

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 7


S.G.T DEGREE COLLEGE, BALLARI.
// 6. Write a C program to demonstrate the use of relational and logical operators.

#include <stdio.h>
#include<conio.h>
void main()
{
int a, b;
clrscr();
printf("Enter two integers (a and b): ");
scanf("%d %d", &a, &b);

// Relational Operators
printf("\n--- Relational Operators ---\n");
printf("Is a equal to b? (a == b): %d\n", a == b);
printf("Is a not equal to b? (a != b): %d\n", a != b);
printf("Is a greater than b? (a > b): %d\n", a > b);
printf("Is a less than b? (a < b): %d\n", a < b);
printf("Is a greater than or equal to b? (a >= b): %d\n", a >= b); to
printf("Is a less than or equal to b? (a <= b): %d\n", a <= b);

// Logical Operators
int x = 1, y = 0; // Example values for logical operations
printf("\n--- Logical Operators ---\n");
printf("Logical AND (x && y): %d\n", x && y);
printf("Logical OR (x || y): %d\n", x || y);
printf("Logical NOT (!x): %d\n", !x);
printf("Logical NOT (!y): %d\n", !y);
getch();
}

OUTPUT:

Enter two integers (a and b): 5 3

--- Relational Operators ---


Is a equal to b? (a == b): 0
Is a not equal to b? (a != b): 1
Is a greater than b? (a > b): 1
Is a less than b? (a < b): 0
Is a greater than or equal to b? (a >= b): 1
Is a less than or equal to b? (a <= b): 0

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 8


S.G.T DEGREE COLLEGE, BALLARI.
--- Logical Operators ---
Logical AND (x && y): 0
Logical OR (x || y): 1
Logical NOT (!x): 0
Logical NOT (!y): 1

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 9


S.G.T DEGREE COLLEGE, BALLARI.
/* 7. Write a C program to demonstrate the use of bitwise and conditional (ternary)
operators. */

#include <stdio.h>
#include<conio.h>
void main()
{
int a, b,max;
clrscr();
printf("Enter two integers (a and b): ");
scanf("%d %d", &a, &b);

printf("\n--- Bitwise Operators ---\n");


printf("Bitwise AND (a & b): %d\n", a & b); // AND
printf("Bitwise OR (a | b): %d\n", a | b); // OR
printf("Bitwise XOR (a ^ b): %d\n", a ^ b); // XOR
printf("Bitwise NOT (~a): %d\n", ~a); // NOT (1's complement of a)
printf("Left Shift (a << 1): %d\n", a << 1); // Left shift by 1 bit
printf("Right Shift (a >> 1): %d\n", a >> 1); // Right shift by 1 bit

printf("\n--- Conditional (Ternary) Operator ---\n");


max = (a > b) ? a : b; // Conditional operator to find the maximum value
printf("The greater number between a and b is: %d\n", max);
getch(); //to display the output on command prompt
}

OUTPUT:

Enter two integers (a and b): 6 3

--- Bitwise Operators ---


Bitwise AND (a & b): 2
Bitwise OR (a | b): 7
Bitwise XOR (a ^ b): 5
Bitwise NOT (~a): -7
Left Shift (a << 1): 12
Right Shift (a >> 1): 3

--- Conditional (Ternary) Operator ---


The greater number between a and b is: 6

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 10


S.G.T DEGREE COLLEGE, BALLARI.
// 8. Write a C program to find the largest of three numbers using if-else statements.

#include <stdio.h>
#include <conio.h>
void main()
{

int a, b, c;
clrscr();
printf("Enter three integers (a, b, c): ");
scanf("%d %d %d", &a, &b, &c);

if (a >= b && a >= c)


{
printf("The largest number is: %d\n", a);
}
else if (b >= a && b >= c)
{
printf("The largest number is: %d\n", b);
}
else
{
printf("The largest number is: %d\n", c);
}
getch();

OUTPUT:

Enter three integers (a, b, c): 5 9 3


The largest number is: 9

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 11


S.G.T DEGREE COLLEGE, BALLARI.
/* 9. Write a C program to classify a student's grade based on marks using nested if-else
and else-if ladder. */

#include <stdio.h>
#include <conio.h>
void main()
{
int marks;
clrscr();
printf("Enter the marks obtained (0-100): ");
scanf("%d", &marks);

if (marks < 0 || marks > 100)


{
printf("Invalid marks! Please enter marks between 0 and 100.\n");
}
else if (marks >= 75)
{
printf("Grade: Distinction\n");
}
else if (marks >= 60)
{
printf("Grade: First Class\n");
}
else if (marks >= 50)
{
printf("Grade: Second Class\n");
}
else if (marks >= 32)
{
printf("Grade: Pass\n");
}
else
{
printf("Grade: Fail\n");
}
getch();
}

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 12


S.G.T DEGREE COLLEGE, BALLARI.
OUTPUT:

Run1
Enter the marks obtained (0-100): 82
Grade: Distinction

Run2
Enter the marks obtained (0-100): 62
Grade: First Class

Run3
Enter the marks obtained (0-100): 55
Grade: Second Class

Run4
Enter the marks obtained (0-100): 40
Grade: Pass

Run5
Enter the marks obtained (0-100): 30
Grade: Fail

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 13


S.G.T DEGREE COLLEGE, BALLARI.
// 10. Write a C program to print the day of the week using switch-case statements.

#include <stdio.h>
#include <conio.h>
void main()
{
int day;
clrscr();
printf("Enter a number (1-7) to represent the day of the week: ");
scanf("%d", &day);

switch (day)
{
case 1: printf("Sunday\n");
break;
case 2: printf("Monday\n");
break;
case 3: printf("Tuesday\n");
break;
case 4: printf("Wednesday\n");
break;
case 5: printf("Thursday\n");
break;
case 6: printf("Friday\n");
break;
case 7: printf("Saturday\n");
break;
default:
printf("Invalid input! Please enter a number between 1 and 7.\n");
break;
}
getch();
}

OUTPUT:
Run1
Enter a number (1-7) to represent the day of the week: 3
Tuesday
Run2
Enter a number (1-7) to represent the day of the week: 6
Friday
Run3
Enter a number (1-7) to represent the day of the week: 9
Invalid input! Please enter a number between 1 and 7

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 14


S.G.T DEGREE COLLEGE, BALLARI.

Part B

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 15


S.G.T DEGREE COLLEGE, BALLARI.
// 1. Write a C program to print the first 10 natural numbers using a while loop.

#include <stdio.h>
#include <conio.h>
void main()
{
int i = 1;
clrscr();
printf("The first 10 natural numbers are:\n");
while (i <= 10)
{
printf("%d\n", i);
i++;
}
getch();
}

OUTPUT:

The first 10 natural numbers are:


1
2
3
4
5
6
7
8
9
10

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 16


S.G.T DEGREE COLLEGE, BALLARI.
// 2. Write a C program to print the factorial of a number using a do- while loop.

#include <stdio.h>
#include <conio.h>
void main()
{
int num, i = 1;
unsigned long factorial = 1;
clrscr();

printf("Enter a positive integer: ");


scanf("%d", &num);

if (num < 0)
{
printf("Factorial of a negative number doesn't exist.\n");
}
else
{
do
{
factorial *= i;
i++;
} while (i <= num);

printf("Factorial of %d = %lu\n", num, factorial);


}
getch();
}

OUTPUT:

Run1
Enter a positive integer: 5
Factorial of 5 = 120

Run2
Enter a positive integer: 7
Factorial of 7 = 5040

Run3
Enter a positive integer: -5
Factorial of a negative number doesn't exist.

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 17


S.G.T DEGREE COLLEGE, BALLARI.
// 3. Write a C program to generate the Fibonacci series using a for loop.

#include <stdio.h>
#include <conio.h>
void main()
{
int i, n, first = 0, second = 1, next;
clrscr();
printf("Enter the number of terms in the Fibonacci series: ");
scanf("%d", &n);
if (n <= 0)
{
printf("Please enter a positive number\n");
}
else
{
printf("Fibonacci series: %d %d ", first, second);

for (i = 3; i <= n; i++)


{
next = first + second;
printf("%d ", next);
first = second;
second = next;
}
printf("\n");
}
getch();
}

OUTPUT:

Run1
Enter the number of terms in the Fibonacci series: 5
Fibonacci series: 0 1 1 2 3

Run2
Enter the number of terms in the Fibonacci series: 8
Fibonacci series: 0 1 1 2 3 5 8 13

Run3
Enter the number of terms in the Fibonacci series: -7
Please enter a positive number.

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 18


S.G.T DEGREE COLLEGE, BALLARI.
// 4. Write a C program to print a multiplication table using nested loops.

#include <stdio.h>
#include<conio.h>
void main()
{
int i, j;
clrscr();
printf(“Multipliction table from 1 to 5\n”);
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= 10; j++)
{
printf("%d x %d = %d\t", i, j, i * j);
}
printf("\n");
}
getch();
}

OUTPUT :

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 19


S.G.T DEGREE COLLEGE, BALLARI.
// 5. Write a C program to read and display elements of a one-dimensional array.

#include <stdio.h>
#include <conio.h>
void main()
{
int n, i;
int array[100]; // Declare an array of size 100
clrscr();

printf("Enter the number of elements in the array: ");


scanf("%d", &n);

// Read elements of the array


printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &array[i]);
}

printf("The elements of the array are:\n");


for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
printf("\n");
getch();
}

OUTPUT:

Enter the number of elements in the array: 5


Enter 5 elements:
Element 1: 10
Element 2: 20
Element 3: 30
Element 4: 40
Element 5: 50
The elements of the array are:
10 20 30 40 50

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 20


S.G.T DEGREE COLLEGE, BALLARI.
// 6. Write a C program to read and display elements of a two-dimensional array.

#include <stdio.h>
#include <conio.h>
void main()
{
int i, j, rows, cols;
int array[10][10];// Declare a two-dimensional array
clrscr();
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);

// Read elements of the array


printf("Enter the elements of the array:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("Element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &array[i][j]);
}
}
// Display elements of the array
printf("The elements of the array are:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("%d\t", array[i][j]);
}
printf("\n");
}
getch();
}

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 21


S.G.T DEGREE COLLEGE, BALLARI.
OUTPUT :

Enter the number of rows: 3


Enter the number of columns: 4
Enter the elements of the array:
Element [1][1]: 1
Element [1][2]: 2
Element [1][3]: 3
Element [1][4]: 4
Element [2][1]: 5
Element [2][2]: 6
Element [2][3]: 7
Element [2][4]: 8
Element [3][1]: 9
Element [3][2]: 10
Element [3][3]: 11
Element [3][4]: 12

The elements of the array are:


1 2 3 4
5 6 7 8
9 10 11 12

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 22


S.G.T DEGREE COLLEGE, BALLARI.
// 7. Write a C program to find the sum and average of elements in an array.

#include <stdio.h>
#include <conio.h>
void main()
{
int i, n, array[100];
float sum = 0.0, average;
clrscr();

printf("Enter the number of elements in the array: ");


scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &array[i]);
sum = sum + array[i]; // Calculate sum
}
average = sum / n;
printf("Sum of the elements: %.2f\n", sum);
printf("Average of the elements: %.2f\n", average);
getch(); //to display the output on command prompt
return 0;
}

OUTPUT :

Enter the number of elements in the array: 5


Enter 5 elements:
Element 1: 10
Element 2: 20
Element 3: 30
Element 4: 40
Element 5: 50
Sum of the elements: 150.00
Average of the elements: 30.00

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 23


S.G.T DEGREE COLLEGE, BALLARI.
/* 8. Write a C program to demonstrate the use of string handling functions (strlen,
strcmp, strcpy, strcat). */

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char str1[100], str2[100];
clrscr();

printf("Enter the first string: ");


scanf("%s", str1); // Reads the first string (without spaces)
printf("Enter the second string: ");
scanf("%s", str2); // Reads the second string (without spaces)

// 1. Length of str1
printf("Length of first string: %u\n", strlen(str1));

// 2. Compare strings
if (strcmp(str1, str2) == 0)
{
printf("Strings are equal.\n");
}
else
{
printf("Strings are different.\n");
}
// 3. Concatenate str1 and str2
strcat(str1, str2);
printf("After concatenation, first string: %s\n", str1);

// 4. Copy str1 to str2


strcpy(str2, str1);// string 1 will overwrite on string2
printf("After copying, first string: %s\n", str2);
getch();
}
OUTPUT :
Enter the first string: Good
Enter the second string: Morning
Length of first string: 4
Strings are different.
After concatenation, first string: Good Morning
After copying, first string: Morning

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 24


S.G.T DEGREE COLLEGE, BALLARI.
// 9. Write a C program to find the square of a number using a user-defined function.

#include <stdio.h>
#include <conio.h>

// User-defined function to calculate square


int square(int num)
{
return num * num;
}

void main()
{
int number, result;
clrscr(); //to clear the previous output

// Input a number from the user


printf("Enter a number: ");
scanf("%d", &number);

// Call the square function


result = square(number);

// Output the result


printf("The square of %d is %d\n", number, result);
getch();
}

OUTPUT:

Enter a number: 5
The square of 5 is 25

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 25


S.G.T DEGREE COLLEGE, BALLARI.
/* 10. Write a C program to calculate the sum of two numbers using a function with
parameters and return type. */

#include <stdio.h>
#include <conio.h>

// Function to calculate the sum of two numbers


int sum(int a, int b)
{
return a + b;
}

void main()
{
int num1, num2, result;
clrscr();

// Input two numbers from the user


printf("Enter the first and second number: ");
scanf("%d%d", &num1, &num2);

// Call the sum function and store the result


result = sum(num1, num2);

// Output the result


printf("The sum of %d and %d is %d\n", num1, num2, result);
getch();

OUTPUT:

Enter the first number: 10


Enter the second number: 20
The sum of 10 and 20 is 30

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 26


S.G.T DEGREE COLLEGE, BALLARI.
/* 11. Write a C program to display a message using a function without parameters and
return type. */

#include <stdio.h>
#include <conio.h>

// Function without parameters and return type


void displayMessage()
{
printf("Hello, welcome to C programming!\n");
}

void main()
{
clrscr();
displayMessage();
getch();
}

OUTPUT:

Hello, welcome to C programming!

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 27


S.G.T DEGREE COLLEGE, BALLARI.
/* 12. Write a C program to declare and initialize pointers and access the value and
address of variables using pointers. */

#include <stdio.h>
#include <conio.h>
int main()
{
int num = 10; // Declare and initialize an integer variable
int *ptr; // Declare a pointer to an integer
clrscr(); //to clear the previous output

ptr = &num; // Initialize the pointer with the address of num

// Access and display the value and address using the variable
printf("Value of num: %d\n", num);
printf("Address of num: %u\n", &num);

// Access and display the value and address using the pointer
printf("Value using pointer: %d\n", *ptr); // Dereferencing pointer
printf("Address using pointer: %u\n", ptr); // Pointer stores the address
getch(); //to display the output on command prompt
}

OUTPUT:

Value of num: 10
Address of num: 65524 // Address may vary
Value using pointer: 10
Address using pointer: 65524 // Address matches with &num

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 28


S.G.T DEGREE COLLEGE, BALLARI.
// 13. Write a program to demonstrate the relationship between pointers and arrays.
#include <stdio.h>
#include <conio.h>
void main()
{
int i, arr[5] = {10, 20, 30, 40, 50}; // Declare and initialize an array
int *ptr = arr, i; // Pointer pointing to the first element of the array
clrscr(); //to clear the previous output
// Access array elements using the pointer
printf("Accessing array elements using pointer arithmetic:\n");
for (i = 0; i < 5; i++)
{
printf("Element at index %d: %d\n", i, *(ptr + i)); // Pointer arithmetic
}
// Access the same elements using array indexing
printf("\nAccessing array elements using array indexing:\n");
for (i = 0; i < 5; i++)
{
printf("Element at index %d: %d\n", i, arr[i]);
}
// Display addresses of array elements
printf("\nAddresses of array elements:\n");
for (i = 0; i < 5; i++)
{
printf("Address of arr[%d]: %p\n", i, (ptr + i)); // Address of each element
}
getch(); //to display the output on command prompt

}
OUTPUT :
Accessing array elements using pointer arithmetic:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50

Accessing array elements using array indexing:


Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 29


S.G.T DEGREE COLLEGE, BALLARI.
Addresses of array elements:
Address of arr[0]: 65516 // Address may vary
Address of arr[1]: 65518
Address of arr[2]: 65520
Address of arr[3]: 65522
Address of arr[4]: 65524

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 30


S.G.T DEGREE COLLEGE, BALLARI.
/* 14. Write a program to perform pointer arithmetic (increment, decrement, addition,
subtraction). */

#include <stdio.h>
#include <conio.h>
void main()
{
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr; // Pointer to the first element of the array
clrscr(); //to clear the previous output

// Initial pointer value


printf("Value at ptr: %d\n", *ptr);

// Pointer increment
ptr++;
printf("After increment: %d\n", *ptr);

// Pointer decrement
ptr--;
printf("After decrement: %d\n", *ptr);

// Pointer addition
ptr = ptr + 2;
printf("After adding 2: %d\n", *ptr);

// Pointer subtraction
ptr = ptr - 1;
printf("After subtracting 1: %d\n", *ptr);
getch(); //to display the output on command prompt
}

OUTPUT:

Value at ptr: 10
After increment: 20
After decrement: 10
After adding 2: 30
After subtracting 1: 20

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 31


S.G.T DEGREE COLLEGE, BALLARI.
/* 15. Write a C program to define a structure for a student (roll number, name, marks)
and demonstrate initialization and access of structure members. */

#include <stdio.h>
#include <conio.h>

// Define a structure for a student


struct Student
{
int rollNumber;
char name[50];
float marks;
};

void main()
{
// Declare and initialize a student structure
struct Student student1 = {1001, "Raja", 85.5};
clrscr();

// Access and display structure members


printf("Student Details:\n");
printf("Roll Number: %d\n", student1.rollNumber);
printf("Name: %s\n", student1.name);
printf("Marks: %.2f\n", student1.marks);
getch();
}

OUTPUT:

Student Details:
Roll Number: 1001
Name: Raja
Marks: 85.50

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 32


S.G.T DEGREE COLLEGE, BALLARI.
/* 16. Write a C program to create an array of structures for students and display their
details. */

#include <stdio.h>
#include <conio.h>

// Define a structure for a student


struct Student
{
int rollNumber;
char name[50];
float marks;
};
void dummy(float *a)
{
float b=*a; //perform some floating access
dummy(&b); // calling a floating point function
}
void main()
{
struct Student s[5]; // Declare an array of 5 students
int i;
clrscr();

for (i = 0; i < 4; i++)


{
printf("Enter details for student %d:\n", i + 1);
printf("Roll Number: ");
scanf("%d", &s[i].rollNumber);
printf("Name: ");
scanf("%s", s[i].name);
printf("Marks: ");
scanf("%f", &s[i].marks);
}
// Display student details
printf("\nStudent Details:\n");
printf("\nRollno\tname\tMarks\n");
for (i = 0; i < 4; i++)
{
printf("%d\t%s\t%.2f\n", s[i].rollNumber,s[i].name,s[i].marks);
}
getch();
}

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 33


S.G.T DEGREE COLLEGE, BALLARI.
OUTPUT:

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 34


S.G.T DEGREE COLLEGE, BALLARI.
/* 17. Write a C program to demonstrate embedded structures (structure within a
structure). */
#include <stdio.h>
#include <conio.h>
struct Address
{
char city[30];
char state[30];
};

// Define a structure for Student that includes Address


struct Student
{
int rollNumber;
char name[50];
struct Address addr; // Embedded structure
};

void main()
{
struct Student student; // Declare a student structure
clrscr(); //to clear the previous output
// Input student details
printf("Enter Roll Number: ");
scanf("%d", &student.rollNumber);
printf("Enter Name: ");
scanf("%s", student.name); // Read name
// Input address details
printf("Enter City: ");
scanf("%s", student.addr.city); // Read city
printf("Enter State: ");
scanf("%s", student.addr.state); // Read state
// Display student details
printf("\nStudent Details:\n");
printf("Roll Number: %d\n", student.rollNumber);
printf("Name: %s\n", student.name);
printf("Address: %s, %s\n", student.addr.city, student.addr.state);
getch();
}

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 35


S.G.T DEGREE COLLEGE, BALLARI.
OUTPUT:

Enter Roll Number: 101


Enter Name: Ajay
Enter City: Ballari
Enter State: Karnataka

Student Details:
Roll Number: 101
Name: Ajay
Address: Ballari, Karnataka

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 36


S.G.T DEGREE COLLEGE, BALLARI.
/* 18. Write a C program to define a union for different data types (int, float, char) and
demonstrate initialization and access of union members. */

#include <stdio.h>
#include <conio.h>

// Define a union for different data types


union Data
{
int intValue;
float floatValue;
char charValue;
};

int main()
{
union Data data; // Declare a union variable
clrscr(); //to clear the previous output

// Assign and display int value


data.intValue = 10;
printf("Int Value: %d\n", data.intValue);

// Assign and display float value


data.floatValue = 15.5;
printf("Float Value: %.2f\n", data.floatValue);

// Assign and display char value


data.charValue = 'A';
printf("Char Value: %c\n", data.charValue);

// Show that only the last assigned member is valid


printf("\nAfter assigning char value:\n");
printf("Int Value: %d\n", data.intValue); // Undefined behavior
printf("Float Value: %.2f\n", data.floatValue); // Undefined behavior
printf("Char Value: %c\n", data.charValue);
getch(); //to display the output on command prompt
return 0;
}

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 37


S.G.T DEGREE COLLEGE, BALLARI.
OUTPUT:

Int Value: 10
Float Value: 15.50
Char Value: A

After assigning char value:


Int Value: 1667457870
Float Value: 0.00
Char Value: A

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 38


S.G.T DEGREE COLLEGE, BALLARI.
/* 19. Write a C program to show the memory usage difference between structures and
unions. */

#include <stdio.h>
#include <conio.h>
// Define a structure
struct Student
{
int rollNumber;
float marks;
char grade;
};

// Define a union
union StudentUnion
{
int rollNumber;
float marks;
char grade;
};

int main()
{
// Create instances of structure and union
struct Student student;
union StudentUnion studentUnion;
clrscr(); //to clear the previous output

// Display the size of structure and union


printf("Size of Structure: %lu bytes\n", sizeof(student));
printf("Size of Union: %lu bytes\n", sizeof(studentUnion));
getch(); //to display the output on command prompt
return 0;
}

OUTPUT:

Size of Structure: 12 bytes


Size of Union: 4 bytes

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 39


S.G.T DEGREE COLLEGE, BALLARI.
/* 20. Write a C Program to Create, Open, Read, Write, and Close a File, and Handle
Errors During File Operations. */

#include <stdio.h>
#include <conio.h>
int main()
{
FILE *file;
char filename[] = "example.txt"; // Name of the file
char writeData[] = "Hello, this is a test file.\n"; // Data to write into the file
char readData[100]; // Buffer to store the data read from the file
clrscr(); //to clear the previous output

// Part 1: Creating/Opening the file for writing


file = fopen(filename, "w");
if (file == NULL)
{
perror("Error opening file for writing");
return 1;
}

// Writing data to the file


if (fputs(writeData, file) == EOF)
{
perror("Error writing to file");
fclose(file);
return 1;
}
fclose(file); // Close the file after writing

// Part 2: Opening the file for reading


file = fopen(filename, "r");
if (file == NULL)
{
perror("Error opening file for reading");
return 1;
}

// Reading and displaying the content of the file


printf("Data read from the file:\n");
while (fgets(readData, sizeof(readData), file) != NULL)
{
printf("%s", readData);
}

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 40


S.G.T DEGREE COLLEGE, BALLARI.
// Checking for read error
if (ferror(file))
{
perror("Error reading from file");
}

fclose(file); // Close the file after reading


getch(); //to display the output on command prompt
return 0;
}

OUTPUT:

Data read from the file:


Hello, this is a test file.

Department of Computer Science, B.C.A 1st Sem ,C programming Lab Page 41

You might also like