0% found this document useful (0 votes)
26 views

C Lab Manual

The document contains examples of C programs to perform tasks like printing patterns, calculating area and perimeter of shapes, displaying variables, finding distance between points, checking conditions, and finding factorials, series, and Armstrong numbers. It also includes programs to separate odd and even numbers in arrays and print unique elements in an array.

Uploaded by

Temperory Mail
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

C Lab Manual

The document contains examples of C programs to perform tasks like printing patterns, calculating area and perimeter of shapes, displaying variables, finding distance between points, checking conditions, and finding factorials, series, and Armstrong numbers. It also includes programs to separate odd and even numbers in arrays and print unique elements in an array.

Uploaded by

Temperory Mail
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

1.

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);

printf(“Perimeter of the Rectangle is: %d units\n”,p);

a=7*5;

printf(“Area of the Rectangle is: %d square units\n”,a);

Output:

Perimeter of the Rectangle is: 24 units

Area of the Rectangle is: 35 square units

Page | 2
1.3 Write a C program to display multiple variables.

Program:-

#include <stdio.h>

void main()

int a = 125, b = 30255;

float x = 2.13459;

double dx = 1.14155927;

char c = ‘W’;

printf(“a = %d\n”, a);

printf(“b = %d\n”, b);

printf(“x = %f\n”, x);

printf(“dx = %lf\n”, dx);

printf(“c = %c\n”, c);

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;

int x,y; //temporary variables

float distance;

printf(“Enter X and Y coordinates of first point”);

scanf(“%d %d”,&x1,&y1);

printf(“Enter X and Y coordinates of Second point”);

scanf(“%d %d”,&x2,&y2);

x=x2-x1;

y=y2-y1;

distance=sqrt((x*x)+(y*y));

printf(“Distance: %.2f”,distance);

return 0;

Output:

Enter X and Y coordinates of first point

3 4

Enter X and Y coordinates of Second point

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;

printf(“Input the first integer: “);

scanf(“%d”, &p);

printf(“Input the second integer: “);

scanf(“%d”, &q);

printf(“Input the third integer: “);

scanf(“%d”, &r);

printf(“Input the fourth integer: “);

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

Input the second integer: 35

Input the third integer: 15

Input the fourth integer: 46

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();

printf("Enter any String:");

gets(str);

l = atol(str);

printf("String = %s integer = %ld\n", str, l);

return (0);

Output:-

1.

Enter any String:2423565

String = 2423565 integer = 2423565

2.

Enter any String: computer

String = computer integer = 0

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;

float base, length, breadth, height, area, radius;

printf(” 1 –> Square\n”);

printf(” 2 –> Rectangle\n”);

printf(” 3 –> Triangle\n”);

printf(” 4 –> Circle\n”);

printf(“Enter the Figure code: “);

scanf(“%d”, &Shape_Code);

switch(Shape_Code)

case 1:

printf(“Enter the Side: “);

scanf(“%f”, &length);

area = length * length;

printf(“Area of the Square=%.2f\n”, area);

break;

case 2:

printf(“Enter Length and Breadth:\n”);

Page | 8
scanf(“%f %f”, &length,&breadth);

area = breadth * length;

printf(“Area of the Rectangle = %.2f\n”, area);

break;

case 3:

printf(“Enter the Base and Height:\n”);

scanf(“%f %f”, &base, &height);

area = 0.5 * base * height;

printf(“Area of the Triangle = %.2f\n”, area);

break;

case 4:

printf(“Enter the Radius: “);

scanf(“%f”, &radius);

area = 3.142 * radius * radius;

printf(“Area of the Circle = %.2f\n”, area);

break;

default:

printf(“Error in Shape code\n”);

break;

return 0;

Output:

1 –> Square

2 –> Rectangle

Page | 9
3 –> Triangle

4 –> Circle

Enter the Figure code: 3

Enter the Base and Height:

Area of the Triangle =27.00

Page | 10
3.3 Write a C Program to find Factorial of a number.

Program:-

#include <stdio.h>

void main()

int num,i,temp;

long int result;

result=1;

printf(“Enter a number: “);

scanf(“%d”,&num);

temp=num;

while(temp>1)

