[go: up one dir, main page]

0% found this document useful (0 votes)
4 views24 pages

c Lab Manual

The document outlines various C programming exercises including calculating the area and circumference of a circle, addition of two numbers, simple interest, temperature conversion, and determining leap years. It provides algorithms and sample code for each program, demonstrating the use of I/O statements, decision-making constructs, and loops. Each program is followed by example outputs and confirms successful execution.

Uploaded by

nedumaran202
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)
4 views24 pages

c Lab Manual

The document outlines various C programming exercises including calculating the area and circumference of a circle, addition of two numbers, simple interest, temperature conversion, and determining leap years. It provides algorithms and sample code for each program, demonstrating the use of I/O statements, decision-making constructs, and loops. Each program is followed by example outputs and confirms successful execution.

Uploaded by

nedumaran202
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/ 24

1

1.I/O STATEMENTS
{
1(a)AREA AND CIRCUM int a,b,c;
Aim: printf("enter a and b");
To write a c program to print the area and scanf("%d%d",&a,&b);
circumference of a circle. c=a+b;
printf("Addition of a and b is %d",c);
Algorithm: }

Step 1 : Start Output:


Step 2 : Get radius from the user. enter a and b
Step 3 : Calculate area a =3.14*r*r and print area 2
Step 4 : Calculate circumference c=2*3.14*r. 3
Step 5 : Print the circumference. Addition of a and b is 5
Step 6 : Stop. Result:
The above program was executed successfully.
Program:

#include<stdio.h> 1(c) SIMPLE INTEREST


int main() Aim:
{ To write a c program to print the simple
float r,a,c; interest
printf("enter the radius");
scanf("%f",&r); Algorithm:
a=3.14*r*r;
printf("area of a circle is %f",a); Step 1 : Start
c=2*3.14*r; Step 2 : Get principle, year and rate from the user.
printf("circumference of the circle is %f",c); Step 3 : Calculate si=(p*n*r)/100
return 0; Step 4 : Print the output.
} Step 5 : Stop.

