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

Programming in c Laboratory (2)

The document outlines a laboratory manual for a Programming in C course at Anna University, detailing various experiments and exercises for students. It includes sections on I/O statements, decision-making constructs, loops, arrays, functions, recursion, pointers, structures, and file operations, along with sample programs and algorithms for each topic. The manual serves as a practical guide for students to complete their lab work and prepare for examinations.

Uploaded by

hemabala2501
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)
2 views

Programming in c Laboratory (2)

The document outlines a laboratory manual for a Programming in C course at Anna University, detailing various experiments and exercises for students. It includes sections on I/O statements, decision-making constructs, loops, arrays, functions, recursion, pointers, structures, and file operations, along with sample programs and algorithms for each topic. The manual serves as a practical guide for students to complete their lab work and prepare for examinations.

Uploaded by

hemabala2501
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/ 145

ANNA UNIVERSITY

UNIVERSITY COLLEGE OF ENGINEERING- DINDIGUL


DINDIGUL – 624622

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CS3271- Programming In C Laboratory

Name :
Register no :
Branch :
ANNA UNIVERSITY
UNIVERSITY COLLEGE OF ENGINEERING - DINDIGUL
DINDIGUL - 624622

BONAFIDE CERTIFICATE

This is to certify that this is a bonafide record of work done by

Mr./Ms. _______________________________________________________________________in

_________________________________________________________________ Laboratory during

the academic year 2021– 2022.

University Registration
No:

Staff in Charge Head of the Department

Submitted for the University Practical Examination held on _____________________

INTERNAL EXAMINER EXTERNAL EXAMINER


INDEX
Ex.n Date Name of the experiment Page no. Signature
o
Table of Contents
Ex.No Name of the experiment Page no.
.
1 I/O statements, operators, expressions.

2 Decision-making constructs: if-else, goto, switch-case, break-


continue.

3 Loops: for, while, do-while.

4 Arrray:1D and 2D, Multi-dimensional array, Traversal.

5 Strings:operation.

6 Functions: call, return, passing parameters by (values , reference),


Passing array to function.

7 Recursion.

8 Pointers:Pointer to functions, Array, Strings, Pointer to Pointer,


Array of pointers.

9 Structures: Nested Structure, Pointer to Structure, Array of


Structures and Unions.

10 Files: Reading and writing, File Pointers, File Operations, Random


Access, Processor directrives.
EX.No. : 1(a) PROGRAM USING I/O STATEMENTS AND EXPRESSIONS

DATE :

AIM

To write a C Program to perform I/O statements and expressions.


ALGORITHM

