ax b
*/
ax b
#include<stdio.h>
main()
{
int a,x,b;
float result=1;
printf("Enter a,x,b values: ");
scanf("%d%d%d",&a,&x,&b);
result=((a*x)+b)/((a*x)-b);
printf("\nResult of the expression is :%f",result);
}
OUTPUT
Enter a, x, b values: 2
3
2
Result of the expression is :2.000000
Output
Enter value of x,y
Value of z
Value of d
:
:
:
5
16
19
1. c.
2e
#include<stdio.h>
#include<math.h>
#define ALPHA 5
#define PI 5
#define EXP 10
#define SIGMA 10
main()
{
int x,m;
float z;
printf("Enter x,m values: ");
scanf("%d%d",&x,&m);
z=1/((ALPHA*sqrt(2)*PI*EXP)-pow(((x-m)/(sqrt(2)*SIGMA)),2));
printf("z: %f",z);
}
Output
Enter x,m values
The Result is
0.002829
ASCENDING ORDER
#include<stdio.h>
main()
{
int a,b,c,small,big,mid;
printf("Enter three values: ");
scanf("%d%d%d",&a,&b,&c);
big=a;
small=b;
if(b>big)
big=b;
if(c<small)
small=c;
if(c>big)
big=c;
if(a<small)
small=a;
mid=a+b+c-big-small;
printf("%d\t%d\t%d",small,mid,big);
}
/*Output
Enter three values:56
34
35
34
35
56*/
/*SUM OF N NUMBERS*/
#include<stdio.h>
main()
{
int n,i,sum=0;
printf("Enter n value: ");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
sum=sum+i;
}
printf("%d",sum);
}
/*
Output
Enter n value:3
6*/
/*1 +
x2 x4
+
+ .......... + 10terms */
2! 4!
#include<stdio.h>
#include<math.h>
main()
{
float fact=1,power,x;
int i,n;
float sum=1.0;
printf("Enter n value: ");
scanf("%d",&n);
printf("Enter x value: ");
scanf("%f",&x);
for(i=2;i<=n;i+=2)
{
power=pow(x,2);
fact=fact*i*(i-1);
sum=sum+(power/fact);
}
printf("Sum=%f",sum);
}
/* OUT PUT
Enter n value:18
Enter x value:3
Sum=5.887725
*/
/* x +
x 3 x5
+
+ ........ + 7terms */
3! 5!
#include<stdio.h>
#include<math.h>
main()
{
float fact=1,power,x;
float sum=x;
int i,n;
printf("Enter n value:");
scanf("%d",&n);
printf("Enter x value: ");
scanf("%f",&x);
for(i=3;i<=n;i+=2)
{
power=pow(x,i);
fact=(fact*i*(i-1));
sum=sum+(power/fact);
}
printf("Sum=%f",sum);
}
/*
Output
Enter n value:13
Enter x value:2
Sum=1.626860
*/
/*
#include<stdio.h>
main()
{
int x,y;
printf("Enter x value: ");
scanf("%d",&x);
if(x>0) y=1;
else if(x==0) y=0;
else if(x<0) y=-1;
printf("%d",y);
}
/*
Output
Enter x value:5
y=1
*/
*/
3. Write a program for counting the total numbers of odd and total number of
even and sum of odd and sum of even
#include<stdio.h>
main()
{
int num,i,n,T_even=0,T_odd=0,s_even=0,s_odd=0;
printf("Enter n value: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter integer %d:",i);
scanf("%d",&num);
if(num%2==0)
{
T_even++;
s_even+=num;
}
else
{
T_odd++;
s_odd+=num;
}
}
printf("Total even numbers=%d \n Sum of even numbers=%d \n Total odd
numbers=%d \n Sum of odd numbers=%d \n",T_even,s_even,T_odd,s_odd);
}
/* Output
Enter n value
Enter integer 1
Enter integer 2
Enter integer 3
Enter integer 4
Enter integer 5
Enter integer 6
Enter integer 7
Enter integer 8
Enter integer 9
Enter integer 10
Total even numbers
Sum of even numbers
Total odd numbers
Sum of odd numbers
:
:
:
:
:
:
:
:
:
:
:
=
=
=
=
10
5
36
25
14
69
8
55
77
26
31
4
84
6
262
/*Matrix Multiplication*/
#include<stdio.h>
main()
{
int A[10][10],B[10][10],C[10][10],p,q,m,n,i,j,k;
printf("Enter order of A(mxn): ");
scanf("%d%d",&m,&n);
printf("Enter order of B(pxq): ");
scanf("%d%d",&p,&q);
if(!(n==p))
printf("\n\nMatrix Multiplication is not possible ");
else
{
printf("Enter the elements of A:");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&A[i][j]);
printf("Enter the elements of B:");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&B[i][j]);
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
C[i][j]=0;
for(k=0;k<n;k++)
C[i][j]+=A[i][k]*B[k][j];
}
printf("\nMultiplication of A and B\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%5d",C[i][j]);
printf("\n");
}
}
}
/*
Output
Enter order of A(mxn):3
3
Enter order of B(pxq):3
2
Enter the elements of A:1 2 3 4 5 6 7 8 9
Enter the elements of B:11 12 13 14 15 16
Multiplication of A and B
82 88
199 214
316 340
#include<stdio.h>
main()
{
char ch;
float num1,num2,ans;
printf("Enter two values: ");
scanf("%f%f",&num1,&num2);
printf("\n*********************--MENU--*********************\n");
printf("+ Addition Subtraction * Multiplication Division > a>b X To exit");
printf("Enter your choice: ");
scanf(" %c",&ch);
switch(ch)
{
case '+': ans=num1+num2;
printf("Sum of the two numbers is : %f",ans);
break;
case '-': ans=num1-num2;
printf("Difference of the two numbers is: %f",ans);
break;
case '*': ans=num1*num2;
printf("Multiplication of the two numbers is: %f",ans);
break;
case '/': ans=num1/num2;
printf("Division of the two numbers is: %f",ans);
break;
case '>': if(num1>num2)
{
printf("num1: %f",num1);
}
else
{
printf("num2: %f",num2);
}
break;
case 'X': printf("Termination by your choice");
exit();
break;
default: printf("Invalid choice");
break;
}
}
Output
Enter two values:2 3
*******************--MENU--********************
+.
Addition
-.
Subtraction
*.
Multiplication
/.
Division
>.
a>b
X.
To exit
Enter your choice: +
Sum of two numbers is 5.000000*/
/*A cloth show room as announced the following sensational discounts on purchase of
items
DISCOUNTS ( % )
PURCHASE AMOUNT
MILL CLOTH
HANDLOOM CLOTH
1 100
-5
101 200
5
7.5
201 - 300
7.5
10
Above 300
10
15
#include<stdio.h>
main()
{
int i;
float pmc,phl,dc,dh,dp,netamount;
printf("Enter purchase amount of pmc and phl: ");
scanf("%f%f",&pmc,&phl);
for(i=0;i<=2;i++)
{
switch(i)
{
case 0:
if(pmc>=1&&pmc<=100)
dp=0.0;
else if(pmc>=101&&pmc<=200)
dp=5.0;
else if(pmc>=201&&pmc<=300)
dp=7.5;
else if(pmc>=301)
dp=10.0;
dc=(pmc*dp)/100;
printf("The purchase amount of pmc is: %f\n\n",pmc);
printf("Discount: %f\n\n",dc);
break ;
case 1:
if(phl>=1&&phl<=100)
dh=5.0;
else if(phl>=101&&phl<=200)
dh=7.5;
else if(phl>=201&&phl<=300)
dh=10.0;
else if(phl>=301)
dh=15.0;
dc=(phl*dh)/100;
printf("The purchase amount of phl is :%f\n\n",phl);
printf("Discount: %f\n\n",dc);
break;
}
}
netamount=(pmc-dc)+(phl-dh);
printf("Net amount is : %f\n\n",netamount);
}
/*
Output
/*REVERSE ORDER*/
#include<stdio.h>
main()
{
int n,rev=0;
float d;
printf("Enter n value: ");
scanf("%d",&n);
while(n!=0)
{
d=n%10;
rev=rev*10+d;
n=n/10;
}
printf("%d",rev);
}
/*
Output
Enter n value:243
342*/
11. a.Series
1
2
2
3
3
4
4
5
5
3
4
5
4
5
#include<stdio.h>
main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
printf("%d\t",i);
printf("\n");
}
}
Output
1
2
3
4
5
2
3
4
5
3
4
5
4
5
11. b. SERIES
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
#include<stdio.h>
main()
{
int i,k,j;
for(i=1;i<=5;i++)
{
printf("\n");
for(k=5;k>=i;k--)
printf(" ");
for(j=1;j<=i;j++)
printf(" %d ",i);
}
}
Output
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
OUTPUT :
The given list 28 58 84 16 270 42 19 77 59 17 27 57
iteration
iteration
iteration
iteration
iteration
iteration
iteration
iteration
iteration
iteration
iteration
1
2
3
4
5
6
7
8
9
10
11
28 58 16 84 42 19 77 59 17 27 57 270
28 16 58 42 19 77 59 17 27 57 84 270
16 28 42 19 58 59 17 27 57 77 84 270
16 28 19 42 58 17 27 57 59 77 84 270
16 19 28 42 17 27 57 58 59 77 84 270
16 19 28 17 27 42 57 58 59 77 84 270
16 19 17 27 28 42 57 58 59 77 84 270
16 17 19 27 28 42 57 58 59 77 84 270
16 17 19 27 28 42 57 58 59 77 84 270
16 17 19 27 28 42 57 58 59 77 84 270
16 17 19 27 28 42 57 58 59 77 84 270
{
printf("q is empty\n");
return(-1);
}
front=(front+1)%max;
return cq[front];
}
void display()
{
int i;
if(rear>front)
for(i=front+1;i<=rear;i++)
printf(" ,%d",cq[i]);
else
{
for(i=front+1;i<max;i++)
printf(" ,%d",cq[i]);
for(i=0;i<=rear;i++)
printf(" ,%d",cq[i]);
}
printf("\n");
}
OUTPUT :
OUTPUT :
enter the
enter the
enter the
enter the
enter the
enter the
number of items
item
item
item
item
item
:
:
:
:
:
:
5
5
4
3
2
1
1
2
3
4
5
OUTPUT :
The given list
56 28 84 66 78 23 90 15 12 27
iteration
iteration
iteration
iteration
iteration
iteration
iteration
iteration
iteration
1
2
3
4
5
6
7
8
9
28
28
28
28
23
23
15
12
12
56
56
56
56
28
28
23
15
15
84 66 78 23 90
84 66 78 23 90
66 84 78 23 90
66 78 84 23 90
56 66 78 84 90
56 66 78 84 90
28 56 66 78 84
23 28 56 66 78
23 27 28 56 66
15
15
15
15
15
15
90
84
78
12 27
12 27
12 27
12 27
12 27
12 27
12 27
90 27
84 90
if(front>rear)
printf("empty");
else
{
printf("deleted item is %d",a[front]);
front++;
}
}
void display(int a[])
{
int i;
if(rear==front)
printf("q is empty");
else
for(i=rear-1;i>=front;i--)
printf("q is...%d\n",a[i]);
}
OUTPUT :
options: 1.insertion 2. deletion 3. display 4.exit
enter the choice 1
enter item 12
do u want to continue(y/n):y
options: 1. insertion 2.deletion 3. display 4.exit
enter choice 1
enter item 15
do u want to continue(y/n):y
options:1.insertion 2. deletion 3. display 4.exit
enter the choice 1
enter item 10
do u want to continue(y/n):y
options: 1.insertion 2. deletion 3. display 4.exit
enter the choice 3
q is ...
12
15
10
do u want to continue(y/n):y
options: 1. insertion 2.deletion 3. display 4.exit
enter choice 2
deleted item is 12
POSTFIX EVALUATION
#include<stdio.h>
int top,s[20];
main()
{
void calaculate(char);
void push(int a[],int);
int pop(int a[]);
char expr[20];
int i;
clrscr();
printf("enter the postfix expression:");
scanf("%s",expr);
for(i=0;i<strlen(expr);i++)
{
if(( expr[i] >='0') && (expr[i] <='9'))
push(s,expr[i]-'0');
else
{
calculate(expr[i]);
}
}
printf("result is...%d",s[top-1]);
getch();
}
void push(int a[],int i)
{
a[top]=i;
top++;
}
int pop(int a[])
{
if(top==0)
printf("Invalid Epression");
else
{
top--;
return(a[top]);
}
}
void calaculate(char c)
{
int no1,no2,no3;
no2=pop(s);
no1=pop(s);
switch(c)
{
case '+':no3=no1+no2;
push(s,no3);
break;
case '-':no3=no1-no2;
push(s,no3);
break;
case '*':no3=no1*no2;
push(s,no3);
break;
case '/':no3=no1/no2;
push(s,no3);
break;
}
}
OUTPUT
enter the postfix expression:23+
*b=t;
}
OUTPUT:
The given list
26 5 37 1 61 11 59 15 48 19
11
1
1
1
1
1
5
5
5
5
5
5
19
11
11
11
11
11
1
19
19
15
15
15
15
15
15
19
19
19
26
26
26
26
26
26
59
59
59
59
48
37
61
61
61
61
37
48
48
48
48
48
59
59
37
37
37
37
61
61
15. a. STACKS
#include<stdio.h>
#include<conio.h>
#define max 5
int top;
main()
{
int stack[max],i,option,pos;
char ch;
void push(int a[],int);
void pop(int a[]);
void peep(int a[],int);
void change(int a[],int,int);
void display(int a[]);
do
{
printf("options : 1.push 2.pop 3.peep
4.change 5.disply 6.exit");
printf("\nenter choice:");
scanf("%d",&option);
switch(option)
{
case 1: printf("enter item");
scanf("%d",&i);
push(stack,i);
break;
case 2: pop(stack);
break;
case 3: printf("enter The position of
the item from the top");
scanf("%d",&pos);
peep(stack,pos);
break;
case 4: printf("enter the position
of the item from the top");
scanf("%d",&pos);
printf("ener the new value");
scanf("%d",&i);
change(stack,pos,i);
break;
case 5: display(stack);
break;
case 6: exit(0);
break;
default:printf("invalidchoice");
break;
}
printf("do u want to continue:");
scanf(" %c",&ch);
}while((ch=='y') || (ch=='Y'));
getch();
}
OUTPUT :
options:1.push 2.pop 3.peep 4.change
enter the choice: 1
enter item 12
do u want to continue(y/n):y
options:1.push 2.pop 3.peep 4.change
enter choice: 1
enter item 15
do u want to continue(y/n):y
options:1.push 2.pop 3.peep 4.change
enter the choice: 1
enter item 10
do u want to continue(y/n):y
options:1.push 2.pop 3.peep 4.change
enter the choice: 5
10--->top
12
15
do u want to continue(y/n):y
options:1.push 2.pop 3.peep 4.change
enter choice: 2
deleted item is 10
do u want to continue(y/n):n
5.disply 6.exit
5.disply 6.exit
5.disply 6.exit
5.disply 6.exit
5.disply 6.exit
13. Program to read data from following table and display the details of
particular vehicle sold during a specified period
VEHICLE TYPE
MONTH
YEAR
PRICE
MARUTHI-800
2003
75000
MARUTHI-DX
2003
95000
GYPSY
2003
110000
MARUTHI VAN
2004
85000
#include<stdio.h>
#include<string.h>
typedef struct
{
int month;
int year;
}month;
typedef struct
{
char vtype[80];
long int price;
month mofs;
}record;
void displaymenu();
void displayall();
void displayspecified();
void insertrecord();
int isbtdates(int,int,int,int,int,int);
record readdata();
FILE *fp;
main()
{
int ch;
while(1)
{
displaymenu();
printf("\n\n ?");
fflush(stdin);
scanf("%d",&ch);
switch(ch)
{
case 1:
displayall();
break;
case 2:
displayspecified();
`
break;
case 3:
insertrecord();
break;
default :
exit(0);
}
}
}
void displaymenu()
{
printf("\n\n\t1.Displays all records");
printf("\n\t2.Displays specified records");
printf("\n\t3. Insert record");
printf("\n\t4.Exit");
}
void displayall()
{
record sales;
if((fp=fopen("data.txt","rb"))==NULL)
{
printf("Records not found\n");
getchar();
return;
}
rewind(fp);
printf("Vehicle type month year price\n\n");
printf("_________________________________________________\n");
while(fread(&sales,sizeof(record),1,fp))
{
printf("\n%12s %d %d %ld",
sales.vtype,sales.mofs.month,sales.mofs.year,sales.price);
}
fclose(fp);
}
void insertrecord()
{
record sales;
fp=fopen("data.txt","ab+");
printf("Enter sales Details\n\n");
sales=readdata();
fwrite(&sales,sizeof(record),1,fp);
fclose(fp);
displayall();
}
record readdata()
{
record sales;
printf("Vehicle type: ");
fflush(stdin);
scanf(" %s",sales.vtype);
printf("month: ");
scanf("%d",&sales.mofs.month);
printf("year: ");
scanf("%d",&sales.mofs.year);
printf("price : ");
scanf("%ld",&sales.price);
return(sales);
}
void displayspecified()
{
int sm,em,sy,ey,flag=0;
record sales;
char vname[80];
if((fp=fopen("data.txt","rb"))==NULL)
{
printf("Records not found\n");
getchar();
return;
}
fflush(stdin);
printf("Enter vehicle type : \n ");
scanf("%s",vname);
printf("Enter starting month : ");
scanf("%d",&sm);
printf("Enetr starting year : ");
scanf("%d",&sy);
printf("Enter ending month : ");
scanf("%d",&em);
printf("Enter ending year : ");
scanf("%d",&ey);
printf("Vehicle type month year price \n\n");
printf("____________________________________________________\n");
while(fread(&sales,sizeof(record),1,fp))
{
if(!strcmp(sales.vtype,vname))
{
if(isbtdates(sm,sy,em,ey,sales.mofs.month,sales.mofs.year))
{
printf("\n%12s %d %d %ld",
sales.vtype,sales.mofs.month,sales.mofs.year,sales.price);
flag=1;
}
}
}
if(flag==0)
{
printf("Records not found \n");
}
fclose(fp);
getchar();
}
int isbtdates(int m1,int y1,int m2,int y2,int m,int y)
{
if(y>y1 && y<y2)
return(1);
if(y>y1 && y==y2 && m<=m2)
return(1);
if(y1==y && y2==y && m>=m1 && m<=m2)
return(1);
if(y1==y && y2>y &&m>=m1)
return(1);
return(0);
}
//output
1.Displays all records
2.Displays specified records
3. Insert record
4.Exit
Vehicle type month year price
_________________________________________________
gypsy
4
2003
110000
maruthi-800
2
2003
75000
while(ch>=1||ch<=5);
}
struct tree *insert(struct tree *s,int x)
{
struct tree *trail,*p,*q;
q=(struct tree *)malloc(sizeof(struct tree));
q->info=x;
q->left=NULL;
q->right=NULL;
p=s;
trail=NULL;
while(p!=NULL)
{
trail=p;
if(p->info>x)
p=p->left;
else
p=p->right;
}
if(trail==NULL)
{
s=q;
return(s);
}
if(trail->info>x)
trail->left=q;
else
trail->right=q;
return(s);
}
struct tree *search(struct tree *p,int x)
{
while(p!=NULL && p->info!=x)
{
if(p->info>x)
p=p->left;
else
p=p->right;
}
return(p);
}
void delete(struct tree *p)
{
struct tree *temp;
if(p==NULL)
printf("\ntrying to delete a noexistent node \n");
else if(p->left==NULL)
{
temp=p;
p=p->right;
free(temp);
}
else if(p->right==NULL)
{
temp=p;
p=p->left;
free(temp);
}
else if((p->left!=NULL) && (p->right!=NULL))
{
temp=p->right;
while(temp->left!=NULL)
temp=temp->left;
temp->left=p->left;
temp=p;
p=p->right;
free(temp);
}
}
void inorder(struct tree *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("%d\t",p->info);
inorder(p->right);
}
}
if(p->info>x)
p=p->left;
else
p=p->right;
}
if(trail==NULL)
{
s=q;
return(s);
}
if(trail->info>x)
trail->left=q;
else
trail->right=q;
return(s);
}
void inorder(struct tree *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("%d\t",p->info);
inorder(p->right);
}
}
void preorder(struct tree *p)
{
if(p!=NULL)
{
printf("%d\t",p->info);
preorder(p->left);
preorder(p->right);
}
}
void postorder(struct tree *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("%d\t",p->info);
}
}
insert(info,&first,&last);
}
}
void insert(struct linked_list *i,struct linked_list **first,struct linked_list
**last)
{
struct linked_list *old,*p;
if(*last==NULL)
{
i->next=NULL;
i->previous=NULL;
*last=i;
*first=i;
return;
}
p=*first;
old=NULL;
while(p)
{
if(p->number<i->number)
{
old=p;
p=p->next;
}
else
{
if(p->previous)
{
p->previous->next=i;
i->next=p;
i->previous=p->previous;
p->previous=i;
return;
}
i->next=p;
i->previous=NULL;
i->previous=i;
*first=i;
return;
}
}
old->next=i;
i->next=NULL;
i->previous=old;
*last=i;
}
struct linked_list *find(int n)
{
struct linked_list *info=first;
while(info)
{
if(n==info->number) return info;
info=info->next;
}
printf("Number entered by you is not in the list ");
return NULL;
}
void item_delete(struct linked_list **first,struct linked_list **last)
{
struct linked_list *info;
int s;
printf("enter number to be deleted");
scanf("%d",&s);
info=find(s);
if(info)
if(*first==info)
{
*first=info->next;
if(*first)
(*first)->previous=NULL;
else
*last=NULL;
}
else
{
info->previous->next=info->next;
if(info!=*last)
info->next->previous=info->previous;
else
*last=info->previous;
}
free(info);
}
}
void list(void)
{
struct linked_list *info=first;
while(info)
{
printf("%d\n",info->number);
info=info->next;
}
printf("\n\n");
}
void search(void)
{
int s;
struct linked_list *info;
printf("enter number to be searched");
scanf("%d",&s);
info=find(s);
if(info)
printf("\nitem found in the list");
else
printf("\n item not found in the list");
}