Output: Program:
enter the radius 5
area of a circle is 78.5 #include<stdio.h>
circumference of the circle is 31.4000 int main()
{
Result: int p,n,r,si;
The above program was executed successfully. printf("enter the principle : ");
scanf("%d",&p);
1(b) ADDITION OF TWO NUMBERS printf("enter the year : ");
scanf("%d",&n);
Aim: printf("enter the rate : ");
To write a c program to print the addition scanf("%d",&r);
of two numbers. si=(p*n*r)/100;
Algorithm: printf("simple interest is %d",si);
Step 1 : Start return 0;
Step 2 : Get a and b value from the user. }
Step 3 : Calculate c=a+b
Step 4 : Print the output. Output:
Step 5 : Stop. enter the principle : 10000
enter the the year : 2
Program: enter the rate : 6
simple interest is 1200
#include<stdio.h> Result:
void main() The above program was executed successfully.
AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments
2
printf("enter a, b c value : ");
1(d) TEMPERATURE CONVERSION scanf("%d%d%d",&a,&b,&c);
Aim: if((a>b)&&(a>c))
To write a c program to print the {
conversion of temperature. printf("a is big");
}
Algorithm: else if(b>c)
{
Step 1 : Start printf("b is big");
Step 2 : Get the Celsius value from the user. }
Step 3 : Calculate f=(1.8*c)+32 else
Step 4 : Print the ouput printf("c is big");
Step 5 : Stop. return 0;
}
Program: Output:
enter a, b c value : 1
#include<stdio.h> 3
int main() 7
{ c is big
int f,c; Result:
printf("enter the celsius : "); The above program was executed successfully.
scanf("%d",&c);
f=(1.8*c)+32; 2(b) LEAP YEAR
printf("temperature in farenheit is %d",f); Aim:
return 0; To write a c program to find Leap year or not.
} Algorithm:
Step 1 : Start
Output: Step 2 : Get the year from the user.
enter the celsius : 33 Step 3 : if year%4==0 print it is leap year.
temperature in farenheit is 91 Step 4 : otherwise it is not leap year.
Step 5 : Stop.
Result: Program:
The above program was executed successfully. #include<stdio.h>
int main()
Decision making Constructs {
int year;
2(a).BIGGEST OF THREE NUMBERS printf("enter the year");
Aim: scanf("%d",&year);
To write a c program to find the largest of if(year%4==0)
3 numbers conversion of temperature. {
printf("It is leap year");
Algorithm: }
Step 1 : Start else
Step 2 : Get the a,b,c value from the user. {
Step 3 : check if a is greater than b and a is greater printf("It is not a leap year");
than c, then print a is greater.
Step 4 : If b is greater than c, then print b is }
greater otherwise c is greater. return 0;
Step 5 : Stop. }
Output:
Program: enter the year : 2024
#include<stdio.h> It is leap year
int main()
Result:
{
The above program was executed successfully.
int a,b,c;
AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments
3
2(c) Day of the week: 2(d) Break Statement
Aim: Aim:
To write a c program to print the day of the week. To write a c program to perform the break
Algorithm: statement.
Step 1 : Start Algorithm:
Step 2 : Get the day from the user. Step 1 : Start
Step 3 : Pass the value to switch and match it with Step 2 : create a for loop
the case. Step 3 : checks the condition i<=5 in loop and
Step 4 : print the output. execute true part, check if i==3 then continue..
Step 5 : Stop. Step 4 : otherwise print the i and increment it and
Program: go to step 3 until for become false.
include<stdio.h> Step 5 : Stop.
int main() Program:
{ #include<stdio.h>
int n; int main()
printf("Enter any number (1 to 7)"); {
int i;
scanf("%d",&n);
for(i=1;i<=5;i++)
switch (n) {
{ if(i==3)
case 1: break;
printf("Today is Monday"); printf("\n%d",i);
break; }
case 2: return 0;
printf("Today is Tuesday"); }
break; Output:
case 3: 1
2
printf("Today is Wednesday");
Result:
break; The above program was executed successfully.
case 4: 2(e) Continue Statement:
printf("Today is Thursday"); Aim:
break; To write a c program to print the day of the week.
case 5: Algorithm:
printf("Today is Friday"); Step 1 : Start
break; Step 2 : create a for loop
case 6: Step 3 : checks the condition i<=5 in loop and
printf("Today is Saturday"); execute true part, check if i==3 then continue..
Step 4 : otherwise print the i and increment it and
break;
go to step 3 until for become false.
case 7: Step 5 : Stop.
printf("Today is Sunday"); Program:
break; #include<stdio.h>
default: int main()
printf("Invalid number entered"); {
} int i;
return 0; for(i=1;i<=5;i++)
} {
Output: if(i==3)
continue;
Enter any number (1 to 7) : 5
printf("\n%d",i);
Today is Friday }
Result: return 0;
The above program was executed successfully. }

AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments


4
Output:
1 Algorithm:
2
4 Step 1 : Start
5 Step 2 : Get n value from the user.
Result: Step 3 : Initialize i=1 and sum=0
The above program was executed successfully. Step 4 : Calculate sum+=I and increment i value.
Step 5 : while i<=n repeat the step 4 until while
Loops: for, do-while, while becomes false.
3(a) Print the natural numbers using while Step 6: Print the output.
loop: Step 7 : Stop.
Aim:
To write a c program to print the natural Program:
numbers using while loop. #include <stdio.h>
Algorithm: int main()
Step 1 : Start {
Step 2 : Get num value. int i,n,sum=0;
Step 3 : while num<=10 printf("\nEnter the number : ");
Step 4 : print the output scanf("%d",&n);
Step 5 : Stop. i=1;
do
Program: {
#include <stdio.h> sum=sum+i;
int main() i++;
{ }while(i<=n);
int num=1; printf("\nTotal is %d",sum);
while(num<=10) return 0;
{ }
printf("%d\n",num); Output:
num++; Enter the number : 5
} Total is 15
return 0;
} Result:
The above program was executed successfully.
Output:
1 3(c) Multiplication Table using for loop:
2
3 Aim:
4 To write a c program to print the
5 multiplication table using for loop.
6 Algorithm:
7 Step 1 : Start
8 Step 2 : Get table value a from user.
9 Step 3 : for(b=1;b<=20;b++)
10 Step 4 : c=a*b;
Step 5 : Print the output
Result: Step 6 : Stop.
The above program was executed successfully.
Program:
3(b) Sum of natural numbers using do-while
loop: #include <stdio.h>
int main()
Aim: {
To write a c program to print the sum of n Natural int a,b,c;
numbers using do-while loop. printf("\nEnter the table value a : ");
AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments
5
scanf("%d",&a); printf("\n%d is palindrome",m);
for(b=1;b<=10;b++) else
{ printf("\n%d is not palindrome",m);
c=a*b; }
printf("\n %d x %d = %d",a,b,c);
Ouput:
}
} Enter a number : 151
Output: 151 is palindrome
Enter the table value a : 5 Result:
5x1=5 The above program was executed successfully.
5 x 2 = 10 3(e) Amstrong Number:
5 x 3 = 15 Aim:
5 x 4 = 20 To create a c program to find the given number is
5 x 5 = 25 Amstrong or not.
5 x 6 = 30 Algorithm:
5 x 7 = 35 Step 1 : Start
5 x 8 = 40 Step 2 : Get a input from user.
5 x 9 = 45 Step 3 : check the condition in while loop n!=0. If
5 x 10 = 50 it is true, divide the number by 10 and get the
Result: remainder and multiply it 3 times and store it in
The above program was executed successfully. sum value and store the quotient value to number
and do the process until number becomes zero.
3(d) Palindrome number: Step 4 : compare the sum value with the given
Aim: input value. If it matches print same otherwise not
To create a c program to find the given number is same.
palindrome or not. Step 5 : Stop.
Algorithm:
Step 1 : Start Program:
Step 2 : Get a input from user. #include <stdio.h>
Step 3 : check the condition in while loop n!=0. If int main()
it is true, divide the number by 10 and get the {
remainder and store it in sum value and store the
int n,r,s,m;
quotient value to number and do the process until
number becomes zero. printf("\nEnter a number : ");
Step 4 : compare the reversed value with the given scanf("%d",&n);
value. If it matches print same otherwise not same. m=n;
Step 5 : Stop. s=0;
Program: while(n!=0)
#include <stdio.h>
{
int main()
r=n%10;
{
s=s+r*r*r;
int n,r,s,m;
n=n/10;
printf("\nEnter a number : ");
}
scanf("%d",&n);
if(m==s)
m=n;
printf("\n%d is Amstrong",m);
s=0;
else
while(n!=0)
printf("\n%d is not Amstrong",m);
{
}
r=n%10;
Ouput:
s=s*10+r;
Enter a number : 153
n=n/10;
151 is Amstrong
}
Result:
if(m==s)
The above program was executed successfully.
AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments
6
4(a) Sum and Average of Marks using Array. Step 4 : create 2 for loops for row and columns
Step 5 : calculate d[i][j]=a[i][j]+b[i][j];
Aim: Step 6: print the result matrix
To create a c program to find the sum and average Step 7 : Stop.
of 5 marks.
Algorithm: Program:
#include<stdio.h>
Step 1 : Start int main()
Step 2 : Get 5 marks from user. {
Step 3 : calculate sum=sum+a[i];
int a[5][5],b[5][5],d[5][5],i,j,r,c;
Step 4 : calculate avg=sum/5;
Step 5 : Print the output printf("\nEnter row and col : ");
Step 6 : Stop. scanf("%d%d",&r,&c);
for(i=0;i<r;i++)
Program: for(j=0;j<c;j++)
{
#include<stdio.h> printf("\nEnter Matrix 1 ele : ");
int main()
scanf("%d",&a[i][j]);
{
int m[10],i,n,sum; }
float avg; for(i=0;i<r;i++)
printf("\nEnter n : "); for(j=0;j<c;j++)
scanf("%d",&n); {
for(i=0;i<n;i++) printf("\nEnter Matrix 2 ele : ");
{ scanf("%d",&b[i][j]);
printf("\nEnter the ele : ");
}
scanf("%d",&m[i]);
} for(i=0;i<r;i++)
sum=0; {
for(i=0;i<n;i++) for(j=0;j<c;j++)
sum=sum+m[i]; {
avg=(float)sum/n; d[i][j] = a[i][j]+b[i][j];
printf("\nSum = %d",sum);
printf("\t%d",d[i][j]);
printf("\nAverage = %f",avg);
return(0); }
} printf("\n");
Output: }
Enter n : 5 return(0);
Enter the mark : 95 }
Enter the mark : 90 Output:
Enter the mark : 85 Enter row and col :
Enter the mark : 90 2
Enter the mark : 100 2
Sum = 460 Enter Matrix 1 ele : 2
Average = 92.000000 Enter Matrix 1 ele : 2
Result: Enter Matrix 1 ele : 2
The above program was executed successfully. Enter Matrix 1 ele : 2
4(b) Matrix Addition: Enter Matrix 2 ele : 3
Aim: Enter Matrix 2 ele : 3
To create a c program to find the addition of 2 Enter Matrix 2 ele : 3
matrices Enter Matrix 2 ele : 3
Algorithm: 5 5
Step 1 : Start 5 5
Step 2 : Get row and col from user. Result:
Step 3 : Get the matrix elements from user. The above program was executed successfully.
AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments
7
4(c) Biggest element of an Array traverse the array , if its true print element found
Aim: otherwise element not found.
To create a c program to find the biggest element Step 5 : Stop.
in an array.
Algorithm: Program:
Step 1 : Start #include<stdio.h>
Step 2 : Get array elements from user. int main()
Step 3 : check the condition if (a[i]>big) and {
traverse the array , if its true store the a[i] to big.
int a[10],i,n,sno,f;
Step 4 : Print the big element
Step 5 : Stop. printf("\nEnter n : ");
scanf("%d",&n);
Program: for(i=0;i<n;i++)
#include<stdio.h> {
int main() printf("\nEnter the ele : ");
{ scanf("%d",&a[i]);
int a[10],i,n,big; }
printf("\nEnter n : "); printf("\nEnter the search ele : ");
scanf("%d",&n); scanf("%d",&sno);
for(i=0;i<n;i++) f=0;
{ for(i=0;i<n;i++)
printf("\nEnter the ele : "); if(a[i]==sno)
scanf("%d",&a[i]); {
} f=1;
big=a[0]; break;
for(i=0;i<n;i++) }
if(a[i]>big) if(f==1)
big=a[i]; printf("\nSearch element found");
printf("\nBiggest element is %d",big); else
return(0); printf("\nSearch element not found");
} return(0);
}
Output: Output:
Enter n : 5 Enter n : 5
Enter the ele : 12 Enter the ele : 12
Enter the ele : 22 Enter the ele : 76
Enter the ele : 33 Enter the ele : 69
Enter the ele : 55 Enter the ele : 88
Enter the ele : 21 Enter the ele : 55
Biggest element is 55 Enter the search ele : 55
Result: Search element found
The above program was executed successfully. Result:
4(d) Search an element from the array The above program was executed successfully.
Aim: 5(a) String length
To create a c program to search an element from Aim:
the array To create a c program to find the length of the
Algorithm: given string
Step 1 : Start Algorithm:
Step 2 : Get array elements from user. Step 1 : Start
Step 3 : Get the search element from user. Step 2 : Get the input string from user.
Step 4 : check the condition if (a[i]==sno) and Step 3 : find the length of the string using strlen
AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments
8
function. 5(c) String Concatenation
Step 4 : Print the length Aim:
Step 5 : Stop. To create a c program to join 2 strings.
Program: Algorithm:
#include<stdio.h> Step 1 : Start
#include<string.h> Step 2 : Get 2 strings from user.
int main() Step 3 : join the 2 strings using strcat function.
Step 4 : Print the output
{
Step 5 : Stop.
char a[20];
int i; Program:
printf("Enter a string: "); #include<stdio.h>
gets(a); #include<string.h>
i=strlen(a); int main()
printf("Length of string: %d", i); {
return 0; char a[10],b[10],c[20];
} int i,j,n;
Output: printf("\nEnter 2 strings : ");
Enter a string: Good Morning gets(a);
Length of string: 12 gets(b);
Result: strcpy(c,a);
The above program was executed successfully. strcat(c,b);
printf("\n%s",c);
5(b) String copy
return 0;
Aim:
To create a c program to copy a string to another }
string Output:
Algorithm: Enter 2 strings:
Step 1 : Start computer
Step 2 : Get the input string from user. science
Step 3 : copy the string to another string using
computer science
strcpy function.
Result:
Step 4 : Print the copied string
The above program was executed successfully.
Step 5 : Stop.
Program:
5(d) String Palindrome
#include<stdio.h>
#include<string.h> Aim:
int main() To create a c program to find the string
{ palindrome.
char a[10],b[10]; Algorithm:
int i,j,n,f; Step 1 : Start
Step 2 : Get a string from user.
printf("\nEnter a string : ");
Step 3 : reverse the given string and compare with
scanf("%s",a); the original string .
strcpy(b,a); Step 4 : check if (value==0) print same otherwise
printf("\n%s",b); not same
return 0; Step 5 : Stop.
}
Output: Program:
#include<stdio.h>
Enter a string : Computer
#include<string.h>
Computer
int main()
Result:
The above program was executed successfully. {
AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments
9
char a[10],b[10]; y=t;
printf("\nEnter a string : "); printf("\nAfter swap in Function Block A=%d
scanf("%s",a); b=%d",x,y);
strcpy(b,a); }
strrev(b); }
if(strcmp(b,a)==0) Output:
printf("same"); Enter a and b
else 5
printf("Not same"); 8
Before Swap in main block A=5 b=8
return 0;
After swap in Function Block A=8 b=5
} After Swap in in main block A=5 b=8

