0% found this document useful (0 votes)
148 views100 pages

Anchal (Ds Lab Manual)

The document contains a list of 41 programming problems/exercises related to C language. Some of the problems include writing programs to find the factorial of a number using functions and recursion, find the largest element in an array, print the Fibonacci series using recursion, add two complex numbers, add and multiply matrices, and implement stack operations. The problems cover basic concepts like arrays, strings, functions, pointers, structures, and algorithms like searching and sorting.

Uploaded by

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

Anchal (Ds Lab Manual)

The document contains a list of 41 programming problems/exercises related to C language. Some of the problems include writing programs to find the factorial of a number using functions and recursion, find the largest element in an array, print the Fibonacci series using recursion, add two complex numbers, add and multiply matrices, and implement stack operations. The problems cover basic concepts like arrays, strings, functions, pointers, structures, and algorithms like searching and sorting.

Uploaded by

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

1

   Program Page num.


Serial
num.
1 Write a program to print the factorial of a given number using
function.

2 Write a program to print the factorial of a given number using


recursion.
3 Write a program to find the largest among array elements.

4 Write a program to print the Fibonacci series using recursion. [0, 1,


1, 2, 3, 5, 8….up to n]
5 Write a program to find out the length of a string without using any
library function.
6 Write a program that will display the string in reverse order using
pointer
7 Write a program that will read the records of employees.

8 Write a program to add two complex numbers.

9 Write a program to find out the addition of two matrices.

10 Write a program to find out the multiplication of two matrices.

11 Write a program to transpose the matrix

12 Write a program to insert an element in an array at the specified


position.
13 Write a program to delete the specified positional element from an
array.
14 Write a program for merging of two arrays.

15 Write a program that will create the array dynamically and display
the array elements in reverse

order.
16 Write a program to implement the basic operations of STACK

17 Write a program to reverse the string using STACK.

18 Write a program to convert decimal number into binary using


STACK
19 Write a program to check the whether the parentheses are
balanced or not in a given expression
using STACK
2

20 Write a program to convert an infix expression into postfix.

21 Write a program to convert an infix expression into prefix.

22 Write a program to evaluate a postfix expression.

23 Write a program to evaluate a prefix expression.

24 Write a program to print the Fibonacci series using function

25 Balanced using STACK

26 Infix to postfix

27 Postfix evaluation

28 Write a program to make structure of student type using insertion


short
29 Write a program to find the day of the week using switch case
function.
30 Call by value

31 Call by reference

32 Initialisation of 1D array

33 Write a program that will perform all operations for 1D array


(MENUDRIVEN PROGRAM FOR 1D ARRAY).
34 MENUDRIVEN PROGRAM FOR 1D ARRAY).

35 Initialisation of 2D array

36 Write a program to find out the subtraction of two matrices.

37 Write a program for bubble short

38 Write a program for selection short

39 Write a program for insertion short

40 Write a program for linear search

41 Write a program for binary search


3

C language based program

1.Write a program to print the factorial of a given no. using function.

#include<stdio.h>

#include<math.h>

int main()

printf("enter a number to find factorial: ");

printf("\nfactorial of a given number is: %d ",fact());

int fact()

int i,fact = 1,n;

scanf("%d",&n);

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

fact = fact*i;

return fact;

}
4

2.Write a program to print the factorial of a given no. using recursion.

#include<stdio.h>

long int multiplyNumbers(int n);

int main()

int n;

printf("Enter a positive integer: ");

scanf("%d",&n);

printf("Factorial of %d = %ld", n, multiplyNumbers(n));

return 0;

long int multiplyNumbers(int n) {

if (n>=1)

return n*multiplyNumbers(n-1);

else

return 1;

3.Write a program to find the largest among array elements.

#include <stdio.h>

int main()
5

int size, i, largest;

printf("\n Enter the size of the array: ");

scanf("%d", &size);

int array[size];

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

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

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

largest = array[0];

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

if (largest < array[i])

largest = array[i];

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

return 0;

4.Write a program to print the fabonacci series using recursion.


6

#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);//printing 0 and 1
for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
printf(" %
}
return 0;
}

5.Write a program to find out the length of a string without using library function.

#include <stdio.h>
#include <string.h>
void main()
{
char str1[50];
int i, l = 0;
printf("\n\nFind the length of a string:\n ");
printf("-------------------------------------\n");
printf("Input a string : ");
scanf("%s", str1);
for (i = 0; str1[i] != '\0'; i++)
{
l++;
7

}
printf("The string contains %d number of characters. \n",l);
printf("So, the length of the string %s is : %d\n\n", str1, l);
}

6.Write a program that will display the string in reverse order using pointer.

##include <stdio.h>

#include <stdlib.h>

