0% found this document useful (0 votes)
23 views35 pages

C Lab Manual

Uploaded by

senprince.2005
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)
23 views35 pages

C Lab Manual

Uploaded by

senprince.2005
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/ 35

iii

BASICS OF C PROGRAMMING
LAB EXPERIMENT

SUBMITTED TO VISHAL SIR


SUBMITTED BY MANVI GUPTA
COURSE BTECH CSE
ROLL NO. 2401301098
YEAR 1st
iii

LIST OF EXPERIMENTS AS PER CURRICULUM

S.No. LIST OF EXPERIMENT EXPERIMENT NO. PAGE DATE SIGN.


NO.

1(a) Write a C Program to compute an electricity bill 1


based on the slab rates. 1

1(b). Write a C program to determine insurance 1 2-3


eligibility, premium rate, and maximum

1(c). Write a C Program to find a student's grade 1 4


using a Switch case.

2(a). Write a C program to generate the first n terms 2 5


of the Fibonacci sequence.

2(b). Write a C program to find the sum of 2 6


individual digits of a positive integer.

2(c). Write a C program to compute the sum of a 2 7


geometric progression: 1+x+x^2+...

2(d). Write a C program to print a pyramid pattern 2 8


of numbers

3(a). Write a C program to find the largest and 3 9


smallest numbers in a list of integers.

3(b). Write a C program to perform addition and 3 10-12


multiplication of two matrices.

4(a) Write a C program to insert a sub-string into a 4 13-14


main string at a given position
iii

S.NO. LIST OF EXPERIMENT EXPERIMENT PAGE NO. DATE SING


NO.

4(b) Write a C program to count the 4 15


lines, words, and characters in a
given.
5(a) Write a C program to generate all 5 16
prime numbers between 1 and n.

5(b) Write a C program using recursion 5 17-18


to find the factorial and GCD of
numbers.
6(a) Write a C program to print the 6 19
elements of an array in reverse
order using pointers.
6(b) Write a C program to count vowels 6 20
and consonants in a string using
pointers. 6c Write a C program to
sort n array elements using
pointers.

6(c) Write a C program to sort n array 6 21


elements using pointers.
7(a) Write a C program for operations 7 22-23
on complex numbers: read, write,
addition, and multiplication.

7(b) Write a C program using 7 24-25


structures to manage employee
details and perform operations on
basic pay.
8(a) . 8a Write a C program to reverse 8 26-27
the first n characters of a text file.

8(b) Write a C program to merge two 8 28-29


files into a new file.
9) 9 Write a C program to develop a 9 30-32
phone book application for
managing contact detail
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:1 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:1
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

EXPERIMENT NO. – 1
1(A) AIM: - Write a C Program to compute an electricity bill based on the following slab rates.

Consumption units Rate (in Rupees/unit): -

0-100 4.0 101-150 4.6

151-200 5.2 201-300 6.3

Above 300 8.0

#include <stdio.h>
int main() {
int oldReading, currentReading, units;
float billAmount = 0;

printf("Enter old meter reading: ");


scanf("%d", &oldReading);
printf("Enter current meter reading: ");
scanf("%d", &currentReading);

units = currentReading - oldReading;

if (units <= 100) {


billAmount = units * 4.0;
} else if (units <= 150) {
billAmount = 100 * 4.0 + (units - 100) * 4.6;
} else if (units <= 200) {
billAmount = 100 * 4.0 + 50 * 4.6 + (units - 150) * 5.2;
} else if (units <= 300) {
billAmount = 100 * 4.0 + 50 * 4.6 + 50 * 5.2 + (units - 200) * 6.3;
} else {
billAmount = 100 * 4.0 + 50 * 4.6 + 50 * 5.2 + 100 * 6.3 + (units - 300) * 8.0;
}
printf("Total bill amount: Rs. %.2f\n", billAmount);
return 0;
}

OUTPUT:-
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:1 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:2
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

