ex. no: 1 odd or even date : aim: algorithm

143
WWW.VIDYARTHIPLUS.COM WWW.VIDYARTHIPLUS.COM V+ TEAM Ex. No: 1 ODD OR EVEN Date : AIM: To write a C program find whether the given number is odd or even. ALGORITHM: Step 1 : Start. Step 2 : Read num, c. Step 3 : if(num= = 0), then print it is neither odd or even otherwise go to step-4. Step 4 : else calculate c=num%2. Step 5 : if(c= =0), then print the given number is even, else print the given number is odd. Step 6 : Stop.

Upload: others

Post on 04-Feb-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 1 ODD OR EVEN

Date :

AIM:

To write a C program find whether the given number is odd or even.

ALGORITHM:

Step 1 : Start.

Step 2 : Read num, c.

Step 3 : if(num= = 0), then print it is neither odd or even

otherwise go to step-4.

Step 4 : else calculate c=num%2.

Step 5 : if(c= =0), then print the given number is even,

else print the given number is odd.

Step 6 : Stop.

Page 2: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int num,c;

printf("\n Enter the no. :");

scanf("%d",&num);

if(num= =0)

printf("it is neither odd nor even");

else{

c=num%2;

if(c= =0)

printf("\nThe given no. is even");

else

printf("\nThe given no. is odd");

}

getch();

}

Page 3: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 4: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 2 BIGGEST OF THREE NUMBERS

Date :

AIM:

To write a C program to find the biggest of three numbers.

ALGORITHM:

Step 1 : Start.

Step 2 : Read a, b, c,max.

Step 3 : if(a>b), then if(a>c), then max = a,

otherwise max = c, else go to step-4.

Step 4 :- if(b>c), then max = b, else max = c.

Step 5 : Print ‘max’ is the biggest value.

Step 6 : Stop.

Page 5: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int a,b,c,max;

printf("\nEnter the three nos\t:");

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

if(a>b)

{

if(a>c)

max = a;

else

max = c;

}

else if(b>c)

max = b;

else

max = c;

printf("%d is the biggest value",max);

getch();

}

Page 6: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 7: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 3 ARMSTRONG OR NOT

Date :

AIM:

To write a C program to find whether the given number is Armstrong or not.

ALGORITHM:

Step 1 : Start.

Step 2 : Read n,t,s,r.

Step 3 : Let t=n & s=0.

Step 4 : while(n>0), calculate r =n%10, s=s+(r*r*r) & n=n/10.

Step 5 : if(t = = s), print ‘t’ is an amstrong number , else print

‘t’ is not an amstrong number.

Step 6 : Stop.

Page 8: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int n,t,s,r;

printf("\n Enter the number");

scanf("%d",&n);

t=n;

s=0;

while(n>0)

{

r =n%10;

s =s+(r*r*r);

n =n/10;

}

if(t= =s)

printf("\n %d is an amstrong number",t);

else

printf("\n %d is not an amstrong number ",t);

getch();

}

Page 9: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 10: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 4 PERFECT OR NOT

Date :

AIM:

To write a C program to find whether the given number is perfect or not.

ALGORITHM:

Step 1 : Start.

Step 2 : Read num,temp=1,i=0,c=0,k.

Step 3 : while(temp<=num/2), k=num%temp.

Step 4 : if(k= = 0) ,c=temp+i & i=c, then temp++.

Step 5 : if(c= =num), print the given no. is a perfect no., else

print the given no. is not a perfect no.

Step 6: Stop.

Page 11: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int num,temp=1,i=0,c=0,k;

printf("Enter the no. :");

scanf("%d",&num);

while(temp<=num/2)

{

k=num%temp;

if(k= =0)

{

c=temp+i;

i=c;

}

temp++;

}

if(c= =num)

printf("The given no. is a perfect no.");

else

printf("The given no. is not a perfect no.");

getch();

}

Page 12: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 13: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 5 PALINDROME OR NOT

Date :

AIM:

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

ALGORITHM:

Step 1 : Start.

Step 2 : Read input, copy, condition, length, i.

Step 3 : To find length of string use length=strlen(input).

Step 4 : for(i=0;i<length;i++) and to change the string to upper case use

input[i]=toupper(input[i]).

Step 5 : To copy the string use strcpy(input,copy).

Step 6 : Reverse the string by strrev(input).

Step 7 : condition=strcmp(input,copy).

Step 8 : if(condition= = 0) print the given input is palindrome, else print the

given string is not palindrome.

Step 9 : Stop.

Page 14: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<ctype.h>

void main()

{

char input[50],copy[50];

int condition,length,i;

clrscr();

printf("Enter the input :");

scanf("%s",input);

length = strlen(input);

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

input[i] = toupper(input[i]);}

strcpy(copy,input);

strrev(input);

condition = strcmp(input,copy);

if(condition = = 0)

printf("\nThe given input is palindrome");

else

printf("\n The given input is not palindrome");

getch();

}

Page 15: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 16: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 6 SUM OF DIGITS

Date :

AIM:

To write a C program to print the sum of digits of a given number.

ALGORITHM:

Step 1 : Start.

Step 2 : Read num, sum=0, rem.

Step 3 : while(num>0), rem = num%10,sum =sum+rem,num=num/10

Step 4 : Print sum.

Step 5: Stop.

Page 17: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

{

long int num, rem, sum=0;

clrscr();

printf("Enter the Number: ");

scanf("%ld", &num);

while(num>0)

{

rem = num%10;

sum = sum + rem;

num = num/10;

}

printf("The Sum of digits of the Given Number is: %ld ",sum);

getch();

}

Page 18: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 19: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 7 FIBONACCI SERIES

Date :

AIM:

To write a C program to print the Fibonacci Series.

ALGORITHM:-

Step 1 : Start.

Step 2 : Read l=-1,j=1,n,k,i.

Step 3 : for(i=0,i<n,i++)

Step 4: k=l+j

Step 5 : Print sum.

Step 6 : l=j;

Step 7 : j=k;

Step 8 : Go to STEP 3.

Step 9: Stop.

Page 20: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

{

int l = -1,j = 1,i,n,k;

clrscr();

printf("Enter the number of terms: ");

scanf("%d",&n);

printf("\n\t\t\t FIBONACCI SERIES\t\t\t\n ");

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

{

k =l+j;

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

l = j;

j = k;

}

getch();

}

Page 21: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 22: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 8 PRIME NUMBER OR NOT

Date :

AIM:

To write a C program that prints the given number is prime or not.

ALGORITHM:

Step 1 : Start.

Step 2 : Read num,temp=2,k.

Step 3 : While(temp<=num/2), k= num%temp

Step 4 : if k = 0

Step 5: Print Number is Composite

Step 6: temp++ goto step 3

Step 7: Print Number is Prime

Step 8 : Stop.

Page 23: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

{

long int num, temp =2, k;

clrscr();

printf("Enter the number: ");

scanf("%ld",&num);

while(temp<=num/2)

{

k = num%temp;

if(k == 0){

printf("\n\n\n\t\t The Number Is Composite!!");

getch();

exit(0);

}

temp++;

}

printf("\n\n\n\t\tThe Number is Prime!!");

getch();

}

