[go: up one dir, main page]

0% found this document useful (0 votes)
24 views69 pages

C Lab Manual Final 2022-ArunSir

The document outlines a series of basic C programming exercises, including programs for printing names, calculating areas of geometric shapes, finding roots of quadratic equations, checking odd/even numbers, and more. Each exercise includes an aim, algorithm, program code, output, and result indicating successful execution. The programs cover fundamental concepts such as loops, conditionals, and array manipulation.

Uploaded by

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

C Lab Manual Final 2022-ArunSir

The document outlines a series of basic C programming exercises, including programs for printing names, calculating areas of geometric shapes, finding roots of quadratic equations, checking odd/even numbers, and more. Each exercise includes an aim, algorithm, program code, output, and result indicating successful execution. The programs cover fundamental concepts such as loops, conditionals, and array manipulation.

Uploaded by

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

1.

BASIC C PROGRAMS
EX.NO:1a
PRINTING STUDENT NAME AND COLLEGE NAME
DATE:

AIM:
To write a program in C to display the student name and college name.

ALGORITHM:

Step 1: Start the program

Step 2: Include the header files

Step 3: Display the student name and college name using printf statement.

Step 4: Stop the program

PROGRAM:
#include<stdio.h>
#include<coinio.h>
void main()
{
printf(“ANBU\n”);
printf(“RVSCET”);
getch();
}
OUTPUT:

ANBU

RVSCET

RESULT:

Thus the C program for displaying the names has been executed successfully and
output is verified.
EX.NO:1b
AREA CALCULATION OF VARIOUS SHAPES
DATE:

AIM:

To write a C program to find the area of various geometric shapes.

ALGORITHM:

Step 1: Start the program

Step 2: Read the radius of circle

Step 3: Calculate the area of circle using the formula (22 * r * r) / 7

Step 4: Read the radius of length and breadth of rectangle

Step 5: Calculate area of rectangle using the formula l * b

Step 6: Read the side of the square

Step 7: Calculate the area of the square using s * s formula

Step 8: Read the breadth and height of triangle

Step 9: Calculate triangle area = (b * h) / 2

Step 10: Stop the program

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
float r,l,b,s,h;
float circle,rectangle,square,triangle;
printf("enter the numbers:");
scanf("%f%f%f%f%f",&r,&l,&b,&s,&h);
circle=(22*r*r)/7;
printf("\narea of the circle is:%f",circle);
rectangle=l*b;
printf("\narea of the rectangle is:%f",rectangle);
square=s*s;
printf("\narea of the square is:%f",square);
triangle=(b*h)/2;
printf("\narea of the triangle is:%f",triangle);
getch();
}

OUTPUT:

Enter the numbers: 4 6 4 5 7

area of the circle is: 50.281754

area of the rectangle is : 24

area of the square is:25

area of the triangle is:14

RESULT:

Thus a C program to find the area of various geometric shapes has been executed
successfully and got verified.
EX.NO:1c

DATE: FIND THE ROOTS OF A QUADRATIC EQUATION

AIM:

To write a C Program to find the roots of a quadratic equation.


ALGORITHM:
Step 1: Start the program
Step 2: Read the variables a,b,c,d,r1,r2.
Step 3: Compute d=b*b-4*a*c
Step 4: Compute r1=(-b+sqrt(d))/2*a;
Step5: Compute r2=(-b-sqrt(d))/2*a;
Step 5: Print the roots of r1 and r2
Step 6: Stop the program
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,r1,r2;
clrscr();
printf(“\nEnter the values of a,b and c”);
scanf(“%f%f%f”,&a,&b,&c);
d=b*b-4*a*c;
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf(“The root1 and root2 of a=%f b=%f c=%f is \n %f \n %f“,a,b,c,r1,r2);
getch();
}
Output:
Enter the values of a,b and c
7
35
9
The root1 and root2 of a=7 b=35 c=9 is
-0.271932
-4.728068

RESULT:

Thus the program to find the root of a quadratic equation has been executed successfully
and output is verified.
EX.NO:1d

DATE: FIND THE GIVEN NUMBER IS ODD OR EVEN

AIM:
To write a C Program to find the given number is odd or even.

ALGORITHM:

Step 1: Start the program