#include<string.h>

int main()

char str1[100],str2[100];

char *p1,*p2;

printf("Enter a String\n");

gets(str1);

p1=str1+strlen(str1)-1;

p2=str2;

while(p1>=str1)

{ *p2=*p1;

p2++;
8

p1--;

}*p2='\0';

printf("Original String: %s\n",str1);

printf("Reverse String: %s",str2);

return 0;

7.Write a program that will read the records of employees. The data members of a record are
as

follows:

Name as array of characters, Id as integer, salary as float

Read the record of employees and display them. Also display the total salary and

average salary as

summary information.

#include<stdio.h>

struct employee

char name[20];

int id;

int salary;

};
9

int main ()

struct employee s1,s2,s3,total,average;

int dummy;

printf("Enter the name, ID, and salary of employee 1 ");

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

scanf("%c",&dummy);

printf("Enter the name, ID, and salary of employee 2");

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

scanf("%c",&dummy);

printf(“Enter the name, ID, and salary of employee 3 “);

scanf(“%s %d %d”,s3.name,&s3.id,&s3.salary);

scanf(“%c”,&dummy);

printf(“Printing the details….\n”);

printf(“%s %d %d\n”,s1.name,s1.id,s1.salary);

printf(“%s %d %d\n”,s2.name,s2.id,s2.salary);

printf(“%s %d %d\n”,s3.name,s3.id,s3.salary);

total.salary= s1.salary+s2.salary+s3.salary;

printf(“Total salary of employees= %d\n”,total.salary);

average.salary= (total.salary/3);

printf(“Average salary of employees= %d”,average.salary);

}
10

8.Write a program to add two complex numbers.

include<stdio.h>

struct complex read(struct complex);

struct complex add(struct complex,struct complex);

void display(struct complex);

struct complex

float real;

float imag;

};

int main()

struct complex a1,a2,a3;

a1=read(a1);

a2=read(a2);

a3=add(a1,a2);

printf("\nsum=");

display(a3);

//read function body

struct complex read(struct complex a1)

fflush(stdin);

printf("\nenter real part:\t");

scanf("%f",&a1.real);

printf("\nenter imaginary part:\t");

scanf("%f",&a1.imag);

return a1;
11

void display(struct complex a)

printf("%f+i%f",a.real,a.imag);

struct complex add(struct complex a, struct complex b)

struct complex temp;

temp.real=a.real+b.real;

temp.imag=a.imag+b.imag;

return temp;

9.write a program to find addition of two matrix

#Include <stdio.h>

int main()

int r, c, i, j;

printf("Enter the number of rows\t");

scanf("%d", &r);

printf("Enter the number of columns\t");


12

scanf("%d", &c);

int a[r][c], b[r][c], sum[r][c];

printf("\nEnter elements of 1st array:\n");

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

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

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

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

printf("1st array elements:\n");

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

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

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

if(j==1){

printf("\n");

printf("Enter elements of 2nd array:\n");

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

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

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

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

printf("2nd array elements:\n");

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

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

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

if(j==1){

printf(“/n”);
13

// adding two arrays

printf("sum of array:\n");

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

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

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

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

if(j==1){

printf("\n");

}
14

10. Write a program to find out the multiplication of two matrices

#include <stdio.h>
15

int main()

int m, n, p, q, c, d, k, sum = 0;

int first[10][10], second[10][10], multiply[10][10];

printf("Enter the number of rows and columns of first matrix\n");

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

printf("Enter the elements of first matrix\n");

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

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

scanf("%d", &first[c][d]);

printf("Enter the number of rows and columns of second matrix\n");

scanf("%d%d", &p, &q);

if ( n != p )

printf("Matrices with entered orders can't be multiplied with each other.\n");

else

printf("Enter the elements of second matrix\n");

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

for ( d = 0 ; d < q ; d++ )

scanf("%d", &second[c][d]);

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

for ( d = 0 ; d < q ; d++ )

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

sum = sum + first[c][k]*second[k][d];

multiply[c][d] = sum;
16

sum = 0;

printf("Product of entered matrices:-\n");

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

for ( d = 0 ; d < q ; d++ )

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

printf("\n");

return 0;

}
17

11. Write a program to transpose the matrix.

#include <stdio.h>

int main()

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

printf("Enter rows and columns: ");

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


18

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

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

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

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

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

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

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

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

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

if (j == c - 1)

printf("\n");

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

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

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

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

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

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

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

if (j == r - 1)

printf("\n");

return 0;
19

12. Write a program to insert an element in an array at the specified position.

#include<stdio.h>

int main()

int a[100],i,n,number,pos;

printf("\nEnter no of elements\n");

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

printf("Enter the elements\n");

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

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

printf("Elements of array are\n");

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

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

printf("Enter the number which you want to insert\n");

scanf("%d",&number);

printf("Enter the position where you want to insert the number\n");

scanf("%d",&pos);

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

a[i+1]=a[i];

n=n+1;

a[pos]=number;

printf("\nOn inserting new array we get is\n");

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

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

}
21
22