Page 24: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 25: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 9 SUM OF ARRAY ELEMENTS

Date :

AIM:

To write a C program to print the sum of array of a given number.

ALGORITHM:

Step 1: Start.

Step 2: Read a[5],b[5],c[5], i, j, k.

Step 3: Get values for Array a[5].

Step 4: Get values for Array b[5].

Step 5: Array c[5] is Sum of Array a[5] and b[5].

Step 6: Print Array c[5].

Step 7: Stop.

Page 26: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

{

int a[5],b[5],c[5],i,j,k;

clrscr();

printf("\n\t Enter 5 numbers in array 1:\n\t\t");

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

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

printf("\n\t Enter 5 numbers in array 2:\n\t\t");

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

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

}

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

{

c[k]=a[k]+b[k];

}

printf("\n\t Sum of the given array:\n\n" );

for(k=0;k<5;k++){

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

}

getch();

}

Page 27: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 28: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 10 CALL BY VALUE

Date :

AIM:

To write a C program that calls a function by call by value.

ALGORITHM:

Step-1 : Start.

Step -2 : Read a,b.

Step -3 : Print a,b.

Step -4 : call swap(a,b)

Step -5 : Print a,b

Step -6 : Stop

Swap(a, b)

Step -1 : Start swap(a,b).

Step -2 : temp = a, a = b, b =temp.

Step -3 : Print a,b.

Step -4 : Return.

Page 29: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

void swap(int,int);

void main(){

int a,b;

clrscr();

printf("\n\t Enter a value for A: ");

scanf("%d",&a);

printf("\n\t Enter a value for B: ");

scanf("%d",&b);

printf("\n\n The values before swap - in main:\n");

printf("\n\t Now the value for A: %d ",a);

printf("\n\t Now the value for B: %d ",b);

swap(a,b);

printf("\n\n The values after swap - in main:\n");

printf("\n\t Now the value for A: %d ",a);

printf("\n\t Now the value for B: %d ",b);

getch();

}

void swap(int a,int b){

int temp;

temp=a;

a=b;

b=temp;

printf("\n\n The values after swap-in swap function :\n");

printf("\n\t Now the value for A: %d ",a);

printf("\n\t Now the value for B: %d ",b);

}

Page 30: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

Enter a value for A: 345

Enter a value for B: 789

The values before swap - in main:

Now the value for A: 345

Now the value for B: 789

The values after swap-in swap function :

Now the value for A: 789

Now the value for B: 345

The values after swap - in main:

Now the value for A: 345

Now the value for B: 789

RESULT:

Thus the program was executed and output is verified successfully.

Page 31: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 11 CALL BY REFERENCE

Date :

AIM:

To write a C program that calls a function by call by reference.

ALGORITHM:

Step -1 : Start.

Step -2 : Read a,b.

Step -3 : Print a,b.

Step -4 : call function (&a,&b)

Step -5 : Print a,b

Step -6 : Stop

function(&a, &b)

Step -1 : Start function(*a,*b).

Step -2 : temp =* a, *a =*b,*b =temp.

Step -3 : Print a,b.

Step -4 : Return.

Page 32: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

void function(int *, int *);

void main(){

int a,b;

clrscr();

printf("\n\t Enter a value for A: ");

scanf("%d",&a);

printf("\n\t Enter a value for B: ");

scanf("%d",&b);

printf("\n\n The values before swap are:\n");

printf("\n\t Now the value for A: %d ",a);

printf("\n\t Now the value for B: %d ",b);

function(&a,&b);

printf("\n\n The values after swap are:\n");

printf("\n\t Now the value for A: %d ",a);

printf("\n\t Now the value for B: %d ",b);

getch();

}

void function(int *a,int *b){

int temp;

temp = *a;

*a=*b;

*b=temp;

return;

}

Page 33: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 34: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 12 FACTORIAL

Date :

AIM:

To write a C program to print the Factorial of a given number.

ALGORITHM:

Step -1 : Start.

Step -2 : Read num,output.

Step -3 : output =fact(num).

Step -4: Definition of fact(num)

Step -5 : Read fact =1

Step -6 : While(num>0),fact =fact*num,num--.

Step -7 : Return fact.

Step -8 : Print fact

Step -9: Stop.

Page 35: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

long int fact(long int n);

void main(){

clrscr();

long int num, output;

printf("Enter the number: ");

scanf("%ld",&num);

output = fact(num);

printf("\n\n\n\t\tThe Factorial of the number is: %ld",output);

getch();

}

long int fact(long int num){

long int fact = 1;

while(num>0){

fact = fact*num;

num--;

}

return fact;

}

Page 36: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 37: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 13 MATRIX ADDITION

Date :

AIM:

To write a C program to add two matrices.

ALGORITHM:

Step -1 : Start.

Step -2 : Declare a two dimensional array

Step -3 : Read two matrices

Step -4 : Use nested for loop

Step -5: Add two matrices

Step -6 : store the answer in array variable

Step -7 : Print result.

Step -8: Stop.

Page 38: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main ( ) {

int a[2][2],b[2][2],s[2][2];

int i,j;

clrscr ();

printf("enter first matrix: \n");

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

for ( j=1; j<=2; j++) {

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

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

}

}

printf("enter second matrix: \n");

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

for(j=1; j<=2;j++) {

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

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

}

}

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

for (j=1;j<=2;j++) {

s[i][j]= a[i][j]+b[i][j];

}

}

printf("The addition matrix is:\n");

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

for (j=1;j<=2;j++) {

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

Page 39: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

}

printf("\n");

}getch ();

}

Page 40: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 41: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 14 MATRIX MULTIPLICATION

Date :

AIM:

To write a C program to multiply two matrices.

ALGORITHM:

Step -1 : Start.

Step -2 : Declare a two dimensional array

Step -3 : Read two matrices

Step -4: Use nested for loop

Step -5: Multiply two matrices

Step -6 : store the answer in array variable

Step -7 : Print result.

Step -8: Stop.

Page 42: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main( ) {

int a[2][2], b[2][2],s[2][2];

int i,j,k;

printf("Enter first matrix:\n" );

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

for( j=1;j<=2;j++) {

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

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

}

}

printf("Enter second matrix:\n");

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

for(j=1;j<=2;j++) {

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

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

}

}

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

for(j=1;j<=2;j++) {

s[i][j]=0;

for(k=1;k<=2;k++) {

s[i][j] =s[i][j]+a[i][k]*b[k][j];

}

}

}

printf("Matrix Multiplication Is: \n");

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

Page 43: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

for (j=1;j<=2;j++){

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

}

printf("\n");

}

getch();

}

Page 44: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 45: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 15 PALINDROME

Date :

AIM:

To write a C program for palindrome checking.

ALGORITHM:

Step -1 : Start.

Step -2 : Declare a two array variables.

Step -3 : Read a string

Step -4 : Use string handling functions

Step -5: Compare the given two strings are same.

Step -6 : Display the message palindrome or not.

Step -7: Stop.