1. Start
2. Declare variables and initializations
3. Read the Input variable.
4. Using I/O statements and expressions for computational processing.
5. Display the output of the calculations.
6. Stop
PROGRAM
#include <stdio.h>
#include <conio.h>
void main()
{
int sumOdd = 0;
int sumEven = 0;
int upperbound;
int absDiff;
int number = 1;
printf("Enter the upper bound: ");
scanf("%d", &upperbound);
while (number <= upperbound)
{
if (number % 2 == 0)
{
sumEven += number;
}
else
{
sumOdd += number;
}
++number;
}

if (sumOdd > sumEven)


{
absDiff = sumOdd - sumEven;
}
else
{
absDiff = sumEven - sumOdd;
}
printf("The sum of odd numbers is %d\n", sumOdd);
printf("The sum of even numbers is %d\n”,sumeven);
printf("The absolute difference is %d\n", absDiff);
getch();
}

RESULT:

Thus a C Program using i/o statements and expressions was executed and the output was obtained.
EX.NO.: 1(b) ARITHMETIC OPERATION

DATE:

AIM

To write a C Program to Design a calculator to perform the operations, namely, addition, subtraction,
multiplication, division and square of a number.

ALGORITHM

1. Start
2. Declare variables
3. Read the Inputs .
4. Calculate Arithmetic operations(+,-,*,/,pow) for the input of two numbers.
5. Display the output of the calculations .
6. Stop

PROGRAM

#include <stdio.h>
#include <conio.h>

int main()
{
int firstNumber, secondNumber; int
sum, difference, product; long square;
float quotient;
printf("Enter First Number: ");
scanf("%d", &firstNumber); printf("Enter
Second Number: "); scanf("%d",
&secondNumber);
sum = firstNumber + secondNumber
difference = firstNumber - secondNumber;
product = firstNumber * secondNumber;
quotient = (float)firstNumber / secondNumber;
square = firstNumber *firstNumber;
printf("\nSum = %d", sum);
printf("\nDifference = %d", difference);
printf("\nMultiplication = %d", product);
printf("\nDivision = %.3f", quotient);
printf("\n Square= %ld", square);
getch();
}

RESULT

Thus a C Program for Arithmetic operations was executed and the output was obtained
Ex.No. :2(a) Decision Making Construct : If else

Date:

AIM

To write a c program to find the roots of the quadratic equation using if…else statement

ALGORITHM

Step 1:start the program.

Step 2:enter the values of a,b,c.

Step 3:find the values of d by using the formula

D=b*b-4*a*c

Step 4:if D is greater than or equal to zero then find the two roots as

root1=(-b+sqrt(d))/(2*a)

root2=(+b+sqrt(d))/(2*a)

Print the two roots.

Step 5:if the D is not greater than or equal to zero then print the statement the roots are imaginary.

Step 6:stop.

PROGRAM

#include<stdio.h>

#include<conio.h>

Void main()

int a,b,c,d;

float root1,root2;

printf(“enter the values of a,b,c\n”);

scanf(“%d %d %d”,&a,&b,&c);

d=b*b-4*a*c;

if(d>=0)
{

root1=(-b+sqrt(d))/(2*a);

root2=(+b+sqrt(d))/(2*a);

printf(“the roots of the values a=%d,b=%d,c=%d are \n %f %f ”,a,b,c,root1,root2);

Else

printf(“the roots are imaginary”)

RESULT

Thus the c program for if else statement was executed and the Output is verified.
EX.NO.:2(b) GOTO STATEMENT.

DATE:

AIM

To write a c program to print the given both number is equal or not.

ALGORITHM

Step 1:Start the program

Step 2:Enter the values of a,b.

Step 3:If a is equivalent to b

Goto step 5

Step 4:If a is not equivalent to b print the statement A and B are not equal

Step 5:Print the statement A and B are equal.

Step 6: Stop.

PROGRAM

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

Void main()

Int a,b;

Printf(“\n enter the numbers:”);

Scanf(“%d %d”,&a&b);

If(a==b)

Goto equal;

Else

Printf(“\n A and B are not equal”);


Exit(0);

Equal

Printf (“A and B are equal”);

RESULT

Thus the c program for goto statement was executed and the output was verified
EX.NO.:2(c) SWITCH STATEMENT

DATE:

AIM

To write a c program for simple menu driven calculator program using

Switch statement.

ALGORITHM

Step 1: start.

Step 2: display menu

Step 3: read the two variables a and b

Step 4: evaluate the option code.

Step 5: evaluate option code with case constants

Step 5.1: case=1

C=a+b

Print c

Goto step 7

Step 5.2: case=2

C=a-b

Print c

Goto step 7

Step 5.3: case=3

C=a*b

Print c

Goto step 7

Step 5.4: case=4

C=a/b

Print c
Goto step 7

Step 5.5: case=0

Exit

Step 6: entered case option is invalid code then print”invalid operation

code”.

Step 7: stop

PROGRAM

#include<stdio.h>

#include<conio.h>

Void main()

Int a,b,c,n;

Clrscr();

Printf(“--MENU--\n”);

Printf(“1 addition\n”);

Printf(“2 subraction\n”);

Printf(“3 multiplication\n”);

Printf(“4 division\n”);

Printf(“0 EXIT\n”);

Printf(“enter your choice:”);

Scanf(“%d”,&n);

If(n<=4&n.0)

Printf(“enter two numbers :”);

Scanf(“%d %d”,&a,&b);

Switch(n)
{

Case 1:

C=a+b;

Printf(“addition: %d\n”);

Break;

Case 2:

C=a-b;

Printf(“subraction: %d\n”);

Break;

Case 3:

C=a*b;

Printf(“multiplication: %d\n”);

Break;

Case 4:

C=a/b;

Printf(“division: %d\n”);

Break;

Case 0:

Exit(0);

Break;

Default:

Printf(“invalid operation code”);

Getch();

RESULT

Thus the c program for switch statement was executed and the output was verified
EX.NO.: 3(a) LOOPS: FOR LOOP

DATE:

AIM

To write a c progaram to find the factorial of the given number.

ALGORITHM

Step 1: start the program

Step 2: enter a number

Step 3: set a loop to find the factorial of the given number using the formula

Fact=fact*I

Step 4:print the factorial of the given number

Step 5: stop

PROGRAM

#include<stdio.h>

#include<conio.h>

Void main()

Int fact=1,i,num;

Printf(“enter thhe number”);

Scanf(“%d”,num);

For(i=1;i<=num;i++)

Fact=fact*i;

Printf(“the factorial of %d is %d”,num,fact);

RESULT

Thus the c program for FOR loop was executed and the output was verified
EX.NO.:3(b) WHILE LOOP

DATE:

AIM

To write a c program to find sum of digits and to check the given number is palindrome or not

ALGORITHM

Step 1: start the program

Step 2: enter the number

Step 3: set a loop upto the number is not equal to zero

Step 4: rem <---- num%10

Step 5: sum<---- sum+rem

Step 6: rnum<---- rnum*10+rem

Step 7: num <---- num/10

Step 8: after the end of the loop print the sum and reverse number of the digit

Step 9: find whether the reverse number is equal to the given number or not.If equal the number is not
palindrome.

Step 10: if not equal the given number is not palindrome

Step 11: stop

PROGRAM

#include<stdio.h>

#include<conio.h>

Void main()

Unsigned long int a,num,sum=0,rnum=0,rem;

Printf(“\n enter the number….”);

Scanf(“%ld”,&num);

a=num;

While(num!=0)
{

Rem=num%10;

Sum=sum+rem;

Rnum=rnum*10+rem;

Num=num/10;

Printf(“\n the sum of the digits of %ld is %ld\n ”,a,sum);

Printf(“\nthe reverse number of the %ld is %ld”,a,rnum);

If(a==num)

Printf(“\n the given number is a palindrom”);

Else

Printf(“\n the given number is not a palindrom”);

RESULT

Thus the c program for while loop was executed and the output was verfied
EX.NO.: 3(c) DO……WHILE

DATE:

AIM

To write a c program to add numbers until the user enter zero using do…while

ALGORITHM

Step 2: declare variables

Step 3: read the input number

Step 4: check the condition the number is not equl to zero

Step 5: sum the number until user enter zero

Step 6: display the output

Step 7: stop

PROGRAM

#include<stdio.h>

#include<conio.h>

Void main()

Double number , sum=0;

Clrscr();

Do

Printf(“enter a number:”);

Scanf(“%lf”,&number);

Sum+=number;

While(number!=0.0)

Printf(“sum=%.2lf”,sum);

Getch();

}
RESULT

Thus the c program for do…while loop was executed sucessfully and the output was verified
EX.NO.: 4(a) ARRAYS: 1D

DATE:

AIM

To write a c program to print the square values of array values

ALGORITHM

Step 1: start the program

Step 2: declare the array value, variable

Step 3: using for loop give the condition to square the values

Step 4: print the statement to print the output

Step 5: stop

PROGRAM

#included<stdio.h>

#include<conio.h>

Void main()

int values[10]={1,2,3,4,5,};

int i;

for(i=5;i<10;++i)

values[i]=i*i;

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

printf("array values [%i]=%i\n",i,values[i]);

getch();

RESULT

Thus the c prograam for one dimensional array was executed sucessfully and the output

was verified.
EX.NO.:4(b) ARRAY:2D

DATE:

AIM

To write a c program to specify two dimensin array

ALGORITHM

Step 1: start the program

Step 2: declare the array values and variables

Step 3: using for loop we are giving a condition that is

For(i=0;i<=3;i++)

Step 4: if the condition is true print the statement inside the for loo[p

Step 5: stop

PROGRAM

#include<stdio.h>

#include<conio.h>

void main()

int stud[4][2];

int i;

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

printf("\n enter the %d student roll no and mark:",i);

scanf("%d %d",&stud[i][0],&stud[i][1]);

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

printf("%d students roll no %d mark %d",i,stud[i][0],stud[i][1]);

getch();

}
RESULT

Thus the c program for two dimensional array was executed sucessfully and the output

were verified
EX.NO.:4(c) MULTIDIMENSIONAL ARRAY

DATE:

AIM

To write a c program to multiply two matrixes

ALGORITHM

Step 1: start the program

Step 2: enter the row and column of the A matrix

Step 3: enter the row and column of the B matrix

Step 4: enter the elements of the A matrix

Step 5: enter the elements of the B matrix

Step 6: print the elements of the A matrix in matrix form

Step 7: print the elements of the B matrix in matrix form

Step 8: set the loop up to row

Step 9: set the loop up to column

Step 10: set another inneer loop up to column

Step 11: multiply the A and B matrix and store the elements in the C matrix

Step 12: print the resultant matrix

Step 13: stop

PROGGRAM

#include<stdio.h>

#include<conio.h>

void main()

int a[25][25],b[25][25],c[25][25],i,j,k,r,s;

int m,n;

printf("\n enter the row and columns of A matrix...");

scanf("%d %d",&m,&n);

printf("\n enter the rows and columns of B matrix...");


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

if(m!=r)

printf("\n the matrix cannot multiplied");

else

printf("\n enter the elements of A matrix");

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

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

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

printf("\n enter the elements of B matrix");

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

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

scanf("\t %d",&b[i][j]);

printf("\n the elements of A matrix");

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

printf("\n");

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

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

printf("\n the elements of B matrix");

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

{
printf("\n");

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

printf("\t %d",b[i][j]);

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

printf("\n");

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

c[i][j]=0;

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

c[i][j]=c[i][j]+a[i][j]*b[i][j];

printf("the multiplication of two matrixes");

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

printf("\n");

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

printf("\t %d",c[i][j]);

RESULT

Thus the c program for multidimensional array was executed sucessfully and the output was

Verified
EX.NO.:4(d) TRAVERSAL ARRAY

DATE:

AIM

To write a c program to find whether the string is palindrome or not

ALGORITHM

Step 1: start the program

Step 2: enter the string.

Step 3: find the string length using the strlen() functions

Step 4: print the string length

Step 5: set a loop up to the half of the string length

Step 6: compare every character above the middle character with the below character of the

Middle character

Step 7: if any character equal prints the given string is palindrome

Step 8: if the character is not equal then print the given string is not a palindrome

Step 9: stop

PROGRAM

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void main()

int len=0,i,j;

char name[25];

printf("enter the string...");

scanf("%s",name);

while(name[len]!='\0')

len++;

printf("\n %d",len);
for(i=0,j=len-1;i<len/2;i++,j--)

if(name[i]!=name[j])

printf("\n the given string is not a palindrome");

exit(0);

printf("\n the given string is a palindrome");

RESULT

Thus the c program or traversal array was executed sucessfully and the output was verified
EX.NO.:5(a) STRING OPERATION: STRLEN

DATE:

AIM

To find the length of the given string

ALGORITHM

Step 1: Start

Step 2: read str

Step 3: set a for loop to calculate length of the string

Step 4: print length of the string using variable len

Step 5: stop

PROGRAM

#include<stdio.h>

#include<conio.h>

void main()

char str[50];

int len;

printf("enter the string:");

scanf("%[^\n]",str);

for(len=0;str[len]!='\0';len++);

printf("length of main string is %d \n",len);

RESULT

Thus the c program for finding the length of the string was executed sucessfully and the

Was verified
EX.NO.:5(b) STRCMP

DATE:

AIM

To write a c program to compare the given strings

ALGORITHM

Step 1: start

Step 2: read the two string str1,str2

Step 3: set a loop to compare two string

Step 4: check if(str1[i]=str2[i])

Step 4.1: print “the both string is equal” else

Step 4.2: print “the both string is not equal”

Step 5: stop

PROGRAM

#include<stdio.h>

#include<conio.h>

void main()

int i;

char str1[20],str2[20];

printf("enter the first and second string:");

scanf("%s %s",str1,str2);

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

if(str1[i]==str2[i])

printf("the both string is equal");

else

printf("the both string is not equal");

}
}

RESULT

Thus the c program to compare the given strings was executed sucessfully and the output

Was verified
EX.NO.:5(c) STRING CONCATENATION

DATE:

AIM

To wirte a c program to concatenate the given strings

ALGORITHM

Step 1: start

Step 2: read str1,str2

Step 3: set a loop to move str3 to str1

Step 4: set a loop to move str3 to str2

Step 5: print str3

Step 6: stop

PROGRAM

#include<stdio.h>

#include<conio.h>

void main()

char str1[10],str2[10],str3[40];

int i,j;

printf("enter the two string:");

scanf("%s %s",str1,str2);

for(i=0;str1[i]!='\0';i++)

str3[i]=str1[i];

for(j=0;str2[j]!='\0';j++)

str3[i]=str2[j];

i++;
}

str3[i]='\0';

printf("concatenated string is....%s\n",str3);

RESULT

Thus the c program to concatenate the given string was executed sucessfully and the output

Was verified
EX.NO.:6(a) FUNCTIONS: CALL AND RETURN

DATE:

AIM

To write a c program to illustrate a function with arguments with return values

ALGORITHM

Step 1: start the program

Step 2: enter the two numbers

Step 3: call the function with two arguments passed to it

Step 4: add the two number in the calling function

Step 5: return the addition values to the called function from the calling function

Step 6: print the addition values in the main function

Step 7: end

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

int a,b,c;

printf("enter the two number.....");

scanf("%d %d",&a,&b);

c=add(a,b);

printf("the addition of two numbers %d and %d is %d",a,b,c);

add(int x,int y)

int z;

z=x+y;

return(z);
}