13.WAP to delete the specified positional element from an array

include <stdio.h>

int main()

int c,array[c], position,n;

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

scanf("%d", &n);

printf("Enter elements\n", c);

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

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

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

printf("a[%d]=%d\n",c,array[c]);

printf("Enter the location where you wish to delete element\n");

scanf("%d", &position);

if (position >= n+1)

printf("Deletion not possible.\n");

else

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

array[c] = array[c+1];

printf("Resultant array:\n");

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

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

return 0;
23

14.Write a program for merging of two arrays.

#include<conio.h>
24

#include<stdio.h>

int main()

int i,j,a[j],b[j],c[2*j];

printf("size of array:\t",j);

scanf("%d",&j);

printf("Enter Elements in 1st Array: \n");

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

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

printf("Enter Elements in 2nd Array: \n");

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

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

printf("\nElements of Array After Merge: ");

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

c[i]=a[i];

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

for(i=0;i<2*j;i++)

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

getch();

}
25

15.Write a program that will create the array dynamically and display the array elements in reverse
order

#include<stdio.h>

#include<stdlib.h>

int main()

int *p,n,i;

printf("How many numbers you want to enter: ");

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

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

printf("\nEnter %d Numbers:\n\n",n);

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

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

printf("\nArray in Reverse Order: \n\n");

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

printf(" %d",*(p+i));

return 0;

}
27

Stack based program

16. Write a program to implement the basic operations of STACK

#include<stdio.h>

#include<conio.h>

int stack[100],choice,n,top,x,i;

void push();

void pop();

void display();

int main()

//clrscr();

top=-1;

printf("\n Enter the size of stack(max=100]:");

scanf("%d",&n);

printf("\n\tstack operators using array");

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

printf("\n\t 1.push\n\t 2.pop\n\t 3.display\n\t 4.exit");

do

printf("enter the choice:\n");

scanf("%d",&choice);

switch(choice)

case 1:

push();
28

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

printf("\n\texit point");

break;

default:

printf("\n\tplease enter a valid choicer (1/2/3/4)");

}while(choice!=4);

return(0);

void push()

if(top>=n-1)

{
29

printf("\n\tstack is over flow");

else

printf("enter the value to be pushed:");

scanf("%d",&x);

top++;

stack[top]=x;

void pop()

if(top<=-1)

printf("stack is under flow");

else

printf("\n\The poped elements is %d",stack[top]);

top--;

void display()

if(top>=0)

printf("\nthe elements in stack\n");

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

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

printf("\n press next choice");

else{

printf("\n the stack is empty");

}
31
32

17. Write a program to reverse the string using STACK.

#include<stdio.h>

#include<stdlib.h>

int main()

int *p,n,i;

printf("How many numbers you want to enter: ");

scanf("%d",&n);

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

printf("\nEnter %d Numbers:\n\n",n);

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

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

printf("\nArray in Reverse Order: \n\n");

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

printf(" %d",*(p+i));

return 0;

}
33

18. Write a program to convert decimal number into binary using STACK

include<stdio.h>

#include<stdlib.h>

#define MAX 50

int isEmpty(int top, int stack_arr[]);


34

void push(int x, int *top, int stack_arr[]);

int pop(int *top, int stack_arr[]);

void DecToBin(int num);

int main()

int num;

printf("Enter an integer : ");

scanf("%d",&num);

printf("Binary Equivalent is : ");

DecToBin(num);

return 0;

void DecToBin(int num)

int stack[MAX], top=-1, rem;

while(num!=0)

rem = num%2;

push(rem, &top, stack);

num/=2;

while(top!=-1)

printf("%d", pop(&top, stack));

printf("\n");

void push(int x, int *top, int stack_arr[])

if(*top == (MAX-1))

printf("Stack Overflow\n");
35

else

*top=*top+1;

stack_arr[*top] = x;

int pop(int *top, int stack_arr[])

int x;

if(*top == -1)

printf("Stack Underflow\n");

exit(1);

else

x = stack_arr[*top];

*top=*top-1;

return x;

}
36

19. Write a program to check the whether the parentheses are balanced or not in a given

Expression using STACK.

#include<stdio.h>

int main()

char expression[50];

int x=0, i=0;

printf("\nEnter an expression");

scanf("%s", expression);

while(expression[i]!= '\0')

if(expression[i]=='(')

x++;

}
37

else if(expression[i]==')')

if(x<0)