Page 46: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include <string.h>

void main(){

char a[100], b[100];

printf("Enter the string to check if it is a palindrome\n");

gets(a);

strcpy(b,a);

strrev(b);

if( strcmp(a,b) == 0 )

printf("Entered string is a palindrome.\n");

else

printf("Entered string is not a palindrome.\n");

getch();

}

Page 47: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 48: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 16 FUNCTION POINTER

Date :

AIM:

To write a C program for Function pointer.

ALGORITHM:

Step -1 : Start.

Step -2 : Declare a pointer variable as function.

Step -3 : Assigning address of function to pointer variable

Step -4 : Define a function definition as show()

Step -5: Call a function.

Step -6 : Display the message.

Step -7: Stop.

Page 49: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

int show();

void main(){

int (*fnPtr)();

clrscr();

fnPtr=show;

printf("Address of function :%u",show);

(*fnPtr)();

getch();

}

int show(){

printf("\nFunction called using pointer!");

return 0;

}

Page 50: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 51: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 17 FUNCTION WITH VARIABLE NUMBER OF ARGUMENTS

Date :

AIM:

To write a C program for Function with variable number of arguments

ALGORITHM:

Step -1 : Start.

Step -2 : Include a header file called stdarg.h

Step -3 : Define a function as fun() with variable number of arguments.

Step -4 : Call the functions related to stdarg header file.

Step -5: Passing variable number of arguments as function arguments.

Step -6: Display the result.

Step -7: Stop.

Page 52: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<stdarg.h>

void fun(char *msg, ...);

int main(){

clrscr();

fun("Sarav", 1, 4, 7, 11, 0);

getch();

return 0;

}

void fun(char *msg, ...){

va_list ptr;

int num;

va_start(ptr, msg);

num = va_arg(ptr, int);

printf("%d", num);

printf("\n");

num = va_arg(ptr, int);

printf("%d", num);

printf("\n");

num = va_arg(ptr, int);

printf("%d", num);

va_end(ptr);

}

Page 53: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 54: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 18 NESTED STRUCTURES

Date :

AIM:

To write a C program to use structures to print name and address of student.

ALGORITHM:

Step -1 : Start.

Step -2 : Read Structure address, student.

Step -3 : Read structure members name, doorno, strtname, place, city.

Step -4 : Print structure members name, doorno, strtname, place, city.

Step -5 : Stop.

Page 55: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

struct addr{

int doorno;

char strtname[15];

char place[20];

char city[20];

};

struct student{

char name[15];

struct addr address;

}s1;

void main(){

clrscr();

printf("\n\t\t Enter Student Name: ");

gets(s1.name);

printf("\n\t Enter Address: \n");

printf("\n\t\t Enter Door number : ");

gets(s1.address.doorno);

printf("\n\t\t Enter Street name : ");

gets(s1.address.strtname);

printf("\n\t\t Enter Place : ");

gets(s1.address.place);

printf("\n\t\t Enter City : ");

gets(s1.address.city);

printf("\n\n\n\t\t Student Name: ");

Page 56: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

puts(s1.name);

printf("\n\t Address: \n");

printf("\n\t\t Door number : ");

puts(s1.address.doorno);

printf("\n\t\t Street name : ");

puts(s1.address.strtname);

printf("\n\t\t Place : ");

puts(s1.address.place);

printf("\n\t\t City : ");

puts(s1.address.city);

getch();

}

Page 57: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 58: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 19 FILE HANDLING-SEQUENTIAL ACCESS

Date :

AIM:

To write a C program for file handling.

ALGORITHM:

Step -1 : Start.

Step -2 : Open a file in write mode and read mode.

Step -3 : Read a string and end with # symbol.

Step -4 : use putc() function to write the given message in file.

Step -5: use getc() function for read the given message as one by one character from

file.

Step -6: Display the message in the window read from file.

Step -7: Stop.

Page 59: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include <stdio.h>

#include <conio.h>

void main(){

char c;

FILE *fp= fopen("write.c","w");

printf("Enter contents to store in file (Enter # at end):\n");

while((c=getchar())!='#')

putc(c,fp);

fclose(fp);

fp = fopen("write.c","r");

while (c!= EOF){

putchar(c);

c = getc(fp);

}

fclose(fp);

getch();

}

Page 60: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 61: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 20 FILE HANDLING-RANDOM ACCESS (FTELL() AND REWIND())

Date :

AIM:

To write a C program random access in file handling.

ALGORITHM:

Step -1: Start.

Step -2 : Declare two character array.

Step -3 : create a file in write mode.

Step -4 : check EOF is reached or not.

Step -5: use the function for handling the file.

Step -6: Display the output.

Step -7: Stop.

Page 62: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include <stdlib.h>

#include <stdio.h>

#define BUFLEN 6

void main(){

char msg[] = "abcdefghijklmnopqrstuvwxyz";

char buf[BUFLEN];

FILE *fp;

clrscr();

if((fp = fopen("TEXT.TXT", "w")) == NULL){

fprintf(stderr, "Error opening file.");

exit(1);

}

if(fputs(msg, fp) == EOF){

fprintf(stderr, "Error writing to file.");

exit(1);

}

fclose(fp);

// Now open the file for reading.

if((fp = fopen("TEXT.TXT", "r")) == NULL){

fprintf(stderr, "Error opening file.");

exit(1);

}

printf("\nImmediately after opening, position = %ld", ftell(fp));

// Read in 5 characters.

Page 63: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

fgets(buf, BUFLEN, fp);

printf("\nAfter reading in %s, position = %ld", buf, ftell(fp));

//Read in the next 5 characters.

fgets(buf, BUFLEN, fp);

printf("\n\nThe next 5 characters are %s, and position now = %ld",buf, ftell(fp));

// Rewind the stream.

rewind(fp);

printf("\n\nAfter rewinding, the position is back at %ld",ftell(fp));

// Read in 5 characters.

fgets(buf, BUFLEN, fp);

printf("\nReading starts at the beginning again: %s\n", buf);

fclose(fp);

getch();

}

Page 64: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 65: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 21 FILE HANDLING-ERROR HANDLING

Date :

AIM:

To write a C program for error handling in files.

ALGORITHM:

Step -1 : Start.

Step -2 : Open a file in open mode..

Step -3 : Read a string

Step -4 : Use Error handling functions for handle the errors.

Step -5: Close the file stream.

Step -6: Stop.

Page 66: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include <stdio.h>

#include <errno.h>

#include <string.h>

extern int errno ;

void main (){

int errnum;

FILE * pf;

clrscr();

pf = fopen ("filesss.txt", "rb");

if (pf == NULL) {

errnum = errno;

fprintf(stderr,"Value of errno: %d\n",errno);

perror("Error printed by perror");

fprintf(stderr,"Error opening file: %s\n",strerror(errnum));

}

else {

fclose (pf);

}

getch();

}

Page 67: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

RESULT:

Thus the program was executed and output is verified successfully.

Page 68: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 22 ARRAY IMPLEMENTATION OF LIST ADT

Date:

AIM:

To implement the List ADT using arrays.

