0% found this document useful (0 votes)
60 views86 pages

C Manual

C language multiple programs with codes

Uploaded by

Himanshu R7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views86 pages

C Manual

C language multiple programs with codes

Uploaded by

Himanshu R7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 86

C PROGRAMMING LAB MANUAL FOR R19

Exercise- 1

1. A

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

Source Code (cline.c):

#include <stdio.h>
int main()
{
printf("######\n");
printf("#\n");
printf("#\n");
printf("#####\n");
printf("#\n");
printf("#\n");
printf("#\n");

return(0);
}

NSRIT Page 1
C PROGRAMMING LAB MANUAL FOR R19

Output:

######
#
#
#####
#
#
#

NSRIT Page 2
C PROGRAMMING LAB MANUAL FOR R19

1. B
Aim: write a c program to compute the perimeter and area of a rectangle with a height of 7
inches and width of 5 inches.

Source code:

#include <stdio.h>
/* height and width of a rectangle in inches */
int width;
int height;

int area;
int perimeter;

int main() {
height = 7;
width = 5;

perimeter = 2*(height + width);


printf("Perimeter of the rectangle = %d inches\n", perimeter);
area = height * width;
printf("Area of the rectangle = %d square inches\n", area);

return(0);
}

NSRIT Page 3
C PROGRAMMING LAB MANUAL FOR R19

Output:

Perimeter of the rectangle = 24 inches

Area of the rectangle = 35 square inches

NSRIT Page 4
C PROGRAMMING LAB MANUAL FOR R19

1. C
Aim: Write a C program to display multiple variables.

Variable declaration:

int a = 125, b = 12345;


long ax = 1234567890;
short s = 4043;
float x = 2.13459;
double dx = 1.1415927;
char c = 'W';
unsigned long ux = 2541567890;

Source code:

#include <stdio.h>
int main()
{
int a = 125, b = 12345;
long ax = 1234567890;
short s = 4043;
float x = 2.13459;
double dx = 1.1415927;
char c = 'W';
unsigned long ux = 2541567890;

printf("a + c = %d\n", a + c);


printf("x + c = %f\n", x + c);
printf("dx + x = %f\n", dx + x);
printf("((int) dx) + ax = %ld\n", ((int) dx) + ax);
printf("a + x = %f\n", a + x);
printf("s + b = %d\n", s + b);
printf("ax + b = %ld\n", ax + b);
printf("s + c = %hd\n", s + c);
printf("ax + c = %ld\n", ax + c);
printf("ax + ux = %lu\n", ax + ux);

return 0;
}

NSRIT Page 5
C PROGRAMMING LAB MANUAL FOR R19

Output:
a + c = 212
x + c = 89.134590
dx + x = 3.276183
((int) dx) + ax = 1234567891
a + x = 127.134590
s + b = 16388
ax + b = 1234580235
s + c = 4130
ax + c = 1234567977
ax + ux = 3776135780

NSRIT Page 6
C PROGRAMMING LAB MANUAL FOR R19

Exercise- 2
2.a

Aim: Write a C program to calculate the distance between the two points.

Formula:

Source code:

#include <stdio.h>
#include <math.h>

int main() {
float x1, y1, x2, y2, gdistance;
printf("Input x1: ");
scanf("%f", &x1);
printf("Input y1: ");
scanf("%f", &y1);
printf("Input x2: ");
scanf("%f", &x2);
printf("Input y2: ");
scanf("%f", &y2);
gdistance = ((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1));
printf("Distance between the said points: %.4f", sqrt(gdistance));
printf("\n");
return 0;
}

NSRIT Page 7
C PROGRAMMING LAB MANUAL FOR R19

Output:

Input x1: 25
Input y1: 15
Input x2: 35
Input y2: 10
Distance between the said points: 11.1803

NSRIT Page 8
C PROGRAMMING LAB MANUAL FOR R19

2. B
Aim: 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 “wrong values”.

Source code:

#include <stdio.h>
int main()
{
int p, q, r, s;
printf("\nInput the first integer: ");
scanf("%d", &p);
printf("\nInput the second integer: ");
scanf("%d", &q);
printf("\nInput the third integer: ");
scanf("%d", &r);
printf("\nInput the fourth integer: ");
scanf("%d", &s);
if((q > r) && (s > p) && ((r+s) > (p+q)) && (r > 0) && (s > 0) && (p%2==0))
{
printf("\nCorrect values\n");
}
else {
printf("\nWrong values\n");
}
return 0;
}

NSRIT Page 9
C PROGRAMMING LAB MANUAL FOR R19

Output:

Input the first integer: 25

Input the second integer: 35

Input the third integer: 15

Input the fourth integer: 46

Wrong values

NSRIT Page 10
C PROGRAMMING LAB MANUAL FOR R19

Exercise - 3
3. A

Aim: Write a C program to convert a string to a long integer.


Source code:

#include<stdio.h>
#include<stdlib.h>
int main ()
{
char buffer[] = "2016 40a0b0 -1101110100110111100110 0x5abfff";
char * ptr_end;
long int i1, i2, i3, i4;

i1 = strtol (buffer,&ptr_end,10);
i2 = strtol (ptr_end,&ptr_end,16);
i3 = strtol (ptr_end,&ptr_end,2);
i4 = strtol (ptr_end,NULL,0);
printf ("\nIn decimals: %ld, %ld, %ld, %ld.\n\n", i1, i2, i3, i4);
return 0;
}

NSRIT Page 11
C PROGRAMMING LAB MANUAL FOR R19

Output:

In decimals: 2016, 4235440, -3624422, 5947391.

NSRIT Page 12
C PROGRAMMING LAB MANUAL FOR R19

3. B

Aim: Write a program in C which is a Menu-Driven Program to compute the area of


the various geometrical shape.
Source code:

#include <stdio.h>
void main ()
{
int choice,r,l,w,b,h;
float area;
printf("Input 1 for area of circle\n");
printf("Input 2 for area of rectangle\n");
printf("Input 3 for area of triangle\n");
printf("Input your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Input radious of the circle : ");
scanf("%d",&r);
area=3.14*r*r;
break;
case 2:
printf("Input length and width of the rectangle : ");
scanf("%d%d",&l,&w);
area=l*w;
break;
case 3:
printf("Input the base and hight of the triangle :");
scanf("%d%d",&b,&h);
area=.5*b*h;
break;
}
printf("The area is : %f\n",area);
}

NSRIT Page 13
C PROGRAMMING LAB MANUAL FOR R19

Output:

Input 1 for area of circle

Input 2 for area of rectangle

Input 3 for area of triangle

Input your choice: 1

Input radius of the circle : 5

The area is: 78.500000

NSRIT Page 14
C PROGRAMMING LAB MANUAL FOR R19

3. C

Aim: Write a C program to calculate the factorial of a given number

Source code:

#include <stdio.h>
void main(){
int i,f=1,num;

printf("Input the number : ");


scanf("%d",&num);

for(i=1;i<=num;i++)
f=f*i;

printf("The Factorial of %d is: %d\n",num,f);


}

NSRIT Page 15
C PROGRAMMING LAB MANUAL FOR R19

Output:

Input the number: 5

The Factorial of 5 is: 120

NSRIT Page 16
C PROGRAMMING LAB MANUAL FOR R19

Exercise – 4
4. A

Aim: Write a program in C to display the n terms of even natural number and their sum.

Source code:

#include <stdio.h>

void main()
{
int i,n,sum=0;

printf("Input number of terms : ");


scanf("%d",&n);
printf("\nThe even numbers are :");
for(i=1;i<=n;i++)
{
printf("%d ",2*i);
sum+=2*i;
}
printf("\nThe Sum of even Natural Number upto %d terms : %d \n",n,sum);
}

NSRIT Page 17
C PROGRAMMING LAB MANUAL FOR R19

Output:

Input number of terms : 5

The even numbers are :2 4 6 8 10

The Sum of even Natural Number upto 5 terms : 30

NSRIT Page 18
C PROGRAMMING LAB MANUAL FOR R19

4. B

Aim: 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.
Source code:

#include <stdio.h>
void main()
{
int i,n;
float s=0.0;
printf("Input the number of terms : ");
scanf("%d",&n);
printf("\n\n");
for(i=1;i<=n;i++)
{
if(i<n)
{
printf("1/%d + ",i);
s+=1/(float)i;
}
if(i==n)
{
printf("1/%d ",i);
s+=1/(float)i;
}
}
printf("\nSum of Series upto %d terms : %f \n",n,s);
}

NSRIT Page 19
C PROGRAMMING LAB MANUAL FOR R19

Output:

Input the number of terms : 5

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

Sum of Series up to 5 terms: 2.283334

NSRIT Page 20
C PROGRAMMING LAB MANUAL FOR R19

4. C

Aim: Write a C program to check whether a given number is an Armstrong number or not.

Source Code:

#include <stdio.h>