Step 2: Read the variables number.
Step 3: Compute if (number%2==0)
Step 4: print the number is even
Step 5: else
Step6: print the number is odd
Step 6: Stop the program
PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int number;
clrscr();
printf(“\nEnter the number”);
scanf(“%d”,&number);
if(number%2==0)
{
printf(“\nThe number is even number”);
}
else
{
printf(“\nThe number is odd number”);
}
getch();
}
OUTPUT:
Enter the number
5
The number is odd number
Enter the number
4
The number is even number

RESULT:
Thus the program to find the given number is odd or even has been executed
successfully.
EX.NO:1e
FIND THE LARGEST OF THREE NUMBERS
DATE:

AIM:

To write a C Program to find the largest among three numbers


ALGORITHM:

Step 1: Start the program


Step 2: Read the variables a, b and c.
Step 3: Compute if (a>b) and (a>c)
Step 4: Print the number a is largest.
Step 5: else go to step 6
Step6: Print the number c is largest
Step 7: else go to step 8
Step 8: Compute if (b>c)
Step 9: print the number b is largest
Step 10: else go to step 11
Step 11:Print the number c is largest
Step 6: Stop the program
PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“\nEnter the numbers a,b and c”);
scanf(“%d %d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
printf(“\n%d is largest number”,a);
else
printf(“\n%d is largest number”,c);
}
else
{
if(b>c)
printf(“\n%d is largest number”,b);
else
printf(“\n%d is largest number”,c);
}
getch();
}

OUTPUT:

Enter the numbers a b and c


5 10 15
15 is largest number

RESULT:
Thus the program to find the largest among three numbers has been successfully
executed and output is verified.
EX.NO:1f
MULTIPLICATION TABLE FOR THE GIVEN NUMBER
DATE:

AIM:

To write a C Program to print multiplication table for the given number

ALGORITHM:

Step 1: Start the program


Step 2: Read the table number you want to print, say table
Step 3: For i=1 to 12
Step 4: Print the value of i * table
Step 4: Stop the program
PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int table;
int i,count;
clrscr();
printf("Enter the number of table you want to print\n");
scanf("%d",&table);
printf("Result:\n");
for(i=1;i<=10;i++)
{
printf("%d * %d =%d\n",i,table,i*table);
}
getch();
}
OUTPUT:

Enter the number of tables you want to print


2
Result:
1* 2=2
2*2=4
3*2=6
4*2=8
5*2=10
6*2=12
7*2=14
8*2=16
9*2=18
10*2=20

RESULT:

Thus the program for printing multiplication table has been successfully executed and
output is verified.
EX.NO:1g
FIRST N NUMBERS DIVISIBLE BY 5
DATE:

AIM:
To write a program in c to print the first n numbers divisible by 5.

ALGORITHM:

Step 1: Start the program


Step 2: Read the value of n
Step 3: Apply for loop (for(i=1;i<=n;i++))
Step 4: check if (i%5==0) then
Step 4: Print i else
Step 5: Stop the program

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
printf("\n Enter the number of terms:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%5==0)
{
printf("\t%d",i);
}
}
getch( );
}
OUTPUT:
Enter the number of terms: 50
5 10 15 20 25 30 35 40 45 50

RESULT:

Thus a C program to print the first n numbers divisible by 5 has been executed
successfully and output is verified.
EX.NO:1h
PALINDROME USING INTEGERS
DATE:

AIM:

To write a C program to check the given integers are palindrome or not.

ALGORITHM:

Step 1: Start the program


Step 2: Read the value of n
Step 3: Initialize result=0 and sum=0
Step 4: Assign e=n
Step 5: Repeat the following steps until n>0
Step 5.1: result = n%10
Step 5.2: sum = sum*10 + result
Step 5.3:n = n/10
Step 6: If e=sum, Print the given number is palindrome
Step 7: Otherwise, Print the given number is not palindrome
Step 5: Stop the program

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int n,result=0,sum=0,e;
clrscr();
printf("Enter no:");
scanf("%d",&n);
e=n;
while(n>0) {
result =n%10;
sum =sum *10+result;
n=n/10;
} if(e==sum)
printf("It ia a Palindrome");
else
printf("It is not a palindrome");
getch();
}

Output:
Enter no:121
It is a Palindrome
Enter no: 345
It is not a palindrome

RESULT:

Thus the program for checking the palindrome of a given number has been
executed successfully.
EX.NO:1i
CHECKING PRIME NUMBER
DATE:

AIM:

To write a C program to check the given integer is prime or not.