ALGORITHM:

Step 1: Start.

Step 2: Declare the necessary functions for implementation.

Step 3: Get the input from the user and store it an array.

Step 4: In Insertion, half of the elements to be shifted upwards and in deletion half of the

elements to be shifted downwards.

Step 5: Display the output using an array.

Step 6: Stop.

Page 69: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

#define MAX 10

void create();

void insert();

void deletion();

void search();

void display();

int a,b[20], n, p, e, f, i, pos;

void main()

{

clrscr();

int ch;

char g='y';

do

{

printf("\n main Menu");

printf("\n 1.Create \n 2.Delete \n 3.Search \n 4.Insert \n 5.Display\n 6.Exit \n");

printf("\n Enter your Choice");

scanf("%d", &ch);

switch(ch)

{

case 1:

create();

break;

Page 70: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

case 2:

deletion();

break;

case 3:

search();

break;

case 4:

insert();

break;

case 5:

display();

break;

case 6:

exit();

break;

default:

printf("\n Enter the correct choice:");

}

printf("\n Do u want to continue:::");

scanf("\n%c", &g);

}

while(g=='y'||g=='Y');

getch();

}

Page 71: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

void create()

{

printf("\n Enter the number of nodes");

scanf("%d", &n);

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

{

printf("\n Enter the Element:",i+1);

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

}

}

void deletion()

{

printf("\n Enter the position u want to delete::");

scanf("%d", &pos);

if(pos>=n)

{

printf("\n Invalid Location::");

}

else

{

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

{

b[i-1]=b[i];

}

n--;

}

printf("\n The Elements after deletion");

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

{

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

Page 72: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

}

}

void search()

{

printf("\n Enter the Element to be searched:");

scanf("%d", &e);

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

{

if(b[i]==e)

{

printf("Value is in the %d Position", i);

}

}

}

void insert()

{

printf("\n Enter the position u need to insert::");

scanf("%d", &pos);

if(pos>=n)

{

printf("\n invalid Location::");

}

else

{

for(i=MAX-1;i>=pos-1;i--)

{

b[i+1]=b[i];

Page 73: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

}

printf("\n Enter the element to insert::\n");

scanf("%d",&p);

b[pos]=p;

n++;

}

printf("\n The list after insertion::\n");

display();

}

void display()

{

printf("\n The Elements of The list ADT are:");

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

{

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

}

}

Page 74: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

Main Menu

1.Create

2.Delete

3.Search

4.Insert

5.Display

6.Exit

Enter your Choice: 1

Enter the number of elements: 4

Enter the elements:

10

20

30

40

Do u want to continue(y/n): y

main Menu

1.Create

2.Delete

3.Search

4.Insert

5.Display

6.Exit

Enter your Choice: 2

Enter the element to delete:20

Elements after deletion: 10

30

40

Do u want to continue(y/n): y

Page 75: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

main Menu

1.Create

2.Delete

3.Search

4.Insert

5.Display

6.Exit

Enter your Choice: 3

Enter the element to search: 100

Element not found

Do u want to continue(y/n): y

main Menu

1.Create

2.Delete

3.Search

4.Insert

5.Display

6.Exit

Enter your Choice: 4

Enter the element to insert: 15

Enter the position to insert:2

Elements after insertion:

10

15

30

40

Do u want to continue(y/n): y

Page 76: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

main Menu

1.Create

2.Delete

3.Search

4.Insert

5.Display

6.Exit

Enter your Choice: 4

RESULT:

Thus, a C program of list ADT using arrays was implemented successfully.

Page 77: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 23 SINGLY LINKED LIST

Date:

AIM:

To write a C program to implement singly linked list.

ALGORITHM:

Step 1: Start

Step 2: Creation: Get the number of elements, and create the nodes having structures

DATA, LINK and store the element in Data field, link them together to form a

linked list.

Step 3: Insertion: Get the number to be inserted and create a new node store the value in

DATA field. And insert the node in the required position.

Step 4: Deletion: Get the number to be deleted. Search the list from the beginning and locate

the node then delete the node.

Step 5: Display: Display all the nodes in the list.

Step 6: Stop.

Page 78: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define NULL 0

typedef struct list

{

int no;

struct list *next;

}LIST;

LIST *p,*t,*h,*y,*ptr,*pt;

void create( void );

void insert( void );

void delet( void );

void display ( void );

int j,pos,k=1,count;

void main()

{

int n,i=1,opt;

clrscr();

p = NULL;

printf("%d",sizeof(LIST));

printf( "Enter the no of nodes :\n " );

scanf( "%d",&n );

count = n;

while( i <= n)

{

Page 79: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

create();

i++;

}

printf("\nEnter your option:\n");

printf("1.Insert \t 2.Delete \t 3.Display \t 4.Exit\n");

do

{

scanf("%d",&opt);

switch( opt )

{

case 1:

insert();

count++;

break;

case 2:

delet();

count--;

if ( count == 0 )

{

printf("\n List is empty\n");

}

break;

case 3:

printf("List elements are:\n");

display();

break;

}

printf("\nEnter your option \n");

}while( opt != 4 );

getch();

Page 80: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

}

void create ( )

{

if( p== NULL )

{

p = ( LIST * ) malloc ( sizeof ( LIST ) );

printf( "Enter the element:\n" );

scanf( "%d",&p->no );

p->next = NULL;

h = p;

}

else

{

t= ( LIST * ) malloc (sizeof( LIST ));

printf( "\nEnter the element" );

scanf( "%d",&t->no );

t->next = NULL;

p->next = t;

p = t;

}

}

void insert()

{

t=h;

p = ( LIST * ) malloc ( sizeof(LIST) );

printf("Enter the element to be inserted:\n");

scanf("%d",&p->no);

printf("Enter the position to insert:\n");

scanf( "%d",&pos );

Page 81: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

if( pos == 1 )

{

h = p;

h->next = t;

}

else

{

for(j=1;j<(pos-1);j++)

t = t->next;

p->next = t->next;

t->next = p;

t=p;

}

}

void delet(){

printf("Enter the position to delete:\n");

scanf( "%d",&pos );

if( pos == 1 )

{

h = h->next ;

}

else

{

t= h;

for(j=1;j<(pos-1);j++)

t = t->next;

pt=t->next->next;

free(t->next);

t->next= pt;

}

Page 82: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

}

void display()

{

t= h;

while( t->next != NULL )

{

printf("\t%d",t->no);

t = t->next;

}

printf( "\t %d\t",t->no );

}

Page 83: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

Enter the no of nodes : 3

Enter the element:1

Enter the element 2

Enter the element 3

Enter your option:

1.Insert 2.Delete 3.Display 4.Exit

3

List elements are:

1 2 3

Enter your option 1

Enter the element to be inserted:

12

Enter the position to insert: 1

Enter your option 3

List elements are:

12 1 2 3

Enter your option 1

Enter the element to be inserted:

13

Enter the position to insert: 3

Enter your option 1

Enter the element to be inserted:

14

Enter the position to insert:6

Enter your option 3

List elements are:

12 1 13 2 3 14