RESULT

Thus the c program to call and return the was executed sucessfully and the output

Was verified
EX.NO.:6(b) FUNCTIONS:PASSING PARAMETER BY VALUES

DATE:

AIM

To write a c program to find the cube of given values

ALGORITHM

Step 1: Start the program.

Step 2: enter the values to be swaped

Step 3: Call the function swap(a,b)

Step 3a: Start function.

Step 3b: Assign t ← x

Step 3c: Assign x ← y

Step 3d: Assign y ← t

Step 3e: Print x and y.

Step 3f: End function.

Step 4: Stop the program.

PROGRAM

#include<stdio.h>

#include<conio.h>

void swap(int, int);

int main()

int x, y;

printf("Enter the value of x and y\n");

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

printf("Before Swapping\nx = %d\ny = %d\n", x, y);

swap(x, y);

printf("After Swapping\nx = %d\ny = %d\n", x, y);

return 0;
}

void swap(int a, int b)

int temp;

temp = b;

b = a;

a = temp;

printf("Values of a and b is %d %d\n",a,b);

RESULT

Thus the c program to swap the two numbers using call by value was executed sucessfully and the

output was verified


EX.NO.:6(c) FUNCTIONS:PASSING PARAMERTER BY REFERENCE

DATE:

AIM

To write a C Program to Sort the list of numbers using pass by reference.