ALGORITHM:

Step 1: Start the program


Step 2: Read the value of n
Step 3: Assign i=2
Step 4: Repeat the steps from 4.1 to 5 until i<=n-1
Step 4.1: Check if n % i = 0 Then Print “Not Prime” and Exit else
Step 4.2: Increment i(i = i+1)
Step 5: if i=n then Print “Prime Number”
Step 6: Stop the program

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,c=0,i,n;
clrscr();
printf(" Enter the no to be checked:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
a=n%i;
if(a==0)
{
c=c+1;
}
}
if(c==2)
{
printf(" It is a prime number");
}
else
{
printf(" It is not a prime number");
}
getch( );
}

OUTPUT:

Enter a no to be checked: 6
It is not a prime number
Enter a no to be checked: 5
It is a prime number

RESULT:

Thus the program for checking the prime number of a given number has been
executed successfully.
EX.NO:1j
FIBONACCI NUMBER
DATE:

AIM:

To create a C program to generate the Fibonacci series for the given number.

ALGORITHM:

Step 1: Start the program


Step 2: Read the value of n
Step 3: Assign f1 = 0, f2=1 and f=0
Step 4: Print f1,f2
Step 5: f=f1+f2
Step 6: Repeat the steps 6.1 to 6.4 until f<n
Step 6.1 f = f1+f2
Step 6.2 f1 = f2
Step 6.3 f2 = f
Step 6.4 Print f
Step 7: Stop the program
PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int f,f1=-1,f2=1,x,a;
clrscr();
printf("Enter a no:");
scanf("%d",&n);
for(a=0;a<n;a++)
{
f=f1+f2;
f1=f2;
f2=f;
printf("\n%d",f);
}
getch();
}

OUTPUT:

Enter a no: 5

01123

RESULT:

Thus the C program to generate the Fibonacci series for the given number has been
executed successfully.
EX.NO:1k
ASCENDING/ DESCENDING ORDER
DATE:

AIM:

To create a C program to sort the numbers in ascending and descending order using an
array.

ALGORITHM:

Step 1: Start the program


Step 2: Read the value of n.
Step 3: Read the value of n array elements a[0],a[1]…a[n-1] using for loop.
Step 4: For i=0 to n
For j=i+1 to n
If a[i] > a[j] then assign temp=a[i], a[i]=a[j], a[j]=temp.
Step 5: For i=0 to n
Print the value of a[i] to print the numbers in ascending order
Step 6: For i=n-1 to 0
Print the value of a[i] to print the numbers in descending order
Step 7: Stop the program.
PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,j,temp;
clrscr();
printf("Enter the no of values");
scanf("%d",&n);
printf("Enter the elements one by one\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}}}
printf("\n Ascending order");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
printf("\n Descending order");
for(i=n-1;i>=0;i--)
printf("\t%d",a[i]);
getch();
}

OUTPUT:

Enter the no of values 5


Enter the elements one by one
23
12
54
76
90
Ascending order 12 23 54 76 90
Descending order 90 76 54 23 12

RESULT:

Thus the program to sort the number in ascending and descending order has been
executed and verified.
EX.NO:1l
MATRIX ADDITION
DATE:

AIM:
To write a C program to add two matrices using an array.

ALGORITHM:

Step 1: Start the program


Step 2: Declare the required array size and Read the values of matrix A and B
Step 3: Apply for loop for matrix A and B
Step 3a: For i=0 to row
For j=0 to column
dij=xij+yij
Step 4: Display the result matrix using for loop and printf function
Step 5: Stop the program

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf(“Enter the values of Matrix A : \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“Enter the values of Matrix B : \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf(“Resultant Matrix:\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,c[i][j]);
}
printf(“\n”);
}
getch();
}
OUTPUT:

Enter the values of Matrix A:

2 2 2

2 2 2

2 2 2

Enter the values of Matrix A:

2 2 2

2 2 2

2 2 2

Resultant Matrix:

4 4 4

4 4 4

4 4 4

RESULT:

Thus the program for addition of two matrices has been executed and verified.
EX.NO:1m
MATRIX MULTIPLICATION
DATE:

AIM:
To write a C program to multiply two matrices using an array.

ALGORITHM:

Step 1: Start the program


