Programming in C Manual - New.docx
Programming in C Manual - New.docx
Sub.Code : CS3271
Sub.Name : Programming in C Laboratory
Regulation : R-2021
COURSE OUTCOMES:
CO1: Demonstrate knowledge on C programming
constructs.
CO2: Develop programs in C using basic constructs.
CO3: Develop programs in C using arrays.
CO4: Develop applications in C using strings, pointers, functions.
CO5: Develop applications in C using structures.
CO6: Develop applications in C using file processing.
LIST OF EXPERIMENTS
7 Implementation of Recursion
1 Experiment 1 X
2 Experiment 2
3 Experiment3
4 Experiment 4
5 Experiment 5
6 Experiment 6
7 Experiment 7
8 Experiment 8
9 Experiment 9
10 Experiment 10
Exp.No:1 Practice of C programs using I/O statements, operators, expressions
Date:
Program 1:
Aim:
To calculate and display the area and circumference of a circle using the radius.
Algorithm:
1. Start
2. Input radius
3. Calculate area = π × r × r
4. Calculate circumference = 2 × π × r
5. Display area and circumference
6. Stop
Program:
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, area, circumference;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = PI * radius * radius;
circumference = 2 * PI * radius;
printf("Area = %.2f\n", area);
printf("Circumference = %.2f\n", circumference);
return 0;
}
Output:
Program 2: Swap values of two variables (with and without third variable)
Aim:
To swap two numbers using and without using a third variable.
1. Input a, b
2. a=a+b
3. b=a-b
4. a=a-b
Program:
#include <stdio.h>
int main() {
int a, b, temp;
// With third variable
printf("Enter two integers (with temp): ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
// Without third variable
printf("Enter two integers (without temp): ");
scanf("%d %d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Output:
pgsql
CopyEdit
Enter two integers (with temp): 3 7
After swapping: a = 7, b = 3
Enter two integers (without temp): 4 9
After swapping: a = 9, b = 4
Aim:
To calculate and display the volume of a cube.
Algorithm:
1. Start
2. Input side of the cube
3. Calculate volume = side³
4. Display volume
5. Stop
Program:
#include <stdio.h>
int main() {
float side, volume;
printf("Enter the side of the cube: ");
scanf("%f", &side);
volume = side * side * side;
printf("Volume of cube = %.2f\n", volume);
return 0;
}
Output:
Program 4: Input 3 integer values and print in forward and reversed order
Aim:
To input 3 integers and display them in forward and reversed order.
Algorithm:
Program:
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter 3 integers: ");
scanf("%d %d %d", &a, &b, &c);
printf("Forward order: %d %d %d\n", a, b, c);
printf("Reversed order: %d %d %d\n", c, b, a);
return 0;
}
Output:
Enter 3 integers: 10 20 30
Forward order: 10 20 30
Reversed order: 30 20 10
Program 5: Check whether the year is a leap year or not
Aim:
To check whether a given year is a leap year.
Algorithm:
Program:
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
printf("%d is a Leap Year.\n", year);
else
printf("%d is not a Leap Year.\n", year);
return 0;
}
Output:
Result:
Thus the Practice of C Program using I/O statements,Operators and expressions has been
successfully executed.
Exp.No:2 Practice of C Program using decision-making constructs: if-else, goto,
switch-case, break-continue
Date:
Aim:
To write a C program to find the sum of the first and last digit of a four-digit number.
Algorithm:
1. Start.
2. Read a four-digit number.
3. Extract the last digit using % 10.
4. Extract the first digit by dividing the number by 1000.
5. Add the first and last digits.
6. Display the result.
7. Stop.
Program:
#include <stdio.h>
int main() {
int num, first, last, sum;
Output:
Aim:
To write a C program to check whether a number is an Armstrong number.
Algorithm:
1. Start.
2. Read the number.
3. Count digits.
4. Extract each digit, raise it to the power of number of digits, and sum.
5. If sum equals original number, it's an Armstrong number.
6. Stop.
Program:
#include <stdio.h>
#include <math.h>
int main() {
int num, original, remainder, result = 0, n = 0;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
return 0;
}
Output:
switch (choice) {
case 1:
printf("Result: %d\n", num1 + num2);
break;
case 2:
printf("Result: %d\n", num1 - num2);
break;
case 3:
printf("Result: %d\n", num1 * num2);
break;
case 4:
if (num2 != 0)
printf("Result: %.2f\n", (float)num1 / num2);
else
printf("Error: Division by zero.\n");
break;
case 5:
printf("Square of %d is: %.2f\n", num1, pow(num1, 2));
break;
}
return 0;
}
Output
Choose operation:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Square of a number
Enter your choice (1-5): 1
Enter first number: 10
Enter second number: 5
Result: 15
Result:
Thus the Practice of C Program using decision making constructs if else,goto,switch
case,break-continue are successfully executed.
Exp.No:3 Practice of C Program using Loops: for, while, do-while
Date:
Aim:
To write a C program to display a star pattern using nested loops.
Algorithm:
1. Start.
2. Read the number of rows.
3. Use outer loop for rows.
4. Use inner loop to print * in each row.
5. Print newline after each row.
6. Stop
Program:
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output:
Aim:
To write a C program to reverse the digits of an integer.
Algorithm:
1. Start.
2. Read the number.
3. Initialize reverse = 0.
4. Loop while number ≠ 0:
o Extract digit using % 10
o Multiply reverse by 10 and add digit
o Divide number by 10
5. Print the reversed number.
6. Stop.
Program:
#include <stdio.h>
int main() {
int num, reverse = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
while (num != 0) {
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Reversed number: %d\n", reverse);
return 0;
}
Output:
Enter an integer: 1234
Reversed number: 4321
Aim:
To write a C program to generate the first n terms of the Fibonacci series.
Algorithm:
1. Start.
2. Read the value of n.
3. Initialize a = 0, b = 1.
4. Print a and b.
5. Loop from 3 to n:
o Calculate next = a + b
o Print next
o Update a = b, b = next
6. Stop.
Program:
#include <stdio.h>
int main() {
int n, i;
int a = 0, b = 1, next;
printf("Enter number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; i++) {
printf("%d ", a);
next = a + b;
a = b;
b = next;
}
printf("\n");
return 0;
}
Output:
Enter number of terms: 7
Fibonacci Series: 0 1 1 2 3 5 8
Result:
Thus the Practice of C Program using Loops :for ,While ,Do while are successfully executed.
Exp.No:4
Algorithm:
1. Start.
2. Declare 2×2 matrices A, B, and Sum.
3. Input elements of matrix A.
4. Input elements of matrix B.
5. Add corresponding elements: Sum[i][j] = A[i][j] + B[i][j].
6. Display the result matrix.
7. Stop.
Program:
#include <stdio.h>
int main() {
int A[2][2], B[2][2], Sum[2][2];
int i, j;
// Matrix Addition
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
Sum[i][j] = A[i][j] + B[i][j];
// Output Result
printf("Sum of matrices:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++)
printf("%d ", Sum[i][j]);
printf("\n");
}
return 0;
}
Output:
Enter elements of first 2x2 matrix:
12
34
Enter elements of second 2x2 matrix:
56
78
Sum of matrices:
68
10 12
Algorithm:
1. Start.
2. Declare matrices A, B, and Product.
3. Input elements of matrices A and B.
4. Use three nested loops:
o Outer two loops for i and j (position in result matrix),
o Inner loop k for calculating the sum of A[i][k] * B[k][j].
5. Store the result in Product[i][j].
6. Display the result matrix.
7. Stop.
Program:
#include <stdio.h>
int main() {
int A[3][3], B[3][3], Product[3][3];
int i, j, k;
// Output Result
printf("Product of matrices:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
printf("%d ", Product[i][j]);
printf("\n");
}
return 0;
}
Output:
Enter elements of first 3x3 matrix:
123
456
789
Enter elements of second 3x3 matrix:
987
654
321
Product of matrices:
30 24 18
84 69 54
138 114 90
Result:
Thus the Implementation of Arrays are successfully executed.
Exp.No:5
Algorithm:
1. Start.
2. Read a string from the user.
3. Initialize a counter to 0.
4. Traverse the string character by character.
5. If the character is a vowel (a, e, i, o, u, case-insensitive), increment the counter.
6. Display the total count.
7. Stop.
Program:
#include <stdio.h>
int main() {
char str[100];
int i, count = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++) {
char ch = str[i];
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
count++;
}
}
To write a C program to check if a string is a palindrome (reads the same backward) without
using string functions.
Algorithm:
1. Start.
2. Read a string.
3. Find the length of the string manually.
4. Compare characters from start and end moving toward the center.
5. If all match, it's a palindrome; else, not.
6. Display result.
7. Stop.
Program:
#include <stdio.h>
int main() {
char str[100];
int i, len = 0, flag = 1;
printf("Enter a string: ");
gets(str);
while (str[len] != '\0') len++; // find length
if (flag)
printf("The string is a palindrome.\n");
else
printf("The string is not a palindrome.\n");
return 0;
🧾 Output:
}
To write a C program to copy a string in two ways: using strcpy() and without any string
functions.
Algorithm:
Start.
Declare two character arrays str1 and str2.
Read the original string into str1.
Initialize a counter i = 0.
Repeat the following steps until the end of the string (str1[i] != '\0'):
● Copy the character from str1[i] to str2[i].
● Increment i by 1.
After the loop ends, add a null terminator str2[i] = '\0'.
Display the copied string str2.
Stop
Program:
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100], str3[100];
int i;
return 0;
🧾 Output:
}
csharp
CopyEdit
Enter a string: hello
Copied without function: hello
Copied with strcpy(): hello
Result:
Thus the Implementation of String Operations are successfully executed.
Exp.No:6
int main() {
int start, end;
printf("Enter the range (start and end): ");
scanf("%d %d", &start, &end);
return 0;
}
Output:
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
return 0;
}
Output:
Enter the number of terms: 10
Fibonacci series up to 10 terms:
0 1 1 2 3 5 8 13 21 34
Result:
Thus the Implementation of String Operations are successfully executed.
Exp.No:7
ALGORITHM:
1. Start.
2. Read the number.
3. Use recursion to extract last digit and buildreverse.
5. End.
Program:
#include <stdio.h>
int reverse(int num, int rev)
{
if (num == 0)
return rev;
else
return reverse(num / 10, rev * 10 +
num % 10); }
void main(){
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Reversed number is %d", reverse(num, 0));
OUTPUT:
Enter a number: 1234
Reversed number is 4321
Program :2 Recursive Fibonacci series
AIM:
To print Fibonacci series using recursion.
ALGORITHM:
1. Define fibo(n) where:
o If n == 0 return 0
o If n == 1 return 1
o Else return fibo(n-1) + fibo(n-2)
2. Loop to print series.
Program:
#include <stdio.h>
int fibo(int n)
{
if (n == 0)
return 0;
else
if (n == 1)
return 1;
else
return fibo(n - 1) + fibo(n - 2);
}
void main(){
int i, n;
printf("Enter number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series:\n");
for (i = 0; i < n; i++)
printf("%d ", fibo(i));
}
OUTPUT:
Enter number of terms: 6
Fibonacci Series:
011235
if (n == 0)
return 1;
return n * rec_fact(n - 1);
}
int nonrec_fact(int n)
{
int f = 1, i;
for (i = 1; i <= n; i++)
f *= i;
return f;
return a;
void main(){
int n, a, b;
printf("Enter number for factorial: ");
scanf("%d", &n);
printf("Recursive Factorial: %d\n", rec_fact(n));
printf("Non-Recursive Factorial: %d\n", nonrec_fact(n));
printf("Enter two numbers for GCD: ");
scanf("%d %d", &a, &b);
printf("Recursive GCD: %d\n", rec_gcd(a, b));
printf("Non-Recursive GCD: %d\n", nonrec_gcd(a, b));
}
OUTPUT:
Enter number for factorial: 5
Recursive Factorial: 120
Non-Recursive Factorial: 120
Enter two numbers for GCD: 12 16
Recursive GCD: 4
Non-Recursive GCD: 4
RESULT:
ALGORITHM:
3. End.
CODING:
#include <stdio.h>
void main()
{
int arr[5], i;
int *p;
p = arr;
printf("Enter 5 elements:\n");
for (i = 0; i < 5; i++)
scanf("%d", p + i);
printf("Array elements are:\n");
for (i = 0; i < 5; i++)
printf("%d ", *(p + i));
}
OUTPUT:
Enter 5 elements:
12345
12345
using pointers.
ALGORITHM:
3. Print result.
PROGRAM:
#include <stdio.h>
void main()
{
int *p;
printf("Enter 5 elements:\n");
for (i = 0; i < 5; i++)
scanf("%d", &arr[i]);
if (found)
else
printf("Element not found");
}
OUTPUT:
Enter 5 elements:
10 20 30 40 50
Enter element to search: 30
Element found at position 3
RESULT:
ALGORITHM:
3. Compute average.
Program:
#include <stdio.h>
void main() {
int arr[5], i, sum = 0;
float avg;
int *p;
printf("Enter 5 elements:\n");
for (i = 0; i < 5; i++)
scanf("%d", &arr[i]);
p = arr;
for (i = 0; i < 5; i++)
sum += *(p + i);
avg = sum / 5.0;
printf("Sum = %d\n", sum);
printf("Average = %.2f", avg);
}
OUTPUT:
Enter 5 elements:
10 20 30 40 50
Sum = 150
Average = 30.00
48
Program 4: String Sorting Using Array of Pointers
AIM:
To sort a list of strings using an array of pointers.
ALGORITHM:
pointer swap).
Program:
#include <stdio.h>
#include <string.h>
void main() {
char *temp;
int i, j;
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
printf("Sorted strings:\n");
for (i = 0; i < 5; i++)
printf("%s\n", str[i]);
}
49
OUTPUT:
Sorted strings:
apple
banana
cherry
date
grape
RESULT:
To create a C program that stores and displays the details of 5 students using a structure.
Algorithm:
1. Define a structure Student with members such as name, age, roll_number, and
marks.
3. Accept input for each student's information (name, age, roll number, marks).
Program:
#include <stdio.h>
struct Student {
char name[50];
int age;
int roll_number;
float marks;
};
int main() {
struct Student students[5]; // Array to hold 5 student
records
return 0;
}
Output:
Student Details:
Student 1:
Name: John
Age: 20
Roll Number: 101
Marks: 88.50
Student 2:
Name: Alice
Age: 22
Roll Number: 102
Marks: 91.20
Student 3:
Name: Bob
Age: 21
Roll Number: 103
Marks: 84.30
Student 4:
Name: Charlie
Age: 23
Roll Number: 104
Marks: 78.90
Student 5:
Name: Eve
Age: 20
Roll Number: 105
Marks: 92.00
Aim:
To create a C program that stores and displays employee details using a structure.
Algorithm:
1. Define a structure Employee with members such as name, id, salary, and department.
Program:
#include <stdio.h>
struct Employee {
char name[50];
int id;
float salary;
char department[50];
};
int main() {
struct Employee emp; // Structure variable to store
employee details
return 0;
}
Output:
yaml
CopyEdit
Enter employee details:
Enter name: David
Enter employee ID: 5001
Enter salary: 55000.50
Enter department: HR
Employee Details:
Name: David
Employee ID: 5001
Salary: 55000.50
Department: HR
ALGORITHM:
Program:
#include <stdio.h>
struct Point
{
int x, y;
};
struct Point getPoint()
{
struct Point p; printf("Enter x and y: ");
scanf("%d %d", &p.x, &p.y);
return p;
}
void main()
{
struct Point pt;
pt = getPoint();
printf("Point: (%d, %d)", pt.x, pt.y);
}
OUTPUT:
Enter x and y: 4 5
Point: (4, 5)
RESULT:
ALGORITHM:
3. Display contents.
Program:
#include <stdio.h>
void main()
{
FILE *fp;
char ch;
fp = fopen("test.txt", "r");
if (fp == NULL)
{
printf("File not found!");
} else
}
}
OUTPUT:
3. Display contents.
Program:
#include <stdio.h>
void main()
{
FILE *fp; char ch;
fp = fopen("test.txt", "r");
if (fp == NULL)
{
printf("File not found!");
else
}
}
Output:
56
Program 3:Reverse first n characters in a file ◆
AIM:
Algorithm:
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void reverse(char *str, int n)
{
for (int i = 0; i < n / 2; i++)
{
char temp = str[i]; str[i] = str[n - i - 1];
str[n - i - 1] = temp;
}
int main()
{
FILE *fp;
char filename[100];
int n;
fclose(fp);
free(buffer);
printf("First %d characters reversed
successfully.\n", n); return 0;
}
◆ Output:
Enter filename: data.txt
AIM:
Algorithm:
Program :
#include <stdio.h>
int main()
{
charsrcFile[100], destFile[100];
char ch;
printf("Enter source file name: ");
scanf("%s", srcFile);
printf("Enter destination file name: ");
scanf("%s", destFile);
src = fopen(srcFile, "r");
if (!src)
{
perror("Error opening source file");
return 1;
}
dest = fopen(destFile, "w");
if (!dest)
{
perror("Error opening destination file");
fclose(src);
return 1;
}
while ((ch = fgetc(src)) != EOF)
{
fputc(ch, dest);
}
59
printf("File copied successfully.\n");
fclose(src);
fclose(dest);
return 0;
}
◆ Output:
Enter source file name: file1.txt
Enter destination file name: file2.txt
File copied successfully.
Result:
The C program on Files are executed successfully and output was verified.