break;

i++;

if(x==0)

printf("Expression is balanced");

else

printf("Expression is unbalanced");

return 0;

}
38

20. Write a program to convert an infix expression into postfix

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct stack

int size;

int top;

char *arr;

};

int stackTop(struct stack* sp){

return sp->arr[sp->top];

int isEmpty(struct stack *ptr)

if (ptr->top == -1)

return 1;

else

return 0;

int isFull(struct stack *ptr)

if (ptr->top == ptr->size - 1)

{
39

return 1;

else

return 0;

void push(struct stack* ptr, char val){

if(isFull(ptr)){

printf("Stack Overflow! Cannot push %d to the stack\n", val);

else{

ptr->top++;

ptr->arr[ptr->top] = val;

char pop(struct stack* ptr){

if(isEmpty(ptr)){

printf("Stack Underflow! Cannot pop from the stack\n");

return -1;

else{

char val = ptr->arr[ptr->top];

ptr->top--;

return val;

int precedence(char ch){

if(ch == '*' || ch=='/')


40

return 3;

else if(ch == '+' || ch=='-')

return 2;

else

return 0;

int isOperator(char ch){

if(ch=='+' || ch=='-' ||ch=='*' || ch=='/')

return 1;

else

return 0;

char* infixToPostfix(char* infix){

struct stack * sp = (struct stack *) malloc(sizeof(struct stack));

sp->size = 10;

sp->top = -1;

sp->arr = (char *) malloc(sp->size * sizeof(char));

char * postfix = (char *) malloc((strlen(infix)+1) * sizeof(char));

int i=0; // Track infix traversal

int j = 0; // Track postfix addition

while (infix[i]!='\0')

if(!isOperator(infix[i])){

postfix[j] = infix[i];

j++;

i++;

else{

if(precedence(infix[i])> precedence(stackTop(sp))){
41

push(sp, infix[i]);

i++;

else{

postfix[j] = pop(sp);

j++;

while (!isEmpty(sp))

postfix[j] = pop(sp);

j++;

postfix[j] = '\0';

return postfix;

int main()

char * infix = "x-y/z-k*d";

printf("postfix is %s", infixToPostfix(infix));

return 0;

}
42

21. Write a program to convert an infix expression into prefix

#include<stdio.h>

#include<string.h>

#include<math.h>

#include<stdlib.h>

#define BLANK ' '

#define TAB '\t'

#define MAX 50

long int pop();

long int eval_pre();

char infix[MAX], prefix[MAX];

long int stack[MAX];

int top;

int isempty();

int white_space(char symbol);

void infix_to_prefix();

int priority(char symbol);

void push(long int symbol);

long int pop();


43

long int eval_pre();

int main()

long int value;

top = -1;

printf("Enter infix : ");

gets(infix);

infix_to_prefix();

printf("prefix : %s\n",prefix);

value=eval_pre();

printf("Value of expression : %ld\n",value);

return 0;

}/*End of main()*/

void infix_to_prefix()

int i,j,p,n;

char next ;

char symbol;

char temp;

n=strlen(infix);

p=0;

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

symbol=infix[i];

if(!white_space(symbol))

switch(symbol)

case ')':
44

push(symbol);

break;

case '(':

while( (next=pop()) != ')')

prefix[p++] = next;

break;

case '+':

case '-':

case '*':

case '/':

case '%':

case '^':

while( !isempty( ) && priority(stack[top])>

priority(symbol) )

prefix[p++] = pop();

push(symbol);

break;

default: /*if an operand comes*/

prefix[p++] = symbol;

while(!isempty( ))

prefix[p++] = pop();

prefix[p] = '\0'; /*End prefix with'\0' to make it a string*/

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

temp=prefix[i];

prefix[i]=prefix[j];
45

prefix[j]=temp;

}/*End of infix_to_prefix()*/

/* This function returns the priority of the operator */

int priority(char symbol )

switch(symbol)

case ')':

return 0;

case '+':

case '-':

return 1;

case '*':

case '/':

case '%':

return 2;

case '^':

return 3;

default :

return 0;

}/*End of switch*/

}/*End of priority()*/

void push(long int symbol)

if(top > MAX)

printf("Stack overflow\n");

exit(1);
46

else

top=top+1;

stack[top] = symbol;

}/*End of push()*/

long int pop()

if(top == -1 )

printf("Stack underflow \n");

exit(2);

return (stack[top--]);

}/*End of pop()*/

int isempty()

if(top==-1)

return 1;

else

return 0;

int white_space(char symbol)

if(symbol==BLANK || symbol==TAB || symbol=='\0')

return 1;

else

return 0;
47

}/*End of white_space()*/

long int eval_pre()

long int a,b,temp,result;

int i;

for(i=strlen(prefix)-1;i>=0;i--)

if(prefix[i]<='9' && prefix[i]>='0')

push( prefix[i]-48 );

else

b=pop();

a=pop();

switch(prefix[i])

case '+':

temp=b+a; break;

case '-':

temp=b-a;break;

case '*':

temp=b*a;break;

case '/':

temp=b/a;break;

case '%':

temp=b%a;break;

case '^':

temp=pow(b,a);

push(temp);
48

result=pop();

return result;

}/*End of eval_pre */

22. Write a program to evaluate a postfix expression.

#include<stdio.h>

int stack[20];

int top = -1;

void push(int x)

stack[++top] = x;

int pop()

return stack[top--];
49

int main()

char exp[20];

char *e;

int n1,n2,n3,num;

printf("Enter the expression :: ");

scanf("%s",exp);

e = exp;

while(*e != '\0')

if(isdigit(*e))

num = *e - 48;

push(num);

else

n1 = pop();

n2 = pop();

switch(*e)

case '+':

n3 = n1 + n2;

break;

case '-':

{
50

n3 = n2 - n1;

break;

case '*':

n3 = n1 * n2;

break;

case '/':

n3 = n2 / n1;

break;

push(n3);

e++;

printf("\nThe result of expression %s = %d\n\n",exp,pop());

return 0;

}
51

23. Write a program to evaluate a prefix expression.

#include<stdio.h>

#include<string.h>

int s[50];

int top=0;

void push(int ch);

int pop();

int main()

int a,b,c,i;

char prefix[50];

printf("\nEnter the prefix string in figures(1 digit nos);");

gets(prefix);

//for(i=0;i<strlen(prefix);i++)

for(i=strlen(prefix)-1;i>=0;i--)

if(prefix[i]=='+')

c=pop()+pop();

push(c);

else if(prefix[i]=='-')

a=pop();

b=pop();

c=b-a;

push(c);

}
52

else if(prefix[i]=='*')

{ a=pop();

b=pop();

c=b*a;

push(c);

else if(prefix[i]=='/')

a=pop();

b=pop();

c=b/a;

push(c);

else

push(prefix[i]-48);

//printf("\n INT=%d - CHAR=%d",prefix[i]-48,c);

printf("\nFinal ans = %d",pop());

getch();

return 0;

void push(int ch)

top++;

s[top]=ch;

int pop()
53

int ch;

ch=s[top];

top=top-1;

return(ch);

Extra program

24.Write a program to print the fabonacci series using function.

#include<stdio.h>

void fab_series();

main()

fab_series();

}
54

void fab_series()

int sum,f0,f1;

f0=0, f1=1;

printf("%d\t%d",f0,f1);

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

sum=f0+f1;

printf("%d",sum);

f0=f1;

f1=sum;

25.Balanced using STACK


55

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

//balanced parenthesis program

#define MAX 30

int top=-1;

int stack[MAX];

void push(char);

char pop();

int equal(char a,char b);

int bal_par(char []);

int main()

char exp[MAX];

int val;

printf("Enter an algebraic expression : ");

gets(exp);

val=bal_par(exp);//1

if(val==1)

printf("Valid expression\n");

else

printf("Invalid expression\n");

return 0;
56

int bal_par(char exp[] )

int i;

char temp;

for(i=0;i<strlen(exp);i++)

if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')

push(exp[i]);

if(exp[i]==')' || exp[i]=='}' || exp[i]==']')

if(top==-1)

printf("Right parentheses are more than left parentheses\n");

return 0;

else

temp=pop();

if(!equal(temp, exp[i])) //eual(a,b) not set 1

printf("Mismatched parentheses are : ");

return 0;

}
57

if(top==-1)

printf("Balanced Parentheses\n");

return 1;

else

printf("Left parentheses more than right parentheses\n");

return 0;

int equal(char a,char b)

if(a=='[' && b==']')

return 1;

if(a=='{' && b=='}')

return 1;

if(a=='(' && b==')')

return 1;

return 0;

void push(char item)

if(top==(MAX-1))

printf("Stack Overflow\n");

return;
58

top=top+1;

stack[top]=item;

}/*End of push()*/

char pop()

if(top==-1)

printf("Stack Underflow\n");

exit(1);

return(stack[top--]);

}/*End of pop()*/

26.Infix to postfix.
59

#include<stdio.h>

#include<stdlib.h> /* for exit() */

#include<ctype.h> /* for isdigit(char ) */

#include<string.h>

#define SIZE 100

char stack[SIZE];

int top = -1;

void push(char item)

//char item

if(top >= SIZE-1)

printf("\nStack Overflow.");

else

top = top+1;

stack[top] = item; //a[top]=element

/* define pop operation */

char pop()

char item ;
60

if(top <0)

printf("stack under flow: invalid infix expression");

getchar();

/* underflow may occur for invalid expression */

/* where ( and ) are not matched */

exit(1);

else

item = stack[top];

top = top-1;

return(item);

/* define function that is used to determine whether any symbol is operator or not

(that is symbol is operand)

* this fucntion returns 1 if symbol is opreator else return 0 */

int is_operator(char symbol)

if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-')

return 1; //yes symbol is operator

}
61

else

return 0;

/* define fucntion that is used to assign precendence to operator.

* Here ^ denotes exponent operator.

* In this fucntion we assume that higher integer value

* means higher precendence */

int precedence(char symbol)

if(symbol == '^')/* exponent operator, highest precedence*/

return(3);

else if(symbol == '*' || symbol == '/')

return(2);

else if(symbol == '+' || symbol == '-') /* lowest precedence */

return(1);

else

{
62

return(0);

void InfixToPostfix(char infix_exp[], char postfix_exp[])

int i, j;

char item;

char x;

push('('); /* push '(' onto stack */

strcat(infix_exp,")"); //

/* add ')' to infix expression */

i=0;

j=0;

item=infix_exp[i]; /* initialize before loop*/

while(item != '\0') /* run loop till end of infix expression */

if(item == '(')

push(item);

else if( isdigit(item) || isalpha(item))


63

postfix_exp[j] = item; //post=a /* add operand symbol to postfix expr */

j++;

else if(is_operator(item) == 1) //1==1 /* means symbol is operator */

x=pop();//+

while(is_operator(x) == 1 && precedence(x)>= precedence(item))

postfix_exp[j] = x; /* so pop all higher precendence operator and */

j++;

x = pop(); /* add them to postfix expresion */

push(x); //onto postfix

/* because just above while loop will terminate we have

oppped one extra item

for which condition fails and loop terminates, so that one*/

push(item); /* push current oprerator symbol onto stack */

else if(item == ')') /* if current symbol is ')' then */

x = pop(); /* pop and keep popping until */

while(x != '(') /* '(' encounterd */

postfix_exp[j] = x;

j++;

x = pop();
64

else

{ /* if current symbol is neither operand not '(' nor ')' and nor

operator */

printf("\nInvalid infix Expression.\n"); /* the it is illegeal symbol */

getchar();

exit(1);

i++;

item = infix_exp[i]; /* go to next symbol of infix expression */

} /* while loop ends here */

if(top>0)

printf("\nInvalid infix Expression.\n"); /* the it is illegeal symbol */

getchar();

exit(1);

if(top>0)

printf("\nInvalid infix Expression.\n"); /* the it is illegeal symbol */

getchar();

exit(1);

}
65

postfix_exp[j] = '\0'; /* add sentinel else puts() fucntion */

/* will print entire postfix[] array upto SIZE */

/* main function begins */

int main()

char infix[SIZE], postfix[SIZE];

printf("ASSUMPTION: The infix expression contains single letter variables and single digit constants
only.\n");

printf("\nEnter Infix expression : ");

gets(infix); //a+b

InfixToPostfix(infix,postfix); //user defined function

printf("Postfix Expression: ");

puts(postfix); //ab+

return 0;
66

27.Postfix evaluation.

#include <stdio.h>

#include <ctype.h>

#define MAX_S 100

#define SIZE 100

int stack[MAX_S];

int top = -1;

void push(int item)

if (top >= MAX_S - 1)

printf("stack over flow");

return;

}
67

else

top = top + 1;

stack[top] = item;

int pop()

int item;

if (top < 0)

printf("stack under flow");

else

item = stack[top];

top = top - 1;

return item;

void EvalPostfix(char postfix[])

int i;

char ch;

int val;

int A, B;
68

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

ch = postfix[i];

if (isdigit(ch))

push(ch - '0');//avoid ascii convert

else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')

A = pop();//3

B = pop();//2

switch (ch) /* ch is an operator */

case '*':

val = B * A;

break;

case '/':

val = B / A;

break;

case '+':

val = B + A;//3+2 val=5

break;
69

case '-':

val = B - A;

break;

push(val);//5 stack

printf(" \n Result of expression evaluation : %d \n", pop());

int main()

int i;

char postfix[SIZE];

printf("ASSUMPTION: There are only four operators(*, /, +, -) in an expression and operand is single
digit only.\n");

printf(" \nEnter postfix expression,\npress right parenthesis ')' for end expression : ");

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

scanf("%c", &postfix[i]);

if (postfix[i] == ')') /* is there any way to eliminate this if */


70

break;

EvalPostfix(postfix);

return 0;

28.Write a program to make structure of student type with the following data members int roll_no,
char name [25] and sort the data according to the roll number of students (Insertion sorting method).

#include<stdio.h>

struct student

int roll_no; char name[25];

}stud[100],t; void main()

{
71

int i,j,n;

printf("Enter the no of students\n"); scanf("%d",&n);

printf("enter student info as roll_no , name \n"); for(i=0;i<n;i++)

scanf("%d %s" ,&stud[i].roll_no,stud[i].name);

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

int temp = stud[i].roll_no; j = i - 1;

while ((temp < stud[j].roll_no) && (j >= 0)) { stud[j + 1].roll_no =stud[j].roll_no;

j = j - 1;

stud[j + 1].roll_no = temp;

printf("\nStudent info in terms of marks from lowest to highest\n"); printf("\nROLL_NO\t\tNAME\n");

printf(" \n"); for(i=0;i<n;i++)

printf("%d\t\t%s\n",stud[i].roll_no,stud[i].name);

}
72
73

29.Write a program to find the day of the week using switch case function.

#include<stdio.h>

void week();

int main()

week();

};

void week()

int week;

printf("enter week number(1-7):"); scanf("%d",&week); switch(week)

case 1:

case 2:

case 3:

case 4:

case 5:
74

case 6:

case 7:

printf("monday");break;

printf("tuesday");break;

printf("wednesday");break;

printf("thursday");break;

printf("friday");break;

printf("saturday");break;

printf("sunday");break;

default:
75

printf("enter correct number");

30.Write a program based on call by value.

#include<stdio.h>

void main()

void swap(int,int); int a=7,b=5;

printf("Before calling swap()a=%d\tb=%d\n",a,b); swap(a,b);

printf("After calling swap()a=%d\tb=%d\n",a,b);

void swap(int fa,int fb)

int temp=fa; fa=fb; fb=temp;

printf("In the swap function a=%d\tb=%d\n",fa,fb);

}
76

