c Lab Manual
c Lab Manual
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: }
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. }
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.
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;
};
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
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
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
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