Enter your option 2

Enter the position to delete:1

Page 84: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Enter your option 3

List elements are:

1 13 2 3 14

Enter your option 2

Enter the position to delete:3

Enter your option 3

List elements are:

1 13 3 14

Enter your option 2

Enter the position to delete:4

Enter your option 3

List elements are:

1 13 3

Enter your option: 6

RESULT:

Thus, a C program for Singly Linked List was implemented successfully.

Page 85: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 24 DOUBLY LINKED LIST

Date:

AIM:

To write a C program to implement doubly linked list with Insert, Delete and Display

operations.

ALGORITHM:

Step 1: Start

Step 2: Creation: Get the number of elements to create the list. Then create the node having the

Structure: BLINK, DATA , FLINK and store the elements in Data field. Link them

together to form a doubly linked list.

Step 3: Insertion: Get the number to be Inserted, create a new node to store the value. Search

the list and insert the node in its right position.

Step 4: Deletion: Get the number to be deleted. Search the list from the beginning and try to

locate node p with DATA. If found then delete the node.

Step 5: FLINK P’s previous node to P’s Next node. BLINK P’s Next node to P’s Previous node

else display “Data not Found”.

Step 6: Display: Display all the nodes in the list.

Step 7: Stop.

Page 86: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM :

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define NULL 0

typedef struct list

{

int no;

struct list *next;

struct list *pre;

}LIST;

LIST *p,*t,*h;

void create( void );

void insert( void );

void delet( void );

void display ( void );

int j,pos,k=1,count;

void main()

{

int n,i=1,opt;

clrscr();

p = NULL;

printf( "Enter the no of nodes :\n " );

scanf( "%d",&n );

count = n;

while( i <= n)

Page 87: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

{ create();

i++;

}

printf("\nEnter your option:\n");

printf("1.Insert \t 2.Delete \t 3.Display \t 4.Exit\n");

do

{

scanf("%d",&opt);

switch( opt ){

case 1:

insert();

count++;

break;

case 2:

delet();

count--;

if ( count == 0 )

{

printf("\n List is empty\n");

}

break;

case 3:

printf("List elements are:\n");

display();

break;

}

printf("\nEnter your option \n");

}while( opt != 4 );

getch();

Page 88: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

}

void create ( )

{

if( p == NULL )

{

p = ( LIST * ) malloc ( sizeof ( LIST ) );

printf( "Enter the element:\n" );

scanf( "%d",&p->no );

p->next = NULL;

p->pre = NULL;

h = p;

}

else

{

t= ( LIST * ) malloc (sizeof( LIST ));

printf( "\nEnter the element" );

scanf( "%d",&t->no );

t->next = NULL;

p->next = t;

t->pre = p;

p = t;

}

}

void insert()

{

t=h;

p = ( LIST * ) malloc ( sizeof(LIST) );

printf("Enter the element to be insrted:\n");

scanf("%d",&p->no);

Page 89: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

printf("Enter the position to insert:\n");

scanf( "%d",&pos );

if( pos == 1 )

{

h = p;

h->next = t;

t->pre = h;

h->pre = NULL;

}

else

{

for(j=1;j<(pos-1);j++)

t = t->next;

p->next = t->next;

t->next = p;

p->pre = t;

}

}

void delet()

{

printf("Enter the position to delete:\n");

scanf( "%d",&pos );

if( pos == 1 )

{

h = h->next ;

h->pre = NULL;

}

else

{

t= h;

Page 90: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

for(j=1;j<(pos-1);j++)

t = t->next;

t->next = t->next->next;

t->next->pre = t;

free( t->next );

}

}

void display()

{

t= h;

while( t->next != NULL )

{

printf("%d\n",t->no);

t = t->next;

}

printf( "%d",t->no );

}

Page 91: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

Enter the no of nodes: 3

Enter the element3

Enter your option:

1.Insert 2.Delete 3.Display 4.Exit

3

List elements are:

1 2 3

Enter your option 1

Enter the element to be inserted:22

Enter the position to insert:1

Enter your option 3

List elements are:

22 1 2 3

Enter your option 1

Enter the element to be inserted:

11

Enter the position to insert:5

Enter your option 3

List elements are:

22 1 2 3 11

Enter your option

2

Enter the position to delete:

1

Enter your option

3

List elements are:

1 2 3 11

Enter your option 4

Page 92: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

RESULT:

Thus a C program for Doubly Linked List was implemented successfully.

Page 93: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 25 STACK USING ARRAY

Date:

AIM:

To write a C program to implement Stack operations such as push, pop and display using

array.

ALGORITHM:

Step 1: Start.

Step 2: Initialy top = -1;

Step 3: push operation increases top by one and writes pushed element to storage[top];

Step 4: pop operation checks that top is not equal to -1 and decreases top variable by 1;

Step 5: display operation checks that top is not equal to -1 and returns storage[top];

Step 6: Stop.

Page 94: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<process.h>

#define size 5

int item;

int s[10];

int top;

void display()

{

int i;

if(top==-1)

{

printf("\nstack is empty");

return;

}

printf("\nContent of stack is:\n");

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

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

}

void push()

{

if(top==size-1)

{

printf("\nStack is full");

return;

}

printf("\nEnter item:\n");

scanf("%d",&item);

s[++top]=item;

Page 95: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

}

void pop()

{

if(top==-1)

{

printf("\nstack is empty");

return;

}

printf("\nDeleted item is: %d",s[top]);

top--;

}

void main()

{

int ch;

top=-1;

clrscr();

printf("\n1.push\t\t2.pop\n3.display\t4.exit\n");

do{

printf("\nEnter your choice:\n");

scanf("%d",&ch);

switch(ch)

{

case 1:// printf("Enter item:\n");

//scanf("%d",&item);

push();

break;

case 2: pop();

break;

case 3: display();

Page 96: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

break;

case 4: exit(0);

default: printf("\nWrong entry ! try again");

}}while(ch<=4);

getch();

}

Page 97: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

1.push 2.pop

3.display 4.exit

Enter your choice:

1

Enter item:

100

Enter your choice:

1

Enter item:

200

Enter your choice:

1

Enter item:

300

Enter your choice:

2

Deleted item is: 300

Enter your choice:

3

Content of stack is:

100 200

Enter your choice:4

RESULT:

Thus a C program for Stack using array was implemented successfully.

Page 98: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 26 STACK USING LINKED LIST

Date:

AIM:

To write a C program to implement Stack operations such as push, pop and display using

linked list.

ALGORITHM:

Step 1: Start.

Step 2: push operation inserts an element at the front.

Step 4: pop operation deletes an element at the front of the list;

Step 5: display operation displays all the elements in the list.

Step 6: Stop.

Page 99: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include "stdio.h"

#include "stdlib.h"

#include "conio.h"

void pop();

void push(int value);

void display();

struct node

{

int data;

struct node *link;

};

struct node *top=NULL,*temp;

void main()