void main(){

int num,r,sum=0,temp;

printf("Input a number: ");

scanf("%d",&num);

for(temp=num;num!=0;num=num/10){

r=num % 10;

sum=sum+(r*r*r);

if(sum==temp)

printf("%d is an Armstrong number.\n",temp);

else

printf("%d is not an Armstrong number.\n",temp);

NSRIT Page 21
C PROGRAMMING LAB MANUAL FOR R19

Output:

Input a number: 153

153 is an Armstrong number.

NSRIT Page 22
C PROGRAMMING LAB MANUAL FOR R19

Exercise – 5
5.1

Aim: Write a program in C to print all unique elements in an array.

Source code:

#include <stdio.h>

void main()
{
int arr1[100], n,ctr=0;
int i, j, k;

printf("\n\nPrint all unique elements of an array:\n");


printf("------------------------------------------\n");

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

printf("\nThe unique elements found in the array are : \n");


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

for(j=0; j<i-1; j++)


{
if(arr1[i]==arr1[j])
{
ctr++;
}
}

for(k=i+1; k<n; k++)


{
if(arr1[i]==arr1[k])
{
ctr++;
}
}

NSRIT Page 23
C PROGRAMMING LAB MANUAL FOR R19

if(ctr==0)
{
printf("%d ",arr1[i]);
}
}
printf("\n\n");
}

NSRIT Page 24
C PROGRAMMING LAB MANUAL FOR R19

Output:

Print all unique elements of an array:

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

Input the number of elements to be stored in the array: 3

Input 3 elements in the array:

Element - 0 : 1

Element - 1 : 5

Element - 2 : 1

The unique elements found in the array are: 5

NSRIT Page 25
C PROGRAMMING LAB MANUAL FOR R19

5. B

Aim: Write a program in C to separate odd and even integers in separate arrays.

Source Code:

#include <stdio.h>

void main()
{
int arr1[10], arr2[10], arr3[10];
int i,j=0,k=0,n;

printf("\n\nSeparate odd and even integers in separate arrays:\n");


printf("------------------------------------------------------\n");

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

for(i=0;i<n;i++)
{
if (arr1[i]%2 == 0)
{
arr2[j] = arr1[i];
j++;
}
else
{
arr3[k] = arr1[i];
k++;
}
}

printf("\nThe Even elements are : \n");


for(i=0;i<j;i++)
{
printf("%d ",arr2[i]);
}

printf("\nThe Odd elements are :\n");


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

NSRIT Page 26
C PROGRAMMING LAB MANUAL FOR R19

{
printf("%d ", arr3[i]);
}
printf("\n\n");
}

NSRIT Page 27
C PROGRAMMING LAB MANUAL FOR R19

Output:

Separate odd and even integers in separate arrays:

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

Input the number of elements to be stored in the array :5

Input 5 elements in the array :

element - 0 : 25

element - 1 : 47

element - 2 : 42

element - 3 : 56

element - 4 : 32

The Even elements are :

42 56 32

The Odd elements are :

25 47

NSRIT Page 28
C PROGRAMMING LAB MANUAL FOR R19

5. C

Aim: Write a program in C to sort elements of array in ascending order.

Source Code:

#include <stdio.h>

void main()
{
int arr1[100];
int n, i, j, tmp;

printf("\n\nsort elements of array in ascending order :\n ");


printf("----------------------------------------------\n");

printf("Input the size of 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]);
}

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


{
for(j=i+1; j<n; j++)
{
if(arr1[j] <arr1[i])
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
printf("\nElements of array in sorted ascending order:\n");
for(i=0; i<n; i++)
{
printf("%d ", arr1[i]);
}
printf("\n\n");
}

NSRIT Page 29
C PROGRAMMING LAB MANUAL FOR R19

Output:

sort elements of array in ascending order :

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

Input the size of array : 5

Input 5 elements in the array :

element - 0 : 2

element - 1 : 7

element - 2 : 4

element - 3 : 5

element - 4 : 9

Elements of array in sorted ascending order:

2 4 5 7 9

NSRIT Page 30
C PROGRAMMING LAB MANUAL FOR R19

Exercise – 6

6. A

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

Source code:
#include <stdio.h>

void main()
{
int arr1[50][50],brr1[50][50],crr1[50][50],i,j,k,r1,c1,r2,c2,sum=0;

printf("\n\nMultiplication of two Matrices :\n");


printf("----------------------------------\n");

printf("\nInput the rows and columns of first matrix : ");


scanf("%d %d",&r1,&c1);
printf("\nInput the rows and columns of second matrix : ");
scanf("%d %d",&r2,&c2);
if(c1!=r2){
printf("Mutiplication of Matrix is not possible.");
printf("\nColumn of first matrix and row of second matrix must be same.");
}
else
{
printf("Input elements in the first matrix :\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}
printf("Input elements in the second matrix :\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&brr1[i][j]);
}
}
printf("\nThe First matrix is :\n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
printf("%d\t",arr1[i][j]);

NSRIT Page 31
C PROGRAMMING LAB MANUAL FOR R19

printf("\nThe Second matrix is :\n");


for(i=0;i<r2;i++)
{
printf("\n");
for(j=0;j<c2;j++)
printf("%d\t",brr1[i][j]);
}
//multiplication of matrix
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
crr1[i][j]=0;
for(i=0;i<r1;i++) //row of first matrix
{
for(j=0;j<c2;j++) //column of second matrix
{
sum=0;
for(k=0;k<c1;k++)
sum=sum+arr1[i][k]*brr1[k][j];
crr1[i][j]=sum;
}
}
printf("\nThe multiplication of two matrices is : \n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c2;j++)
{
printf("%d\t",crr1[i][j]);
}
}
}
printf("\n\n");
}

NSRIT Page 32
C PROGRAMMING LAB MANUAL FOR R19

Output:
Multiplication of two Matrices :
----------------------------------
Input the rows and columns of first matrix : 2
2
Input the rows and columns of second matrix : 2
2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Input elements in the second matrix :
element - [0],[0] : 5
element - [0],[1] : 6
element - [1],[0] : 7
element - [1],[1] : 8
The First matrix is :
1 2
2 4

The Second matrix is :


5 6
7 8
The multiplication of two matrices is :
19 22
43 50

NSRIT Page 33
C PROGRAMMING LAB MANUAL FOR R19

6.b
Aim: Write a program in C to find transpose of a given matrix.
Source code:

#include <stdio.h>

void main()

{
int arr1[50][50],brr1[50][50],i,j,k=0,r,c;

printf("\n\nTranspose of a Matrix :\n");


printf("---------------------------\n");

printf("\nInput the rows and columns of the matrix : ");


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

printf("Input elements in the first matrix :\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}

printf("\nThe matrix is :\n");


for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
printf("%d\t",arr1[i][j]);
}

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
brr1[j][i]=arr1[i][j];
}
}

printf("\n\nThe transpose of a matrix is : ");


for(i=0;i<c;i++){
printf("\n");
for(j=0;j<r;j++){
printf("%d\t",brr1[i][j]);

NSRIT Page 34
C PROGRAMMING LAB MANUAL FOR R19

}
}
printf("\n\n");
}

NSRIT Page 35
C PROGRAMMING LAB MANUAL FOR R19

Output:
Transpose of a Matrix :
---------------------------
Input the rows and columns of the matrix : 2 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4

The matrix is :
1 2
2 4
The transpose of a matrix is :
1 3
2 4

NSRIT Page 36
C PROGRAMMING LAB MANUAL FOR R19

Exercise – 7

7. A
Aim: Write a program in C to search an element in a row wise and column wise sorted
matrix.
Source code:

#include <stdio.h>

int searchElement(int arr2D[4][4], int n, int x)

int i = 0, j = n-1;

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

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

printf("\nThe element Found at the position in the matrix is: %d, %d", i, j);

return 1;

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

j--;

else

i++;

printf("\nThe given element not found in the 2D array.");

return 0;

int main()

int arr2D[4][4] = { {15, 23, 31, 39},

NSRIT Page 37
C PROGRAMMING LAB MANUAL FOR R19

{18, 26, 36, 43},

{25, 28, 37, 48},

{30, 34, 39, 50},

};

int i,j,v;

v=37;

//------------- print original array ------------------

printf("The given array in matrix form is : \n");

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

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

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

printf("\n");

//------------------------------------------------------

printf("The given value for searching is: %d",v);

searchElement(arr2D, 4, v);

return 0;

NSRIT Page 38
C PROGRAMMING LAB MANUAL FOR R19

Output:
The given array in matrix form is :
15 23 31 39
18 26 36 43
25 28 37 48
30 34 39 50
The given value for searching is: 37
The element Found at the position in the matrix is: 2, 2

NSRIT Page 39
C PROGRAMMING LAB MANUAL FOR R19

7. B
Aim: Write a program in C to print individual characters of string in reverse order.
Source Code:

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

void main()

char str[100]; /* Declares a string of size 100 */

int l,i;

printf("\n\nPrint individual characters of string in reverse order :\n");

printf("------------------------------------------------------\n");

printf("Input the string : ");

fgets(str, sizeof str, stdin);

l=strlen(str);

printf("The characters of the string in reverse are : \n");

for(i=l;i>=0;i--)

printf("%c ", str[i]);

printf("\n");

NSRIT Page 40
C PROGRAMMING LAB MANUAL FOR R19

Output:

Print individual characters of string in reverse order :


-----------------------------------------------------------
Input the string : w3resource.com
The characters of the string in reverse are :
m o c . e c r u o s e r 3 w

NSRIT Page 41
C PROGRAMMING LAB MANUAL FOR R19

Exercise – 8

8. A

Aim: Write a program in C to compare two strings without using string library functions.
Source code:
#include<stdio.h>
 
int main() {
   char str1[30], str2[30];
   int i;
 
   printf("\nEnter two strings :");
   gets(str1);
   gets(str2);
 
   i = 0;
   while (str1[i] == str2[i] && str1[i] != '\0')
      i++;
   if (str1[i] > str2[i])
      printf("str1 > str2");
   else if (str1[i] < str2[i])
      printf("str1 < str2");
   else
      printf("str1 = str2");
 
   return (0);
}

NSRIT Page 42
C PROGRAMMING LAB MANUAL FOR R19

Output:
Enter two strings: NSRIT
CSE Department
Str1 > Str2

NSRIT Page 43
C PROGRAMMING LAB MANUAL FOR R19

8. B
Aim: Write a program in C to copy one string to another string.
Source code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void main()
{
char str1[100], str2[100];
int i;

printf("\n\nCopy one string into another string :\n");


printf("-----------------------------------------\n");
printf("Input the string : ");
fgets(str1, sizeof str1, stdin);

/* Copies string1 to string2 character by character */


i=0;
while(str1[i]!='\0')
{
str2[i] = str1[i];
i++;
}

//Makes sure that the string is NULL terminated


str2[i] = '\0';

printf("\nThe First string is : %s\n", str1);


printf("The Second string is : %s\n", str2);
printf("Number of characters copied : %d\n\n", i);
}

NSRIT Page 44
C PROGRAMMING LAB MANUAL FOR R19

Output:

Copy one string into another string :


-----------------------------------------
Input the string : This is a string to be copied.

The First string is : This is a string to be copied.

The Second string is : This is a string to be copied.

Number of characters copied : 31

NSRIT Page 45
C PROGRAMMING LAB MANUAL FOR R19

Exercises - 9

9. A
Aim: Write a C Program to Store Information Using Structures with Dynamically
Memory Allocation
Source code:
#include <stdio.h>
#include<stdlib.h>

struct course
{
int marks;
char subject[30];
};

int main()
{
struct course *ptr;
int i, noOfRecords;
printf("Enter number of records: ");
scanf("%d", &noOfRecords);

// Allocates the memory for noOfRecords structures with pointer ptr pointing to the base
address.
ptr = (struct course*) malloc (noOfRecords * sizeof(struct course));

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


{
printf("Enter name of the subject and marks respectively:\n");
scanf("%s %d", &(ptr+i)->subject, &(ptr+i)->marks);
}

printf("Displaying Information:\n");

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


printf("%s\t%d\n", (ptr+i)->subject, (ptr+i)->marks);

return 0;
}

NSRIT Page 46
C PROGRAMMING LAB MANUAL FOR R19

Output:
Enter number of records: 2
Enter name of the subject and marks respectively
Programming
22
Enter name of the subject and marks respectively:
Structure
33
Displaying Information:
Programming 22
Structure 33

NSRIT Page 47
C PROGRAMMING LAB MANUAL FOR R19

9. B
Aim: Write a program in C to demonstrate how to handle the pointers in the program.

Source code:
#include <stdio.h>
int main()
{
int* ab;
int m;
m=29;
printf("\n\n Pointer : How to handle the pointers in the program :\n");
printf("------------------------------------------------------------\n");
printf(" Here in the declaration ab = int pointer, int m= 29\n\n");

printf(" Address of m : %p\n",&m);


printf(" Value of m : %d\n\n",m);
ab=&m;
printf(" Now ab is assigned with the address of m.\n");
printf(" Address of pointer ab : %p\n",ab);
printf(" Content of pointer ab : %d\n\n",*ab);
m=34;
printf(" The value of m assigned to 34 now.\n");
printf(" Address of pointer ab : %p\n",ab);
printf(" Content of pointer ab : %d\n\n",*ab);
*ab=7;
printf(" The pointer variable ab is assigned the value 7 now.\n");
printf(" Address of m : %p\n",&m);//as ab contain the address of m
//so *ab changed the value of m and now m become 7
printf(" Value of m : %d\n\n",m);
return 0;
}

NSRIT Page 48
C PROGRAMMING LAB MANUAL FOR R19

Output:

Pointer: How to handle the pointers in the program:


------------------------------------------------------------
Here in the declaration ab = int pointer, int m= 29
Address of m : 0x7fff24a3f8bc
Value of m : 29
Now ab is assigned with the address of m.
Address of pointer ab : 0x7fff24a3f8bc
Content of pointer ab : 29
The value of m assigned to 34 now.
Address of pointer ab : 0x7fff24a3f8bc
Content of pointer ab : 34
The pointer variable ab is assigned the value 7 now.
Address of m : 0x7fff24a3f8bc
Value of m : 7

NSRIT Page 49
C PROGRAMMING LAB MANUAL FOR R19

Exercise - 10

10. A
Aim: Write a program in C to demonstrate the use of & (address of) and *(value at
address) operator
Source code:
#include <stdio.h>
void main()
{
int m=300;
float fx = 300.60;
char cht = 'z';

printf("\n\n Pointer : Demonstrate the use of & and * operator :\n");


printf("--------------------------------------------------------\n");

int *pt1;
float *pt2;
char *pt3;
pt1= &m;
pt2=&fx;
pt3=&cht;
printf ( " m = %d\n",m);
printf ( " fx = %f\n",fx);
printf ( " cht = %c\n",cht);
printf("\n Using & operator :\n");
printf("-----------------------\n");
printf ( " address of m = %p\n",&m);
printf ( " address of fx = %p\n",&fx);
printf ( " address of cht = %p\n",&cht);
printf("\n Using & and * operator :\n");
printf("-----------------------------\n");
printf ( " value at address of m = %d\n",*(&m));
printf ( " value at address of fx = %f\n",*(&fx));
printf ( " value at address of cht = %c\n",*(&cht));
printf("\n Using only pointer variable :\n");
printf("----------------------------------\n");
printf ( " address of m = %p\n",pt1);
printf ( " address of fx = %p\n",pt2);
printf ( " address of cht = %p\n",pt3);
printf("\n Using only pointer operator :\n");
printf("----------------------------------\n");
printf ( " value at address of m = %d\n",*pt1);
printf ( " value at address of fx= %f\n",*pt2);
printf ( " value at address of cht= %c\n\n",*pt3);
}

NSRIT Page 50
C PROGRAMMING LAB MANUAL FOR R19

Output:
Pointer: Demonstrate the use of & and * operator:
--------------------------------------------------------
m = 300
fx = 300.600006
cht = z
Using & operator:
-----------------------
address of m = 0x7fff71cd0b38
address of fx = 0x7fff71cd0b3c
address of cht = 0x7fff71cd0b37
Using & and * operator :
-----------------------------
value at address of m = 300
value at address of fx = 300.600006
value at address of cht = z
Using only pointer variable :
----------------------------------
address of m = 0x7fff71cd0b38
address of fx = 0x7fff71cd0b3c
address of cht = 0x7fff71cd0b37
Using only pointer operator :
----------------------------------
value at address of m = 300
value at address of fx= 300.600006
value at address of cht= z

NSRIT Page 51
C PROGRAMMING LAB MANUAL FOR R19

10. B
Aim: Write a program in C to add two numbers using pointers.

Source code:

#include <stdio.h> 

int main()

   int first, second, *p, *q, sum;

   printf("Enter two integers to add\n");

   scanf("%d%d", &first, &second);

   p = &first;

   q = &second;

   sum = *p + *q;

   printf("Sum of the numbers = %d\n", sum);

   return 0;

NSRIT Page 52
C PROGRAMMING LAB MANUAL FOR R19

Output:

Enter two integers to add

Sum of entered numberce=9

NSRIT Page 53
C PROGRAMMING LAB MANUAL FOR R19

Exercise – 11

11. A

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

Source Code:

#include <stdio.h>
long addTwoNumbers(long *, long *);

int main()
{
long fno, sno, *ptr, *qtr, sum;

printf("\n\n Pointer : Add two numbers using call by reference:\n");


printf("-------------------------------------------------------\n");

printf(" Input the first number : ");


scanf("%ld", &fno);
printf(" Input the second number : ");
scanf("%ld", &sno);
sum = addTwoNumbers(&fno, &sno);
printf(" The sum of %ld and %ld is %ld\n\n", fno, sno, sum);
return 0;
}
long addTwoNumbers(long *n1, long *n2)
{
long sum;
sum = *n1 + *n2;
return sum;
}

NSRIT Page 54
C PROGRAMMING LAB MANUAL FOR R19

Output:

Pointer: Add two numbers using call by reference:

Input the first number: 5

Input the second number: 6

The sum of 5 and 6 is 11

NSRIT Page 55
C PROGRAMMING LAB MANUAL FOR R19

11. B

Aim: Write a program in C to find the largest element using Dynamic Memory Allocation.
Source code:

#include <stdio.h>

#include <stdlib.h>

int main()

int i,n;

float *element;

printf("\n\n Pointer : Find the largest element using Dynamic Memory Allocation :\
n");

printf("-------------------------------------------------------------------------\n");

printf(" Input total number of elements(1 to 100): ");

scanf("%d",&n);

element=(float*)calloc(n,sizeof(float)); // Memory is allocated for 'n' elements

if(element==NULL)

printf(" No memory is allocated.");

exit(0);

printf("\n");

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

printf(" Number %d: ",i+1);

scanf("%f",element+i);

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

NSRIT Page 56
C PROGRAMMING LAB MANUAL FOR R19

if(*element<*(element+i))

*element=*(element+i);

printf(" The Largest element is : %.2f \n\n",*element);

return 0;

NSRIT Page 57
C PROGRAMMING LAB MANUAL FOR R19

Output:]

Pointer: Find the largest element using Dynamic Memory Allocation :


-------------------------------------------------------------------------
Input total number of elements (1 to 100): 5
Number 1: 5
Number 2: 7
Number 3: 2
Number 4: 9
Number 5: 8
The Largest element is: 9.00

NSRIT Page 58
C PROGRAMMING LAB MANUAL FOR R19

Exercise – 12

12. A
Aim: Write a program in C to swap elements using call by reference.
Source code:
#include <stdio.h>
void swapNumbers(int *x,int *y,int *z);
int main()
{
int e1,e2,e3;
printf("\n\n Pointer : Swap elements using call by reference :\n");
printf("------------------------------------------------------\n");
printf(" Input the value of 1st element : ");
scanf("%d",&e1);
printf(" Input the value of 2nd element : ");
scanf("%d",&e2);
printf(" Input the value of 3rd element : ");
scanf("%d",&e3);

printf("\n The value before swapping are :\n");


printf(" element 1 = %d\n element 2 = %d\n element 3 = %d\n",e1,e2,e3);
swapNumbers(&e1,&e2,&e3);
printf("\n The value after swapping are :\n");
printf(" element 1 = %d\n element 2 = %d\n element 3 = %d\n\n",e1,e2,e3);
return 0;
}
void swapNumbers(int *x,int *y,int *z)
{
int tmp;
tmp=*y;
*y=*x;
*x=*z;
*z=tmp;
}

NSRIT Page 59
C PROGRAMMING LAB MANUAL FOR R19

Output:

Pointer : Swap elements using call by reference :


------------------------------------------------------
Input the value of 1st element : 5
Input the value of 2nd element : 6
Input the value of 3rd element : 7

The value before swapping are :


element 1 = 5
element 2 = 6
element 3 = 7

The value after swapping are :


element 1 = 7
element 2 = 5
element 3 = 6

NSRIT Page 60
C PROGRAMMING LAB MANUAL FOR R19

12. B

Aim: Write a program in C to count the number of vowels and consonants in a string
using a pointer.
Source code:
#include <stdio.h>
int main()
{
char str[100];
char *p;
int vCount=0,cCount=0;

printf("Enter any string: ");


fgets(str, 100, stdin);

//assign base address of char array to pointer


p=str;

//'\0' signifies end of the string


while(*p!='\0')
{
if(*p=='A' ||*p=='E' ||*p=='I' ||*p=='O' ||*p=='U'
||*p=='a' ||*p=='e' ||*p=='i' ||*p=='o' ||*p=='u')
vCount++;
else
cCount++;
//increase the pointer, to point next character
p++;
}

printf("Number of Vowels in String: %d\n",vCount);


printf("Number of Consonants in String: %d",cCount);
return 0;
}

NSRIT Page 61
C PROGRAMMING LAB MANUAL FOR R19

Output:

Entered a string: beginnersbook


No of vowels in string: 5
No of consonants in string: 9

NSRIT Page 62
C PROGRAMMING LAB MANUAL FOR R19

Exercise -13

13. A

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

Source Code:

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

int* findLarger(int *n1, int *n2)


{
if(*n1 > *n2)
return n1;
else
return n2;
}

NSRIT Page 63
C PROGRAMMING LAB MANUAL FOR R19

Output:

Pointer: Show a function returning pointer :

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

Input the first number: 5

Input the second number: 6

The number 6 is larger.

NSRIT Page 64
C PROGRAMMING LAB MANUAL FOR R19

13. B

Aim: Write a C program to find sum of n elements entered by user. To perform this
program, allocate memory dynamically using malloc( ) function.
Source code:
#include #include <stdio.h>
#include <stdlib.h>

int main()
{
int n, i, *ptr, sum = 0;

printf("Enter number of elements: ");


scanf("%d", &n);

ptr = (int*) malloc(n * sizeof(int));

// if memory cannot be allocated


if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}

printf("Enter elements: ");


for(i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}

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

// deallocating the memory


free(ptr);

return 0;
}
}