result=result*temp;

temp- -;

printf(“Factorial of %d is: %lu\n “,num,result);

Output:

Enter a number: 11

Factorial of 11 is: 39916800

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;

printf(“Enter a number: “);

scanf(“%d”, &n);

sum=0;

for(i=2,j=1;j<=n;i=i+2,j++)

printf(“%d\t”,i);

sum+=i;

printf(“\n Total is: %d\n”,sum);

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;

printf(“Input the number of terms : “);

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;

printf(“\nSum of Series up to %d terms : %f \n”,n,s);

Page | 13
Output:

Enter the number of terms: 6

1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6

Sum of Series up to 6 terms: 2.450000

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.

EX : 371 is an Armstrong number.

(3^3) + (7^3) + (1^3) = 371.

Code for check Armstrong number in c

Program:-

#include <stdio.h>

int main()

int num, temp, digit, sum = 0;

printf(“Enter a three-digit number: “);

scanf(“%d”, &num);

temp = num;

while (temp != 0)

digit = temp % 10;

sum += digit * digit * digit;

temp /= 10;

if (sum == num)

printf(“%d is an Armstrong number.”, num);

else

Page | 15
printf(“%d is not an Armstrong number.”, num);

return 0;

Output:

i) Enter a three-digit number: 153

Given number is Armstrong.

ii) Enter a three-digit number: 516

Given number is not Armstrong.

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];

int size, i, j, count;

printf(“Enter size of array: “);

scanf(“%d”, &size); /* Input size of array and elements in array */

printf(“Enter elements in array: \n”);

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

scanf(“%d”, &arr[i]);

printf(“\nUnique elements in the array are: \n”);

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:

Enter the size of array: 6

Enter elements in array:

6 5 4 1 5 4

Unique elements in the array are:

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;

printf(“Enter no.of terms: “);

scanf(“%d”,&n);

printf(“Enter the values of array:\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
}

printf(“Odd array elements are:\n”);

for(i=0;i<O_count;i++)

printf(“%d\t”,odd[i]);

printf(“\nEven array elements are:\n”);

for(i=0;i<E_count;i++)

printf(“%d\t”,even[i]);

Output:

Enter no.of terms: 6

Enter the values of array:

6 9 52 63 2 64

Odd array elements are:

9 63

Even array elements are:

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;

printf(“Enter no.of elements: “);

scanf(“%d”,&n);

