Question: Write a program to compute the seconds from a given age.
Solution : C Language
We are trying to contribute to the net what ever Technical knowledge. I Believe that knowledge increases by spreading and sharing with others.
Question: Write a program to compute the seconds from a given age.
Solution : C Language
Control Statements (Conditional Control Statements)
If Conditions
Example 1: Write a program to compute net amount from the given quantity purchased and rate per quantity. Discount @10% is allowed if quantity purchased exceeds 100.
Net Amount = (Quantity Purchased x Rate Per Quantity) – Discount.
Displaying a Tree - both Recursive and Non Recursive methods
/*tree traversal inorder,preorder,postorder with recursive and nonrecursive*/
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
typedef struct s
{ int val;
struct s *left,*right;
}node;
typedef struct ss
{ node *root;
}tree;
tree* maktr(void)
{
tree* t;
t=(tree*)malloc(sizeof(tree));
t->root=NULL;
return t;
}
void instr(tree *t,int v)
{
node *newn,*nd,*par;
nd=t->root;
newn=(node*)malloc(sizeof(node));
newn->val=v;
newn->left=newn->right=NULL;
if(nd==NULL)//tree is empty
{ t->root=newn;
return;
}
while(nd!=NULL)
{ if(v<nd->val)
{ par=nd;
nd=nd->left;
}
else if(v>nd->val)
{ par=nd;
nd=nd->right;
}
else
{
printf("\n the node with same exists");
return;
}
}
if(v<par->val)
par->left=newn;
else
par->right=newn;
}
node *instr1(node *nd,int v)
{
if(nd==NULL)
{ nd=(node*)malloc(sizeof(node));
nd->val=v;
nd->left=nd->right=NULL;
}
else if(v<nd->val)
nd->left=instr1(nd->left,v);
else
nd->right=instr1(nd->right,v);
return nd;
}
void inrdsp(node *nd)
{ if(nd)
{ inrdsp(nd->left);
printf(" %d",nd->val);
inrdsp(nd->right);
}
}
void predsp(node *nd)
{ if(nd)
{ printf(" %d",nd->val);
predsp(nd->left);
predsp(nd->right);
}
}
void posdsp(node *nd)
{ if(nd)
{ posdsp(nd->left);
posdsp(nd->right);
printf(" %d",nd->val);
}
}
typedef struct st
{ node *info;
struct st *next;
}node1;
typedef struct st1
{ node1 *top;
}stack;
stack* crst(void)
{
stack *st;
st=(stack*)malloc(sizeof(stack));
st->top=NULL;
return st;
}
void pushst(stack* st,node *kv)
{
node1* newn;
newn=(node1*)malloc(sizeof(node1));
newn->info=kv;
newn->next=NULL;
if(st->top==NULL)
st->top =newn;
else
{ newn->next=st->top;
st->top=newn;
}
}
node* popst(stack* s)
{ node *p;
if(s->top==NULL)
{ printf("\n The stack is empty");
return NULL;
}
p=s->top->info;
s->top=s->top->next;
return p;
}
void pretrav(tree *t)
{
node *nk=t->root;
stack *s=crst();
do
{
while(nk!=NULL)
{ printf(" %d",nk->val);
if(nk->right!=NULL)
pushst(s,nk->right);
nk=nk->left;
}
if(s->top!=NULL)
nk=popst(s);
else
nk=NULL;
}while(nk!=NULL);//end while
}
void inrtrav(tree *t)
{
node *k=t->root;
stack* s=crst();
while(k!=NULL)
{ while(k!=NULL)
{ pushst(s,k);
k=k->left;
}
while(s->top!=NULL)
{
k=popst(s);
printf(" %d",k->val);
if(k->right!=NULL)
{ k=k->right; break;
}
else
k=NULL;
}// end while 2
}//end while 1
}
void postrav(tree *t)
{
node *nk=t->root,*nk1;
stack *s=crst();
do
{
while(nk!=NULL)
{
pushst(s,nk);
if(nk->right!=NULL)
{ nk1=nk->right;
nk1->val=-nk1->val;
pushst(s,nk1);
}
nk=nk->left;
}
while(s->top!=NULL)
{ nk=popst(s);
if(nk->val>=0)
printf(" %d",nk->val);
else
{ nk->val=-nk->val;
break;
}
}
}while(s->top!=NULL);
}
void main()
{
tree *t;
int v,i;
t=maktr();
clrscr();
for(i=0;i<5;i++)
{
printf("\n Enter the value of node");
scanf("%d",&v);
t->root=instr1(t->root,v);
}
printf("\nThe inorder display of the tree\n");
inrdsp(t->root);
printf("\nThe preorder display of the tree\n");
predsp(t->root);
printf("\nThe postorder display of the tree\n");
posdsp(t->root);
printf("\nThe inorder display of the tree\n");
inrtrav(t);
printf("\nThe preorder display of the tree\n");
pretrav(t);
printf("\nThe postorder display of the tree\n");
postrav(t);
getch();
}
Stack implementation as a class
# include<iostream.h>
# include<process.h>
# include<conio.h>
# define SIZE 20
class stack
{
int a[SIZE];
int tos; // Top of Stack
public:
stack();
void push(int);
int pop();
int isempty();
int isfull();
};
stack::stack()
{
tos=0; //Initialize Top of Stack
}
int stack::isempty()
{
return (tos==0?1:0);
}
int stack::isfull()
{
return (tos==SIZE?1:0);
}
void stack::push(int i)
{
if(!isfull())
{
a[tos]=i;
tos++;
}
else
{
cerr<<"Stack overflow error !
Possible Data Loss !";
}
}
int stack::pop()
{
if(!isempty())
{
return(a[--tos]);
}
else
{
cerr<<"Stack is empty! What to pop...!";
}
return 0;
}
void main()
{
stack s;
int ch=1,num;
while(ch!=0)
{
cout<<"Stack Operations Mani Menu
1.Push
2.Pop
3.IsEmpty
4.IsFull
0.Exit
<BR>;
cin>>ch;
switch(ch)
{
case 0:
exit(1); //Normal Termination of Program
case 1:
cout<<"Enter the number to push";
cin>>num;
s.push(num);
break;
case 2:
cout<<"Number popped from the stack is: "<<s.pop()<<endl;
break;
case 3:
(s.isempty())?(cout<<"Stack is empty.<BR>):(cout<<"Stack is not empty.<BR>);
break;
case 4:
(s.isfull())?(cout<<"Stack is full.<BR>):(cout<<"Stack is not full.<BR>);
break;
default:
cout<<"Illegal Option.
Please try again<BR>;
}
}//end of while
getch();
}