{

int choice,data;

while(1) //infinite loop is used to insert/delete infinite number of elements in stack

{

printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n");

printf("\nEnter ur choice:");

scanf("%d",&choice);

switch(choice)

{

case 1: //To push a new element into stack

printf("Enter a new element :");

scanf("%d",&data);

push(data);

Page 100: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

break;

case 2: // pop the element from stack

pop();

break;

case 3: // Display the stack elements

display();

break;

case 4: // To exit

exit(0);

}

}

getch();

//return 0;

}

void display()

{

temp=top;

if(temp==NULL)

{

printf("\nStack is empty\n");

}

printf("\n The Contents of the Stack are...");

while(temp!=NULL)

{

printf(" %d ->",temp->data);

temp=temp->link;

}

Page 101: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

}

void push(int data)

{

temp=(struct node *)malloc(sizeof(struct node)); // creating a space for the new element.

temp->data=data;

temp->link=top;

top=temp;

display();

}

void pop()

{

if(top!=NULL)

{

printf("The poped element is %d",top->data);

top=top->link;

}

else

{

printf("\nStack Underflow");

}

display();

}

Page 102: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

1.Push

2.Pop

3.Display

4.Exit

Enter ur choice:1

Enter a new element :10

The Contents of the Stack are... 10 ->

1.Push

2.Pop

3.Display

4.Exit

Enter ur choice:1

Enter a new element :20

The Contents of the Stack are... 20 -> 10 ->

1.Push

2.Pop

3.Display

4.Exit

Enter ur choice:1

Enter a new element :30

The Contents of the Stack are... 30 -> 20 -> 10 ->

1.Push

2.Pop

3.Display

4.Exit

Enter ur choice:2

The poped element is 30

The Contents of the Stack are... 20 -> 10 ->

Page 103: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

1.Push

2.Pop

3.Display

4.Exit

Enter ur choice:3

The Contents of the Stack are... 20 -> 10 ->

1.Push

2.Pop

3.Display

4.Exit

Enter ur choice:4

RESULT:

Thus a C program for Stack using linked list was implemented successfully.

Page 104: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 27 QUEUE USING ARRAY

Date:

AIM:

To write a C program to implement Queue operations such as enqueue, dequeue and

display using array.

ALGORITHM:

Step 1: Start.

Step 2: Initialize front=0; rear=-1.

Step 3: Enqueue operation moves a rear by one position and inserts a element at the rear.

Step 4: Dequeue operation deletes a element at the front of the list and moves the front by one

position

Step 5: Display operation displays all the element in the list.

Step 6: Stop.

Page 105: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

#define SIZE 5 /* Size of Queue */

int Q[SIZE],f=0,r=-1; /* Global declarations */

Qinsert(int elem)

{ /* Function for Insert operation */

if( Qfull())

printf("\n\n Overflow!!!!\n\n");

else

{

++r;

Q[r]=elem;

}

}

int Qdelete()

{ /* Function for Delete operation */

int elem;

if(Qempty()){ printf("\n\nUnderflow!!!!\n\n");

return(-1); }

else

{

elem=Q[f];

f=f+1;

return(elem);

}

}

Page 106: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

int Qfull()

{ /* Function to Check Queue Full */

if(r==SIZE-1) return 1;

return 0;

}

int Qempty()

{ /* Function to Check Queue Empty */

if(f > r) return 1;

return 0;

}

display()

{ /* Function to display status of Queue */

int i;

if(Qempty()) printf(" \n Empty Queue\n");

else

{

printf("Front->");

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

printf("%d ",Q[i]);

printf("<-Rear");

}

}

void main()

{ /* Main Program */

int opn,elem;

do

{

clrscr();

Page 107: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

printf("\n ### Queue Operations using Arrays### \n\n");

printf("\n Press 1-Insert, 2-Delete,3-Display,4-Exit\n");

printf("\n Your option ? ");

scanf("%d",&opn);

switch(opn)

{

case 1: printf("\n\nRead the element to be Inserted ?");

scanf("%d",&elem);

Qinsert(elem); break;

case 2: elem=Qdelete();

if( elem != -1)

printf("\n\nDeleted Element is %d \n",elem);

break;

case 3: printf("\n\nStatus of Queue\n\n");

display(); break;

case 4: printf("\n\n Terminating \n\n"); break;

default: printf("\n\nInvalid Option !!! Try Again !! \n\n");

break;

}

printf("\n\n\n\n Press a Key to Continue . . . ");

getch();

}while(opn != 4);

getch();

}

Page 108: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

### Queue Operations using Arrays###

Press 1-Insert, 2-Delete,3-Display,4-Exit

Your option ? 1

Read the element to be Inserted ?100

Press a Key to Continue . . .

### Queue Operations using Arrays###

Press 1-Insert, 2-Delete,3-Display,4-Exit

Your option ? 1

Read the element to be Inserted ?200

Press a Key to Continue . . .

### Queue Operations using Arrays###

Press 1-Insert, 2-Delete,3-Display,4-Exit

Your option ? 1

Read the element to be Inserted ?300

Press a Key to Continue . . .

### Queue Operations using Arrays###

Press 1-Insert, 2-Delete,3-Display,4-Exit

Your option ? 2

Deleted Element is 100

Press a Key to Continue . . .

### Queue Operations using Arrays###

Press 1-Insert, 2-Delete,3-Display,4-Exit

Your option ? 3

Status of Queue

Front->200 300 <-Rear

Press a Key to Continue . . .

RESULT:

Thus a C program for Queue using array was implemented successfully.

Page 109: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 28 QUEUE USING LINKED LIST

Date:

AIM:

To write a C program to implement Queue operations such as enqueue, dequeue and

display using linked list.

ALGORITHM:

Step 1: Start.

Step 2: enqueue operation inserts an element at the rear of the list.

Step 4: dequeue operation deletes an element at the front of the list.

Step 5: display operation display all the element in the list.

Step 6: Stop.

Page 110: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

struct node

{

int info;

struct node *link;

}*front = NULL, *rear = NULL;

void insert();

void delet();

void display();

int item;

void main()

{

int ch;

do

{

printf("\n\n1.\tEnqueue\n2.\tDequeue\n3.\tDisplay\n4.\tExit\n");

printf("\nEnter your choice: ");

scanf("%d", &ch);

switch(ch)

{

case 1:

insert();

break;

case 2:

delet();

break;

case 3:

Page 111: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

display();

break;

case 4:

exit(0);

default:

printf("\n\nInvalid choice. Please try again...\n");

}

} while(1);

getch();

}

void insert()

{

printf("\n\nEnter ITEM: ");

scanf("%d", &item);

if(rear == NULL)

{

rear = (struct node *)malloc(sizeof(struct node));

rear->info = item;

rear->link = NULL;

front = rear;

}

else

{

rear->link = (struct node *)malloc(sizeof(struct node));

rear = rear->link;

rear->info = item;

rear->link = NULL;

}

}

Page 112: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

void delet()

{

struct node *ptr;

if(front == NULL)

printf("\n\nQueue is empty.\n");

else

{

ptr = front;

item = front->info;

front = front->link;

free(ptr);

printf("\nItem deleted: %d\n", item);

if(front == NULL)

rear = NULL;

}

}

void display()