1(B)
An insurance company computes the premium amount based on the following;
• If a person's health is excellent and the person is between 25 and 35 years of age and lives in a city, and is a male
then the premium is Rs.4 per thousand and the policy amount cannot exceed Rs.2 lakhs.
• If a person satisfies all the above conditions and is female then the premium is Rs.3 per thousand and the policy
amount cannot exceed Rs.1 lakh.
• If a person's health is poor and the person is between 25 and 35 years of age and lives in a village and is a male then
premium is Rs.6 per thousand and the policy cannot exceed Rs. 10000.
• In all other cases the person is not insured.
• Write a C program to determine whether the person should be insured or not, his/her premium rate and maximum
amount for which he/she can be insured.

CODE: -
#include <stdio.h>

void computePremium(char health[], char gender[], char residence[], int age) {


float premiumRate = 0;
int policyAmount = 0;

if (health[0] == 'e' && age >= 25 && age <= 35 && residence[0] == 'c') {
if (gender[0] == 'm') {
premiumRate = 4.0;
policyAmount = 200000;
} else if (gender[0] == 'f') {
premiumRate = 3.0;
policyAmount = 100000;
}
} else if (health[0] == 'p' && age >= 25 && age <= 35 && residence[0] == 'v' && gender[0] == 'm') {
premiumRate = 6.0;
policyAmount = 10000;
}

if (premiumRate == 0) {
printf("The person is not insured.\n");
} else {
printf("The person is insured.\n");
printf("Premium rate: Rs.%.2f per thousand\n", premiumRate);
printf("Maximum insured amount: Rs.%d\n", policyAmount);
}
}

int main() {
char health[20], gender[10], residence[20];
int age;

printf("Enter health (excellent/poor): ");


scanf("%s", health);
printf("Enter gender (male/female): ");
scanf("%s", gender);
printf("Enter residence (city/village): ");
scanf("%s", residence);
printf("Enter age: ");
scanf("%d", &age);

// Compute and display the premium and policy amount


computePremium(health, gender, residence, age);
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:1 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:3
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

computePremium(health, gender, residence, age);


return 0;
}

OUTPUT: -
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:1 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:4
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

1(C)
AIM: Write a C Program to find the grade for a student using a Switch case. The user needs to enter
subject score (varies from 0 to 100) and then display the grade as described below.

#include <stdio.h>
int main() {
int score;
char grade;
printf("Enter the subject score (0 to 100): ");
scanf("%d", &score);

switch (score / 10) {


case 10:
case 9:
grade = 'O';
break;
case 8:
grade = 'A';
break;
case 7:
grade = 'B';
break;
case 6:
grade = 'C';
break;
case 5:
grade = 'D';
break;
case 4:
grade = 'E';
break;
default:
grade = 'F';
break;
}
printf("The grade is: %c\n", grade);
return 0;
}

OUTPUT: -
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:5
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

EXPERIMENT NO. – 2
2(A) A Fibonacci sequence is defined as follows:
The first and second terms in the sequence are 0 and 1.
Sub-sequent terms are found by adding the preceding two terms in the sequence.
Write a C program to generate the first n terms of the sequence.

#include <stdio.h>

