Sunday, 16 March 2014

Factorial of a given number...

Simplest factorial program,use of one for loop . Program designed in simple logic so enjoy...
User can enter any desire valid input for the factorial output..


Source Code:-

#include<iostream.h>
#include<conio.h>
void main()
{  clrscr();
    float n,i;
    float a=1;
    cout<<"Program for Factorial of any Number (by: Tarun Rawat)\n\n";
    cout<<"enter a value ";
    cin>>n;
    for(i=1;i<=n;i++)
       {a=a*i;}
    cout<<"factorial is "<<a;
    getch();
}

Stack Operations using arrays

C++ program for implementation of Stack (push,pop & display) operation.Stack based on the LIFO (Last In First Out) behavior means, the last element is pushed(insert) inside the stack , is the first element to get pop(deleted).Stack is a data structure in which the objects are arranged in a non linear order. In stack, elements are added or deleted from only one end, i.e. top of the stack. Here we implement the PUSH, POP, DISPLAY stack operations using the array.
Source Code:-


#include<iostream.h>
#include<conio.h>

int stack[20],top=-1; //global declaration
void push();
void pop();
void display();
void main()
{ clrscr();
  int ch;
  cout<<"Program for Stack Operations by-Tarun Rawat\n\n";
  do
  {   cout<<"\n1.Push";
      cout<<"\n2.Pop";
      cout<<"\n3.Display";
      cout<<"\n4.Exit";
      cout<<"\nEnter your choice : ";
      cin>>ch;
      if(ch==1)
    push();
      else if(ch==2)
    pop();
      else if(ch==3)
    display();
      else if(ch==4)
       cout<<"\nEnd Of program";
  }while(ch!=4);
}
//to push elements in a stack
void push()
{
  if(top>9)
    cout<<"\nStack Overflow";
  else
  {   top=top+1;
      cout<<"\nEnter a value : ";
      cin>>stack[top];
  }

}

//to pop/delete stack elements
void pop()
{
  if(top==-1)
   cout<<"\nStack Overflow. No Elements to pop";
  else
  {
      cout<<stack[top]<<" is deleted..";
      top--;
  }
}

//displaying stack elements
void display()
{
    int i;
    if(top==-1)
      cout<<"\nStack underflow. No elements to display..";
    else
    {
    cout<<"\nThe stack elements are : ";
    for(i=0;i<=top;i++)
    {
      cout<<"\t"<<stack[i];
    }
      cout<<"\n";

    }
}

Circular Linked List in C++

Program for the implementation of  Circular Linked List in C++ language. In this program you can insert in circular link list, although the output seems as the singular link list but the code is written for circular list. Further addition if circular display function for viewing the operation don in the list. In Circular Linked List ending node not having the null value. Last node again point to starting node that's why it called as Circular Linked list.


Source Code:-

#include<iostream.h>
#include<conio.h>
#include<malloc.h>
struct node
{int info;
struct node *next;
}*start, *temp,*current;

void circular(int);
void circulardisplay();

void main()
{clrscr();
start=NULL;
int item,choice,location,element,position;
cout<<endl<<"Circular Singly Linked List by-Tarun Rawat\n";
again:

cout<<"\n1.Insert Item in Circular Linked List\n2.Display circular Linked List \n3.Exit\n";
cout<<"Enter choice : ";
cin>>choice;
switch(choice)
{case 1:cout<<"Enter item to insert: ";
       cin>>item;
       circular(item);
       goto again;
case 2:cout<<"\nInserted item = ";
       circulardisplay();cout<<"\n";
       goto again;
case 3:cout<<"\nTHANK YOU";
default:break;
}
getch();
}

void circular(int item)
{temp=(node*)malloc(sizeof(node));
temp->info=item;
temp->next=start;
node* current;
if(start==NULL)
{start=temp;
temp->next=start;
}
else
{current=start;
 while(current->next!=start)
  {current=current->next;
  }
 current->next=temp;
 }
}

void circulardisplay()
{current= start;
 do{
 cout<<current->info<<" ";
 current=current->next;
 }while(current!=start);

}