{

struct node *ptr = front;

if(rear == NULL)

printf("\n\nQueue is empty.\n");

else

{

printf("\n\n");

while(ptr != NULL)

{

printf("%d\t",ptr->info);

ptr = ptr->link;

}

}}

Page 113: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

1. Enqueue

2. Dequeue

3. Display

4. Exit

Enter your choice: 1

Enter ITEM: 12

1. Enqueue

2. Dequeue

3. Display

4. Exit

Enter your choice: 1

Enter ITEM: 15

1. Enqueue

2. Dequeue

3. Display

4. Exit

Enter your choice: 1

Enter ITEM: 20

1. Enqueue

2. Dequeue

3. Display

4. Exit

Enter your choice: 2

Item deleted: 12

1. Enqueue

2. Dequeue

3. Display

4. Exit

Page 114: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Enter your choice:3

15 20

1. Enqueue

2. Dequeue

3. Display

4. Exit

Enter your choice:4

RESULT:

Thus a C program for Queue using linked list was implemented successfully.

Page 115: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 29 INFIX TO POSTFIX EXPRESSION USING STACK

Date:

AIM:

To write a C program to implement the convertion of infix to postfix expression using

Stack.

ALGORITHM:

Step 1: Start.

Step 2: Create a stack to store operand and operator.

Step 3: In Postfix notation the operator follows the two operands and in the infix notation the

operator is in between the two operands.

Step 4: Consider the sum of A and B. Apply the operator “+” to the operands A and B and write

the sum as A+B is INFIX. + AB is PREFIX. AB+ is POSTFIX

Step 5: Get an Infix Expression as input and evaluate it by first converting it to postfix and then

evaluating the postfix expression.

Step 6: The expressions with in innermost parenthesis must first be converted to postfix so that

they can be treated as single operands. In this way Parentheses can be successively

eliminated until the entire expression is converted.

Step 7: The last pair of parentheses to be opened with in a group of parentheses encloses the first

expression with in that group to be transformed. This last-in first-out immediately

suggests the use of Stack. Precedence plays an important role in the transforming infix to

postfix.

Step 8: Stop.

Page 116: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<string.h>

#define MAX 20

int top=-1;

char pop();

char stack[MAX];

void push(char item);

int prcd(char symbol){

switch(symbol){

case '+':

case '-':return 2;

break;

case '*':

case '/':return 4;

break;

case '^':

case '$':return 6;

break;

case '(':

case ')':

case '#':return 1;

break;

}

}

Page 117: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

int isoperator(char symbol){

switch(symbol){

case '+':

case '-':

case '*':

case '/':

case '^':

case '$':

case '(':

case ')':return 1;

break;

default:

return 0;

}

}

void convertip(char infix[],char postfix[]){

int i,symbol,j=0;

stack[++top]='#';

for(i=0;i<strlen(infix);i++){

symbol=infix[i];

if(isoperator(symbol)==0){

postfix[j]=symbol;

j++;

}

else{

if(symbol=='(')

push(symbol);

else if(symbol==')'){

while(stack[top]!='('){

postfix[j]=pop();

j++;

Page 118: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

}

pop();//pop out (.

}

else{

if(prcd(symbol)>prcd(stack[top]))

push(symbol);

else{

while(prcd(symbol)<=prcd(stack[top])){

postfix[j]=pop();

j++;

}

push(symbol);

}//end of else.

}//end of else.

}//end of else.

}//end of for.

while(stack[top]!='#'){

postfix[j]=pop();

j++;

}

postfix[j]='\0';//null terminate string.

}

void main(){

char infix[20],postfix[20];

clrscr();

printf("Enter the valid infix string:\n");

gets(infix);

convertip(infix,postfix);

printf("The corresponding postfix string is:\n");

puts(postfix);

Page 119: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

getch();

}

void push(char item){

top++;

stack[top]=item;

}

char pop(){

char a;

a=stack[top];

top--;

return a;

}

Page 120: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

Enter the valid infix string:

(a+b)*c

The corresponding postfix string is:

ab+c*

RESULT:

Thus a C program to implement the conversion of infix to postfix expression using Stack.

Page 121: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 30 EVALUATION OF POSTFIX EXPRESSION USING STACK

Date:

AIM:

To write a C program to implement the evaluation of postfix expression using Stack.

ALGORITHM:

Step-1: Start.

Step 2: Scan the Postfix string from left to right.

Step 3: Initialize an empty stack.

Step 4: If the scanned character is an operand, add it to the stack. If the scanned character is an

operator, there will be atleast two operands in the stack.

Step 5:If the scanned character is an Operator, then we store the top most element of the

stack(topStack) in a variable temp. Pop the stack. Now evaluate topStack(Operator)temp.

Let the result of this operation be retVal. Pop the stack and Push retVal into the stack.

Repeat this step till all the characters are scanned.

Step 6: After all characters are scanned, we will have only one element in the stack. Return

topStack.

Step 7: Stop.

Page 122: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

struct stack{

int top;

float a[50];

}s;

void main(){

char pf[50];

float d1,d2,d3;

int i;

clrscr();

s.top=-1;

printf("\n\n Enter the postfix expression:");

gets(pf);

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

switch(pf[i]){

case '0':

case '1':

case '2':

case '3':

case '4':

case '5':

case '6':

case '7':

case '8':

case '9':

s.a[++s.top]=pf[i]-'0';

Page 123: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

break;

case '+':

d1=s.a[s.top--];

d2=s.a[s.top--];

s.a[++s.top]=d1+d2;

break;

case '-':

d2=s.a[s.top--];

d1=s.a[s.top--];

s.a[++s.top]=d1-d2;

break;

case '*':

d2=s.a[s.top--];

d1=s.a[s.top--];

s.a[++s.top]=d1*d2;

break;

case '/':

d2=s.a[s.top--];

d1=s.a[s.top--];

s.a[++s.top]=d1/d2;

break;

}

}

printf("\n\n The value of expression is%f",s.a[s.top]);

getch();

}

Page 124: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

Enter the postfix expression: 56*7-

The value of expression is 23.000000

RESULT:

Thus a C program to implement the evaluation of postfix expression using Stack was

implemented successfully.

Page 125: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 31 LINEAR SEARCH

Date:

AIM:

To write a C program to implement the concept of linear search.

ALGORITHM:

Step 1: Start with the first item in the list.

Step 2: Compare the current item to the target

Step 3: If the current value matches the target then we declare victory and stop.

Step 4: If the current value is less than the target then set the current item to be the next item and

repeat from 2.

Step 5: Stop.

Page 126: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main() {

int arr[20];

int i,size,sech;

printf("\n\t-- Linear Search --\n\n");

printf("Enter total no. of elements : ");

scanf("%d",&size);

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

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

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

}

printf("Enter the element to be searched: ");

scanf("%d",&sech);

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

if(sech==arr[i]) {

printf("Element exits in the list at position : %d",i+1);

break;

}

}

getch();

}

Page 127: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

-- Linear Search --

Enter total no. of elements : 5

Enter 1 element : 10

Enter 2 element : 4

Enter 3 element : 2