Step 2: Declare the required array size and Read the values of matrix A and B
Step 3: Apply for loop for matrix A and B
Step 3a: For i=0 to row
For j=0 to column
cij=0
For k=0 to column
Compute cij=cij+aik*bkj
Step 4: Display the result matrix using for loop and printf function
Step 5: Stop the program

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j,k;
clrscr();
printf(“Enter the values of Matix A : \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“Enter the values of Matix B : \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf(“Resultant Matrix\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,c[i][j]);
}
printf(“\n”);
}
getch();
}
OUTPUT:

Enter the values of Matrix A:

1 1 1

1 1 1

1 1 1

Enter the values of Matrix A:

1 1 1

1 1 1

1 1 1

Resultant Matrix:

2 4 6

8 10 12

14 16 18

RESULT:

Thus the program for multiplication of two matrices has been executed and verified.
EX.NO:1n
PROGRAM TO FIND TRANSPOSE OF MATRIX
DATE:

AIM:
To write a C program to compute the transpose of matrix using an array.

ALGORITHM:

Step 1: Start the program


Step 2: Declare the required array size and Read the values of matrix A and B
Step 3: Apply for loop for matrix A and B
Step 4: Perform the transpose using an array
Step 4: Display the transpose matrix using for loop and printf function
Step 5: Stop the program
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j;
clrscr();
printf(“Enter the values in array : \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“\nTranspose of given matrix : \n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\n”,i,j,a[j][i]);
}
}
getch();

}
Output:

Enter the values in array

1 2 3

4 5 6

7 8 9

Transpose of given matrix:

1 4 7

2 5 8

3 6 9

RESULT:

Thus the program to compute transpose of matrix has been executed successfully.
EX.NO:1o
FACTORIAL USING RECURSIVE FUNCTION
DATE:

AIM:
To write a C program to compute the factorial of a given number.

ALGORITHM:

Step 1: Start the program


Step 2: Read the number a
Step 3: Call the f=factorial function with value of a
Step 3a:Check If (x==0) then
return 1
else
return x*factorial(x-1)
Step 4: Print the factorial of the number
Step 5: Stop the program

PROGRAM:

#include<stdio.h>
#include<conio.h>
int fact(int);
int main()
{
int a;
clrscr();
printf("\nEnter the number:");
scanf("%d",&a);
printf("The factorial of %d! is %d",a,fact(a));
}
int fact(int x)
{
int f;
if(x==1)
return(1);
else
f=x*fact(x-1);
return(f);
}
OUTPUT:

Enter the number:5


The factorial of 5! is 120

RESULT:

Thus a C program to find factorial of a given number using recursive function has been
executed successfully and got verified.
2. PROGRAMS USING STRING FUNCTIONS

EX.NO:2a
STRING FUNCTIONS
DATE:

AIM:

To create a C program to manipulate the strings using string functions

ALGORITHM:

Step 1: Start the program


Step 2: Read the value of string str
Step 3: Perform the following operations
3a.Copy the string from str to str1 using strcpy(str1,str)
3b.Find the length of a string using strlen(str)
3c.Reverse a string using strrev(str)
3d.Concatenate the strings using strcat(str1,str)
3e.Compare the two strings using strcmp(str1,str)
3f.Convert a string to uppercase using strupr(str)
3e. Convert a string to lowercase using strlwr(str)
Step 4: Print the copied string str1.
Step 5: Stop the program.
Program:

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

void main()
{
char str[50],str1[50];
int l;
clrscr();
printf("\nEnter the string1:");
gets(str);
printf("\nEnter the string2:");

gets(str1);
l=strlen(str);
printf("Length of the string is :%d\n",l);
strupr(str);
printf("Upper case string is :%s\n",str);
strlwr(str);

printf("Lower case string is :%s\n",str);


strcmp(str,str1);
if(strcmp==0)
printf("Strings are equal\n");
else
printf("Strings are not equal\n");

strcat(str,str1);
printf("concatenated string is: %s\n",str);
strcpy(str1,str);
printf("Copied string is: %s\n",str1);
strrev(str);
printf("Reversed string is :%s\n",str);

getch();
}
OUTPUT:

Enter the string1:rvs

Enter the string2:college

Length of the string is:3

Upper case string is:RVS

Lower case string is:rvs

Strings are not equal

Concatenated string is:rvscollege

Copied string is:rvscollege

Reversed string is:egellocsvr

RESULT:

Thus the C program for manipulations of strings has been successfully executed and
verified.
EX.NO:2b
PALINDROME USING STRINGS
DATE:

AIM:

To write a C program to check the given string is palindrome or not.

ALGORITHM:

Step 1: Start the program


Step 2: Read the string using an array

Step 3: Copy and reverse the string


Step 4: Compare the copied string and the reversed string using the condition
If(strcmp(s1,s2)==0) then
Step 5: Print the string is palindrome else
Step 6: Print the string is not a palindrome
Step 7: Stop the program

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[15],s2[15];clrscr( );
printf("\nEnter the string:");
scanf("%s",s1);
strcpy(s2,s1);
strrev(s1);
if(strcmp(s1,s2)==0)
printf("\n The string is palindrome");
else
printf("\n The string is not a palindrome");
getch();
}
OUTPUT:
Enter the string:ece
The string is palindrome
Enter the string:cse
The string is not a palindrome

RESULT:

Thus the C program for palindrome of strings has been successfully executed and
verified.
EX.NO:2c
COUNT THE NUMBER OF VOWELS,CONSONANTS DIGITS AND
DATE: WHITE SPACES PRESENT IN A LINE OF TEXT

AIM:
To write a program in c to accept a line of text and count the number of vowels,
consonants, white spaces and digits present in it.

ALGORITHM:

Step 1: Start the program


Step 2: Read the string line
Step 3: Check vowels from the string line using if condition then
Step 4: Check consonants using else if condition then
Step 5: Check digits and white spaces using else if condition then
Step 3: Print the number of consonants, white spaces, digits and vowels
Step 4: Stop the program

PROGRAM:

#include<stdio.h>
#include<conio.h>
int main()
{
char line[150];
int i, vowels, consonants, digits, spaces;
clrscr();m
vowels = consonants = digits = spaces = 0;
printf("Enter a line of string: ");
scanf("%[^\n]", line);
for(i=0; line[i]!='\0'; ++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')
{
++vowels;
}
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
{
++consonants;
}
else if(line[i]>='0' && line[i]<='9')
{
++digits;
}
else if (line[i]==' ')
{
++spaces;
}
}

printf("Vowels: %d",vowels);
printf("\nConsonants: %d",consonants);
printf("\nDigits: %d",digits);
printf("\nWhite spaces: %d", spaces);
getch();
return 0;
}

OUTPUT:

Enter a line of string: RVS college of engg and tech


Vowels:7
Consonants:16
Digits:0
Whitespaces:5

RESULT:

Thus a C program to count vowels, consonants and digits has been executed successfully
and got verified.
3. PROGRAMS USING STRUCTURES AND POINTERS
EX.NO:3a
SWAPPING TWO NUMBERS USING CALL BY VALUE AND CALL
DATE: BY REFENCE

AIM:

To write a C program for swapping of two numbers using call by value and call by
reference.

ALGORITHM:

Call by value:

Step 1: Start the program

Step 2: Declare a function and read the values

Step 3: Define the function

Step 4: Pass the value of the actual arguments to the formal arguments

Step 5: Swap the two numbers

Step 6: Print the result

Step 7: Stop the program

Call by reference:

Step 1: Start the program

Step 2: Declare a function and pointer variables

Step 3: Read the values and define the function

Step 4: Pass the address of the actual arguments to the formal arguments

Step 5: Swap the two numbers

Step 6: Print the result

Step 7: Stop the program


PROGRAM:

Call by value:

#include <stdio.h>

#include<conio.h>

int add(int,int);

int main()

int a,b,c;

clrscr();

printf("\nEnter two number:");

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

c=add(a,b);

printf("\nSum is:%d",c);

int add(int x,int y)

int z;

z=x+y;

return(z);

Call by reference:

#include <stdio.h>

#include<conio.h>

void swap(int*,int*);

void main()
{

int x,y;

printf("\nEnter value of x:");

scanf("%d",&x);

printf("\nEnter value of y:");

scanf("%d",&y);

swap(&x,&y);

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

void swap(int *a,int *b)

int c;

c=*a;

*a=*b;

*b=c;

printf("\nx=%d,y=%d",*a,*b);

}
OUTPUT:

Call by value:

Enter two number:6

Sum is:13

OUTPUT:

Call by reference:

Enter value of x:5

Enter value of y:6

x=6,y=5

RESULT:

Thus the C program for swapping of two numbers has been executed successfully
and output is verified
EX.NO:3b
MARKSHEET OF ‘N’ STUDENTS USING STRUCTURES
DATE:

AIM:
To create a program to print the mark sheet of ‘n’ student using structures

ALGORITHM:

Step 1: Start the program


Step 2: Create the structure student with fields name,rollno, m1,m2,m3,total.
Step 3: Create the structure variable s1[10] for the structure student.
Step 4: Read the number of students num.
Step 5: Read the value of name, rollno, marks (m1,m2,m3) for the specified number of
students using structure variable s1[i] for i= 0 to num.
Step 6: Calculate the total for each student using s1[i] for i=0 to num.
Step 7: For i=0 to num
Print the students details such as name, rollno, marks and total for all students
using s1[i]
Step 8: Stop the program

PROGRAM:

#include<stdio.h>
#include<conio.h>
struct student
{
char name[10][10];
int rollno,m1,m2,m3,total;
};
void main()
{
int num,i,j;
struct student s1[10];
clrscr();
printf("Enter the number of students");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("Enter the roll number\n");
scanf("%d",&s1[i].rollno);
printf("Enter the name \n");
scanf("%s",&s1[i].name);
printf("Enter the mark1\n");
scanf("%d",&s1[i].m1);
printf("Enter the mark2\n");
scanf("%d",&s1[i].m2);
printf("Enter the mark3\n");
scanf("%d",&s1[i].m3);
s1[i].total=s1[i].m1+s1[i].m2+s1[i].m3;
}
printf("The details of the mark list is as follows \n");
printf("\nRollno");
printf("\tName ");
printf("\tMark1");
printf("\tMark2");
printf("\tMark3");
printf("\tTotal");
printf("\n");
for(i=0;i<num;i++)
{
printf("\n%d",s1[i].rollno);
printf("\t%s",s1[i].name);
printf("\t%d",s1[i].m1);
printf("\t%d",s1[i].m2);
printf("\t%d",s1[i].m3);
s1[i].total=s1[i].m1+s1[i].m2+s1[i].m3;
printf("\t%d",s1[i].total);
}
getch();
}
OUTPUT:
Enter the number of stuents2
Enter the roll number
4561
Enter the name
Lokesh
Enter the mark198
Enter the mark278
Enter the mark369
Enter the roll number
4562
Enter the name
Mani
Enter the mark188
Enter the mark289
Enter the mark398
The details of the mark list is as follows
Rollno Name Mark1 Mark2 Mark3 Total
4561 Lokesh 98 78 69 245
4562 Mani 88 89 98 275

RESULT:
The program to print the mark sheet of students using Structure has been executed
successfully.
EX.NO:3c
ARRAY USING POINTERS
DATE:

AIM:
To write a “C” Program to print elements of array using pointers

ALGORITHM:

Step 1: Start the program


Step 2: Read the value of n.
Step 3: Read the value of array of elements x[i] from i=0 to n-1.
Step 4: Assign the address of x[0] to pointer variable p
Step 5: For i=0 to n
Print the value of array elements using pointer variable p
Step 6: Stop the program

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,x[10];
int *ipa;
clrscr();
printf("Enter the number of elements:\n");
scanf("%d",&n);
printf("Enter the array numbers one by one\n");
for(i=0;i<n;i++)
scanf("%d",&x[i]);
printf("The elements present in the array are\n");
ipa=&x[0];
for(i=0;i<n;i++)
printf("\n%d",*(ipa+i));
getch();
}
OUTPUT:
Enter the number of elements:
4
Enter the array numbers one by one
8201
The elements present in the array are
8201

RESULT:
Thus a C program has been written to print elements of array using pointers.
4. IMPLEMENTATION OF LINEAR SEARCH AND BINARY SEARCH

EX.NO:4a
LINEAR SEARCH
DATE:

AIM:
To write a C program to search the key value in an array using linear search.

ALGORITHM:
Step 1: Start the program

Step 2: Read the elements of an array.


Step 3: Read the number to be searched
Step 5: Set f=0
for i=0 to n-1
if(a[i]=x)
f=1

print the element found and exit


end
if f=0
print the element not found
Step 7: Print the result
Step 8: Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,key,f=0;
clrscr();
printf("\n\n LINEAR SEARCH \n\n");
printf("Enter the array limit:");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched:\n");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf("Element is found\n");
f=1;
break;
}
}
if(f==0)
{
printf("Element is not found\n");
}
getch();
}
OUTPUT:

LINEAR SEARCH
Enter the array limit:5
Enter array elements:
1
3
5
7
9
Enter the element to be searched: 9
Element is found

LINEAR SEARCH
Enter the array limit 5
Enter array elements:
2
4
6
8
10
Enter the element to be searched: 9
Element is not found

RESULT:
Thus c program to search the key value with an array using linear search has been
executed and output is verified.
EX.NO:4b
BINARY SEARCH
DATE:

AIM:
To write a c program to search a number using binary search.

ALGORITHM:
Step 1: start the program
Step 2: read the element.

Step 3: enter the search elment.


Step 4: initialize first=0 and last=n-1
while(first<=last)
middle=(first+last)/2
if ( search < array[middle] )
last = middle - 1;

else if( search > array[middle] )


first= middle + 1;
else
print result found
f=1
Step 5: if f=0

Print element not found


Step 7: print the result
Step 8: stop the program.

PROGRAM:

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

int main()
{
int arr[50],i,n,x,flag=0,first,last,mid;
clrscr();
printf("Enter size of array:");
scanf("%d",&n);
printf("\nEnter array element(ascending order)\n");

for(i=0;i<n;++i)
scanf("%d",&arr[i]);

printf("\nEnter the element to search:");


scanf("%d",&x);

first=0;
last=n-1;

while(first<=last)
{
mid=(first+last)/2;

if(x==arr[mid]){
flag=1;
break;
}
else
if(x>arr[mid])
first=mid+1;
else
last=mid-1;
}

if(flag==1)
printf("\nElement is found at position %d",mid+1);
else
printf("\nElement is not found");
getch();
return 0;

}
OUTPUT:

Binary Search using Array


Enter size of array:5
Enter array elements (Ascending order)
2
4
6
8
10
Enter the element to search: 10
Element is found at position 5.

Binary Search using Array


Enter size of array:5
Enter array elements(Ascending order)
2
4
6
8
10
Enter the element to search: 11
Element is not found

RESULT:
Thus a c program to search a number using binary search has been executed and
verified.
EX.NO:4c
BINARY SEARCH USING RECURSION
DATE:

AIM:
To write a c program to search a number using binary search using recursion.

ALGORITHM:
Step 1 : Find the middle element of array. using ,

middle = initial_value + end_value / 2 ;


Step 2 : If middle = element, return ‘element found’ and index.
Step 3 : if middle > element, call the function with end_value = middle - 1 .
Step 4 : if middle < element, call the function with start_value = middle + 1 .
Step 5 : Apply binarySearch() is a recursive function, which is used to search an item in the
sorted array and return the index of the item to the calling function.
Step 8: stop the program.

#include <stdio.h>

int binarySearch(int*, int, int, int, int);

int main()

int arr1[10], i, n, md, c, low, hg;

printf("\n\n Recursion : Binary searching :\n");

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

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

scanf("%d", &n);

printf(" Input %d numbers of elements in the array in ascending order :\n", n);
for (i = 0; i < n; i++)

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

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

printf(" Input the number to search : ");

scanf("%d", &md);

low = 0, hg = n - 1;

c = binarySearch(arr1, n, md, low, hg);

if (c == 0)

printf(" The search number not exists in the array.\n\n");

else

printf(" The search number found in the array.\n\n");

return 0;

int binarySearch(int arr1[], int n, int md, int low, int hg)

int mid, c = 0;

if (low <= hg)

mid = (low + hg) / 2;

if (md == arr1[mid])

{
c = 1;

else if (md < arr1[mid])

return binarySearch(arr1, n, md, low, mid - 1);

else

return binarySearch(arr1, n, md, mid + 1, hg);

} else

return c;

OUTPUT:

Input the number of elements to store in the array :3

Input 3 numbers of elements in the array in ascending order :

element - 0 : 15

element - 1 : 25

element - 2 : 35

Input the number to search : 35

The search number found in the array.

RESULT:
Thus a c program to search a number using binary search using recursion has been
executed and verified.
5. SORTING
EX.NO:5
SELECTION SORT
DATE:

AIM:

To write a C program to sort the given numbers using selection sort.

ALGORITHM:

Algorithm for Selection Sort:

Step 1 − Set min to the first location

Step 2 − Search the minimum element in the array

Step 3 – swap the first location with the minimum value in the array

Step 4 – assign the second element as min.