NSRIT Page 65
C PROGRAMMING LAB MANUAL FOR R19

Output:

Enter size: 2
Addresses of previously allocated memory:26855472
26855476

Enter the new size: 4


Addresses of newly allocated memory:26855472
26855476
26855480
26855484

NSRIT Page 66
C PROGRAMMING LAB MANUAL FOR R19

Exercise - 14

14. A

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

Source code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);

ptr = (int*) calloc(n, sizeof(int));


if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}

printf("Enter elements: ");


for(i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}

NSRIT Page 67
C PROGRAMMING LAB MANUAL FOR R19

Output:

Enter size: 2
Addresses of previously allocated memory:26855472
26855476

Enter the new size: 4


Addresses of newly allocated memory:26855472
26855476
26855480
26855484

NSRIT Page 68
C PROGRAMMING LAB MANUAL FOR R19

14. B

Aim: Write a program in C to convert decimal number to binary number using the function.
Source code:

#include<stdio.h>

long toBin(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 = toBin(dno);
printf("\n The Binary value is : %ld\n\n",bno);

return 0;
}
long toBin(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;
}

NSRIT Page 69
C PROGRAMMING LAB MANUAL FOR R19

Output:

Function: convert decimal to binary :


-------------------------------------------
Input any decimal number: 65