Output: Result:
Enter a string : ramar The above program was executed successfully.
same
Result: 6(b) Pass by reference
The above program was executed successfully. Aim:
To create a c program for pass by reference
6(a) Pass by value Algorithm:
Aim: Step 1 : Start
To create a c program for pass by value Step 2 : Get a and b values from user.
Algorithm: Step 3 : print the values before swapping.
Step 1 : Start Step 4 : call the swap function and pass the
Step 2 : Get a and b values from user. address instead of value - swap(&a,&b);
Step 3 : print the values before swapping. Step 5 : swap the values using temp variable.
Step 4 : call the swap function swap(a,b); Step 6 : print the a and b values after swapping.
Step 5 : swap the values using temp variable. Step 7 : Stop.
Step 6 : print the a and b values after swapping.
Step 7 : Stop. Program:
#include <stdio.h>
Program: void swap(int * ,int * );
#include <stdio.h> int main()
void swap(int ,int ); {
int main() int a,b;
{ printf("\nEnter a and b ");
int a,b; scanf("%d%d",&a,&b);
printf("\nEnter a and b "); printf("\nBefore Swap in main block A=%d
scanf("%d%d",&a,&b); b=%d",a,b);
printf("\nBefore Swap in main block A=%d swap(&a,&b);
b=%d",a,b); printf("\nAfter Swap in in main block A=%d
swap(a,b); b=%d",a,b);
printf("\nAfter Swap in in main block A=%d }
b=%d",a,b); void swap(int *x,int *y)
} {
void swap(int x,int y) int t;
{ {
int t; t=*x;
{ *x=*y;
t=x; *y=t;
x=y;
AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments
10
printf("\nAfter swap in Function Block A=%d for(i=0;i<n-1;i++)
b=%d",*x,*y); for(j=i+1;j<n;j++)
} if(a[i]>a[j])
} {
Output: c=a[i];
Enter a and b a[i]=a[j];
5 a[j]=c;
8 }
Before Swap in main block A=5 b=8
}
After swap in Function Block A=8 b=5
After Swap in in main block A=8 b=5 Output:
Enter n : 5
Result: Enter the ele : 12
The above program was executed successfully. Enter the ele : 66
Enter the ele : 5
6(c) Passing Array to function Enter the ele : 32
Aim:
Enter the ele : 3
To create a c program for passing array to a
function. 3
Algorithm: 5
Step 1 : Start 12
Step 2 : Get array values and n from the user. 32
Step 3 : call the sort function pass the array and n 66
sort(a,n);
Step 4 : Arrange the array using 2 for loops and Result:
compare a[i]>a[j] if its true swap the values. Do
the process until the loop false. The above program was executed successfully.
Step 5 : print the sorted array.
Step 6 : Stop.

7(a) Factorial using Recursion


Program:
#include<stdio.h> Aim:
void sort(int*,int); To create a c program to find factorial using
int main() recursive function.
Algorithm:
{
Step 1 : Start
int a[10],i,n,j,c; Step 2 : Get the input from the user.
printf("\nEnter n : "); Step 3 : call the factorial function
scanf("%d",&n); Step 4 : check if n==1 or 0 return 1
for(i=0;i<n;i++) Otherwise return (n*fact(n-1))
{ Step 5 : print the output.
Step 6 : Stop.
printf("\nEnter the ele : ");
scanf("%d",&a[i]); Program:
} #include <stdio.h>
sort(a,n); int fact(int);
for(i=0;i<n;i++) int main()
printf("\n%d",a[i]); {
return(0); int n,f;
} printf("\nEnter n : ");
void sort(int *a,int n) scanf("%d",&n);
{ f=fact(n);
int i,j,c;
AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments
11
printf("\nFactorial of %d is : %d ",n,f); return(fibo(n-1)+fibo(n-2));
} }
int fact(int n) Output:
{ Enter n : 10
if(n==0||n==1) 0 1 1 2 3 5 8
return 1;
13 21 34
else
return(n*fact(n-1)); Result:
} The above program was executed successfully.
Output:
Enter n : 5
7(c) Binary search using recursion
Factorial of 5 is : 120
Aim:
Result:
To create a c program to find factorial using
The above program was executed successfully. recursive function.
Algorithm:
7(b) Fibonacci series using recursion
Step 1 : Start
Aim: Step 2 : Get the input from the user.
To create a c program to find factorial using Step 3 : call the binary function and pass the array,
recursive function. low, high and search number to it.
Algorithm: Step 4 : find mid=(low+high)/2 . compare mid
Step 1 : Start value with search number. If its found print it
Step 2 : Get the input from the user. otherwise return BinarySearch(a,sno,l,m-1);
Step 3 : call the factorial function or return BinarySearch(a,sno,m+1,h);
Step 4 : check if n==0 return 0 , if n==1 return 1 Step 5 : print the output.
Otherwise return(fibo(n-1)+fibo(n-2)); Step 6 : Stop.
Step 5 : print the output.
Step 6 : Stop. Program:
#include <stdio.h>
int BinarySearch(int *,int ,int ,int );
Program:
int main()
#include <stdio.h>
{
int fibo(int);
int a[50],l,m,h,i,n,sno,f;
int main()
printf("Enter Array size: ");
{
scanf("%d",&n);
int n,f,i;
printf("Enter the values in Sorted order \n");
printf("\nEnter n : ");
for(i=0;i<n;i++)
scanf("%d",&n);
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("Enter the search value : ");
{
scanf("%d",&sno);
printf("%d\t",fibo(i));
l=0;
}
h=n-1;
}
f=BinarySearch(a,sno,l,h);
int fibo(int n)
if(f==1)
{
printf("search element found");
if(n==0)
else
return 0;
printf("search element not found");
else if(n==1)
return 0;
return 1;
}
else
int BinarySearch(int *a,int sno,int l,int h)
AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments
12
{ printf("\nEnter a :");
int m,f=0; scanf("%d",&a);
while(l<=h) t=a;
{ square(&a);
m=(l+h)/2; printf("\nSquare of %d is %d",t,a);
if(a[m]==sno) }
{ void square(int *a)
f=1; {
break; *a=(*a)*(*a);
} }
else if(a[m]>sno) Output:
return BinarySearch(a,sno,l,m-1); Enter a :5
else if(a[m]<sno) Square of 5 is 25
return BinarySearch(a,sno,m+1,h);
Result:
}
return(f); The above program was executed successfully.
} 8(b) Pointer to Array:
Output:
Aim:
Enter Array size: 5
To create a c program using pointer to array
Enter the values in Sorted order Algorithm:
5 Step 1 : Start
10 Step 2 : create a pointer variable and store the
15 array first element address to it.
20 Step 3 : get the array input from user
Step 4 : sum the array values
25
Step 5 : using *(p+i) instead of a[i] in the print.
Enter the search value : 20 Step 6 : print the output.
search element found Step 7 : Stop.
Result:
The above program was executed successfully. Program:
#include <stdio.h>
int main()
8(a) Pointer to Function {
int a[10],i,n,sum;
Aim:
To create a c program using pointer to function int *p;
Algorithm: p=&a[0];
Step 1 : Start printf("\nEnter n : ");
Step 2 : Get the input from the user. scanf("%d",&n);
Step 3 : call the square function and pass address for(i=0;i<n;i++)
to work in pointer
{
Step 4 : do the square process
Step 5 : print the output. printf("\nEnter the ele : ");
Step 6 : Stop. scanf("%d",p+i);
}
Program: sum=0;
#include <stdio.h> for(i=0;i<n;i++)
void square(int *); sum=sum+*(p+i);
int main() printf("\nsum = %d",sum);
{ }
int a,t;

AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments


13
Output: AVCCE
Enter n : 3 Result:
Enter the ele : 12 The above program was executed successfully
Enter the ele : 6 8(d) Pointer to pointer:
Enter the ele : 2 Aim:
sum = 20 To create a c program using pointer to pointer
Algorithm:
Result: Step 1 : Start
The above program was executed successfully. Step 2 : create a pointer variable and double
pointer variable
8(c) Pointer to String: Step 3 assign the a variable address to *p
Aim: Step 4 : assign the *p variable address to **pp
To create a c program using pointer to string Step 5 : print the output.
Algorithm: Step 6 : Stop.
Step 1 : Start
Step 2 : create a pointer variable and store the Program:
array first element address to it. #include<stdio.h>
Step 3 get the input string from user void main ()
Step 4 : use *(p+i) instead of a[i] in the print and {
loop condition. int a = 10;
Step 5 : print the output.
int *p;
Step 6 : Stop.
int **pp;
Program: p = &a;
#include<stdio.h> pp = &p;
void main() printf("address of a: %u\n",p);
{ printf("address of p: %u\n",pp);
char a[20]; printf("value stored at a: %d\n",a);
char *p=&a[0]; printf("value stored at p: %d\n",*p);
int i; printf("value stored at pp: %d\n",**pp);
printf("Enter a string: "); }
scanf("%s",a); Output:
i=0; address of a: 1127528932
while(*(p+i)!='\0') address of p: 1127528920
{
value stored at a: 10
printf("%c",*(p+i));
i++; value stored at p: 10
} value stored at pp: 10
}
Result:
Output:
Enter a string: AVCCE The above program was executed successfully.

AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments


14
8(e) Array of Pointers
Aim:
To create a c program using array of pointers
Algorithm:
Step 1 : Start
Step 2 : create an array pointer variable and 3 integer variable
Step 3 assign the address of the 3 integers to pointer array
Step 4 : print the output.
Step 5 : Stop.

Program:
#include<stdio.h>
int main ()
{
int *pa[3];
int a=5,b=10,c=15,i;
pa[0]=&a;
pa[1]=&b;
pa[2]=&c;
for(i=0;i<3;i++)
printf("\nAddress = %u\tValue = %d",pa[i],*pa[i]);
return 0;
}
Output:
Address = 2618387804 Value = 5
Address = 2618387800 Value = 10
Address = 2618387796 Value = 15
Result:
The above program was executed successfully.
9(a) Nested Structure
Aim:
To create a c program using nested structure
Algorithm:
Step 1 : Start
Step 2 : create a structure for student detail
Step 3 : create a structure for dob detail
Step 4 : create a object for student struct
Step 5 : print the output.
Step 6 : Stop.

Program:
#include<stdio.h>
struct date
{
int d;
char m[20];
int y;
};

AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments


15
struct stud
{
int sno;
char name[20];
struct date dob;
};
int main ()
{
struct stud s;
printf("\nEnter the Student no,name,dob : ");
scanf("%d%s%d%s%d",&s.sno,&s.name,&s.dob.d,&s.dob.m,&s.dob.y);
printf("\nStudent No : %d",s.sno);
printf("\nStudent Name : %s",s.name);
printf("\nStudent DOB : %d-%s-%d",s.dob.d,s.dob.m,s.dob.y);
return 0;
}
Output:
Enter the Student no,name,dob :
12
krish
14
03
2005
Student No : 12
Student Name : krish
Student DOB : 14-03-2005
Result:
The above program was executed successfully
9(b) Array of Structure
Aim:
To create a c program using array of structure
Algorithm:
Step 1 : Start
Step 2 : create a structure for employee detail
Step 3 : create an array of variable for employee struct
Step 4 : get the input from user
Step 5 : print the output.
Step 6 : Stop.
Program:
#include <stdio.h>
struct Employee
{
int eno;
char ename[20];
float salary;
};
AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments
16
int main()
{
struct Employee e[100];
int n,i;
printf("\nEnter the size :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the Employee no ,name , salary ");
scanf("%d%s%f",&e[i].eno,&e[i].ename,&e[i].salary);
}
printf("\nEmployee no \tEmployee name \tEmployee Salary");
for(i=0;i<n;i++)
{
printf("\n%d\t\t%s\t\t%0.1f", e[i].eno,e[i].ename, e[i].salary);
}
return(0);
}
Output:
Enter the size :2
Enter the Employee no ,name , salary
12
shyam
50000
Enter the Employee no ,name , salary
14
krish
60000
Employee no Employee name Employee Salary
12 shyam 50000.0
14 krish 60000.0
Result:
The above program was executed successfully
9(c) Pointer to Structure
Aim:
To create a c program using pointer to structure
Algorithm:
Step 1 : Start
Step 2 : create a structure for student detail
Step 3 : create an array variable for student struct
Step 4 : create the pointer variable for struct and store the address. Create a sort function, sort the names
using string compare
Step 5 : print the output.
Step 6 : Stop.

AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments


17
Program:
#include <stdio.h>
#include <string.h>
struct stud
{
int sno;
char sname[20];
};
void sort(struct stud *,int);
int main()
{
struct stud s[10];
struct stud *p;
p=&s[0];
int n,i;
printf("\nEnter the size : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the Student no : ");
scanf("%d",&s[i].sno);
printf("\nEnter the Student name : ");
scanf("%s",s[i].sname);
}
sort(p,n);
printf("\nStudent No\tStudent Name");
for(i=0;i<n;i++)
{
printf("\n%d\t\t\t%s",p->sno,p->sname);
p++;
}
return(0);
}
void sort(struct stud *s,int n)
{
int i,j;
struct stud t;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(strcmp(s[i].sname,s[j].sname)>0)
{
t=s[i];
s[i]=s[j];
s[j]=t;
}
}

AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments


18
Output:
Enter the size : 3
Enter the Student no : 12
Enter the Student name : Krish
Enter the Student no : 14
Enter the Student name : Arun
Enter the Student no : 16
Enter the Student name : Shyam
Student No Student Name
14 Arun
12 Krish
16 Shyam
Result:
The above program was executed successfully
9(d) Unions
Aim:
To create a c program using union
Algorithm:
Step 1 : Start
Step 2 : create a union data type
Step 3 : create an object for union
Step 4 : use sizeof to find the size.
Step 5 : print the output.
Step 6 : Stop.
Program:
#include<stdio.h>
union emp
{
int sno;
float wt;
};
int main ()
{
union emp u;
u.sno=5;
u.wt=55.00;
printf("\nEmployee No : %d",u.sno);
printf("\nEmployee weight : %f",u.wt);
printf("\nsize of union = %d ",sizeof(u));
return 0;
}
Output:
Employee No : 1113325568
Employee weight : 55.000000
size of union = 4
Result:
The above program was executed successfully
AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments
19
10(a) File Operation -1
Aim:
To create a c program for file handling using getc and putc
Algorithm:
Step 1 : Start
Step 2 : create a file pointer FILE *f
Step 3 : open a file using file name and mode sample.txt in w mode
Step 4 : use putc to write a file. Close the file
Step 5 : open the file in read mode. Use getc to read characters.
Step 6 : print the output.
Step 7 : Stop

Program:
#include<stdio.h>
int main ()
{
char fname[20],ch;
FILE *f;
printf("\nEnter the file name");
scanf("%s",fname);
f=fopen(fname,"w");
printf("\nEnter some text and press * to terminate") ;
while((ch=getchar())!='*')
putc(ch,f);
fclose(f);
printf("\nThe content is read from file \n");
f=fopen(fname,"r");
while(!feof(f))
{
ch=getc(f);
putchar(ch);
}
fclose(f);
return 0;
}
Output:
Enter the file name sample.txt
Enter some text and press * to terminate This is File operation in C programming.*
The content is read from file
This is File operation in C programming.
Result:
The above program was executed successfully

AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments


20
10(a) File Operation -2
Aim:
To create a c program for file handling using fread and fwrite

Algorithm:
Step 1 : Start
Step 2 : create a file pointer FILE *f
Step 3 : open a file using file name and mode sample.txt in w mode
Step 4 : use fwrite to write a file. Pass struct address, size of struct, size, file pointer Close the file
Step 5 : open the file in read mode. Use fread to read from file. Pass struct address, size of struct, size, file
pointer , Close the file
Step 6 : print the output.
Step 7 : Stop

Program:
#include<stdio.h>
struct stud
{
int sno;
char sname[20];
};
int main ()
{
char fname[20],ch;
struct stud s[50];
int i,n;
FILE *f;
printf("\nEnter the file name");
scanf("%s",fname);
f=fopen(fname,"w");
printf("\nEnter the size : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the Student no : ");
scanf("%d",&s[i].sno);
printf("\nEnter the Student name : ");
scanf("%s",s[i].sname);
fwrite(&s[i],sizeof(s[i]),1,f);
}
fclose(f);
f=fopen (fname, "r");
AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments
21
for(i=0; i<n; i++){
printf ("\ndetails of student %d are", i+1);
fread (&s[i], sizeof (s[i]) ,1,f);
printf("\nstudent number = %d", s[i]. sno);
printf("\nstudent name = %s", s[i]. sname);
}
fclose(f);
return 0;
}
Output:
Enter the file name
ganesh.txt
Enter the size : 3
Enter the Student no : 12
Enter the Student name : krish
Enter the Student no : 14
Enter the Student name : harris
Enter the Student no : 15
Enter the Student name : shyam
details of student 1 are
student number = 12
student name = krish
details of student 2 are
student number = 14
student name = harris
details of student 3 are
student number = 15
student name = shyam
Result: The above program was executed successfully

AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments


22
10(c) Random Access in file
Aim:
To create a c program for Random Access in file
Algorithm:
Step 1 : Start
Step 2 : create a file pointer FILE *f
Step 3 : open a file using file name and mode sample.txt in r mode
Step 4 : use fseek to change position of the file
Step 5 : use ftell to tell the current position. Use rewind to go to beginning of the file
Step 6 : print the output.
Step 7 : Stop

Program:
#include<stdio.h>
int main ()
{
char ch;
FILE *f;
int cp,n;
f=fopen("test.txt","r");
cp=ftell(f);
ch=fgetc(f);
printf("\nvalue at %d is %c",cp,ch);
fseek(f,5,0);
cp=ftell(f);
ch=fgetc(f);
printf("\nvalue at %d is %c",cp,ch);
fseek(f,5,1);
cp=ftell(f);
ch=fgetc(f);
printf("\nvalue at %d is %c",cp,ch);
fseek(f,-2,2);
cp=ftell(f);
ch=fgetc(f);
printf("\nvalue at %d is %c",cp,ch);
rewind(f);
cp=ftell(f);
ch=fgetc(f);
printf("\nvalue at %d is %c",cp,ch);
printf("\nEnter value of char to read n ");
scanf("%d",&n);
fseek(f,-n,2);
while((ch=getc(f))!=EOF)
printf("%c",ch);
fclose(f);
return 0;
}
Output:
value at 0 is h

AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments


23
value at 5 is e
value at 11 is
value at 28 is c
value at 0 is h
Enter value of char to read n 15
computerscience
Result:
The above program was executed successfully
10(d) Preprocessor Directives
Aim:
To create a c program for preprocessor directives
Algorithm:
Step 1 : Start
Step 2 : define MIN(a,b) ((a)<(b)?(a):(b))
Step 3 : define PI 3.14 ,define n 2
Step 4 : call min function in main
Step 5 : check if(n%2==0) even otherwise odd
Step 6 : print the output.
Step 7 : Stop

Program:
#include <stdio.h>
#define MIN(a,b) ((a)<(b)?(a):(b))
#define PI 3.14
#define n 2
int main()
{
printf("Minimum between 10 and 20 is: %d\n", MIN(10,20));
printf("%f",PI);
#undef PI
//printf("%f",PI);
#if(n%2==0)
printf("\n%d is even number",n);
#else
printf("\n%d is odd number",n);
#endif
return 0;
}
Output:
Minimum between 10 and 20 is: 10
3.140000
2 is even number
Result:
The above program was executed successfully

AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments


24

AVC -IT https://www.youtube.com/@ganeshcsrockerz C Programming Lab Experiments

You might also like