ALGORITHM
Step 1: Start
Step 2: Declare variables and create an array
Step 3: Read the Input for number of elements and each element.
Step 4: Develop a function to sort the array by passing reference
Step 5: Compare the elements in each pass till all the elements are sorted.
Step 6: Display the output of the sorted elements .
Step 7: Stop
PROGRAM
#include <stdio.h>
#include <conio.h>
void main()
{
int n,a[100],i;
void sortarray(int*,int);
printf("\nEnter the Number of Elements in an array : ");
scanf("%d",&n);
printf("\nEnter the Array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]); sortarray(a,n);
printf("\nAfter Sorting \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]); getch();
}
void sortarray(int* arr,int num)
{
int i,j,temp;
{
for(i=0;i<num;i++)
for(j=i+1;j<num;j++)
if(arr[i] > arr[j])
{
temp=arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

RESULT

Thus a C Program Sorting using pass by reference was executed and the output was obtained.
EX.NO.:6(d) FUNCTIONS:PASSING ARRAY TO FUNCTIONS

DATE:

AIM

To write a c program to illustrate the method of passing of a two dimensional array to a function

ALGORITHM

Step 1: start the program

Step 2: Start at index zero, compare the element with the next one (a[0] & a[1] (a is the name of the array)),
and swap if a[0] > a[1]. Now compare a[1] & a[2] and swap if a[1] > a[2]. Repeat this process until the end of
the array. After doing this, the largest element is present at the end. This whole thing is known as a pass. In the
first pass, we process array elements from [0,n-1].

Step 3: Repeat step one but process array elements [0, n-2] because the last one, i.e., a[n-1], is present at its
correct position. After this step, the largest two elements are present at the end.

Step 4: Repeat this process n-1 times

Step 5: stop

PROGRAM

#include<stdio.h>

#include<conio.h>

void bubble_sort(long [], long);

int main()

long array[100], n, c, d, swap;

printf("Enter number of elements\n");

scanf("%ld", &n);

printf("Enter %ld longegers\n", n);

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

scanf("%ld", &array[c]);

bubble_sort(array, n);

printf("Sorted list in ascending order:\n");

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


printf("%ld\n", array[c]);

return 0;

void bubble_sort(long list[], long n)

long c, d, t;

for (c = 0 ; c < ( n - 1 ); c++)

for (d = 0 ; d < n - c - 1; d++)

if (list[d] > list[d+1])

t = list[d];

list[d] = list[d+1];

list[d+1] = t;

RESULT

Thus the c program to sort the elements using bubble sort for passing array to function was

Executed sucesssfully and the output was verified


EX.NO.:7 RECURSION

DATE:

AIM

To write a c program for binary search using recursion

ALGORITHM

Step 1: start the program

Step 2: declare an aarray a[50],I,n and loc as ‘int’ data type

Step 3: declare bin () function as ‘int’ data type

Step 4: enter the size of matrix

Step 5: for I=0 to less than ‘n’

Step 5.1: read and placed in a[i]

Step 6: for I=0 to less than ‘n’

Step 6.1: print a(i)

Step 7: enter the elements to be searched

Step 8: assign loc= bin(a,o,n)

Step 9: check if (loc==0)

Step 9.1: print “unsucessful search %d not found”

Step 10: else

Step 10.1: print “sucessful search found”

Step 11: stop the program

RECURSIVE FUNCTION

Step 1: global declaration of an array b(50) low and high

Step 2: declare mid as ‘static int’ of local declaration and ‘i’ as ‘int’ as ‘int’ data type

Step 3: mid=(low+high)

Step 3.1: check if(key<b(mid))

Step 3.2: high = mid-1

Step 3.2.1: bin(b,low,high)

Step 3.3: else if(key==b(mid))


Step 3.3.1: low=mid+1

Step 3.3.2: bin (b,low,high)

Step 3.4: else if (key==b(mid))

Step 3.4.1: return(mid+1)

Step 4: else

Step 4.1: return(0)

Step 5: stop the program

PROGRAM

#include<stdio.h>

#include<conio.h>

int key;

void main()

int a[50],i,n,loc;

int bin (int*,int,int);

printf("enter the size of the array:");

scanf("%d",&n);

printf("\n enter array elements(ascending order):\n");

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

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

printf("\n enter elements to be searched:");

scanf("%d",&key);

loc=bin(a,0,n);

if(loc==0)

printf("unsucessful search. %d not found.\n",key);

else

{
printf("sucessful search.\n");

printf("%d found at position %d. \n",key,loc);

int bin(int b[],int low,int high)

static int mid;

int i;

if(low<=high)

mid=(low+high)/2;

if(key<b[mid])

high=mid-1;

bin(b,low,high);

else if(key>b[mid])

low=mid+1;

bin(b,low,high);

else if(key==b[mid])

return(mid+1);

else

return(0);

}
RESULT

Thus the c program recursion process was executed sucessfully and the output was verified
EX.NO.:8(a) POINTERS:POINTERS TO FUNCTIONS

DATE:

AIM

To write a c program for printing the sum of the salary and bonus using pointers to functions

ALGORITHM:

Step 1: start the program

Step 2: declare the function(globally) with pointers

Step 3: declare the values and variables inside the main()

Step 4: call the functions and do the operation

Step 5: print the result

Step 6: stop

PROGRAM

#include <stdio.h>

void salaryhike(int *var, int b)

*var = *var+b;

void main()

int salary=0, bonus=0;

printf("Enter the employee current salary:");

scanf("%d", &salary);

printf("Enter bonus:");

scanf("%d", &bonus);

salaryhike(&salary, bonus);

printf("Final salary: %d", salary);

}
RESULT

Thus the c program for printing the sum of salary and bonus using pointer to functions

Was executed sucessfully and the output was verified


EX.NO.:8(b) POINTERS:POINTERS TO ARRAY

DATE:

AIM

To write a c program using pointers to array

ALGORITHM

Step 1: start the program

Step 2: define the size of the array by #define ARRAY_SIZE 5

Step 3: declare the values and variables

Step 4: declare pointer variables

Step 5: using for loop condition for(i=0; i< ARRAY_SIZE ; ++i), assign the array values

Step 6: print the result

Step7: stop

PROGRAM

#include<stdio.h>

#include<conio.h>

#define ARRAY_SIZE 5

void main()

int arr[ARRAY_SIZE] = {1,2,3,4,5};

int i = 0;

int (*ptr)[ARRAY_SIZE];

ptr = &arr;

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

printf(" arr[%d] = %d\n",i,(*ptr)[i]);

}
RESULT

Thus the c program using pointer to array was executed sucessfully and the output was verified.
EX.NO.:8(c) POINTERS: POINTERS TO STRING

DATE:

AIM

To write a c program for displaying a string using pointers to string

ALGORITHM

Step 1: start the program

Step 2: declare the string variables

Step 3: declare the pointer variables

Step 4: print the string

Step 5: move the pointer to the next allocation

Step 6: stop

PROGRAM

#include <stdio.h>

#include<conio.h>

void main()

char str[6] = "Hello";

char *ptr = str;

while(*ptr != '\0')

printf("%c", *ptr);

ptr++;

RESULT

Thus the c program for displaying the string using pointer to strings
EX.NO.:8(d) POINTERS: POINTERS TO POINTERS

DATE:

AIM

To write a c program for pointers to pointers

ALGORITHM

Step 1: start the program

Step 2: declare the values for the variables

Step 3: declare the pointer variables

Step 4: declare the double pointer variables

Step 5: the pointer ‘p’ is pointing to the address of ‘a’ the double pointer ‘pp’ is pointing to the address ‘p’

Step 6: both the address are printed

Step 7: print the values stored in the address

Step 8: stop

PROGRAM

#include<stdio.h>

#include<conio.h>

void main ()

int a = 10;

int *p;

int **pp;

p = &a;

pp = &p;

printf("address of a: %x\n",p);

printf("address of p: %x\n",pp);

printf("value stored at p: %d\n",*p);

printf("value stored at pp: %d\n",**pp);

}
RESULT

Thus the c program for pointer to pointer was executed sucessfully and the output was vereifed
EX.NO.:8(e) POINTERS: ARRAY OF POINTER

DATE:

AIM

To write a c program for displaying values of array using array of pointers

ALGORITHM

Step 1: start the program

Step 2: declare the values to the variables

Step 3: create an array of integer pointers

Step 4: using the for loop ,print the values using pointers

Step 5: stop

PROGRAM

#include <stdio.h>

#include <conio.h>

void main()

int a = 10;

int b = 20;

int c = 30;

int i = 0;

int *arr[3] = {&a,&b,&c};

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

printf("Value of arr[%d] = %d\n", i, *arr[i]);

RESULT

Thus the c program for pointers to pointers was executed sucessfully amd the output was verified
EX.NO.:9(a) STRUCTURES: NESTED STRUCTURES

DATE:

AIM

To write a c program to generate the fibbonaci series

ALGORITHM

Step 1: start the program

Step 2: enter the number

Step 3: check whether the number is zero or not

if zero print zero values. If not zero or not

Step 4: set a loop upto the given number

Step 5: fib= fib+a;

a=b;

b=c;

Step 6: every increament in the loop prints the values of fib

Step 7: after the execution of loop stops the program

PROGRAM

#include<stdio.h>

#include<conio.h>

Void main()

int num, fib=0, a=0, b=1,i;

printf("enter the number");

scanf("%d",&num);

printf("\n fibbonaci series \n");

if(num==0)

printf("0");

else

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

fib=fib+a;

a=b;

b=fib;

printf("%d\t",fib);

RESULT

Thus the c program to generate the fibbonaci series was executed sucessfully and the output

Was verified
EX.NO.:9(b) STRUCTURES: POINTERS TO STRUCTURES

DATE:

AIM

To write a c program for displaying the students records using pointers to structures

ALGORITHM

Step 1: start the program

Step 2: declare the structure

Step 3: declare the variables

Step 4: declare the structure variables

Step 5: print the details using the pointer structure

Step 6: stop

PROGRAM

#include <stdio.h>

#include <string.h>

struct student

int id;

char name[30];

float percentage;

};

void main()

int i;

struct student record1 = {1, "Raju", 90.5};

struct student *ptr;

ptr = &record1;

printf("Records of STUDENT1: \n");


printf(" Id is: %d \n", ptr->id);

printf(" Name is: %s \n", ptr->name);

printf(" Percentage is: %f \n\n", ptr->percentage);

RESULT

Thus the c program for pointers to structures was executed sucessfully and output was verified
EX.NO.:9(c) STRUCTURES: ARRAY OF STRUCTURES

DATE:

AIM

To write a c program for displaying three students name, id, marks using array of structures

ALGORITHM

Step 1: start the program

Step 2: declare the structure

Step 3: declare the structure variables

Step 4: declare the variables

Step 5: give the data for name ,id ,and the marks of three students

Step 6: print the name, id, marks of the students

Step 7: stop

PROGRAM

#include<stdio.h>

#include<conio.h>

struct student

char name[20];

int id;

float marks;

};