The Binary value is: 1000001

NSRIT Page 70
C PROGRAMMING LAB MANUAL FOR R19

Exercise - 15

15. A
Aim: Write a program in C to check whether a number is a prime number or not using
the function.
Source code:
#include<stdio.h>
int PrimeOrNot(int);
int main()
{
int n1,prime;
printf("\n\n Function : check whether a number is prime number or not :\n");
printf("---------------------------------------------------------------\n");

printf(" Input a positive number : ");


scanf("%d",&n1);
prime = PrimeOrNot(n1);
if(prime==1)
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 PrimeOrNot(int n1)
{
int i=2;
while(i<=n1/2)
{
if(n1%i==0)
return 0;
else
i++;
}
return 1;
}

NSRIT Page 71
C PROGRAMMING LAB MANUAL FOR R19

Output:

Function : check whether a number is prime number or not

---------------------------------------------------------------
Input a positive number : 5
The number 5 is a prime number.

NSRIT Page 72
C PROGRAMMING LAB MANUAL FOR R19

15. B
Aim: Write a program in C to get the largest element of an array using the function.

Source code:

#include<stdio.h>
#define MAX 100

int findMaxElem(int []);


int n;

int main()
{
int arr1[MAX],mxelem,i;
printf("\n\n Function : get largest element of an array :\n");
printf("-------------------------------------------------\n");

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]);
}
mxelem=findMaxElem(arr1);

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