Enter 4 element : 17

Enter 5 element : 100

Enter the element to be searched: 17

Element exits in the list at position : 4

RESULT:

Thus a C program for the concept of linear search was implemented successfully.

Page 128: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 32 BINARY SEARCH

Date:

AIM:

To write a C program to implement the concept of binary search.

ALGORITHM:

Step 1: Set the list to be the whole list

Step 2: Find the middle value of the list

Step 3: If the middle value is equal to the target then we declare victory and stop.

Step 4: If the middle item is less than the target, then we set the new list to be the upper half of

the old list and we repeat from step 2 using the new list.

Step 5: If the middle value is greater than the target, then we set the new list to be the bottom half

of the list, and we repeat from step 2 with the new list.

Step 6: Stop.

Page 129: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main(){

int n,i,search,f=0,low,high,mid,a[20];

clrscr();

printf("Enter the n value:");

scanf("%d",&n);

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

printf("Enter the number in ascending order a[%d]=",i);

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

}

printf("Enter the search element:");

scanf("%d",&search);

low=1;

high=n;

while(low<=high){

mid=(low+high)/2;

if(search<a[mid]){

high=mid-1;

}

else if(search>a[mid]){

low=mid+1;

}

else{

f=1;

printf("obtained in the position %d:",mid);

getch();

exit();

}

Page 130: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

}

if(f==0)

printf("not present");

getch();

}

Page 131: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

Enter the n value: 5

Enter the number in ascending order a[1]=10

Enter the number in ascending order a[2]=8

Enter the number in ascending order a[3]=9

Enter the number in ascending order a[4]=24

Enter the number in ascending order a[5]=1

Enter the search element:9

obtained in the position 3:

RESULT:

Thus a C program for the concept of binary search was implemented successfully.

Page 132: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 33 BUBBLE SORT

Date:

AIM:

To write a C program to implement the concept of bubble sort.

ALGORITHM:

Step 1: Start.

Step 2: Repeat Steps 3 and 4 for i=1 to 10

Step 3: Set j=1

Step 4: Repeat while j<=n

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

Then interchange a[i] and a[j]

[End of if]

(B) Set j = j+1

[End of Inner Loop]

[End of Step 1 Outer Loop]

Step 5: Stop.

Page 133: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main(){

int n, i, j, temp , a[100];

printf("Enter the total integers you want to enter (make it less than 100):\n");

scanf("%d",&n);

printf("Enter the %d integer array elements:\n",n);

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

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

}

for(i=0;i<n-1;i++){

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

if(a[j+1]<a[j]){

temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;

}

}

}

printf("The sorted numbers are:");

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

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

}

getch();

}

Page 134: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

Enter the total integers you want to enter (make it less than 100):

5

Enter the 5 integer array elements:

99

87

100

54

150

The sorted numbers are: 54 87 99 100 150

RESULT:

Thus a C program for the concept of bubble sort was implemented successfully.

Page 135: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 34 MERGE SORT

Date:

AIM:

To write a C program to implement the concept of merge sort.

ALGORITHM:

Step 1: Start.

Step 2: First you divide the number of elements by 2 and seperate them as two.

Step 3: Divide those two which are divided by 2.

Step 4: Divide them until you get a single element.

Step 5: Start comparing the starting two pair of elements with each other and place them in

ascending order.

Step 6: When you combine them compare them so that you make sure they are sorted.

Step 7: When all the elements are compared the array will be surely sorted in an ascending order.

Step 8: Stop.

Page 136: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

void merge(int [],int ,int ,int );

void part(int [],int ,int );

void main(){

int arr[30];

int i,size;

printf("\n\t------- Merge sorting method -------\n\n");

printf("Enter total no. of elements : ");

scanf("%d",&size);

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

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

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

}

part(arr,0,size-1);

printf("\n\t------- Merge sorted elements -------\n\n");

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

printf("%d ",arr[i]);

getch();

}

void part(int arr[],int min,int max){

int mid;

if(min<max){

mid=(min+max)/2;

part(arr,min,mid);

part(arr,mid+1,max);

merge(arr,min,mid,max); } }

Page 137: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

void merge(int arr[],int min,int mid,int max){

int tmp[30];

int i,j,k,m;

j=min;

m=mid+1;

for(i=min; j<=mid && m<=max ; i++){

if(arr[j]<=arr[m]){

tmp[i]=arr[j];

j++;

}

else{

tmp[i]=arr[m];

m++;

}

}

if(j>mid){

for(k=m; k<=max; k++){

tmp[i]=arr[k];

i++;

}

}

else{

for(k=j; k<=mid; k++){

tmp[i]=arr[k];

i++;

}

}

for(k=min; k<=max; k++)

arr[k]=tmp[k]; }

Page 138: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

------- Merge sorting method -------

Enter total no. of elements : 6

Enter 1 element : 100

Enter 2 element : 99

Enter 3 element : 75

Enter 4 element : 155

Enter 5 element : 11

Enter 6 element : 43

------- Merge sorted elements -------

11 43 75 99 100 155

RESULT:

Thus a C program for the concept of merge sort was implemented successfully.

Page 139: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

Ex. No: 35 QUICK SORT

Date:

AIM:

To write a C program to implement the concept of Quick sort.

ALGORITHM:

Step 1: Start.

Step 2: Choose any element of the array to be the pivot.

Step 3: Divide all other elements (except the pivot) into two partitions.

o All elements less than the pivot must be in the first partition.

o All elements greater than the pivot must be in the second partition.

Step 4: Use recursion to sort both partitions.

Step 5: Join the first sorted partition, the pivot, and the second sorted partition.

Step 6: Stop.

Page 140: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

PROGRAM:

#include<stdio.h>

#include<conio.h>

void qsort(int arr[20], int fst, int last);

void main(){

int arr[30];

int i,size;

printf("Enter total no. of the elements : ");

scanf("%d",&size);

printf("Enter total %d elements : \n",size);

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

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

qsort(arr,0,size-1);

printf("Quick sorted elements are as : \n");

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

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

getch();

}

void qsort(int arr[20], int fst, int last){

int i,j,pivot,tmp;

if(fst<last){

pivot=fst;

i=fst;

j=last;

while(i<j){

while(arr[i]<=arr[pivot] && i<last)

i++;

while(arr[j]>arr[pivot])

j--;

Page 141: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

if(i<j){

tmp=arr[i];

arr[i]=arr[j];

arr[j]=tmp;

}

}

tmp=arr[pivot];

arr[pivot]=arr[j];

arr[j]=tmp;

qsort(arr,fst,j-1);

qsort(arr,j+1,last);

} }

Page 142: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM

OUTPUT:

Enter total no. of the elements: 5

Enter total 5 elements:

11

97

54

10

100

Quick sorted elements are as :

10 11 54 97 100

RESULT:

Thus a C program for the concept of Quick sort was implemented successfully.

Page 143: Ex. No: 1 ODD OR EVEN Date : AIM: ALGORITHM

WWW.VIDYARTHIPLUS.COM

WWW.VIDYARTHIPLUS.COM V+ TEAM