void main()

struct student s1,s2,s3;

int dummy;

printf("Enter the name, id, and marks of student 1 ");

scanf("%s %d %f",s1.name,&s1.id,&s1.marks);

scanf("%c",&dummy);
printf("Enter the name, id, and marks of student 2 ");

scanf("%s %d %f",s2.name,&s2.id,&s2.marks);

scanf("%c",&dummy);

printf("Enter the name, id, and marks of student 3 ");

scanf("%s %d %f",s3.name,&s3.id,&s3.marks);

scanf("%c",&dummy);

printf("Printing the details....\n");

printf("%s %d %f\n",s1.name,s1.id,s1.marks);

printf("%s %d %f\n",s2.name,s2.id,s2.marks);

printf("%s %d %f\n",s3.name,s3.id,s3.marks);

RESULT

Thus the c program for array of structure was executed sucessfully and the output was verified
EX.NO.:9(d) STRUCTURES: ARRAY OF UNION

DATE:

AIM

To write a c program for union of two array using array of union

ALGORITHM

Step 1: start the program

Step 2: declare the variables

Step 3: declare the function calls

Step 4: fuctions are called first and process to be done. After this control pass to the main function

Step 5: enter the sizes of the two arrays to be union

Step 6: enter the values of the two arrays to be union

Step 7: using for loop the arrays are union