return 0;
}
int findMaxElem(int arr1[])
{
int i=1,mxelem;
mxelem=arr1[0];
while(i < n)
{
if(mxelem<arr1[i])
mxelem=arr1[i];
i++;
}
return mxelem;
}

NSRIT Page 73
C PROGRAMMING LAB MANUAL FOR R19

Output:

Function : get largest element of an array :


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

NSRIT Page 74
C PROGRAMMING LAB MANUAL FOR R19

Exercise - 16

16. A
Aim: Write a program in C to append multiple lines at the end of a text file.
Source code:

#include <stdio.h>

int main ()
{
FILE * fptr;
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);
//----- Read the file after appended -------
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);
//------- End of reading ------------------
return 0;
}

NSRIT Page 75
C PROGRAMMING LAB MANUAL FOR R19

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

NSRIT Page 76
C PROGRAMMING LAB MANUAL FOR R19

16. B

Aim: Write a program in C to copy a file in another name.


Source code:

#include <stdio.h>
#include <stdlib.h> // For exit()
  
int main()
{
    FILE *fptr1, *fptr2;
    char filename[100], c;
  
    printf("Enter the filename to open for reading \n");
    scanf("%s", filename);
  
    fptr1 = fopen(filename, "r");
    if (fptr1 == NULL)
    {
        printf("Cannot open file %s \n", filename);
        exit(0);
    }
  
    printf("Enter the filename to open for writing \n");
    scanf("%s", filename);
     
 fptr2 = fopen(filename, "w");
    if (fptr2 == NULL)
    {
        printf("Cannot open file %s \n", filename);
        exit(0);
    }
  
    c = fgetc(fptr1);
    while (c != EOF)
    {
        fputc(c, fptr2);
        c = fgetc(fptr1);
    }
  
    printf("\nContents copied to %s", filename);
  
    fclose(fptr1);
    fclose(fptr2);
    return 0;
}