int main() {

int n, t1 = 0, t2 = 1, nextTerm;

printf("Enter the number of terms to generate: ");

scanf("%d", &n);

printf("The first %d terms of the Fibonacci sequence are: ", n);

for (int i = 1; i <= n; ++i) {

printf("%d ", t1);

nextTerm = t1 + t2;

t1 = t2;

t2 = nextTerm;

return 0;

OUPUT: -
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:6
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

2(B)
AIM: Write a C program to find the sum of individual digits of a positive integer

#include <stdio.h>

int main() {
int num, sum = 0, digit;

printf("Enter a positive integer: ");


scanf("%d", &num);

if (num < 0) {
printf("Please enter a positive integer.\n");
return 1;
}

while (num > 0) {


digit = num % 10;
sum += digit;
num /= 10;
}

printf("The sum of the individual digits is: %d\n", sum);

return 0;
}

OUTPUT: -
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:7
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

2(C)
AIM Write a C program to read two numbers x and n, and then compute the sum of the
geometric progression: 1+x+x2+x3+…………+xn. Show appropriate error message for n<0.
(Example: if n is 3 and x is 5, then the sum is: 1+5+25+125) .
#include <stdio.h>
int main() {
int x, n;
int term = 1;
int sum = 0;

printf("Enter the value of x: ");


scanf("%d", &x);
printf("Enter the value of n: ");
scanf("%d", &n);

if (n > 0) {
for (int i = 0; i <= n; i++) {
sum += term;
term *= x;
}
printf("The sum of the geometric progression is: %d\n", sum);
} else {
printf("Error: n must be a non-negative integer.\n");

}
return 0;
}

OUPUT: -
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:8
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

2(d)

AIM: W r i t e a C p r o g r a m t o p r i n t t h e f o l l o w i n g p a t t e r n .
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

#include <stdio.h>

int main() {
int n = 5;
int i, j, k;

for (i = 1; i <= n; i++) {

for (j = n; j > i; j--) {


printf(" ");
}

for (k = 1; k <= i; k++) {


printf("%d ", k);
}

for (k = i - 1; k >= 1; k--) {


printf("%d ", k);
}

printf("\n");
}
return 0;
}

OUTPUT: -
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:9
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

EXPERIMENT NO. – 3
3(A)
AIM: Write a C program to find both the largest and smallest numbers in a list of integers.

#include <stdio.h>
int main() {
int n, i, num;
int largest, smallest;

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


scanf("%d", &n);

if (n <= 0) {
printf("Error: The list must have at least one element.\n");
return 1;
}

printf("Enter number 1: ");


scanf("%d", &num);
largest = num;
smallest = num;

for (i = 1; i < n; i++) {


printf("Enter number %d: ", i + 1);
scanf("%d", &num);

if (num > largest) {


largest = num;
}
if (num < smallest) {
smallest = num;
}
}
printf("The largest number is: %d\n", largest);
printf("The smallest number is: %d\n", smallest);
return 0;
}

Output: -
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:10
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

3(B)
AIM: Write a C program that uses function to perform the following:
i) Addition of Two Matrices ii) Multiplication of Two Matrice

i) #include <stdio.h>

#define MAX 100

void addMatrices(int a[][MAX], int b[][MAX], int result[][MAX], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = a[i][j] + b[i][j];
}
}
}

void printMatrix(int matrix[][MAX], int rows, int cols) {


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int rows, cols;
int a[MAX][MAX], b[MAX][MAX], result[MAX][MAX];

printf("Enter the number of rows and columns: ");


scanf("%d %d", &rows, &cols);

printf("Enter elements of the first matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &a[i][j]);
}
}

printf("Enter elements of the second matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &b[i][j]);
}
}

addMatrices(a, b, result, rows, cols);

printf("Resultant matrix after addition:\n");


printMatrix(result, rows, cols);
return 0;
}
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:11
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

OUTPUT: -

ii)
#include <stdio.h>

#define MAX 100

void multiplyMatrices(int a[][MAX], int b[][MAX], int result[][MAX], int rows1, int cols1, int cols2) {
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
result[i][j] = 0;
for (int k = 0; k < cols1; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
}

void printMatrix(int matrix[][MAX], int rows, int cols) {


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int rows1, cols1, rows2, cols2;
int a[MAX][MAX], b[MAX][MAX], result[MAX][MAX];

printf("Enter the number of rows and columns for the first matrix: ");
scanf("%d %d", &rows1, &cols1);

// Input dimensions of the second matrix


printf("Enter the number of rows and columns for the second matrix: ");
scanf("%d %d", &rows2, &cols2);

// Check if matrix multiplication is possible


GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:12
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

printf("Enter the number of rows and columns for the second matrix: ");
scanf("%d %d", &rows2, &cols2);

if (cols1 != rows2) {
printf("Matrix multiplication not possible.\n");
return 1;
}

printf("Enter elements of the first matrix:\n");


for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
scanf("%d", &a[i][j]);
}
}

printf("Enter elements of the second matrix:\n");


for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
scanf("%d", &b[i][j]);
}
}

multiplyMatrices(a, b, result, rows1, cols1, cols2);

printf("Resultant matrix after multiplication:\n");


printMatrix(result, rows1, cols2);