printf(“Enter elements in to the array:\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:

Enter no.of elements: 6

Enter elements in to the array:

996 52 63 48 169 255

Ascending order is:

48 52 63 169 225 996

6.1 Write a program in C for multiplication of two square Matrices.

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;

printf(“Enter size of 1st matrix:\n”);

scanf(“%d%d”,&m1,&n1);

printf(“Enter size of 2nd matrix:\n”);

scanf(“%d%d”,&m2,&n2);

if(m2!=n1)

Page | 22
{

printf(“Multiplication is not possible for these matrices\n”);

exit();

printf(“Enter First Matrix:\n”);

for(i=0;i<m1;i++)

for(j=0;j<n1;j++)

scanf(“%d”,&a[i][j]);

printf(“Enter Second Matrix:\n”);

for(i=0;i<m2;i++)

for(j=0;j<n2;j++)

scanf(“%d”,&b[i][j]);

printf(“Multiplication of two Matrices is:\n”);

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:

Enter size of 1st matrix:

3 3

Enter size of 2nd matrix:

3 3

Enter First Matrix:

2 2 2 2 2 2 2 2 2

Enter Second Matrix:

1 2 3 4 3 2 1 0 2

Multiplication of two Matrices is:

12 10 14

12 10 14

12 10 14

6.2. Write a program in C to find transpose of a given matrix.

Program:-

include <stdio.h>

int main()

int a[10][10], transpose[10][10], r, c, i, j;

printf("Enter rows and columns: ");

scanf("%d %d", &r, &c);

printf("\nEnter matrix elements:\n");

for (i = 0; i < r; ++i)

Page | 24
for (j = 0; j < c; ++j)

printf("Enter element a%d%d: ", i + 1, j + 1);

scanf("%d", &a[i][j]);

printf("\nEntered matrix: \n");

for (i = 0; i < r; ++i)

for (j = 0; j < c; ++j)

printf("%d ", a[i][j]);

printf("\n");

// Finding the transpose of matrix a

for (i = 0; i < r; ++i)

for (j = 0; j < c; ++j)

transpose[j][i] = a[i][j];

// Displaying the transpose of matrix a

printf("\nTranspose of the matrix:\n");

for (i = 0; i < c; ++i)

for (j = 0; j < r; ++j) {

Page | 25
printf("%d ", transpose[i][j]);

printf("\n");

return 0;

Output

Enter rows and columns: 2

Enter matrix elements:

Enter element a11: 1

Enter element a12: 4

Enter element a13: 0

Enter element a21: -5

Enter element a22: 2

Enter element a23: 7

Entered matrix:

1 4 0

-5 2 7

Transpose of the matrix:

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;

printf(“Enter sixe of matrix:\n”);

scanf(“%d%d”,&m,&n);

printf(“Enter the elements of matrix:\n”);

for(i=0;i<m;i++)

for(j=0;j<n;j++)

scanf(“%d”,&a[i][j]);

printf(“Enter the search element: “);

scanf(“%d”,&x);

i=0;

j=n-1;

while(i<n&&j>=0)

if(a[i][j]==x)

printf(“Element is found at %d, %d\n”,i+1,j+1);

f++’

break;

Page | 27
}

if(a[i][j]<x)

i++;

else

j- -;

If(f==0)

printf(“Element is not found\n”);

Output:

Enter size of matrix:

3 3

Enter the elements of matrix:

12 25 37

41 46 55

61 75 82

Enter the search element: 47

Element is not found.

7.2 Write a program in C to print individual characters of string in reverse order.

Program:-

#include <stdio.h>

#include<string.h>

int main()

Page | 28
char str[100];

int i;

printf(“Enter a string:\n”);

gets(str);

printf(“Reverse order of given string is:\n”);

i=strlen(str);

for(i=i-1;i>=0;i–)

printf(“%c”,str[i]);

printf(“/n”);

Output:-

Enter a string: Computer

Reverse order of given string is:

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;

printf(“Enter the first string:\n”);

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])

printf(“Given strings are not identical\n”);

f++;

break;

if(f==0)

printf(“Given strings are identical\n”);

Output:

Enter the first string:

Computer

Enter the second string:

computer

Given strings are identical

8.2 Write a program in C to copy one string to another string.

Program:-

#include <stdio.h>

Page | 30
#include <string.h>

int main()

char source[1000], destination[1000];

printf(“Input a string\n”);

gets(source);

strcpy(destination, source);

printf(“Source string: %s\n”, source);

printf(“Destination string: %s\n”, destination);

return 0;

Output:

Enter a string:

Computer Science

Source string: Computer Science

Destination 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()

struct item *ptr;

int i, n;

printf(“Enter number of items: “);

scanf(“%d”, &n);

// Allocates the memory for noOfRecords structures with pointer ptr pointing to the base address.

ptr = (struct item*) malloc(n);

for(i = 0; i < n; i++)

printf(“Enter name of the Item, Cost and MRP respectively:\n”);

scanf(“%s %f %f”, (ptr+i)->name, &(ptr+i)->Cost, &(ptr+i)->MRP);

printf(“Item details as follows:\n”);

for(i = 0; i < n ; i++)

printf(“%s\t%.2f\t%.2f\n”, (ptr+i)->name, (ptr+i)->Cost, (ptr+i)->MRP);

return 0;

Output:

Enter no.of Items: 2

Enter name of the Item, Cost and MRP respectively:

Page | 32
Pen

15

20

Enter name of the Item, Cost and MRP respectively:

Book

36

55

Item details as follows:

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;

printf(” Here in the declaration p = int pointer, int a= 852\n”);

printf(” Address of a : %u\n”,&a);

printf(” Value of a : %d\n”,a);

p=&a;

printf(” Now p is assigned with the address of a.\n”);

Page | 33
printf(” Address of pointer p : %u\n”,p);

printf(” Content of pointer p : %d\n”,*p);

a=563;