Step 5 − Repeat the process until we get a sorted array.

PROGRAM:

#include <stdio.h>
int main()
{
int a[100], n, i, j, position, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d Numbersn", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
swap=a[i];
a[i]=a[position];
a[position=swap;
}
}
printf("Sorted Array:n");
for(i = 0; i < n; i++)
printf("%dn", a[i]);
return 0;
}
OUTPUT:

Enter number of elements


4
Enter 4 elements
4
2
7
1
Sorted Array:
1
2
4
7

RESULT:

Thus a c program to sort the given numbers using selection sort has been executed and
verified successfully.
6. FILE HANDLING (SEQUENTIAL ACCESS)

EX.NO:6a
READING A FILE

DATE:

AIM:

To write a C program to read a file in read mode.

PROGRAM

#include<stdio.h>
void main( )
{
FILE *fp ;
char ch ;
fp = fopen("sample.txt","r") ;
while ( 1 )
{
ch = fgetc ( fp ) ;
if ( ch == EOF )
break ;
printf("%c",ch) ;
}
fclose (fp ) ;
}
OUTPUT:

The content of the file (sample.txt) will be printed

Sample.txt
Department of CSE

RESULT:

Thus a c program to opens a file in read mode has been executed and verified
successfully.
EX.NO:6b
EMPLOYEE INFORMATION
DATE:

AIM:

To write a C program to store employee information as entered by user from console.

PROGRAM:

#include <stdio.h>
void main()
{
FILE *fptr;
int id;
char name[30];
float salary;
fptr = fopen("emp.txt", "w+");/* open for writing */
if (fptr == NULL)
{
printf("File does not exists \n");
return;
}
printf("Enter the id\n");
scanf("%d", &id);
fprintf(fptr, "Id= %d\n", id);
printf("Enter the name \n");
scanf("%s", name);
fprintf(fptr, "Name= %s\n", name);
printf("Enter the salary\n");
scanf("%f", &salary);
fprintf(fptr, "Salary= %.2f\n", salary);
fclose(fptr);
}
OUTPUT:
Enter e id
1
Enter the name
sonoo
Enter the salary
120000

emp.txt
Id= 1
Name= sonoo
Salary= 120000

RESULT:

Thus a c program to store employee information as entered by user from console has
been executed and verified successfully.
RANDOM ACCESS
EX.NO:6c
FSEEK
DATE:

AIM:

To write a C program to set the file pointer to the specified offset and to write data into file
at desired location.
PROGRAM:

#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
char c;
clrscr();
fp=fopen("myfile.txt","r");
while((c=fgetc(fp))!=EOF){
printf("%c",c);
}
fclose(fp);
getch();
}
OUTPUT:
Enter e id

myfile.txt

This is sonoo jaiswal

1
Salary= 120000

RESULT:

Thus C program to set the file pointer to the specified offset and to write data into file at
desired location has been executed and verified successfully.
RANDOM ACCESS
EX.NO:6d
FTELL
DATE:

AIM:

To write a C program to returns the current file position of the specified stream and to get
the total size of a file after moving file pointer at the end of file

PROGRAM:

#include <stdio.h>
#include <conio.h>
void main (){
FILE *fp;
int length;
clrscr();
fp = fopen("file.txt", "r");
fseek(fp, 0, SEEK_END);
length = ftell(fp);
fclose(fp);
printf("Size of file: %d bytes", length);
getch();
}
OUTPUT:
Enter e id

Size of file: 21 bytes

1
Salary= 120000

RESULT:

Thus C program to returns the current file position of the specified stream and to get the
total size of a file after moving file pointer at the end of file has been executed and verified
successfully.
RANDOM ACCESS
EX.NO:6e
REWIND
DATE:

AIM:

To write a C program to set the file pointer at the beginning of the stream and to use
stream many times.
PROGRAM:

#include<stdio.h>

#include<conio.h>

void main(){

FILE *fp;

char c;

clrscr();

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

while((c=fgetc(fp))!=EOF){

printf("%c",c);

rewind(fp);//moves the file pointer at beginning of the file

while((c=fgetc(fp))!=EOF){

printf("%c",c);

fclose(fp);

getch();

}
OUTPUT:
Enter e id

this is a simple textthis is a simple text

1
Salary= 120000

RESULT:

Thus C program to set the file pointer at the beginning of the stream and to use stream
many times has been executed and verified successfully.

You might also like