Professional Documents
Culture Documents
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
26 Infix to postfix
27 Postfix evaluation
31 Call by reference
32 Initialisation of 1D array
35 Initialisation of 2D array
#include<stdio.h>
#include<math.h>
int main()
int fact()
scanf("%d",&n);
fact = fact*i;
return fact;
}
4
#include<stdio.h>
int main()
int n;
scanf("%d",&n);
return 0;
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
#include <stdio.h>
int main()
5
scanf("%d", &size);
int array[size];
scanf("%d", &array[i]);
largest = array[0];
largest = array[i];
return 0;
#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';
return 0;
7.Write a program that will read the records of employees. The data members of a record are
as
follows:
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 ()
int dummy;
scanf("%s %d %d",s1.name,&s1.id,&s1.salary);
scanf("%c",&dummy);
scanf("%s %d %d",s2.name,&s2.id,&s2.salary);
scanf("%c",&dummy);
scanf(“%s %d %d”,s3.name,&s3.id,&s3.salary);
scanf(“%c”,&dummy);
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;
average.salary= (total.salary/3);
}
10
include<stdio.h>
struct complex
float real;
float imag;
};
int main()
a1=read(a1);
a2=read(a2);
a3=add(a1,a2);
printf("\nsum=");
display(a3);
fflush(stdin);
scanf("%f",&a1.real);
scanf("%f",&a1.imag);
return a1;
11
printf("%f+i%f",a.real,a.imag);
temp.real=a.real+b.real;
temp.imag=a.imag+b.imag;
return temp;
#Include <stdio.h>
int main()
int r, c, i, j;
scanf("%d", &r);
scanf("%d", &c);
scanf("%d", &a[i][j]);
for(j=0;j<c;j++) {
if(j==1){
printf("\n");
scanf("%d", &b[i][j]);
for(j=0;j<c;j++) {
if(j==1){
printf(“/n”);
13
printf("sum of array:\n");
printf("%d\t",sum[i][j]);
if(j==1){
printf("\n");
}
14
#include <stdio.h>
15
int main()
int m, n, p, q, c, d, k, sum = 0;
scanf("%d", &first[c][d]);
if ( n != p )
else
scanf("%d", &second[c][d]);
multiply[c][d] = sum;
16
sum = 0;
printf("%d\t", multiply[c][d]);
printf("\n");
return 0;
}
17
#include <stdio.h>
int main()
scanf("%d", &a[i][j]);
if (j == c - 1)
printf("\n");
transpose[j][i] = a[i][j];
if (j == r - 1)
printf("\n");
return 0;
19
#include<stdio.h>
int main()
int a[100],i,n,number,pos;
printf("\nEnter no of elements\n");
scanf("%d",&n);
20
for (i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("a[%d] = %d\n",i,a[i]);
scanf("%d",&number);
scanf("%d",&pos);
for(i=n-1;i>=pos;i--)
a[i+1]=a[i];
n=n+1;
a[pos]=number;
for(i=0;i<n;i++)
printf("a[%d] = %d\n",i,a[i]);
}
21
22
include <stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &array[c]);
printf("a[%d]=%d\n",c,array[c]);
scanf("%d", &position);
else
array[c] = array[c+1];
printf("Resultant array:\n");
printf("%d\n", array[c]);
return 0;
23
#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);
for(i=0;i<j;i++)
scanf("%d",&a[i]);
for(i=0;i<j;i++)
scanf("%d",&b[i]);
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;
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);
for(i=n-1;i>=0;i--)
printf(" %d",*(p+i));
return 0;
}
27
#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;
scanf("%d",&n);
printf("\n\t--------------");
do
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:
}while(choice!=4);
return(0);
void push()
if(top>=n-1)
{
29
else
scanf("%d",&x);
top++;
stack[top]=x;
void pop()
if(top<=-1)
else
top--;
void display()
if(top>=0)
for(i=top;i>=0;i--)
printf("\n %d",stack[i]);
30
else{
}
31
32
#include<stdio.h>
#include<stdlib.h>
int main()
int *p,n,i;
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);
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 main()
int num;
scanf("%d",&num);
DecToBin(num);
return 0;
while(num!=0)
rem = num%2;
num/=2;
while(top!=-1)
printf("\n");
if(*top == (MAX-1))
printf("Stack Overflow\n");
35
else
*top=*top+1;
stack_arr[*top] = x;
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
#include<stdio.h>
int main()
char expression[50];
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stack
int size;
int top;
char *arr;
};
return sp->arr[sp->top];
if (ptr->top == -1)
return 1;
else
return 0;
if (ptr->top == ptr->size - 1)
{
39
return 1;
else
return 0;
if(isFull(ptr)){
else{
ptr->top++;
ptr->arr[ptr->top] = val;
if(isEmpty(ptr)){
return -1;
else{
ptr->top--;
return val;
return 3;
return 2;
else
return 0;
return 1;
else
return 0;
sp->size = 10;
sp->top = -1;
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()
return 0;
}
42
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define MAX 50
int top;
int isempty();
void infix_to_prefix();
int main()
top = -1;
gets(infix);
infix_to_prefix();
printf("prefix : %s\n",prefix);
value=eval_pre();
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;
symbol=infix[i];
if(!white_space(symbol))
switch(symbol)
case ')':
44
push(symbol);
break;
case '(':
prefix[p++] = next;
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
priority(symbol) )
prefix[p++] = pop();
push(symbol);
break;
prefix[p++] = symbol;
while(!isempty( ))
prefix[p++] = pop();
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()*/
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()*/
printf("Stack overflow\n");
exit(1);
46
else
top=top+1;
stack[top] = symbol;
}/*End of push()*/
if(top == -1 )
exit(2);
return (stack[top--]);
}/*End of pop()*/
int isempty()
if(top==-1)
return 1;
else
return 0;
return 1;
else
return 0;
47
}/*End of white_space()*/
int i;
for(i=strlen(prefix)-1;i>=0;i--)
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 */
#include<stdio.h>
int stack[20];
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;
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++;
return 0;
}
51
#include<stdio.h>
#include<string.h>
int s[50];
int top=0;
int pop();
int main()
int a,b,c,i;
char prefix[50];
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);
getch();
return 0;
top++;
s[top]=ch;
int pop()
53
int ch;
ch=s[top];
top=top-1;
return(ch);
Extra program
#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);
sum=f0+f1;
printf("%d",sum);
f0=f1;
f1=sum;
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 30
int top=-1;
int stack[MAX];
void push(char);
char pop();
int main()
char exp[MAX];
int val;
gets(exp);
val=bal_par(exp);//1
if(val==1)
printf("Valid expression\n");
else
printf("Invalid expression\n");
return 0;
56
int i;
char temp;
for(i=0;i<strlen(exp);i++)
push(exp[i]);
if(top==-1)
return 0;
else
temp=pop();
return 0;
}
57
if(top==-1)
printf("Balanced Parentheses\n");
return 1;
else
return 0;
return 1;
return 1;
return 1;
return 0;
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<string.h>
char stack[SIZE];
//char item
printf("\nStack Overflow.");
else
top = top+1;
char pop()
char item ;
60
if(top <0)
getchar();
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
if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-')
}
61
else
return 0;
return(3);
return(2);
return(1);
else
{
62
return(0);
int i, j;
char item;
char x;
strcat(infix_exp,")"); //
i=0;
j=0;
if(item == '(')
push(item);
j++;
x=pop();//+
j++;
postfix_exp[j] = x;
j++;
x = pop();
64
else
{ /* if current symbol is neither operand not '(' nor ')' and nor
operator */
getchar();
exit(1);
i++;
if(top>0)
getchar();
exit(1);
if(top>0)
getchar();
exit(1);
}
65
int main()
printf("ASSUMPTION: The infix expression contains single letter variables and single digit constants
only.\n");
gets(infix); //a+b
puts(postfix); //ab+
return 0;
66
27.Postfix evaluation.
#include <stdio.h>
#include <ctype.h>
int stack[MAX_S];
return;
}
67
else
top = top + 1;
stack[top] = item;
int pop()
int item;
if (top < 0)
else
item = stack[top];
top = top - 1;
return item;
int i;
char ch;
int val;
int A, B;
68
ch = postfix[i];
if (isdigit(ch))
A = pop();//3
B = pop();//2
case '*':
val = B * A;
break;
case '/':
val = B / A;
break;
case '+':
break;
69
case '-':
val = B - A;
break;
push(val);//5 stack
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 : ");
scanf("%c", &postfix[i]);
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
{
71
int i,j,n;
while ((temp < stud[j].roll_no) && (j >= 0)) { stud[j + 1].roll_no =stud[j].roll_no;
j = j - 1;
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;
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
#include<stdio.h>
void main()
}
76
#include<stdio.h>
int temp;
temp=*a;
*a=*b;
*b=temp;
void main()
int A,B;
scanf("%d%d",&A,&B);
77
Swap(&A,&B);
#include<stdio.h>
void main()
int A[50],n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
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;
do
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;
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;
scanf("%d", &search);
break;
if (i == 5)
void bubble_sort()
int i,k, t;
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("%d\t", a[i]);
void binary_search()
{
83
int i,search,mid,first,last;
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)
break;
else
last=mid-1;
mid=(first+last)/2;
if(first>last)
void reverse_m()
int i, t, end;
end = j- 1;
t = a[i];
a[i] = a[end];
84
a[end]= t;
end--;
printf("%d\n", a[i]);
void del_ele()
int i,position,n;
scanf("%d", &position);
else
a[i] = a[i+1];
printf("Resultant array:\n");
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);
a[pos - 1] = x;
printf("\n");
}
86
87
#include<stdio.h>
int main()
int r,c,i,j;
scanf("%d",&r);
scanf("%d",&c);
int a[r][c];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
for(j=0;j<c;j++)
if(j==1)
printf("\n");
}
88
#include <stdio.h>
int main()
int r, c, i, j;
scanf("%d", &r);
scanf("%d", &c);
scanf("%d", &a[i][j]);
for(j=0;j<c;j++)
if(j==1)
printf("\n");
scanf("%d", &b[i][j]);
90
for(j=0;j<c;j++)
if(j==1)
printf("\n");
printf("%d\t",sub[i][j]);
if(j==1){ printf("\n");
}
91
92
#include <stdio.h>
int main()
scanf("%d", &array[c]);
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(“%d\n”, array[c]);
return 0;
#include<stdio.h>
void main()
94
int A[50],n,i,j,temp,position;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
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;
for(i=0;i<n;i++)
printf("%d ",A[i]);
}
95
#include<stdio.h>
void main()
int A[50],n,i,j,temp;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
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;
for(i=0;i<n;i++)
printf("%d ",A[i]);
}
97
#include<stdio.h>
void
main ()
if (A[i] == ele)
flag = 1;
i + 1);
break;
if (flag == 0)
}
98
#include<stdio.h>
void main()
int A[50],ele,n,i,u=4,l=0,mid,flag=0;
scanf("%d",&n);
99
for(i=0;i<n;i++)
scanf("%d",&A[i]);
for(i=0;i<n;i++)
printf("%d ",A[i]);
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;
break;
if(flag==0)
}
100