C Programming Lab (23DS102)
1. To demonstrate the usage of operators and data types using C.
A-Size of different types of data.
#include <stdio.h> void
main()
{
// printing sizes of data types directly
printf("\ndirect sizes:\n");
printf("sizes of char: %zu bytes\n", sizeof(char));
printf("size of int: %zu bytes\n", sizeof(int));
printf("sixe of float: %zu bytes\n", sizeof(float));
printf("size of double: %zu bytes\n", sizeof(double));
}
Output :
B-Basic arithmetic operations on supplied data.
//working of arithmetic operators
#include<stdio.h>
int main() { int
num1,num2;
printf("Enter the first integer:");
scanf("%d",&num1);
printf("Enter the second integer:");
scanf("%d",&num2);
printf("sum:%d\n",num1+num2);
printf("difference:%d\n",num1-num2);
printf("product:%d\n",num1*num2);
if(num2!=0)
{
printf("Quotient:%f\n",(float)num1/num2);
} else
{
printf("cannot divide by zero.\n");
B.Sc. Data Science, R.L.S Institute Page 1
C Programming Lab (23DS102)
} if(num2!=0)
{
printf("Remainder:%d\n",num1%num2);
} else
{
printf("cannot calculate remainder when dividing by zero.\n");
} return
0;
}
Output :
1.
2.
B.Sc. Data Science, R.L.S Institute Page 2
C Programming Lab (23DS102)
2. To demonstrate the usage of if, if...else and switch…case. Use of
multiplechoice operations.
#include<stdio.h>
int main() {
int choice,n,a;
char ch;
printf("Menu\n");
printf("1. Display a greeting\n");
printf("2. Check even or odd\n");
printf("3. Check +ve or -ve\n");
printf("4. Determine Vowel orconsonant\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch (choice)
{
case 1:
printf("Hello! Welcome to the world of c programming\n");
break;
case 2:
printf("Enter a value: "); scanf("%d",&n);
if (n% 2 ==0)
{
printf("The number is even\n");
}else
{
printf("The number is odd\n");
}
break;
case 3:
printf("Enter the value: "); scanf("%d",&a);
if(a>=0)
{
printf("The value is +ve\n");
}
else
{
printf("The value is -ve\n");
B.Sc. Data Science, R.L.S Institute Page 3
C Programming Lab (23DS102)
}
break;
case 4:
printf("Enter a character: ");
scanf(" %c", &ch); //Added a space before %c to consume any whitespace
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||
ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
{
printf("The character is a vowel\n");
}
else {
printf("The character is a consonant\n");
}
break;
default:
printf("Invalid choice\n");
break;
}
return 0;
}
Output :
B.Sc. Data Science, R.L.S Institute Page 4
C Programming Lab (23DS102)
B.Sc. Data Science, R.L.S Institute Page 5
C Programming Lab (23DS102)
B.Sc. Data Science, R.L.S Institute Page 6
C Programming Lab (23DS102)
3. Demonstration of Switch Case in Calculator.
#include<stdio.h> int main() { char
op; int Num1,Num2; printf("Enter
an operator (+,-,*,/) :");
scanf("%c",&op);
printf("Enter two operands:");
scanf("%d%d",&Num1,&Num2);
switch(op)
{
case '+':
printf("%d+%d=%d",Num1,Num2,Num1+Num2);
break;
case '-':
printf("%d-%d=%d",Num1,Num2,Num1-Num2);
break; case '*':
printf("%d*%d=%d",Num1,Num2,Num1*Num2);
break;
case '/':
printf("%d/%d=%f",Num1,Num2,(float)Num1/Num2);
break;
default:
printf("Error! Invalid operator");
break;
} return
0;
}
Output :
1.
B.Sc. Data Science, R.L.S Institute Page 7
C Programming Lab (23DS102)
2.
3.
4.
5.
B.Sc. Data Science, R.L.S Institute Page 8
C Programming Lab (23DS102)
4. To demonstrate the concept of while, do-while, for loops, break and
continue.
#include<stdio.h>
int calculateFactorial(int num)
{
int result = 1;
for (int i=1;i<=num; i++)
{
result *= i;
}
return result;
}
int main() {
int user_value; printf("Enter a
positive integer: ");
scanf("%d",&user_value);
int factorial_while = 1,factorial_do_while = 1, factorial_for = 1,factorial_break =
1,factorial_continue = 1;
int counter =user_value;
while (counter> 0)
{
factorial_while *=counter--;
}
int do_while_counter =user_value; do{
factorial_do_while *= do_while_counter--;
}while (do_while_counter> 0);
for(int i= 1; i<= user_value; i++)
{
factorial_for *= i;
}
int break_counter = 1;
while (1)
{
factorial_break *= break_counter;
if(factorial_break> 50)
{
break;
}
break_counter++;
B.Sc. Data Science, R.L.S Institute Page 9
C Programming Lab (23DS102)
}
for (int i= 1; i<=user_value; i++
{
if(i % 2 == 0)
{
continue;
}
factorial_continue *=i;
}
printf("\nFactorial using while loop: %d\n", factorial_while);
printf("\nFactorial using do-while loop: %d\n", factorial_do_while);
printf("\nFactorial using for loop: %d\n", factorial_for);
printf("\nFactorial using break statement: %d\n", factorial_break);
printf("\nFactorial using continue statement: %d\n",
factorial_continue);
return 0;
Output:
B.Sc. Data Science, R.L.S Institute Page 10
C Programming Lab (23DS102)
5. To demonstrate the concept of an arrays
. - Storage and access of multiple values under one nameand doing aggregation operations.
#include<stdio.h>
int main()
{
int numbers[5]={10,20,30,40,50};
printf("Array elements: ");
for(int i=0; i<5; i++)
{
printf("%d ",numbers[i]);
}
printf("\n");
int sum=0;
for(int i=0; i<5; i++)
{
sum +=numbers[i];
}
double average =(double)sum/5;
printf("Sum of array elements:%d\n",sum);
printf("Average of array elements:%.2f\n",average);
return 0;
}
Output :
B.Sc. Data Science, R.L.S Institute Page 11
C Programming Lab (23DS102)
6. To demonstrate the concept of an arrays
A-Binary search in Array.
#include <stdio.h>
int binarySearch(int arr[], int low, int high, int key)
{
while (low <= high)
{
int mid = low + (high - low) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main()
{
int sortedArray[5];
int arraySize = sizeof(sortedArray) / sizeof(sortedArray[0]);
printf("Enter sorted integers (size 5):\n");
for (int i = 0; i < arraySize; i++)
{
printf("Enter integer #%d: ", i + 1);
scanf("%d", &sortedArray[i]);
}
int searchKey;
printf("Enter the key to search: ");
scanf("%d", &searchKey);
int searchResult = binarySearch(sortedArray, 0, arraySize - 1, searchKey);
if (searchResult != -1)
printf("Binary Search: %d found at index %d\n", searchKey, searchResult);
else
printf("Binary Search: %d not found in the array\n", searchKey); return 0;
}
B.Sc. Data Science, R.L.S Institute Page 12
C Programming Lab (23DS102)
Output :
1.
2.
B.Sc. Data Science, R.L.S Institute Page 13
C Programming Lab (23DS102)
B- Bubble Sorting the data in an array.
#include <stdio.h>
void bubbleSort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int unsortedArray[5];
int unsortedSize = sizeof(unsortedArray) / sizeof(unsortedArray[0]);
printf("Enter integers (size 5) for Bubble Sort:\n");
for (int i = 0; i < unsortedSize; i++)
{
printf("Enter integer #%d: ", i + 1);
scanf("%d", &unsortedArray[i]);
}
printf("Unsorted Array: ");
printArray(unsortedArray, unsortedSize);
bubbleSort(unsortedArray, unsortedSize);
printf("Array after Bubble Sort: ");
printArray(unsortedArray, unsortedSize);
return 0;
B.Sc. Data Science, R.L.S Institute Page 14
C Programming Lab (23DS102)
Output :
B.Sc. Data Science, R.L.S Institute Page 15
C Programming Lab (23DS102)
7. Perform Matrix Multiplication using Array.
#include<stdio.h>
int main() {
int a[10] [10],b[10] [10],mul[10] [10],r,c,i,j,k;
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++) {
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++) {
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++) {
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
B.Sc. Data Science, R.L.S Institute Page 16
C Programming Lab (23DS102)
}
printf("\n");
} return
0;
}
Output :
B.Sc. Data Science, R.L.S Institute Page 17
C Programming Lab (23DS102)
8. To demonstrate the concept of strings.
#include <stdio.h>
#include <string.h>
int main() {
char str1[50], str2[50];
printf("Enter string 1: ");
gets(str1);
printf("Enter string 2: ");
gets(str2);
strcpy(str1, str2);
printf("\nAfterstrcpy(), strl: %s, str2: %s\n", str1, str2);
strrev(str1);
printf("After strrey(), str1 reversed: %s\n", str1);
printf("Length of str2: %d\n", strlen(str2));
if (strcmp(str1, str2) == 0)
printf("str1 and str2 are equal. \n");
else
printf("str1 and str2 are equal.\n");
printf("str1 and str2 are not equal.\n");
strcat(str1, str2);
printf("After strcat(), str1: %s\n", str1);
return 0;
}
Output :
B.Sc. Data Science, R.L.S Institute Page 18
C Programming Lab (23DS102)
9. To demonstrate the usage of functions and recursion.
-To find the Fibonacci Series of n numbers.
-To find the Factorial of n numbers.
#include <stdio.h> int
fibonacci(int n)
{ if (n <= 1) return n; return
fibonacci(n - 1) + fibonacci(n - 2);
}
int factorial(int n)
{
if (n == 0 || n == 1)
return 1; return n *
factorial(n - 1);
} int main() {
int num; printf("Enter a number: ");
scanf("%d", &num);
printf("\nFibonacci Series up to %d numbers:\n", num);
for (int i = 0; i < num; ++i)
printf("%d ", fibonacci(i));
printf("\n\nFactorial of %d: %d\n", num, factorial(num));
return 0;
}
Output :
B.Sc. Data Science, R.L.S Institute Page 19
C Programming Lab (23DS102)
10. To demonstrate the concept of unions.
-Storage of different types of data - Student Database.
#include <stdio.h>
union StudentData
{
int studentID;
int numCourses;
char course[50];
};
struct Student
{
char name[50];
int age;
union StudentData data;
};
int main()
{
struct Student students[3];
for (int i = 0; i< 3; i++)
{
printf("Enter name for student %d: ", i + 1);
scanf("%s", students[i].name);
printf("Enter age for student %d: ", i + 1);
scanf("%d", &students[i].age);
printf("Enter student ID for student %d: ", i + 1);
scanf("%d", &students[i].data.studentID);
printf("\n");
}
printf("Student Information:\n");
for (int i = 0; i< 3; i++)
{
printf("Name: %s\n", students[i].name);
printf("Age: %d\n", students[i].age);
printf("Student ID: %d\n", students[i].data.studentID);
printf("\n");
}
B.Sc. Data Science, R.L.S Institute Page 20
C Programming Lab (23DS102)
return 0;
}
Output :
B.Sc. Data Science, R.L.S Institute Page 21
C Programming Lab (23DS102)
11. To demonstrate the concept of pointers- Swapping of two number.
#include <stdio.h>
void swapNumbers(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("\nOriginal Numbers:\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);
swapNumbers(&num1, &num2);
printf("\nAfter Swapping:\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);
return 0;
}
Output:
B.Sc. Data Science, R.L.S Institute Page 22
C Programming Lab (23DS102)
12. To demonstrate the concept of File. (CRUD operations on respective
database)
- Perform Various file Operations.
#include <stdio.h>
void createFile()
{
FILE *file = fopen("sample.txt", "w");
if (file != NULL)
{
fprintf(file, "Hello, this is a sample file.");
fclose(file);
printf("File created successfully!\n");
}
else
{
printf("Error in creating the file.\n");
}
}
void readFile()
{
FILE *file = fopen("sample.txt", "r");
char buffer[100];
if (file != NULL)
{
while (fgets(buffer, sizeof(buffer), file) != NULL)
{
printf("%s", buffer);
}
fclose(file);
}
else {
printf("Error in reading the file.\n");
}
}
void updateFile()
{
FILE *file = fopen("sample.txt", "a");
B.Sc. Data Science, R.L.S Institute Page 23
C Programming Lab (23DS102)
if (file != NULL)
{
fprintf(file, "\nUpdated content.");
fclose(file);
printf("File updated successfully!\n");
}
else {
printf("Error in updating the file.\n");
}
}
void deleteFile()
{
if (remove("sample.txt") == 0)
{
printf("File deleted successfully!\n");
}
else {
printf("Error in deleting the file.\n");
}
} int
main() {
createFile();
printf("\nFile Contents:\n");
readFile(); updateFile();
printf("\nUpdated File Contents:\n");
readFile();
deleteFile(); return
0;
}
Output :
B.Sc. Data Science, R.L.S Institute Page 24
C Programming Lab (23DS102)
B.Sc. Data Science, R.L.S Institute Page 25