NSRIT Page 77
C PROGRAMMING LAB MANUAL FOR R19

Output:

Enter the filename to open for reading


a.txt
Enter the filename to open for writing
b.txt
Contents copied to b.txt

NSRIT Page 78
C PROGRAMMING LAB MANUAL FOR R19

16. C

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

Source code:

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

NSRIT Page 79
C PROGRAMMING LAB MANUAL FOR R19

Output:

Remove a file from the disk:

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

Input the name of file to delete: test.txt

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

NSRIT Page 80
C PROGRAMMING LAB MANUAL FOR R19

Additional programs:

Aim: Write a c program to illustrating factorial with recursion without recursion

Source code:

#include<stdio.h>
#include<conio.h>
void main()
{
int n1, n2, ch;
clrscr();
printf("\n Enter the first number:");
scanf("%d", &n1);
printf("\n Enter the second number:");
scanf("%d", &n2);
printf("\n [1] -> Addition ");
printf("\n [2] -> Subtraction ");
printf("\n [3] -> Multiplication ");
printf("\n [4] -> Division ");
printf("\n\n Enter your choice <1...4>:");
scanf("%d", &ch);
switch(ch)
{
case 1 :
printf("\n %d + %d = %d", n1, n2, n1 + n2) ;
break ;
case 2 :
printf("\n %d - %d = %d", n1, n2, n1 - n2) ;
break ;
case 3 :
printf("\n %d * %d = %d", n1, n2, n1 * n2);
break ;
case 4 :
printf("\n %d / %d = %.2f", n1, n2, (float)n1 / n2);

NSRIT Page 81
C PROGRAMMING LAB MANUAL FOR R19

break ;
default :
printf("\n Invalid choice");
break ;
}
getch();
}