printf(” The value of a assigned to 563 now.\n”);

printf(” Address of pointer p : %p\n”,p);

printf(” Content of pointer p : %d\n”,*p);

*p=1954;

printf(” The pointer variable p is assigned the value 1954 now.\n”);

printf(” Address of a : %p\n”,&a);

printf(” Value of a : %d\n\n”,a);

Output:

Here in the declaration p = int pointer, int a= 852

Address of a : 0x7ffee0797354

Value of a : 852

Now p is assigned with the address of a.

Address of pointer p : 0x7ffee0797354

Content of pointer p : 852

The value of a assigned to 563 now.

Address of pointer p : 0x7ffee0797354

Content of pointer p : 563

The pointer variable p is assigned the value 1954 now.

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

10.2. Write a program in C to add two numbers using pointers.

Page | 35
Program:-

#include<stdio.h>

void main()

int x,y;

int *p,*q,*r;

printf("Enter any two Numbers :");

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:-

Enter any two Numbers: 23 45

Value of *p=23

Value of *q=45

Value of *r=68

11.1 Write a program in C to add numbers using call by reference.

Program:-

Page | 36
#include<stdio.h>

int add(int *p,int *q);

void main()

int x,y;

printf("Enter any two numbers :");

scanf("%d %d",&x,&y);

printf("Addition of two numbers=%d",add(&x,&y));

int add(int *p,int *q)

int *r;

*r=*p+*q;

return *r;

Output:-

Enter any two numbers :23 56

Addition of two numbers=79

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();

printf("Enter any size:");

scanf("%d",&n);

p=(int *)malloc(n);

printf("Enter %d Elements :\n",n);

for(i=0;i<n;i++)

scanf("%d",(p+i));

for(i=0;i<n;i++)

if(*(p+i)>max)

max=*(p+i);

printf("Largest Number is %d\n",max);

Output:-

Enter any size:5

Enter 5 Elements :

12

97

Page | 38
34

56

67

Largest Number is 97

12.1 Write a program in C to swap elements using call by reference.

Program:-

#include<stdio.h>

void swap(int *p,int *q);

void main()

int x,y;

printf("Enter any two numbers :");

scanf("%d %d",&x,&y);

printf("Before swaping x and y \n");

printf("x=%d \n y=%d\n",x,y);

swap(&x,&y);

printf("After swaping x and y \n");

printf("x=%d \n y=%d\n",x,y);

void swap(int *p,int *q)

*p=*p+*q;

Page | 39
*q=*p-*q;

*p=*p-*q;

Output:-

Enter any two numbers :45 89

Before swaping x and y

x=45

y=89

After swaping x and y

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

13.1 Write a program in C to show how a function returning pointer.

#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:-

Pointer : Show a function returning pointer :

--------------------------------------------------

Input the first number : 5

Input the second number : 6

The number 6 is larger

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()

int num, i, *ptr, sum=0;

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

scanf("%d", &num);

ptr= (int*) malloc (num);

if(ptr==NULL)

printf("Memory not allocated.");

Page | 42
}

printf("Enter elements :\n");

for(i=0;i<num;i++)

scanf("%d",ptr+i);

sum += *(ptr+i);

printf("Sum= %d",sum);

return 0;

Output:-

Enter total number of elements:3

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:-

Enter total number of elements:4

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>

#define MAX 100

int largest( int []);

int n;

int main()

int arr1[MAX],max,i;

printf(" Input the number of elements to be stored in the array :");

scanf("%d",&n);

printf(" Input %d elements in the array :\n",n);

for(i=0;i<n;i++)

printf(" element - %d : ",i);

scanf("%d",&arr1[i]);

max=largest(arr1);

printf(" The largest element in the array is : %d\n\n",max);

return 0;

int largest(int arr1[])

{
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

The content of the file test.txt is :

test line 1
test line 2
test line 3
test line 4
test line 5
test line 6
test line 7

16.2 Write a program in C to copy a file in another name.

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.

16.3. Write a program in C to remove a file from the disk.

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:-

Remove a file from the disk :

----------------------------------

Input the name of file to delete : test.txt

The file test.txt is deleted successfully..!!

Page | 53

You might also like