Find HCF of two number

 C++ Program to find HCF of two number.
The largest integer which is perfectly divisible to two or more numbers is known as H.C.F or G.C.D of those two numbers.There are many ways to find the H.C.F of two numbers is C++ programming.  Below code will take two integers from user and displays the H.C.F of those two integers.This is the best way to find HCF of two numbers.
In this method, smaller number is subtracted from larger number and that number is stored in place of larger number. This process is continued until, two numbers become equal which will be HCF.


Source Code:-

#include<iostream.h>
#include<conio.h>
void main()
{   clrscr();
    int num1,num2;
    cout<<" Find HCF of two number by-Tarun Rawat\n\n";
    cout<<"Enter first number : ";
    cin>>num1;
    cout<<"Enter second number : ";
    cin>>num2;
    cout<<"HCF of "<<num1<<" & "<<num2<<" is : ";
    while(num1!=num2)
    {if(num1>num2)
        num1-=num2;
    else num2-=num1;
    }
    cout<<num1;
    getch();
}


Reverse Traversing Of Four Digit No........

 Program print the input number , in right to left order (means  reverse traversing)

note: user can only put four digit number , if you want to insert more number leave message ....





Source Code:-

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
  int n,a,b,c,d;
  cout<<"Reverse Traversal by Tarun Rawat\n";
  cout<<"Enter four digit number \n";
  cin>>n;
  if(n!=0)
   { a=n%10;
     b=n/10%10;
     c=n/100%10;
     d=n/1000%10;
     cout<<a<<b<<c<<d;
   }
getch();
}

Program to Check Leap Year

 C++ program to check leap year : c++ code to check leap year, year will be entered by the user.
Simply use of nested if else property..


Source Code:-

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
  int year;
  cout<<"Program to check Leap year by-Tarun Rawat\n";
  cout<<"Enter a year to check : ";
  cin>>year;
  if ( year%400 == 0)
    cout<<year<<" is a leap year\n";
  else if ( year%100 == 0)
    cout<<year<<" is not a leap year\n";
  else if ( year%4 == 0 )
    cout<<year<<" is a leap year\n";
  else
    cout<<year<<" is not a leap year\n";
getch();

}

Matrix multiplication in c++

 c++ program to multiply matrices (two dimensional array), this program multiplies two matrices which will be entered by the user. Firstly user will enter the order of a matrix. If the entered orders of two matrix is such that they can't be multiplied then an error message is displayed on the screen.
You have already studied the logic to multiply them in Mathematics.
Matrices are frequently used while doing programming and are used to represent graph data structure, in solving system of linear equations and many more. Lot of research is being done on how to multiply matrices using minimum number of operations. You can also implement it using pointers.

Source Code:-

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
  int a[10][10],b[10][10],c[10][10],l,m,n,o,i,j,k;
  cout<<"Matrix Multiplication by-Tarun Rawat\n";
  again:
  cout<<"\nEnter Row & Column Of Matrix A \n";
  cin>>l>>m;
  cout<<"\nEnter Row & Column Of Matrix B \n";
  cin>>n>>o;
  if(m==n)
   {cout<<"Matrix Can Be Multiply \n";
   }
  else{ cout<<"Incorrect Matrix For Multiplication \n";
       goto again;
       }
  cout<<"\nEnter Elements Of Matrix A: \n";
  for(i=0;i<l;i++)
     { for(j=0;j<m;j++)
 {cin>>a[i][j];
 }
     }
  cout<<"\nEnter Elements Of Matrix B: \n";
    for(i=0;i<n;i++)
       { for(j=0;j<o;j++)
   {cin>>b[i][j];
   }
       }
    for(i=0;i<l;i++)
       {for(j=0;j<o;j++)
  {c[i][j]=0;
     for(k=0;k<m;k++)
 {c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
 }
  }
       }
   cout<<"\nMultiplication Of Matrix A & B:  ";
   for(i=0;i<l;i++)
      {cout<<"\n";
for(j=0;j<o;j++)
   { cout<<c[i][j]<<"  ";
   }
      }
getch();

}