Thursday, February 1, 2024

C Language - Write a program to swap the values of two variables without using a third variable


Question: Write a program to swap the values of two variables without using a third variable.

Solution :


C Language

#include <stdio.h>

#include <conio.h>

void main()

{

            int a,b;

            clrscr();

            printf("\nENTER TWO NUMBERS FOR a AND b:\n");

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

            printf("\nBEFORE SWAPING THE VALUE OF a=%d AND b=%d",a,b);

            a=a+b;

            b=a-b;

            a=a-b;

            printf("\nAFTER SWAPING THE VALUE OF a=%d AND b=%d",a,b);

            getch();

}

C Language - Write a program to compute the seconds from a given age.

 

Question: Write a program to compute the seconds from a given age.

Solution : C Language

#include <stdio.h>
#include <conio.h>
void main()
{
            long unsigned age,sec;
            clrscr();
            printf("\nENTER YOUR AGE: ");
            scanf("%lu",&age);
            sec=age*365*24*60*60;
            printf("\nAGE IN SECONDS: %lu",sec);
            getch();
}
 

C Language - Conditional Control Statements - If Conditions (Solved Program source code)

 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.

#include <stdio.h>
#include <conio.h>
void main()
{
            int qty,rate;
            float disc=0.0,net;
            clrscr();
            printf("\nENTER QUANTITY: ");
            scanf("%d",&qty);
            printf("\nENTER RATE: ");
            scanf("%d",&rate);
            if (qty>100)
                        disc=qty*rate*10/100;
            net=(qty*rate)-disc;
            printf("\nNET AMOUNT: %0.2f",net);
            getch();
}

Wednesday, January 31, 2024

Displaying a Tree - both Recursive and Non Recursive methods - DSTC using C Language (Source Code Implemented)

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();
}

 

C++ Stack implementation as a class- DSTC using C Language (Source Code Implemented)

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();

}