return 0;
}

OUTPUT: -
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:13
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

EXPERIMENT-4
4 (A)
AIM : Write a C program to insert a sub-string in to a main string at a given position..

#include <stdio.h>
#include <string.h>

int main() {
char mainStr[100], subStr[50], result[150];
int position, i = 0, j = 0;

printf("Enter the main string: ");


gets(mainStr);

printf("Enter the sub-string: ");


gets(subStr);

printf("Enter the position to insert the sub-string: ");


scanf("%d", &position);

if (position < 0 || position > strlen(mainStr)) {


printf("Invalid position!\n");
return 1;
}

while (i < position) {


result[i] = mainStr[i];
i++;
}

while (subStr[j] != '\0') {


result[i] = subStr[j];
i++;
j++;
}

j = position;
while (mainStr[j] != '\0') {
result[i] = mainStr[j];
i++;
j++;
}

result[i] = '\0';
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:14
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

result[i] = '\0';

printf("The new string is: %s\n", result);

return 0;
}

OUTPUT: -
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:15
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

4(B)
AIM: Write a C program to count the lines, words and characters in a given text.

#include <stdio.h>
#include <ctype.h>
int main() {
char ch;
int lines = 0, words = 0, characters = 0;
int inWord = 0;

printf("Enter text ( Ctrl+Z for exit):\n");

while ((ch = getchar()) != EOF) {


characters++;

if (ch == '\n') {
lines++;
}

if (isspace(ch)) {
inWord = 0;
} else if (!inWord) {
inWord = 1;
words++;
}
}

if (characters > 0) {
lines++;
}

printf("Lines: %d\n", lines);


printf("Words: %d\n", words);
printf("Characters: %d\n", characters);
return 0;
}

OUTPUT: -

-
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:16
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

EXPERIMENT-5

5(A)
AIM: Write a C program to generate all the prime numbers between 1 and n, where n is a value entered by the
user. Define a separate function to generate prime numbers.

#include <stdio.h>
int isPrime(int num) {
if (num <= 1) return 0;
for (int i = 2; i < num; i++) {
if (num % i == 0) return 0;
}
return 1;
}

int main() {
int n;

printf("Enter the value of n: ");


scanf("%d", &n);

printf("Prime numbers between 1 and %d are:\n", n);


for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
printf("%d ", i);
}
}
printf("\n");

return 0;
}

OUTPUT: -
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:17
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

5(B)
AIM: Write C program that uses recursive function to find the following.
i) Factorial of a given integer ii) GCD of two given integers

#include <stdio.h>

int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}

int gcd(int a, int b) {


if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}

int main() {
int choice, num, num1, num2;

while (1) {
printf("\nMenu:\n");
printf("1. Calculate Factorial\n");
printf("2. Calculate GCD\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial of a negative number doesn't exist.\n");
} else {
printf("Factorial of %d = %d\n", num, factorial(num));
}
break;
case 2:
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
printf("GCD of %d and %d = %d\n", num1, num2, gcd(num1, num2));
break;
case 3:
printf("Exiting the application.\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:18
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

case 3:
printf("Exiting the application.\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}

return 0;
}

Output: -
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:19
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

EXPERIMENT-6
6(A)
AIM: Write a C program to print the elements of an array in reverse order using pointers.

#include <stdio.h>

void printArrayInReverse(int *arr, int size) {


int *ptr = arr + size - 1;

for (int i = 0; i < size; i++) {


printf("%d ", *ptr);
ptr--;
}
printf("\n");
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);

printf("Original Array: ");


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

printf("Array in Reverse Order: ");


printArrayInReverse(arr, size);

return 0;
}

OUTPUT: -
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:20
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

6(B)
AIM: Write a C program to count the number of vowels and consonants in a string using pointers

#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
char *ptr;
int vowels = 0, consonants = 0;

printf("Enter a string: ");


gets(str);

ptr = str;

while (*ptr != '\0') {


if (isalpha(*ptr)) {
char ch = tolower(*ptr);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
} else {
consonants++;
}
}
ptr++;
}
printf("Number of vowels: %d\n", vowels);
printf("Number of consonants: %d\n", consonants);