31.WRITE A PROGRAM BASED ON CALL BY REFERENCE.

#include<stdio.h>

void Swap(int *a,int *b)

int temp;

temp=*a;

*a=*b;

*b=temp;

void main()

int A,B;

printf("Enter the numbers:");

scanf("%d%d",&A,&B);
77

printf("Values are: %d and %d\n",A,B);

Swap(&A,&B);

printf("After Swapping, Values are: %d and %d",A,B);

32.WRITE A PROGRAM TO INITIALIZE 1-D ARRAY.

#include<stdio.h>

void main()

int A[50],n,i;

printf("Enter the size of the Array:");

scanf("%d",&n);

printf("Enter the elements of the Array:");

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

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

printf("The Array is:");


78

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

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

33.Write a program that will perform all operations for 1D array (MENUDRIVEN PROGRAM
FOR 1D ARRAY).

#include<stdio.h>

void ini_m();

void display_m();

void linear_search();

void bubble_sort();

void binary_search();

void reverse_m();

void del_ele();

void insert_ele();

#define j 5
79

int a[j];

int main()

int choice;

printf("enter 1 for initialization\nenter 2 for display\nenter 3 for linear search\nenter 4 for


bubble sort\nenter 5 for binary search\nenter 6 for reverse\nenter 7 for delete element\nenter
8 for insert element\nenter 9 for exit\n");

do

printf("\nenter the choice: ");

scanf("%d",&choice);

switch(choice)

case 1:

ini_m();

break;

case 2:

{display_m();

break;

case 3:

linear_search();

break;

case 4:

{
80

bubble_sort();

break;

case 5:

binary_search();

break;

case 6:

reverse_m();

case 7:

del_ele();

break;

case 8:

insert_ele();

break;

case 9:

printf("exit point");

break;

default:

{
81

printf("wrong choice");

while(choice!=9);

return 0;

void ini_m()

int i;

printf("enter the 5 elements in array",i);

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

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

void display_m()

int i;

for(int i=0;i<j;i++)

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

void linear_search()

int i, search;

printf("Enter a number to search\n");

scanf("%d", &search);

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

if (a[i] == search) /* If required element is found */


82

printf("%d is present at location %d.\n", search, i+1);

break;

if (i == 5)

printf("%d isn't present in the array.\n", search);

void bubble_sort()

int i,k, t;

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

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

if (a[k] > a[k+1]) /* For decreasing order use '<' instead of '>' */

t = a[k];

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

a[k+1] = t;

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

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

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

void binary_search()

{
83

int i,search,mid,first,last;

printf("Enter a number to search\n");

scanf("%d", &search);

first=0;

last=j-1;

mid=(first+last)/2;

while (first<=last)

if(a[mid]<search)

first=mid+1;

else if(a[mid]==search)

printf("%d found at location %d\n",search,mid+1);

break;

else

last=mid-1;

mid=(first+last)/2;

if(first>last)

printf("element not found\n",search);

void reverse_m()

int i, t, end;

end = j- 1;

for (i= 0; i< j/2; i++) {

t = a[i];

a[i] = a[end];
84

a[end]= t;

end--;

printf("Reversed array elements are:\n");

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

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

void del_ele()

int i,position,n;

printf("Enter the location where you wish to delete element\n");

scanf("%d", &position);

if (position >= j+1)

printf("Deletion not possible.\n");

else

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

a[i] = a[i+1];

printf("Resultant array:\n");

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

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

void insert_ele()

int i, x, pos,n;

n=j+1;

printf("element to be inserted",x);

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

printf("position",pos);

scanf("%d",&pos);

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

a[i] = a[i - 1];

a[pos - 1] = x;

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

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

printf("\n");

}
86
87

35.WRITE A PROGRAM TO INITIALIZE 2-D ARRAY.

#include<stdio.h>

int main()

int r,c,i,j;

printf("enter number of rows\t",r);

scanf("%d",&r);

printf("enter number of column\t",c);

scanf("%d",&c);

int a[r][c];

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

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

printf("enter values for a[%d][%d]:",i+1,j+1);

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

printf("Two Dimensional array elements:\n");

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

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

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

if(j==1)

printf("\n");

}
88

36.Write a program to find out the subtraction of two matrices.

#include <stdio.h>

int main()

int r, c, i, j;

printf("Enter the number of rows\t");


89

scanf("%d", &r);

printf("Enter the number of columns\t");

scanf("%d", &c);

int a[r][c], b[r][c], sub[r][c];

printf("\nEnter elements of 1st array:\n");

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

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

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

printf("1st array elements:\n");

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

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

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

if(j==1)

printf("\n");

printf("Enter elements of 2nd array:\n");

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

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

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

printf("2nd array elements:\n");

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

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

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

if(j==1)

printf("\n");

// subtacting two arrays printf("sub of array:\n"); for (i = 0; i < r; ++i)

for (j = 0; j < c; ++j) { sub[i][j] = a[i][j] - b[i][j];

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

if(j==1){ printf("\n");

}
91
92

37.Write a program for BUBBLE sort

#include <stdio.h>

int main()

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

printf("EntWrite a program for SELECTION sort.38%d", &n);

printf("Enter %d integers\n", n);

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

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

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

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

if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */

swap = array[d];

array[d] = array[d+1];

array[d+1] = swap;

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

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


93

printf(“%d\n”, array[c]);

return 0;

38.Write a program for SELECTION sort.

#include<stdio.h>

void main()
94

int A[50],n,i,j,temp,position;

printf("Enter the size of the Array:");

scanf("%d",&n);

printf("Enter the elments of the Array:");

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

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

printf("The Array is:");

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

printf("%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)

temp=A[i];

A[i]=A[position];

A[position]=temp;

printf("\nThe Sorted Array is:");

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

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

}
95

39.Write a program for INSERTION sort.

#include<stdio.h>

void main()

int A[50],n,i,j,temp;

printf("Enter the size of the Array:");

scanf("%d",&n);

printf("Enter the elments of the Array:");

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

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

printf("The Array is:");

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

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

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

temp=A[i];

j=i-1;

while((temp<A[j])&&(j>=0))

A[j+1]=A[j];

j--;

A[j+1]=temp;

printf("\nThe Sorted Array is:");

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

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

}
97

40.Write a program for LINEAR search.

#include<stdio.h>

void

main ()

int A[50], ele, n, i, flag = 0;

printf ("Enter the size of the Array:");

scanf ("%d", &n);

printf ("Enter the elWrite a prografor (i = 0; i < n; i++)

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

printf ("The Array is:");

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

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

printf ("\nEnter the element u r searching for:");

scanf ("%d", &ele);

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

if (A[i] == ele)

flag = 1;

printf ("Element is found at index number %d and position %d", i,

i + 1);

break;

if (flag == 0)

printf ("Element not found!!");

}
98

41.Write a program for BINARY search.

#include<stdio.h>

void main()

int A[50],ele,n,i,u=4,l=0,mid,flag=0;

printf("Enter the size of the Array:");

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

printf("Enter the elements of the Array:");

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

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

printf("The Array is:");

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

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

printf("\nEnter the element u r searching for:");

scanf("%d",&ele);

while(l<=u)

mid=(l+u)/2;

if(A[mid]>ele)

u=mid-1;

else if(A[mid]<ele)

l=mid+1;

else

flag=1;

printf("The Element is found at index %d and position %d",mid,mid+1);

break;

if(flag==0)

printf("Element not found!!");

}
100

You might also like