NSRIT Page 82
C PROGRAMMING LAB MANUAL FOR R19

Output:
Enter the first number: 3
Enter the second number: 6
[1]  Addition
[2]  Subtraction
[3]  Multiplication
[4]  Division
Enter your choice <1…4> : 1
3+6 = 9

NSRIT Page 83
C PROGRAMMING LAB MANUAL FOR R19

2.

Aim: Write a c program to make a single calculator to add, subtract, multiply or divide
using switch case

Source code:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
int recfib(int);
void nonrecfib(int);
clrscr();
printf("\n Enter the numbers:");
scanf("%d",&n);
printf("\n The Fibonacci series using recursion is:");
for(i=0;i<n;i++)
printf("%d\t",recfib(i));
printf("\n The Fibonacci series without using recursion is:");
nonrecfib(n);
}
int recfib(int x)
{
if(x==0)
return 0;
else if(x==1)
return 1;
else
return recfib(x-1)+recfib(x-2);
}
void nonrecfib(int c)
{
int a=0,b=1,d,i;
printf("\n%d\t%d",a,b);

NSRIT Page 84
C PROGRAMMING LAB MANUAL FOR R19

for(i=3;i<=c;i++)
{
d=a+b;
printf("\t%d",d);
a=b;
b=d;
}
}

NSRIT Page 85
C PROGRAMMING LAB MANUAL FOR R19

Output:
Enter the numbers: 8
The Fibonacci series using recursion is: 
0 1 1 2 3 5 8 13
The Fibonacci series without using recursion is:
 0 1 1 2 3 5 8 13

NSRIT Page 86

You might also like