return 0;
}

OUTPUT: -
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:21
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

6(C)
AIM: Write a C program to store n elements in an array and print the elements in sorted order usingpointers.

#include <stdio.h>
void sortArray(int *arr, int n) {
int temp;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (*(arr + i) > *(arr + j)) {
temp = *(arr + i);
*(arr + i) = *(arr + j);
*(arr + j) = temp;
}
}
}
}

int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
sortArray(arr, n);
printf("The elements in sorted order are:\n");
for (int i = 0; i < n; i++) {
printf("%d ", *(arr + i));
}
printf("\n");
return 0;
}

OUTPUT: -
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:22
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

EXPERIMENT-7
7(A)
AIM: ) Write a C program that performs the following operations:
Reading a complex number ii. Writing a complex number
iii. Addition of two complex number iv. Multiplication of two complex numbers

#include <stdio.h>

typedef struct {
float real;
float imag;
} Complex;

Complex readComplex() {
Complex c;
printf("Enter the real part: ");
scanf("%f", &c.real);
printf("Enter the imaginary part: ");
scanf("%f", &c.imag);
return c;
}

void writeComplex(Complex c) {
printf("%.2f + %.2fi\n", c.real, c.imag);
}

Complex addComplex(Complex c1, Complex c2) {


Complex result;
result.real = c1.real + c2.real;
result.imag = c1.imag + c2.imag;
return result;
}

Complex multiplyComplex(Complex c1, Complex c2) {


Complex result;
result.real = c1.real * c2.real - c1.imag * c2.imag;
result.imag = c1.real * c2.imag + c1.imag * c2.real;
return result;
}

int main() {
Complex c1, c2, sum, product;

printf("Enter the first complex number:\n");


c1 = readComplex();

printf("Enter the second complex number:\n");


c2 = readComplex();

printf("First complex number: ");


writeComplex(c1);
printf("Second complex number: ");
writeComplex(c2);

// Adding the complex numbers


GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:23
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

printf("Enter the second complex number:\n");


c2 = readComplex();

printf("First complex number: ");


writeComplex(c1);
printf("Second complex number: ");
writeComplex(c2);

sum = addComplex(c1, c2);


printf("Sum of the complex numbers: ");
writeComplex(sum);

product = multiplyComplex(c1, c2);


printf("Product of the complex numbers: ");
writeComplex(product);

return 0;
}
Explanation:
OUTPUT: 1.- Structure Definition:
o typedef struct { float real; float imag; } Complex; defines a structure to represent
complex numbers with real and imaginary parts.
2. Function to Read a Complex Number:
o Complex readComplex(): Reads the real and imaginary parts of a complex number
from the user and returns the complex number.
3. Function to Write (Display) a Complex Number:
o void writeComplex(Complex c): Prints the complex number in the format "a + bi".
4. Function to Add Two Complex Numbers:
o Complex addComplex(Complex c1, Complex c2): Adds the real parts and the
imaginary parts of two complex numbers and returns the resulting complex number.
5. Function to Multiply Two Complex Numbers:
o Complex multiplyComplex(Complex c1, Complex c2): Multiplies two complex
numbers using the formula (a + bi)(c + di) = (ac - bd) + (ad + bc)i and returns the
resulting complex number.
6. Main Function:
o Reads two complex numbers from the user.
o Writes the complex numbers to the console.
o Calculates and displays the sum of the two complex numbers.
o Calculates and displays the product of the two complex numbers.
Feel free to test this program with different complex numbers. Let me know if you have any questions
or need further assistance!
Message Copilot
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:24
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

7(B)
AIM: D e f i n e a s t r u c t u r e t o s t o r e e m p l o y e e d e t a i l s i n c l u d e E m p l o y e e - N u m b e r , E m p l o y e e -
Name, Basic-pay, Date-of-Joining. Write a C program for the foll owing.
i.A fun ction to st ore 10 employee details.
ii. A function to implement the follow ing rules while revising the basic pay.
If Basic-pay<=Rs.5000 then increase it by 15%.
If Basic-pay> Rs .5000 and <=Rs .25000 the n it increase by10%.
If Basic-pay>Rs.25000 then there is no cha nge in Basic -pay.
iii. A funct ion t o print the detail s of employees who have completed 20 years of service from
the Date-of-Joining

