C Lab Manual
C Lab Manual
1 Write a C program to print a block F using hash (#), where the F has a height of six
characters and width of five and four characters.
Program:-
#include <stdio.h>
void main()
printf(“######\n”);
printf(“#\n”);
printf(“####\n”);
printf(“#\n”);
printf(“#\n”);
printf(“#\n”);
Output:
######
#
####
#
#
#
Page | 1
1.2 Write a C program to compute the perimeter and area of a rectangle with a height of 7
inches and width of 5 inches.
Program:-
#include <stdio.h>
void main()
int p,a;
p=2*(7+5);
a=7*5;
Output:
Page | 2
1.3 Write a C program to display multiple variables.
Program:-
#include <stdio.h>
void main()
float x = 2.13459;
double dx = 1.14155927;
char c = ‘W’;
Output:
a = 125
b = 30255
x = 2.134590
dx = 1.14155927
c=W
Page | 3
2.1 Write a C program to calculate the distance between two points.
Program:-
#include<stdio.h>
#include<math.h>
int main()
int x1,x2,y1,y2;
float distance;
scanf(“%d %d”,&x1,&y1);
scanf(“%d %d”,&x2,&y2);
x=x2-x1;
y=y2-y1;
distance=sqrt((x*x)+(y*y));
printf(“Distance: %.2f”,distance);
return 0;
Output:
3 4
4 5
Distance: 1.414
Page | 4
2.2 Write a C program that accepts 4 integers p, q, r, s from the user where r and s are
positive and p is even. If q is greater than r and s is greater than p and if the sum of r and s
is greater than the sum of p and q print “Correct values”, otherwise print “Wrong values”.
Program:-
#include <stdio.h>
int main()
{
int p, q, r, s;
scanf(“%d”, &p);
scanf(“%d”, &q);
scanf(“%d”, &r);
scanf(“%d”, &s);
if((r>0) && (s>0) && (p%2==0) && (q>r) && (s>p) && ((r+s)>(p+q))
printf(“Correct values\n”);
else
printf(“Wrong values\n”);
return 0;
Output
Page | 5
Input the first integer: 25
Wrong values
Page | 6
3.1 Write a C program to convert a string to a long integer.
Program:-
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
long l;
char str[20];
clrscr();
gets(str);
l = atol(str);
return (0);
Output:-
1.
2.
Page | 7
3.2 Write a program in C which is a Menu-Driven Program to compute the area of the
various geometrical shape.
Program:-
#include <stdio.h>
int main()
int Shape_Code;
scanf(“%d”, &Shape_Code);
switch(Shape_Code)
case 1:
scanf(“%f”, &length);
break;
case 2:
Page | 8
scanf(“%f %f”, &length,&breadth);
break;
case 3:
break;
case 4:
scanf(“%f”, &radius);
break;
default:
break;
return 0;
Output:
1 –> Square
2 –> Rectangle
Page | 9
3 –> Triangle
4 –> Circle
Page | 10
3.3 Write a C Program to find Factorial of a number.
Program:-
#include <stdio.h>
void main()
int num,i,temp;
result=1;
scanf(“%d”,&num);
temp=num;
while(temp>1)
result=result*temp;
temp- -;
Output:
Enter a number: 11
Page | 11
4.1 Write a program in C to display the n terms of even natural numbers and their sum.
Program:-
#include <stdio.h>
int main()
int n,i,j,sum;
scanf(“%d”, &n);
sum=0;
for(i=2,j=1;j<=n;i=i+2,j++)
printf(“%d\t”,i);
sum+=i;
return 0;
Output:
Enter a numbers: 6
2 4 6 8 10 12
Total is: 42
Page | 12
4.2 Write a program in C to display the n terms of harmonic series and their sum.
1 + 1/2 + 1/3 + 1/4 + 1/5 … 1/n terms.
Program:-
#include <stdio.h>
void main()
int i,n;
float s=0;
scanf(“%d”,&n);
printf(“\n”);
for(i=1;i<=n;i++)
if(i==1)
printf(“%d”,i);
s+=i;
continue;
printf(” + 1/%d”,i);
s+=1/(float)i;
Page | 13
Output:
Page | 14
4.3 Write a C Program to Find Whether the Given Number is Armstrong Number or not.
Description:-
An Armstrong number is a number that is the sum of its own digits each raised to the power of
the number of digits.
Program:-
#include <stdio.h>
int main()
scanf(“%d”, &num);
temp = num;
while (temp != 0)
temp /= 10;
if (sum == num)
else
Page | 15
printf(“%d is not an Armstrong number.”, num);
return 0;
Output:
Page | 16
5.1 Write a program in C to print all unique elements in an array.
Program:-
#include <stdio.h>
void main()
int arr[100];
scanf(“%d”, &arr[i]);
for(i=0;i<size;i++)
count=0;
for(j=0;j<i;j++)
if(arr[i]==arr[j])
count=1;
break;
if(count==0)
Page | 17
printf(“%d\t”, arr[i]);
Output:
6 5 4 1 5 4
6 5 4 1
Page | 18
5.2 Write a program in C to separate odd and even integers in separate arrays.
Program:-
#include<stdio.h>
void main()
int arr[100],even[100],odd[100];
int i,n,E_count,O_count;
scanf(“%d”,&n);
for(i=0;i<n;i++)
scanf(“%d”,&arr[i]);
E_count=O_count=0;
for(i=0;i<n;i++)
if(arr[i]%2==0)
even[E_count]=arr[i];
E_count++;
else
odd[O_count]=arr[i];
O_count++;
Page | 19
}
for(i=0;i<O_count;i++)
printf(“%d\t”,odd[i]);
for(i=0;i<E_count;i++)
printf(“%d\t”,even[i]);
Output:
6 9 52 63 2 64
9 63
6 52 2 6
Page | 20
5.3 Write a program in C to sort elements of array in ascending order.
Program:-
#include <stdio.h>
int main()
int a[100],i,j,n,temp;
scanf(“%d”,&n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n-1;i++)
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
Page | 21
printf(“Ascending order is:\n”);
for(i=0;i<n;i++)
printf(“%d\t”,a[i]);
Output:
Program:-
#include<stdio.h>
#include<stdlib.h>
int main()
int m1,n1,m2,n2;
int a[10][10],b[10][10],c[10][10],i,j,k;
scanf(“%d%d”,&m1,&n1);
scanf(“%d%d”,&m2,&n2);
if(m2!=n1)
Page | 22
{
exit();
for(i=0;i<m1;i++)
for(j=0;j<n1;j++)
scanf(“%d”,&a[i][j]);
for(i=0;i<m2;i++)
for(j=0;j<n2;j++)
scanf(“%d”,&b[i][j]);
for(i=0;i<m1;i++)
for(j=0;j<n2;j++)
c[i][j]=0;
for(k=0;k<n1;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
printf(“%d\t”,c[i][j]);
printf(“\n”);
return 0;
Page | 23
}
Output:
3 3
3 3
2 2 2 2 2 2 2 2 2
1 2 3 4 3 2 1 0 2
12 10 14
12 10 14
12 10 14
Program:-
include <stdio.h>
int main()
Page | 24
for (j = 0; j < c; ++j)
scanf("%d", &a[i][j]);
printf("\n");
transpose[j][i] = a[i][j];
Page | 25
printf("%d ", transpose[i][j]);
printf("\n");
return 0;
Output
Entered matrix:
1 4 0
-5 2 7
1 -5
4 2
0 7
Page | 26
7.1 Write a program in C to search an element in a row wise and column wise sorted
matrix.
Program:-
#include <stdio.h>
void main()
int a[10][10],n,m,i,j,x,f=0;
scanf(“%d%d”,&m,&n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
scanf(“%d”,&x);
i=0;
j=n-1;
while(i<n&&j>=0)
if(a[i][j]==x)
f++’
break;
Page | 27
}
if(a[i][j]<x)
i++;
else
j- -;
If(f==0)
Output:
3 3
12 25 37
41 46 55
61 75 82
Program:-
#include <stdio.h>
#include<string.h>
int main()
Page | 28
char str[100];
int i;
printf(“Enter a string:\n”);
gets(str);
i=strlen(str);
for(i=i-1;i>=0;i–)
printf(“%c”,str[i]);
printf(“/n”);
Output:-
retupmoC
8.1Write a program in C to compare two strings without using string library functions.
Program:-
#include <stdio.h>
void main()
char str1[100],str2[100];
int I,f=0;
gets(str1);
Page | 29
printf(“Enter the second string:\n”);
gets(str2);
for(i=0;str1[i]!=’\0′ || str2[i]!=’\0′;i++)
if(str1[i]!=str2[i])
f++;
break;
if(f==0)
Output:
Computer
computer
Program:-
#include <stdio.h>
Page | 30
#include <string.h>
int main()
printf(“Input a string\n”);
gets(source);
strcpy(destination, source);
return 0;
Output:
Enter a string:
Computer Science
9.1 Write a C Program to Store Information Using Structures with Dynamically Memory
Allocation.
Program:
#include <stdio.h>
#include<stdlib.h>
struct item
float Cost,MRP;
Page | 31
char name[30];
};
int main()
int i, n;
scanf(“%d”, &n);
// Allocates the memory for noOfRecords structures with pointer ptr pointing to the base address.
return 0;
Output:
Page | 32
Pen
15
20
Book
36
55
Pen 15 20
Book 36 55
9.2 Write a program in C to demonstrate how to handle the pointers in the program.
Program:-
#include <stdio.h>
void main()
int* p;
int a;
a=852;
p=&a;
Page | 33
printf(” Address of pointer p : %u\n”,p);
a=563;
*p=1954;
Output:
Address of a : 0x7ffee0797354
Value of a : 852
Address of a : 0x7ffee0797354
Page | 34
Value of a : 1954
10.1 Write a program in C to demonstrate the use of & (address of) and *(value at address)
operator.
Program:-
#include<stdio.h>
void main()
int *p;
int x=20;
p=&x;
printf("Value of x=%d\n",*p);
printf("Address of x=%p\n",p);
printf("Value of x=%d\n",x);
printf("Address of p=%p\n",&p);
Output:-
Value of x=20
Address of x=FFF2
Value of x=20
Address of p=FFF4
Page | 35
Program:-
#include<stdio.h>
void main()
int x,y;
int *p,*q,*r;
scanf("%d %d",&x,&y);
p=&x;
q=&y;
*r=*p+*q;
printf("Value of *p=%d\n",*p);
printf("Value of *q=%d\n",*q);
printf("Value of *r=%d\n",*r);
Output:-
Value of *p=23
Value of *q=45
Value of *r=68
Program:-
Page | 36
#include<stdio.h>
void main()
int x,y;
scanf("%d %d",&x,&y);
int *r;
*r=*p+*q;
return *r;
Output:-
11.2. Write a program in C to find the largest element using Dynamic Memory Allocation
Program:-
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
Page | 37
void main()
int n,max=0;
int *p,i;
clrscr();
scanf("%d",&n);
p=(int *)malloc(n);
for(i=0;i<n;i++)
scanf("%d",(p+i));
for(i=0;i<n;i++)
if(*(p+i)>max)
max=*(p+i);
Output:-
Enter 5 Elements :
12
97
Page | 38
34
56
67
Largest Number is 97
Program:-
#include<stdio.h>
void main()
int x,y;
scanf("%d %d",&x,&y);
printf("x=%d \n y=%d\n",x,y);
swap(&x,&y);
printf("x=%d \n y=%d\n",x,y);
*p=*p+*q;
Page | 39
*q=*p-*q;
*p=*p-*q;
Output:-
x=45
y=89
x=89
y=45
12.2. Write a program in C to count the number of vowels and consonants in a string using
a pointer.
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
char *ch;
int vowels,con;
vowels =con=0;
clrscr();
printf("Enter any String:\n");
while((*ch=getchar())!='\n')
{
switch(*ch)
{
case 'a':
case 'A':
case 'e':
Page | 40
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': vowels ++;
break;
default:
if(isalpha(*ch))
con++;
}
}
printf("No of Vowels =%d \n", vowels);
printf("No of Consonants =%d",con);
}
Output:-
Enter any String:
This is a pointer program
No of Vowels =8
No of Consonants =13
#include <stdio.h>
int* findLarger(int*, int*);
void main()
{
int numa=0;
int numb=0;
int *result;
printf("\n\n Pointer : Show a function returning pointer :\n");
printf("--------------------------------------------------\n");
printf(" Input the first number : ");
scanf("%d", &numa);
printf(" Input the second number : ");
scanf("%d", &numb);
result=findLarger(&numa, &numb);
printf(" The number %d is larger. \n\n",*result);
}
Page | 41
int* findLarger(int *n1, int *n2)
{
if(*n1 > *n2)
return n1;
else
return n2;
}
Output:-
--------------------------------------------------
13.2. Write a C program to find sum of n elements entered by user. To perform this
program, allocate memory dynamically using malloc( ) function.
Program:-
#include<stdio.h>
#include<stdlib.h>
int main()
scanf("%d", &num);
if(ptr==NULL)
Page | 42
}
for(i=0;i<num;i++)
scanf("%d",ptr+i);
sum += *(ptr+i);
printf("Sum= %d",sum);
return 0;
Output:-
10
20
30
Sum=60
14.1 Write a C program to find sum of n elements entered by user. To perform this
program, allocate memory dynamically using calloc( ) function. Understand the difference
between the above two programs
Program:-
#include<stdio.h>
#include<stdlib.h>
int main()
{
int num, i, *ptr, sum=0;
printf("Enter total number of elements: ");
Page | 43
scanf("%d",&num);
ptr= (int*) calloc (num, sizeof(int));
if(ptr==NULL)
{
printf("Memory not allocated.");
}
printf("Enter elements :\n");
for(i=0;i<num;i++)
{
scanf("%d",ptr+i);
sum += *(ptr+i);
}
printf("Sum= %d",sum);
return 0;
}
Output:-
10
20
30
Sum=61
14.2 Write a program in C to convert decimal number to binary number using the
function.
Program:-
#include<stdio.h>
Page | 44
long Binary(int);
int main()
{
long bno;
int dno;
printf("\n\n Function : convert decimal to binary :\n");
printf("-------------------------------------------\n");
printf(" Input any decimal number : ");
scanf("%d",&dno);
bno = Binary(dno);
printf("\n The Binary value is : %ld\n\n",bno);
return 0;
}
long Binary(int dno)
{
long bno=0,remainder,f=1;
while(dno != 0)
{
remainder = dno % 2;
bno = bno + remainder * f;
f = f * 10;
dno = dno / 2;
}
return bno;
}
Output:-
Input any decimal number : 65
The Binary value is: 1000001
15.1 Write a program in C to check whether a number is a prime number or not using the
function.
Page | 45
Program:-
#include<stdio.h>
int Prime(int);
int main()
{
int n1,f;
printf(" Input a positive number : ");
scanf("%d",&n1);
f = Prime(n1);
if(f==2)
printf(" The number %d is a prime number.\n",n1);
else
printf(" The number %d is not a prime number.\n",n1);
return 0;
}
int Prime(int n1)
{
int i=1,f=0;
while(i<=n1)
{
if(n1%i==0)
f++;
i++;
}
return f;
}
Output:-
Input a positive number : 5
Page | 46
The number 5 is a prime number
15.2 Write a program in C to get the largest element of an array using the function.
Program:-
#include<stdio.h>
int n;
int main()
int arr1[MAX],max,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr1[i]);
max=largest(arr1);
return 0;
{
Page | 47
int i=1,max;
max=arr1[0];
while(i < n)
if(max<arr1[i])
max=arr1[i];
i++;
return max;
Output:-
Input the number of elements to be stored in the array :5
Input 5 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3
element - 3 : 4
element - 4 : 5
The largest element in the array is : 5
16.1 Write a program in C to append multiple lines at the end of a text file.
Program:-
#include <stdio.h>
int main ()
{
FILE * fptr;
Page | 48
int i,n;
char str[100];
char fname[20];
char str1;
printf("\n\n Append multiple lines at the end of a text file :\n");
printf("------------------------------------------------------\n");
printf(" Input the file name to be opened : ");
scanf("%s",fname);
fptr = fopen(fname, "a");
printf(" Input the number of lines to be written : ");
scanf("%d", &n);
printf(" The lines are : \n");
for(i = 0; i < n+1;i++)
{
fgets(str, sizeof str, stdin);
fputs(str, fptr);
}
fclose (fptr);
fptr = fopen (fname, "r");
printf("\n The content of the file %s is :\n",fname);
str1 = fgetc(fptr);
while (str1 != EOF)
{
printf ("%c", str1);
str1 = fgetc(fptr);
}
printf("\n\n");
fclose (fptr);
return 0;
}
Page | 49
Output:-
Append multiple lines at the end of a text file :
------------------------------------------------------
Input the file name to be opened : test.txt
Input the number of lines to be written : 3
The lines are :
test line 5
test line 6
test line 7
test line 1
test line 2
test line 3
test line 4
test line 5
test line 6
test line 7
Program:-
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fptr1, *fptr2;
char ch, fname1[20], fname2[20];
printf("\n\n Copy a file in another name :\n");
printf("----------------------------------\n");
printf(" Input the source file name : ");
scanf("%s",fname1);
Page | 50
fptr1=fopen(fname1, "r");
if(fptr1==NULL)
{
printf(" File does not found or error in opening.!!");
exit(1);
}
printf(" Input the new file name : ");
scanf("%s",fname2);
fptr2=fopen(fname2, "w");
if(fptr2==NULL)
{
printf(" File does not found or error in opening.!!");
fclose(fptr1);
exit(2);
}
while(1)
{
ch=fgetc(fptr1);
if(ch==EOF)
{
break;
}
else
{
fputc(ch, fptr2);
}
}
printf(" The file %s copied successfully in the file %s. \n\n",fname1,fname2);
fclose(fptr1);
fclose(fptr2);
Page | 51
}
Output:-
Copy a file in another name :
----------------------------------
Input the source file name : test.txt
Input the new file name : test1.txt
The file test.txt copied successfully in the file test1.txt.
Program:-
#include <stdio.h>
void main()
{
int status;
char fname[20];
printf("\n\n Remove a file from the disk :\n");
printf("----------------------------------\n");
printf(" Input the name of file to delete : ");
scanf("%s",fname);
status=remove(fname);
if(status==0)
{
printf(" The file %s is deleted successfully..!!\n\n",fname);
}
else
{
printf(" Unable to delete file %s\n\n",fname);
}
}
Page | 52
Output:-
----------------------------------
Page | 53