C PROGRAMMING
Laboratory Programs
Aim | Algorithm | Program | Output & Result
Programs 1 – 21
Program 1: Swap Two Variables Using a Third (Temporary) Variable
AIM
To write a C program to swap two variables by using a third (temporary) variable.
ALGORITHM
• Step 1: Start
• Step 2: Declare three variables a, b, and temp.
• Step 3: Read the values of a and b from the user.
• Step 4: Assign temp = a.
• Step 5: Assign a = b.
• Step 6: Assign b = temp.
• Step 7: Print the swapped values of a and b.
• Step 8: Stop
PROGRAM (C Code)
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter value of a: ");
scanf("%d", &a);
printf("Enter value of b: ");
scanf("%d", &b);
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
OUTPUT
Enter value of a: 10
Enter value of b: 20
After swapping: a = 20, b = 10
RESULT
The program successfully swaps two variables using a temporary variable.
Program 2: Evaluate an Arithmetic Expression
AIM
To write a C program to evaluate the value of a given arithmetic expression (a + b) * c / d with user
input.
ALGORITHM
• Step 1: Start
• Step 2: Declare variables a, b, c, d, and result as float.
• Step 3: Read values of a, b, c, and d from the user.
• Step 4: Compute result = (a + b) * c / d.
• Step 5: Print the result.
• Step 6: Stop
PROGRAM (C Code)
#include <stdio.h>
int main() {
float a, b, c, d, result;
printf("Enter values of a, b, c, d: ");
scanf("%f %f %f %f", &a, &b, &c, &d);
result = (a + b) * c / d;
printf("Result of (a+b)*c/d = %.2f\n", result);
return 0;
}
OUTPUT
Enter values of a, b, c, d: 2 3 4 5
Result of (a+b)*c/d = 4.00
RESULT
The program correctly evaluates the arithmetic expression using user-provided inputs.
Program 3: Print Numbers 1 to 10 Using goto Statement
AIM
To write a C program that prints a series of numbers from 1 to 10 using a goto statement.
ALGORITHM
• Step 1: Start
• Step 2: Declare and initialize variable i = 1.
• Step 3: Label START: Print the value of i.
• Step 4: Increment i by 1.
• Step 5: If i <= 10, goto START.
• Step 6: Stop
PROGRAM (C Code)
#include <stdio.h>
int main() {
int i = 1;
START:
printf("%d\n", i);
i++;
if (i <= 10)
goto START;
return 0;
}
OUTPUT
1
2
3
4
5
6
7
8
9
10
RESULT
The program prints numbers 1 to 10 using the goto statement.
Program 4: Arithmetic Operations Using Switch Case
AIM
To write a C program that accepts two numbers and a code (1-4) and performs arithmetic operations
using switch case.
ALGORITHM
• Step 1: Start
• Step 2: Read two numbers a and b, and a code from the user.
• Step 3: Use switch(code):
• Case 1: Print a + b
• Case 2: Print a - b
• Case 3: Print a * b
• Case 4: Print a / b (check division by zero)
• Default: Print invalid choice
• Step 4: Stop
PROGRAM (C Code)
#include <stdio.h>
int main() {
float a, b;
int code;
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);
printf("Enter code (1-Add 2-Sub 3-Mul 4-Div): ");
scanf("%d", &code);
switch(code) {
case 1: printf("Sum = %.2f\n", a + b); break;
case 2: printf("Difference = %.2f\n", a - b); break;
case 3: printf("Product = %.2f\n", a * b); break;
case 4:
if(b != 0) printf("Division = %.2f\n", a / b);
else printf("Division by zero!\n");
break;
default: printf("Invalid choice\n");
}
return 0;
}
OUTPUT
Enter two numbers: 10 5
Enter code (1-Add 2-Sub 3-Mul 4-Div): 1
Sum = 15.00
RESULT
The program performs the correct arithmetic operation based on the user's code input.
Program 5: Right-Angled Triangle Pattern
AIM
To write a C program to print a right-angled triangle pattern with n rows.
ALGORITHM
• Step 1: Start
• Step 2: Read the value of n from the user.
• Step 3: Use outer loop i from 1 to n.
• Step 4: Use inner loop j from 1 to i, print '*' followed by space.
• Step 5: After inner loop, print newline.
• Step 6: Repeat until i > n.
• Step 7: Stop
PROGRAM (C Code)
#include <stdio.h>
int main() {
int n, i, j;
printf("Enter number of rows: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
for(j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
return 0;
}
OUTPUT
Enter number of rows: 5
*
* *
* * *
* * * *
* * * * *
RESULT
The program prints the right-angled triangle star pattern for the given number of rows.
Program 6: Check Armstrong Number
AIM
To write a C program to check whether a given number is an Armstrong number.
ALGORITHM
• Step 1: Start
• Step 2: Read the number n from the user.
• Step 3: Count the number of digits in n, store in 'digits'.
• Step 4: Store n in a temporary variable temp.
• Step 5: Extract each digit, raise it to the power 'digits', sum them.
• Step 6: If sum == n, print 'Armstrong Number', else print 'Not Armstrong'.
• Step 7: Stop
PROGRAM (C Code)
#include <stdio.h>
#include <math.h>
int main() {
int n, temp, digits = 0, sum = 0, rem;
printf("Enter a number: ");
scanf("%d", &n);
temp = n;
while(temp != 0) { digits++; temp /= 10; }
temp = n;
while(temp != 0) {
rem = temp % 10;
sum += (int)pow(rem, digits);
temp /= 10;
}
if(sum == n)
printf("%d is an Armstrong number.\n", n);
else
printf("%d is NOT an Armstrong number.\n", n);
return 0;
}
OUTPUT
Enter a number: 153
153 is an Armstrong number.
Enter a number: 100
100 is NOT an Armstrong number.
RESULT
The program correctly identifies whether the given number is an Armstrong number.
Program 7: Matrix Multiplication
AIM
To write a C program to perform multiplication of two matrices and display the resulting matrix.
ALGORITHM
• Step 1: Start
• Step 2: Read dimensions of matrices A (m x n) and B (n x p).
• Step 3: Read elements of both matrices.
• Step 4: Initialize result matrix C to 0.
• Step 5: Use three nested loops: for i, for j, for k — compute C[i][j] += A[i][k] * B[k][j].
• Step 6: Display the resultant matrix C.
• Step 7: Stop
PROGRAM (C Code)
#include <stdio.h>
int main() {
int m, n, p, i, j, k;
printf("Enter rows and cols of A (m n): "); scanf("%d %d", &m, &n);
printf("Enter cols of B (p): "); scanf("%d", &p);
int A[m][n], B[n][p], C[m][p];
printf("Enter matrix A:\n");
for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&A[i][j]);
printf("Enter matrix B:\n");
for(i=0;i<n;i++) for(j=0;j<p;j++) scanf("%d",&B[i][j]);
for(i=0;i<m;i++) for(j=0;j<p;j++) C[i][j]=0;
for(i=0;i<m;i++)
for(j=0;j<p;j++)
for(k=0;k<n;k++)
C[i][j] += A[i][k]*B[k][j];
printf("Resultant Matrix:\n");
for(i=0;i<m;i++){for(j=0;j<p;j++) printf("%d ",C[i][j]); printf("\n");}
return 0;
}
OUTPUT
Enter rows and cols of A (m n): 2 2
Enter cols of B (p): 2
Enter matrix A: 1 2 3 4
Enter matrix B: 5 6 7 8
Resultant Matrix:
19 22
43 50
RESULT
The program correctly multiplies two matrices and displays the result.
Program 8: Largest Element in Each Row of a 2D Array
AIM
To write a C program to find and print the largest element in each row of a 2D array.
ALGORITHM
• Step 1: Start
• Step 2: Read dimensions m (rows) and n (columns).
• Step 3: Read all elements of the 2D array.
• Step 4: For each row i, initialize max = arr[i][0].
• Step 5: Compare each element in the row; update max if element > max.
• Step 6: Print the max for each row.
• Step 7: Stop
PROGRAM (C Code)
#include <stdio.h>
int main() {
int m, n, i, j, max;
printf("Enter rows and columns: ");
scanf("%d %d", &m, &n);
int arr[m][n];
printf("Enter elements:\n");
for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&arr[i][j]);
for(i=0;i<m;i++) {
max = arr[i][0];
for(j=1;j<n;j++)
if(arr[i][j] > max) max = arr[i][j];
printf("Largest in row %d: %d\n", i+1, max);
}
return 0;
}
OUTPUT
Enter rows and columns: 3 3
Enter elements: 1 5 3 8 2 6 4 9 7
Largest in row 1: 5
Largest in row 2: 8
Largest in row 3: 9
RESULT
The program correctly identifies and prints the largest element in each row.
Program 9: Reverse a String Without Library Functions
AIM
To write a C program to reverse a given string without using library functions (strrev).
ALGORITHM
• Step 1: Start
• Step 2: Read the string from the user.
• Step 3: Find the length of the string by counting until '\0'.
• Step 4: Use two pointers — start (0) and end (length-1).
• Step 5: Swap characters at start and end, increment start, decrement end.
• Step 6: Repeat until start >= end.
• Step 7: Print the reversed string.
• Step 8: Stop
PROGRAM (C Code)
#include <stdio.h>
int main() {
char str[100];
int i, len = 0;
char temp;
printf("Enter a string: ");
scanf("%s", str);
while(str[len] != '\0') len++;
for(i = 0; i < len / 2; i++) {
temp = str[i];
str[i] = str[len - 1 - i];
str[len - 1 - i] = temp;
}
printf("Reversed string: %s\n", str);
return 0;
}
OUTPUT
Enter a string: hello
Reversed string: olleh
RESULT
The program successfully reverses a string without using any string library functions.
Program 10: Count Characters in a String Without Library Functions
AIM
To write a C program to count the characters in a given string without using string library functions.
ALGORITHM
• Step 1: Start
• Step 2: Read the string from the user.
• Step 3: Initialize count = 0.
• Step 4: Traverse each character; increment count until '\0' is reached.
• Step 5: Print the count.
• Step 6: Stop
PROGRAM (C Code)
#include <stdio.h>
int main() {
char str[100];
int count = 0;
printf("Enter a string: ");
scanf("%[^\n]", str);
while(str[count] != '\0')
count++;
printf("Number of characters: %d\n", count);
return 0;
}
OUTPUT
Enter a string: Hello World
Number of characters: 11
RESULT
The program counts and displays the number of characters in the string without using
strlen().
Program 11: Replace Character at a Given Position in a String
AIM
To write a C program to replace a character of a given string at a given position.
ALGORITHM
• Step 1: Start
• Step 2: Read the string from the user.
• Step 3: Read the position (pos) and new character (ch) from the user.
• Step 4: Replace str[pos] = ch.
• Step 5: Print the modified string.
• Step 6: Stop
PROGRAM (C Code)
#include <stdio.h>
int main() {
char str[100], ch;
int pos;
printf("Enter a string: ");
scanf("%s", str);
printf("Enter position to replace (0-indexed): ");
scanf("%d", &pos);
printf("Enter new character: ");
scanf(" %c", &ch);
str[pos] = ch;
printf("Modified string: %s\n", str);
return 0;
}
OUTPUT
Enter a string: Hello
Enter position to replace (0-indexed): 0
Enter new character: J
Modified string: Jello
RESULT
The program successfully replaces the character at the specified position in the string.
Program 12: Swap Two Variables Using Call by Value
AIM
To write a C program to swap two variables using the call by value method.
ALGORITHM
• Step 1: Start
• Step 2: Define a function swap(int a, int b).
• Step 3: Inside the function, use a temp variable to swap a and b and print them.
• Step 4: In main(), read two variables x and y.
• Step 5: Call swap(x, y).
• Step 6: Print x and y in main (unchanged — call by value).
• Step 7: Stop
PROGRAM (C Code)
#include <stdio.h>
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
printf("Inside function: a = %d, b = %d\n", a, b);
}
int main() {
int x, y;
printf("Enter x and y: ");
scanf("%d %d", &x, &y);
swap(x, y);
printf("In main: x = %d, y = %d (unchanged)\n", x, y);
return 0;
}
OUTPUT
Enter x and y: 5 10
Inside function: a = 10, b = 5
In main: x = 5, y = 10 (unchanged)
RESULT
The program demonstrates call by value — changes inside the function do not affect the
original variables.
Program 13: Calculator Using User Defined Functions
AIM
To write a C program to implement a calculator using user defined functions.
ALGORITHM
• Step 1: Start
• Step 2: Define functions add(), subtract(), multiply(), divide().
• Step 3: Read two numbers and an operator from the user.
• Step 4: Based on operator, call the appropriate function.
• Step 5: Print the result.
• Step 6: Stop
PROGRAM (C Code)
#include <stdio.h>
float add(float a, float b) { return a + b; }
float subtract(float a, float b) { return a - b; }
float multiply(float a, float b) { return a * b; }
float divide(float a, float b) { return (b != 0) ? a / b : 0; }
int main() {
float a, b;
char op;
printf("Enter expression (e.g. 10 + 5): ");
scanf("%f %c %f", &a, &op, &b);
switch(op) {
case '+': printf("Result = %.2f\n", add(a, b)); break;
case '-': printf("Result = %.2f\n", subtract(a, b)); break;
case '*': printf("Result = %.2f\n", multiply(a, b)); break;
case '/': printf("Result = %.2f\n", divide(a, b)); break;
default: printf("Invalid operator\n");
}
return 0;
}
OUTPUT
Enter expression (e.g. 10 + 5): 15 * 4
Result = 60.00
RESULT
The calculator program correctly performs arithmetic using user-defined functions.
Program 14: GCD of Two Numbers Using Recursion
AIM
To write a C program to find the GCD of two numbers using recursion.
ALGORITHM
• Step 1: Start
• Step 2: Define recursive function gcd(int a, int b).
• Step 3: Base case: if b == 0, return a.
• Step 4: Recursive case: return gcd(b, a % b).
• Step 5: In main(), read two numbers a and b.
• Step 6: Call gcd(a, b) and print the result.
• Step 7: Stop
PROGRAM (C Code)
#include <stdio.h>
int gcd(int a, int b) {
if(b == 0) return a;
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 = %d\n", a, b, gcd(a, b));
return 0;
}
OUTPUT
Enter two numbers: 48 18
GCD of 48 and 18 = 6
RESULT
The program successfully computes the GCD using recursive Euclidean algorithm.
Program 15: Fibonacci Series Using Recursion
AIM
To write a C program that prints Fibonacci series of N numbers using recursion.
ALGORITHM
• Step 1: Start
• Step 2: Define recursive function fib(int n).
• Step 3: Base cases: fib(0) = 0, fib(1) = 1.
• Step 4: Recursive case: fib(n) = fib(n-1) + fib(n-2).
• Step 5: In main(), read N from the user.
• Step 6: Loop from 0 to N-1 and print fib(i) each iteration.
• Step 7: Stop
PROGRAM (C Code)
#include <stdio.h>
int fib(int n) {
if(n == 0) return 0;
if(n == 1) return 1;
return fib(n-1) + fib(n-2);
}
int main() {
int n, i;
printf("Enter N: ");
scanf("%d", &n);
printf("Fibonacci series: ");
for(i = 0; i < n; i++)
printf("%d ", fib(i));
printf("\n");
return 0;
}
OUTPUT
Enter N: 8
Fibonacci series: 0 1 1 2 3 5 8 13
RESULT
The program prints the Fibonacci series up to N terms using recursion.
Program 16: Search Element in Array Using Pointers
AIM
To write a C program to search for a specific element in an array using pointers.
ALGORITHM
• Step 1: Start
• Step 2: Read array size n and elements.
• Step 3: Read the element to search.
• Step 4: Use a pointer ptr pointing to the first array element.
• Step 5: Traverse the array using the pointer.
• Step 6: If *ptr == key, print the index and stop.
• Step 7: If not found after full traversal, print 'Element not found'.
• Step 8: Stop
PROGRAM (C Code)
#include <stdio.h>
int main() {
int n, key, i;
printf("Enter size of array: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for(i = 0; i < n; i++) scanf("%d", &arr[i]);
printf("Enter element to search: ");
scanf("%d", &key);
int *ptr = arr;
int found = 0;
for(i = 0; i < n; i++, ptr++) {
if(*ptr == key) {
printf("Element found at index %d\n", i);
found = 1; break;
}
}
if(!found) printf("Element not found\n");
return 0;
}
OUTPUT
Enter size of array: 5
Enter elements: 10 25 38 47 56
Enter element to search: 38
Element found at index 2
RESULT
The program successfully searches for an element in the array using pointer traversal.
Program 17: Copy String Using Pointers Without strcpy()
AIM
To write a C program to copy one string into another using pointers without using the built-in strcpy()
function.
ALGORITHM
• Step 1: Start
• Step 2: Read the source string.
• Step 3: Declare a destination string.
• Step 4: Use two pointers src and dest pointing to the respective strings.
• Step 5: Copy each character: *dest = *src, increment both pointers.
• Step 6: Repeat until *src == '\0'.
• Step 7: Null-terminate the destination string.
• Step 8: Print the copied string.
• Step 9: Stop
PROGRAM (C Code)
#include <stdio.h>
int main() {
char src[100], dest[100];
char *s = src, *d = dest;
printf("Enter source string: ");
scanf("%s", src);
while(*s != '\0') {
*d = *s;
s++; d++;
}
*d = '\0';
printf("Copied string: %s\n", dest);
return 0;
}
OUTPUT
Enter source string: programming
Copied string: programming
RESULT
The program copies a string using pointer manipulation without strcpy().
Program 18: Store Address of Another Variable Using Pointer
AIM
To write a C program to store the address of another variable using a pointer.
ALGORITHM
• Step 1: Start
• Step 2: Declare an integer variable num and a pointer ptr.
• Step 3: Read a value into num.
• Step 4: Store the address of num in ptr: ptr = &num.
• Step 5: Print the value of num, address of num, and value via pointer *ptr.
• Step 6: Stop
PROGRAM (C Code)
#include <stdio.h>
int main() {
int num;
int *ptr;
printf("Enter a number: ");
scanf("%d", &num);
ptr = #
printf("Value of num : %d\n", num);
printf("Address of num : %p\n", (void*)&num);
printf("ptr holds address : %p\n", (void*)ptr);
printf("Value via pointer : %d\n", *ptr);
return 0;
}
OUTPUT
Enter a number: 42
Value of num : 42
Address of num : 0x7ffd12a3b4c8
ptr holds address : 0x7ffd12a3b4c8
Value via pointer : 42
RESULT
The program demonstrates pointer usage by storing and accessing the address of a variable.
Program 19: Student Database System Using Structures
AIM
To write a C program to build a student database system using structures with average marks
calculation.
ALGORITHM
• Step 1: Start
• Step 2: Define a structure Student with name, rollno, and marks[3].
• Step 3: Define function calcAverage(struct Student s) that returns the average of three subjects.
• Step 4: In main(), read number of students.
• Step 5: For each student, read name, roll number, and marks.
• Step 6: Call calcAverage() and display student details with average.
• Step 7: Stop
PROGRAM (C Code)
#include <stdio.h>
struct Student {
char name[50];
int rollno;
float marks[3];
};
float calcAverage(struct Student s) {
return ([Link][0] + [Link][1] + [Link][2]) / 3.0;
}
int main() {
int n, i;
printf("Enter number of students: ");
scanf("%d", &n);
struct Student stu[n];
for(i = 0; i < n; i++) {
printf("Enter name: "); scanf("%s", stu[i].name);
printf("Enter roll number: "); scanf("%d", &stu[i].rollno);
printf("Enter marks (Math Physics Chemistry): ");
scanf("%f %f %f",&stu[i].marks[0],&stu[i].marks[1],&stu[i].marks[2]);
}
printf("\n%-15s %-8s %-10s\n","Name","Roll No","Average");
for(i = 0; i < n; i++)
printf("%-15s %-8d %.2f\n",stu[i].name,stu[i].rollno,calcAverage(stu[i]));
return 0;
}
OUTPUT
Enter number of students: 2
Enter name: Alice Roll: 101 Marks: 85 90 78
Enter name: Bob Roll: 102 Marks: 70 65 80
Name Roll No Average
Alice 101 84.33
Bob 102 71.67
RESULT
The student database system correctly stores and displays student details with computed
averages.
Program 20: Read and Write String to Binary File Using fwrite() and
fread()
AIM
To write a C program to write a string to a binary file using fwrite() and read it back using fread().
ALGORITHM
• Step 1: Start
• Step 2: Read a string from the user.
• Step 3: Open a binary file in write mode ("wb").
• Step 4: Use fwrite() to write the string to the file.
• Step 5: Close the file.
• Step 6: Open the same file in read mode ("rb").
• Step 7: Use fread() to read the content back.
• Step 8: Print the read content.
• Step 9: Close the file and stop.
PROGRAM (C Code)
#include <stdio.h>
#include <string.h>
int main() {
char str[100], buffer[100];
FILE *fp;
printf("Enter a string: ");
scanf("%[^\n]", str);
fp = fopen("[Link]", "wb");
fwrite(str, sizeof(char), strlen(str), fp);
fclose(fp);
fp = fopen("[Link]", "rb");
int n = fread(buffer, sizeof(char), 100, fp);
buffer[n] = '\0';
fclose(fp);
printf("Read from file: %s\n", buffer);
return 0;
}
OUTPUT
Enter a string: Hello Binary World
Read from file: Hello Binary World
RESULT
The program successfully writes a string to a binary file and reads it back correctly.
Program 21: Move File Pointer Using fseek() and Read from Specific
Position
AIM
To write a C program to move the file pointer to a specific location within a file using fseek() and read
content from that position.
ALGORITHM
• Step 1: Start
• Step 2: Open a file in write mode and write some content to it.
• Step 3: Close the file.
• Step 4: Open the file in read mode.
• Step 5: Use fseek(fp, offset, SEEK_SET) to move pointer to the desired position.
• Step 6: Read characters from that position using fgetc() and print them.
• Step 7: Close the file.
• Step 8: Stop
PROGRAM (C Code)
#include <stdio.h>
int main() {
FILE *fp;
int offset;
char ch;
fp = fopen("[Link]", "w");
fprintf(fp, "Hello, File Handling in C!");
fclose(fp);
fp = fopen("[Link]", "r");
printf("Enter offset to seek: ");
scanf("%d", &offset);
fseek(fp, offset, SEEK_SET);
printf("Content from position %d: ", offset);
while((ch = fgetc(fp)) != EOF)
printf("%c", ch);
printf("\n");
fclose(fp);
return 0;
}
OUTPUT
Enter offset to seek: 7
Content from position 7: File Handling in C!
RESULT
The program successfully moves the file pointer using fseek() and reads content from the
specified position.