C Programming Lab Manual-1
C Programming Lab Manual-1
0 0 4 2
COURSE OBJECTIVES:
LIST OF EXPERIMENTS:
Note: The lab instructor is expected to design problems based on the topics
listed. TheExamination shall not be restricted to the sample experiments
designed.
TOTAL: 60 PERIODS
COURSE OUTCOMES:
TEXT BOOKS:
1. ReemaThareja, “Programming in C”, Oxford University Press, Second Edition, 2016.
2. Kernighan, B.W and Ritchie,D.M, “The C Programming language”,
Second Edition,Pearson Education, 2015.
REFERENCES:
1. Paul Deitel and Harvey Deitel, “C How to Program with an Introduction to
C++”, Eighthedition, Pearson Education, 2018.
2. Yashwant Kanetkar, Let us C, 17th Edition, BPB Publications, 2020.
3. Byron S. Gottfried, "Schaum's Outline of Theory and Problems of
Programming with C",McGraw-Hill Education, 1996.
4. Pradip Dey, Manas Ghosh, “Computer Fundamentals and Programming in C”, Second
5. Edition, Oxford University Press, 2013.
6. Anita Goel and Ajay Mittal, “Computer Fundamentals and Programming in
C”, 1stEdition, Pearson Education, 2013.
List of Experiments
Page Staff
Sl.No Ex.No. Date Title of the Experiments
No. sign
1. 1a Programs using I/O statements
2. 1b Programs using Operators and Expressions
3. 2a Find Odd or Even using goto statement
4. 2b Finding Leap Year or Not
5. 2c Creating a Menu Driven Calculator
6. 2d Checking vowel or not using switch case
Print 1 to n numbers except multiples of 5
7. 2e
using continue statement
8. 3a Finding Armstrong Number
Sum of first N natural numbers using
9. 3b
Looping
10. 3c Sum the cubes of series of N numbers
Finding Sum Of Weights & Sorting The
11. 4a
Elements Based On Weights
Finding The Persons Having Above
12. 4b
Average Height
13. 4c Finding Body Mass Index Using Array
14. 4d Matrix Addition and Multiplication
Reverse String Without Changing The
15. 5
Position Of Special Character
16. 6a Swap the values using call by value
Sorting The Values Using Pass By
17. 6b
Reference
Sum of array elements by passing to a
18. 6c
function
19. 7a Towers of Hanoi
20. 7b Finding factorial of a number
21. 8a Pointer to pointer
22. 8b Array of Pointer
23. 9a Employee Salary Slip
24. 9b Students Internal Mark Sheet
25. 9c Accessing Union Members
26. 10a Sequential Access File
27. 10b Random Access File
28. 10c Processor Directives
EX.NO.:1a PROGRAMS USING I/O STATEMENTS
AIM:
To Write a C program to use Input and Output Statements
PROCEDURE:
SYNTAX:
Unformatted I/O
◦ charvariable=getchar();
◦ charvariable=getche();
◦ charvariable=getch();
◦ gets(chararray_variable);
◦ putch(charvariable);
◦ puts(chararray_variable);
Formatted I/O
◦ scanf(“format specifiers”,address of variables);
◦ printf(“Any text”);
◦ printf(“Any text,format spcifiers”,variables);
◦ printf(“format spcifiers”,variables);
SAMPLE PROGRAMS:
P1: Get & Display a single character using getch()
#include<stdio.h>
int main() {
char c1;
c1 = getch(); // Using getch() to read a character without echoing
putch(c1); // Corrected function name from putch() to putchar()
return 0;
}
P2: Get & Display a single character using getche()
#include <stdio.h>
#include <conio.h>
int main() {
char c;
return 0;
}
P3: Get & Display a single character using getchar()
#include<stdio.h>
#include<conio.h>
main()
{
char c1;
}
P4: Get & Display a More characters using gets()
#include<stdio.h>
#include<conio.h>
main()
{
char name[20];
}
P5: Get & Display a student’s regno,Name and GPA using scanf & printf
#include<stdio.h>
#include<conio.h>
main()
{
int regno;
char name[25];
float GPA;
printf(“\nEnter a Student Regno,Name & GPA\n”);
scanf();
prinntf(“\n------------------------------------------------------\n”);
prinntf(“\n\t\t REG.NO \t\t NAME \t\t GPA \t\t\n”);
prinntf(“\n------------------------------------------------------\n”);
printf();
}
OUTPUT:
RESULT:
Thus the C program for I/O statements has been written & executed successfully.
AIM:
To Write C programs using Operators and Expressions
PROCEDURE:
SYNTAX:
Expression = Operand operator Operand
Operator : Symbol
Operand : variable or Constant
var1=Var2+Var3 (or) Var1=Value1+Value2; (or) Var1=Var2+value
Logical Operators : returns True(1) or false(0) values-Condition1&& Condition2
Ternary Operator: if condition is true True part will be executed. Otherwise, False part
will be executed - Var1= ( condition)? True part: False Part
SAMPLE PROGRAMS:
P1: Arithmetic Operators
#include<stdio.h>
#include<conio.h>
Int main()
{
int a,b;
a=20;
b=5;
printf(“Addition of a b = %d”,a+b);
printf(“Subtraction ofa and b”,a-b);
printf(“Multiplication of a and b”,a*b);
printf(“Division ofa and b”,a/b);
printf(“Modulo Division of a and b “,a%b);
getch();
}
P2: Bit wise Operators
#include<stdio.h>
#include<conio.h>
Int main()
{
int a,b;
a=20;
b=5;
printf(“Bit wise OR of a b is = %d”,a|b);
printf(“Bit wise AND of a b is = %d”,a&&b );
printf(“One’s complement of a is = %d”,~a );
getch();
}
P3: Relational Operators
#include<stdio.h>
#include<conio.h>
Int main()
{
int a,b;
a=20;
b=5;
printf(“a>b %d”,a>b);
getch();
}
P4: Logical, Ternary Operators, Increment & decrement Operators
#include<stdio.h>
#include<conio.h>
int main() {
int a, b, c, d;
a = 20;
b = 5;
c = (a > b) ? (a + b) : (a - b);
printf("The value of c is = %d\n", c); // Corrected printf statement to print the value of c
printf("a++ & ++b is = %d %d\n", ++a, ++b); // Corrected printf statement to print the values
of a++ and ++b
getch();
return 0;
}
OUTPUT:
RESULT:
Thus the C program for expressions has been written & executed successfully.
AIM:
To Write a C program to find whether a given number is odd or even using goto
statement.
PROCEDURE:
SYNTAX:
if(condition)
statement;
else
statement;
SAMPLE PROGRAM:
#include <stdio.h>
#include <stdlib.h>
void main()
{
int num;
printf("Enter a number\n");
scanf("%d", &num);
if (num % 2 == 0)
goto even;
else
goto odd;
even:
printf("%d is even\n", num);
exit(0);
odd:
printf("%d is odd\n", num);
}
OUTPUT 1:
Enter a number
74
74 is even
OUTPUT 2:
Enter a number
35
35 is odd
RESULT:
Thus the C program for finding even or odd is written & executed Successfully.
AIM:
To Write a C program to find whether the given the given year is leap year or Not.
PROCEDURE:
PROGRAM:
#include <stdio.h>
int main(){
int y;
printf("Enter the year to check: ");
scanf("%d",&y);
if (((y % 4 == 0) && (y % 100!= 0)) || (y%400 == 0))
printf("It is a leap year");
else
printf("It is not a leap year");
return 0;
}
Output:
RESULT:
Thus the C program for finding leap year or not is written & executed Successfully.
AIM:
To Write a C program for Creating a Menu Driven Calculator using switch case.
PROCEDURE:
PROGRAM:
#include<stdio.h>
#include<conio.h>
int main() {
int option, a, b, c;
switch (option) {
case 1:
printf("Enter two values\n");
scanf("%d%d", &a, &b);
c = a + b;
break;
case 2:
printf("Enter two values\n");
scanf("%d%d", &a, &b);
c = a - b;
break;
case 3:
printf("Enter two values\n");
scanf("%d%d", &a, &b);
c = a * b;
break;
case 4:
printf("Enter two values\n");
scanf("%d%d", &a, &b);
if (b != 0) {
c = a / b;
} else {
printf("Cannot divide by zero\n");
getch();
return 1; // Returning 1 to indicate error condition
}
break;
default:
printf("Choose the correct option\n");
getch();
return 1; // Returning 1 to indicate error condition
}
Output:
RESULT:
Thus the c program for creating a Menu Driven Calculator using switch case is
written & executed successfully.
AIM:
To Write a C program for Checking vowel or not using switch case.
PROCEDURE:
PROGRAM:
Output:
RESULT:
Thus the c program for Checking vowel or not using switch case switch case is
written & executed successfully.
AIM:
To Write a C program to Print 1 to n numbers except multiples of 5 using continue
statement.
ALGORITHM:
PROGRAM:
OUTPUT:
RESULT:
AIM:
To Write a C program to find whether a given number is Armstrong number or not.
ALGORITHM:
#include<stdio.h>
#include<conio.h>
main()
{
int N,A,digit,cube,sum=0;
printf(“Enter a Number\n”);
scanf(“%d”,&N);
A=N;
while( )
{
digit=N%10;
cube=(digit*digit*digit);
sum=sum+cube;
N=N/10;
}
if ( sum==A)
printf(“%d is Armstrong Number”, );
else
printf(“%d is not an Armstrong Number”, );
getch();
}
OUTPUT:
AIM:
To Write a C program for sum of first n natural numbers using while, do…while and for
loop.
ALGORITHM: 1
ALGORITHM: 2
ALGORITHM: 3
PROGRAM:1
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
i = 1;
while (i <= n) {
sum += i;
++i;
}
PROGRAM:2
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
i = 1;
do
{
sum += i;
++i;
} while (i <= n);
printf("Sum = %d", sum);
return 0;
}
PROGRAM:3
#include <stdio.h>
int main() {
int n, i, sum = 0;
OUTPUT:
Sum =55
RESULT:
Thus the C program to for sum of first n natural numbers using while, do…while and for
loop has been executed successfully and the output was verified.
AIM:
To Write a C program to Sum the cubes of series of N numbers.
ALGORITHM:
PROGRAM:
#include <stdio.h>
int main() {
int n;
int sum = 0;
printf("sum = %d\n", sum); // Corrected the printf statement to display the sum
return 0;
}
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
double cubroot,cube;
int a[10]={},w[10],i,j,N=6,count=0,A,sum=0,t,k;
for(i=0;i<N;i++)
{
A=a[i];
w[i]=0;
cubroot=ceil(pow(A,(1/3)));
cube=cubroot*cubroot*cubroot;
if(A==cube)
w[i]=w[i]+5;
if(A%4==0 && A%6==0)
w[i]=w[i]+4;
for(j=2;j<=A;j++)
{
if(A%j == 0)
count=count+j;
}
if(A==1 || A==count)
w[i]=w[i]+3;
}
printf("\n\nELEMENT\t\t\t\tWEIGHT \n\n");
for(i=0;i<N;i++)
{
printf("%d \t\t\t\t%d\n\n",a[i],w[i]);
sum=sum+w[i];
}
printf("The sum of Weights is = sum);
printf("Sorting of the Elements based on weight values\n");
for(i=0;i<N;i++)
for(j=0;j<N;j++)
if(w[j]>w[j+1])
{
t=w[j];
w[j]=w[j+1];
w[j+1]=t;
k=a[j];
a[j]=a[j+1];
a[j+1]=k;
}
for(i=0;i<N;i++)
{
printf("<%d,%d> \t",a[i],w[i]);
}
getch();
}
OUTPUT:
RESULT:
Thus the program is written & executed Successfully.
AIM:
Write a C program to populate an array with height of persons and find how many persons are
above the average height.
ALGORITHM:
#include<stdio.h>
#include<conio.h>
void main()
{
float height[20],avg;
int N,i,sum=0,count=0;
clrscr(); // if code did’t work remove this line
printf("Enter the no. of persons");
scanf("%d",&N);
printf("Enter the persons height on eby one\n");
for(i=0;i<N;i++)
{
scanf("%f",&height[i]);
sum=sum+height[i];
}
avg=sum/N;
for(i=0;i<N;i++)
{
if(height[i]>avg)
{ count=count+1; }
}
printf("Totally %d Persons are having above average height",count);
getch();
}
OUTPUT:
RESULT:
Thus the program is written & executed Successfully.
AIM:
To Write a C program to perform the following,
▪ Populate a two dimensional array with height and weight of persons
▪ Compute the Body Mass Index of the individuals.
ALGORITHM:
1. Start the program.
2. Read the Number of Persons.
3. Enter the height,weight values.
4. Read height in centi meters & weight in Kilograms of a person.
5. Height in Meter= height in cm/100;
6. Compute the body mass index value using the following formula
BMI=(weight in Kg)/(Height in Meters)2
7. Store the BMI values in a resultant array.
8. Print the results.
Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
int main() {
float height[20], weight[20], BMI[20], HIM[20];
int i, N;
printf("\nPerson\tHeight\tWeight\tBMI\n");
for (i = 0; i < N; i++) {
BMI[i] = weight[i] / (HIM[i] * HIM[i]);
printf("%d\t%.2f\t%.2f\t%.2f\n", (i + 1), HIM[i], weight[i], BMI[i]);
}
getch();
return 0;
}
OUTPUT:
Enter No. of the elements
3
RESULT:
Thus the program is written & executed Successfully.
AIM:
To Write a C program to calculate Matrix Addition and Multiplication.
ALGORITHM:1
ALGORITHM:2
PROGRAM:1
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
return 0;
}
PROGRAM:2
#include <stdio.h>
printf("\nOutput Matrix:\n");
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
printf("%d ", result[i][j]);
if (j == column - 1)
printf("\n");
}
}
}
int main() {
int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2;
printf("Enter rows and column for the first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for the second matrix: ");
scanf("%d %d", &r2, &c2);
return 0;
}
OUTPUT :1
10 8 6
OUTPUT :2
Enter elements:
Enter a11: 2
Enter a12: -3
Enter a13: 4
Enter a21: 53
Enter a22: 3
Enter a23: 5
Enter elements:
Enter a11: 3
Enter a12: 3
Enter a21: 5
Enter a22: 0
Enter a31: -3
Enter a32: 4
Output Matrix:
-21 22
159 179
RESULT:
Thus the C program to calculate Matrix Addition and Multiplication has been
executed successfully and the output was verified.
SPECIAL CHARACTER
AIM:
To Write a C program for reversing a string without changing the position of special
characters. (Example input:a@gh%;j and output:j@hg%;a)
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<conio.h> //if code did’t work remove this line
#include<ctype.h>
int main()
{
char str[50];
printf("Enter a string\n");
scanf("%s", str);
printf("Input string: %s\n", str);
reverse(str);
printf("Output string: %s\n", str);
return 0;
}
RESULT:
Thus the C program to Reverse String Without Changing The Position of Special
Character has been executed successfully and the output was verified.
AIM:
To Write a C program for Swapping of two numbers using call by value.
ALGORITHM:
PROGRAM:
#include <stdio.h>
void swap(int, int);
int main()
{
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(x, y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
void swap(int a, int b)
{
int temp;
temp = b;
b = a;
a = temp;
printf("Values of a and b is %d %d\n",a,b);
}
OUTPUT:
RESULT:
Thus the C program for Swapping of two numbers using call by value has been
executed successfully and the output was verified.
Ex.No.: 6b SORT THE LIST OF NUMBERS USING PASS BY REFERENCE
AIM:
Write a C program to sort numbers using pass by Reference.
ALGORITHM:
PROGRAM:
#include<stdio.h>
int main() {
int a[20], N, i;
printf("Enter the number of elements: ");
scanf("%d", &N);
printf("Enter the elements one by one:\n");
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
sort(a, N);
printf("\nSorted Order:\n");
for(i = 0; i < N; i++)
printf("%d\t", a[i]);
printf("\n");
return 0;
}
OUTPUT:
Enter the no. of elements
3
Enter the Elements one by one
100
3
45
Sorted Order
3 45 100
RESULT:
Thus the C program for Sorting the Values Using Pass By Reference has been executed
successfully and the output was verified
Ex.No.: 7a TOWERS OF HANOI
AIM:
To Write a C program to solve towers of Hanoi using recursion.
ALGORITHM:
PROGRAM:
#include <stdio.h>
void main()
{
int num;
printf("Enter the number of disks: ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are:\n");
towers(num, 'A', 'C', 'B');
}
RESULT:
Thus the program is written & executed successfully.
AIM:
To Write a C program for finding factorial of a number using recursion.
ALGORITHM:
PROGRAM:
#include <stdio.h>
long factorial(int n)
{
if (n == 0)
return 1;
else
return (n * factorial(n - 1));
}
int main()
{
int number;
long fact;
fact = factorial(number);
OUTPUT:
Enter a number: 6
Factorial of 5 is: 720
RESULT:
Thus the C program for finding factorial of a number using recursion has been executed
successfully and the output was verified
AIM:
To Write a C program to print the value and address of the variable using pointer to pointer.
ALGORITHM:
PROGRAM:
#include <stdio.h>
void main()
{
int a = 10;
int *p;
int **pp;
OUTPUT:
RESULT:
Thus the C program to print the value and address of the variable using pointer to pointer.
has been executed successfully and the output was verified
ALGORITHM:
PROGRAM:
#include <stdio.h>
int main () {
char *names[] = {"Zara Ali", "Hina Ali", "Nuha Ali", "Sara Ali", "Sameer Ali"};
int i = 0;
return 0;
}
OUTPUT:
RESULT:
Thus the C program to read and display 5 student names using array of pointer has been
executed successfully and the output was verified
ALGORITHM:
PROGRAM:
#include <stdio.h>
struct employee
{
char ename[25];
int eid;
char edes[20];
char edept[20];
int esal;
};
OUTPUT:
RESULT:
Thus the C program to generate employee salary slip using structure & pointer has been
executed successfully and the output was verified.
AIM:
To Write a C program to compute internal marks of students for five different subjects
using nested structure.
ALGORITHM:
PROGRAM:
#include <stdio.h>
struct student
{
char name[20];
int t[3][5]; // Changed size from [15][15] to [3][5] to match the input logic
int mark[5];
};
int main()
{
struct student s;
int j, k;
internal(s);
return 0;
}
OUTPUT:
RESULT:
Thus the C program to compute internal marks of students for five different subjects
using nested structure has been executed successfully and the output was verified.
AIM:
To Write a C program for accessing student details using union.
ALGORITHM:
PROGRAM:
OUTPUT:
RESULT:
Thus the C program for accessing student details using union has been executed
successfully and the output was verified.
AIM:
Write a C program to count the number of account holders whose balance is less than the
minimum balance using sequential access file.
ALGORITHM:
PROGRAM:
#include <stdio.h>
void insert();
void count();
int main(void)
{
int choice = 0;
while (choice != 3)
{
printf("\n1 insert records\n");
printf("2 Count min balance holders\n");
printf("3 Exit\n");
printf("Enter choice:");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
count();
break;
}
}
return 0; // Added return statement
}
void insert()
{
unsigned int account;
char name[30];
double balance;
FILE* cfPtr;
if ((cfPtr = fopen("clients.dat", "a")) == NULL) { // Changed "w" to "a" for append mode
puts("File could not be opened");
}
else {
int records, i = 0;
printf("Enter the No. of records: ");
scanf("%d", &records);
while (i < records)
{
printf("Enter the account, name, and balance: ");
scanf("%u%29s%lf", &account, name, &balance); // Changed %d to %u for unsigned int
fprintf(cfPtr, "%u %s %.2f\n", account, name, balance); // Changed %d to %u for unsigned
int
i++;
}
fclose(cfPtr);
}
}
void count()
{
unsigned int account;
char name[30];
double balance;
float minBal = 5000.00;
int count = 0;
FILE *cfPtr;
OUTPUT:
1 insert records
2 Count min balance holders
3 Exit
Enter choice:1
Enter the No. of records 2
Enter the account, name, and balance.1001 A 10000
Enter the account, name, and balance.1002 B 300
1 insert records
2 Count min balance holders
3 Exit
Enter choice:2
Account Name Balance
1002 B 300.00
The number of account holders whose balance is less than the minimum balance: 1
1 insert records
2 Count min balance holders
3 Exit
Enter choice:
RESULT:
Thus the c program to count the number of account holders whose balance is less than the
minimum balance using sequential access file has been executed successfully and the output was
verified.
AIM:
To Write a C program to update telephone details of an individual or a company into a
telephone directory using random access file.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <string.h>
struct teledir
{
int no;
char name[4]; // Increased the size to accommodate null terminator
};
int main()
{
struct teledir t1, t2, t3;
int i, n, p, newp;
FILE *fp, *fp1;
fp = fopen("td.txt", "w");
if (fp == NULL)
{
printf("Error opening file.\n");
return 1;
}
fp = fopen("td.txt", "r");
if (fp == NULL)
{
printf("Error opening file.\n");
return 1;
}
fp = fopen("td.txt", "r+");
if (fp == NULL)
{
printf("Error opening file.\n");
return 1;
}
fp = fopen("td1.txt", "r");
if (fp == NULL)
{
printf("Error opening file.\n");
return 1;
}
return 0;
}
OUTPUT:
Enter the no of records
3
Enter the record
111 abc
222 xyz
333 nmo
111abc
222xyz
333nmo
Enter number to be modified & a new number
222 9898
111 abc
9898 xyz
333 nmo
RESULT:
Thus the c program to update telephone details of an individual or a company into a
telephone directory using random access file has been executed successfully and the output was
verified.
AIM:
To Write a C program to find greatest of three numbers using ternary operator in
preprocessor directives.
ALGORITHM:
PROGRAM:
OUTPUT:
RESULT:
Thus the c program to find greatest of three numbers using ternary operator in preprocessor
directives has been executed successfully and the output was verified.