#include <stdio.h>
#include <string.h>
typedef struct {
int empNumber;
char empName[50];
float basicPay;
int joinDay;
int joinMonth;
int joinYear;
} Employee;

void storeEmployeeDetails(Employee employees[], int size) {


for (int i = 0; i < size; i++) {
printf("Enter details for employee %d:\n", i + 1);
printf("Employee Number: ");
scanf("%d", &employees[i].empNumber);
printf("Employee Name: ");
scanf(" %[^\n]", employees[i].empName);
printf("Basic Pay: ");
scanf("%f", &employees[i].basicPay);
printf("Date of Joining (DD MM YYYY): ");
scanf("%d %d %d", &employees[i].joinDay, &employees[i].joinMonth, &employees[i].joinYear);
printf("\n");
}
}
void reviseBasicPay(Employee employees[], int size) {
for (int i = 0; i < size; i++) {
if (employees[i].basicPay <= 5000) {
employees[i].basicPay += employees[i].basicPay * 0.15;
} else if (employees[i].basicPay > 5000 && employees[i].basicPay <= 25000) {
employees[i].basicPay += employees[i].basicPay * 0.10;
}
}
}
void LongServiceEmployees(Employee employees[], int size, int currentYear) {
for (int i = 0; i < size; i++) {
if (currentYear - employees[i].joinYear >= 20) {
printf("Employee Number: %d\n", employees[i].empNumber);
printf("Employee Name: %s\n", employees[i].empName);
printf("Basic Pay: %.2f\n", employees[i].basicPay);
printf("Date of Joining: %02d-%02d-%04d\n", employees[i].joinDay, employees[i].joinMonth,
employees[i].joinYear);
printf("\n");
}
}
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:25
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

int main() {
int size = 10;
Employee employees[size];

EmployeeDetails(employees, size);

reviseBasicPay(employees, size);

int currentYear;
printf("Enter the current year: ");
scanf("%d", &currentYear);

printf("Employees who have completed 20 years of service:\n");


printLongServiceEmployees(employees, size, currentYear);

return 0
}

Output: -
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:26
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

EXPERIMENT-8
8(A)
AIM: Write a C program to reverse the first n characters of a given text file.
#include <stdio.h>
#include <stdlib.h>

void reverseCharacters(char *str, int n) {


int start = 0;
int end = n - 1;
char temp;

while (start < end) {


temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}

int main() {
FILE *file;
char *filename = "input.txt";
int n;

printf("Enter the number of characters to reverse: ");


scanf("%d", &n);

file = fopen(filename, "r+");


if (file == NULL) {
printf("Error opening the file %s\n", filename);
return 1;
}

char *buffer = (char *)malloc((n + 1) * sizeof(char));


if (buffer == NULL) {
printf("Memory allocation error\n");
fclose(file);
return 1;
}

// Reverse the first n characters


reverseCharacters(buffer, n);

// Move the file pointer to the beginning of the file


fseek(file, 0, SEEK_SET);
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:27
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

fread(buffer, sizeof(char), n, file);


buffer[n] = '\0';

reverseCharacters(buffer, n);

]
fseek(file, 0, SEEK_SET);

fwrite(buffer, sizeof(char), n, file);

free(buffer);
fclose(file);

printf("First %d characters have been reversed and written back to the file.\n", n);

return 0;
}

OUTPUT: -
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:28
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

8(b)

AIM: Write a Program to Demonstrate Friend Function and Friend Class.

#include <stdio.h>
#include <stdlib.h>

void mergeFiles(const char *file1, const char *file2, const char *outputFile) {
FILE *f1 = fopen(file1, "r");
FILE *f2 = fopen(file2, "r");
FILE *out = fopen(outputFile, "w");

if (f1 == NULL || f2 == NULL || out == NULL) {


printf("Error opening files.\n");
exit(1);
}

char ch;

while ((ch = fgetc(f1)) != EOF) {


fputc(ch, out);
}
while ((ch = fgetc(f2)) != EOF) {
fputc(ch, out);
}

printf("Files have been merged successfully into %s.\n", outputFile);

fclose(f1);
fclose(f2);
fclose(out);
}

int main() {
const char *file1 = "file1.txt";
const char *file2 = "file2.txt";
const char *outputFile = "merged.txt";

mergeFiles(file1, file2, outputFile);

return 0;
}
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:29
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

OUTPUT: -
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:30
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

EXPERIMENT-9

AIM: Develop a phone book application to save users contact information include name, mobile number and email
id as well as to edit and delete contact details.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX 100


typedef struct {
char name[50];
char mobile[15];
char email[50];
} Contact;

Contact phoneBook[MAX];
int contactCount = 0;

void addContact() {
if (contactCount < MAX) {
printf("Enter Name: ");
scanf(" %[^\n]", phoneBook[contactCount].name);
printf("Enter Mobile Number: ");
scanf("%s", phoneBook[contactCount].mobile);
printf("Enter Email ID: ");
scanf("%s", phoneBook[contactCount].email);
contactCount++;
printf("Contact added successfully.\n");
} else {
printf("Phone book is full.\n");
}
}

void displayContacts() {
if (contactCount == 0) {
printf("No contacts to display.\n");
return;
}
for (int i = 0; i < contactCount; i++) {
printf("Contact %d:\n", i + 1);
printf("Name: %s\n", phoneBook[i].name);
printf("Mobile: %s\n", phoneBook[i].mobile);
printf("Email: %s\n", phoneBook[i].email);
printf("\n");
}
}

void editContact() {
char name[50];
printf("Enter the name of the contact to edit: ");
scanf(" %[^\n]", name);
for (int i = 0; i < contactCount; i++) {
if (strcmp(phoneBook[i].name, name) == 0) {
printf("Enter new Mobile Number: ");
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:31
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

scanf(" %[^\n]", name);


for (int i = 0; i < contactCount; i++) {
if (strcmp(phoneBook[i].name, name) == 0) {
for (int j = i; j < contactCount - 1; j++) {
phoneBook[j] = phoneBook[j + 1];
}
contactCount--;
printf("Contact deleted successfully.\n");
return;
}
}
printf("Contact not found.\n");
}

void saveContacts() {
FILE *file = fopen("phonebook.txt", "w");
if (file == NULL) {
printf("Error opening file.\n");
return;
}
for (int i = 0; i < contactCount; i++) {
fprintf(file, "%s %s %s\n", phoneBook[i].name, phoneBook[i].mobile, phoneBook[i].email);
}
fclose(file);
printf("Contacts saved to file successfully.\n");
}

void loadContacts() {
FILE *file = fopen("phonebook.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
return;
}
contactCount = 0;
while (fscanf(file, " %49[^\n] %14s %49s", phoneBook[contactCount].name, phoneBook[contactCount].mobile,
phoneBook[contactCount].email) != EOF) {
contactCount++;
}
fclose(file);
printf("Contacts loaded from file successfully.\n");
}
int main() {
int choice;
loadContacts();
do {
printf("\nPhone Book Menu:\n");
printf("1. Add Contact\n");
printf("2. Display Contacts\n");
printf("3. Edit Contact\n");
printf("4. Delete Contact\n");
printf("5. Save Contacts\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
GEETA UNIVERSITY, PANIPAT
LABORATORY MANUAL
EXPERIMENT NO.:4 ISSUE NO: ISSUE DATE :
Course Outcome: REV. DATE PAGE:32
SCHOOL OF COMPUTER LABORATORY: BASIC OF C PROGRAMING
SCIENCE & ENGINEERING SEMESTER: 1

scanf("%d", &choice);

switch (choice) {
case 1:
addContact();
break;
case 2:
displayContacts();
break;
case 3:
editContact();
break;
case 4:
deleteContact();
break;
case 5:
saveContacts();
break;
case 6:
printf("Exiting the application.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 6);

return 0;
}

OUTPUT: -

You might also like