Step 8: print the result as output

Step 9: stop

PROGRAM

#include<stdio.h>

#include<conio.h>

int removerepeated(int size,int a[]);

void sort(int size,int a[]);

void main()

int i,size1,size2,size,j=0,k;

printf("Enter size of an array1\n");

scanf("%d",&size1);

printf("Enter size of an array2\n");

scanf("%d",&size2);

int a[size1],b[size2],uni[size1+size2];

printf("Enter numbers for array 1\n");


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

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

printf("Enter numbers for array 2\n");

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

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

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

uni[j]=a[i];

j++;

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

uni[j]=b[i];

j++;

sort(size1+size2,uni);

size=removerepeated(size1+size2,uni);

printf("Array afetr Union \n");

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

printf("%d\n",uni[i]);

int removerepeated(int size,int a[])


{

int i,j,k;

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

for(j=i+1;j<size;)

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

for(k=j;k<size;k++)

a[k]=a[k+1];

size--;

Else

j++;

return(size);

void sort(int size,int a[]){

int i,j,temp;

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

for(j=i+1;j<size;j++){

if(a[i]>a[j]){

temp=a[i];
a[i]=a[j];

a[j]=temp;

RESULT

Thus the c program for union of values of two array using array of union was executed sucessfully

And the output was verified.


EX.NO.:10(a) FILES: READING AND WRITING

DATE:

AIM

To write a c program to create, write and read files using files

ALGORITHM

Step 1: start the program

Step 2: declare the main function

Step 3: declare the file pointer

Step 4: creating the file using the file pointer

Step 5: checking the file is created or not by if statement

Step 6: data is given to the file for writing

Step 7: again opening the file for reading the data

Step 8: checking whether the is opening or not

Step 9: close the file by fclose()

Step 10: stop

PROGRAM

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void main()

FILE *fp;

char fName[20];

printf("\nEnter file name to create :");

scanf("%s",fName);

fp=fopen(fName,"w");

if(fp==NULL)
{

printf("File does not created!!!");

exit(0);

printf("File created successfully.");

putc('A',fp);

putc('B',fp);

putc('C',fp);

printf("\nData written successfully.");

fclose(fp);

fp=fopen(fName,"r");

if(fp==NULL)

printf("\nCan't open file!!!");

exit(0);

printf("Contents of file is :\n");

printf("%c",getc(fp));

printf("%c",getc(fp));

printf("%c",getc(fp));

fclose(fp);

RESULT

Thus the c program for writing and reading the file was executed sucessfully and the output

Was verified
EX.NO.:10(b) FILE: FILE POINTER

DATE:

AIM

To write a c program for count the number of character in the file

ALGORITHM

Step 1: start the program

Step 2: declare the file pointer

Step 3: declare the variables

Step 4: open the text file for reading

Step 5: using the while statements and if statement we are countig the number of character in the file

Step 6: print the number of character in the file

Step 7: close the file by fclose()

Step 8: stop

PROGRAM

#include<stdio.h>

#include<conio.h>

void main()

FILE *fp;

char ch;

int noc = 0;

fp = fopen("ok.txt", "r");

while(1)

ch = fgetc(fp);

if(ch == EOF)

break;
}

noc++;

fclose(fp);

printf("\nNumber of characters in file: %d", noc);

RESULT

Thus the c program for count the number of character in a text file was executed sucessfully

and the output was verified


EX.NO.:10(c) FILES: FILE OPERATION

DATE:

AIM

To write a c program for file operation using the fprrintf() and scanf() statement

ALGORITHM

Step 1: start the program

Step 2: declare the file pointer

Step 3: declare the variables

Step 4: file is first opening for writing the file

Step 5: using fprintf() assigning the new data to the file

Step 6: file is opening for second time for reading the file

Step 7: using fscanf() in while loop the new data is printed

Step 8: close the file by fclose()

Step 9: stop

PROGRAM

#include<stdio.h>

#include<conio.h>

void main()

FILE *fp;

fp = fopen("ok.txt", "w");

fprintf(fp, "Hello file by fprintf...\n");

fclose(fp);

char buff[255];

fp = fopen("ok.txt", "r");

while(fscanf(fp, "%s", buff)!=EOF)

printf("%s ", buff );


}

fclose(fp);

RESULT

Thus the c program for file operation using fprintf() and fscanf() statement was executed

sucessfully and the output was verified


EX.NO.:10(d) FILES: RANDOM ACCESS

DATE:

AIM

To write a c program to finding and displaying the current position of the pointer using random

access file

ALGORITHM

Step 1: start the program

Step 2: declare the file pointer variable

Step 3: using fopen() we are opening the file both in write and read mode

Step 4: using random access file command the current position of file is found

Step 5: print the currrent position of the file

Step 6: print the modified text of the file

Step 7: close the file by fclose()

Step 8: stop

PROGRAM

#include <stdio.h>

#include <conio.h>

void main ()

FILE *fp;

int c;

fp = fopen("ok.txt","w+");

fputs("This is study.com", fp);

fseek( fp, 7, SEEK_SET );

fputs(" C Programming", fp);

printf("The current position of the file pointer is: %ld\n", ftell(fp));

rewind(fp);

printf("The current position of the file pointer is: %ld\n", ftell(fp));


while(1)

c = fgetc(fp);

if( feof(fp) )

break;

printf("%c", c);

fclose(fp);

RESULT

Thus the c program for printing the current position of the file using random access file was

executed sucessfully and the output was verified


EX.NO.:10(e) FILES: PROCESSOR DIRECTRIVES

DATE:

AIM

To write a c program for printing some values like height, number, letter, letter sequence,

Characters,etc., using the processor directrives

ALGORITHM

Step 1: start the program

Step 2: defining and declaring the values for the variables like number, letter, letter sequence,

backslash character,etc., By using #define statement

Step 3: main() function is declared

Step 4: give the print statements to print the height, number, letter,letter sequence and backslash

Character

Step 5: stop

PROGRAM

#include <stdio.h>

#include <conio.h>

#define height 100

#define number 3.14

#define letter 'A'

#define letter_sequence "ABC"

#define backslash_char '\?'

void main()

{ printf("value of height : %d \n", height );

printf("value of number : %f \n", number );

printf("value of letter : %c \n", letter );

printf("value of letter_sequence : %s \n", letter_sequence);

printf("value of backslash_char : %c \n", backslash_char);

}
RESULT

Thus the c program for printing stuffs by processor directives was exected sucessfully and output was

Verified

You might also like