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

}

Convert Binary to Decimal or Decimal to Binary

C++ Program to convert Binary number to Decimal number or vice versa.This program converts either binary number entered by user to decimal number or decimal number entered by user to binary number in accordance with the character entered by user.This program asks user to enter alphabet 'b' to convert
decimal number to binary and alphabet 'd' to convert binary number to decimal.
In accordance with the character entered, user is asked to enter either binary value to convert to decimal or decimal value to convert to binary.To perform conversion, two functions are made decimal_binary(); to convert decimal to binary and binary_decimal(); to convert binary to decimal. Decimal number entered by user is passed to decimal_binary() and this function computes the binary value of that number and returns it main() function. Similarly, binary number is passed to function binary_decimal() and this function computes decimal value of that number and returns it to main() function.

Source Code:-

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

int binary_decimal(int n);
int decimal_binary(int n);
int main()
{  clrscr();
   int n;
   char c;
   cout<<"Convet Decimal to Binary & vice versa by-Tarun Rawat\n\n";
   cout<<"1. Enter alphabet 'd' to convert binary to decimal.\n";
   cout<<"2. Enter alphabet 'b' to convert decimal to binary.\n";
   cin>>c;
   if (c =='d' || c == 'D')
   {   cout<<"\nEnter a binary number : ";
       cin>>n;
       cout<<n<<" in binary = "<<binary_decimal(n)<<" in decimal";
   }
   if (c =='b' || c == 'B')
   {   cout<<"\nEnter a decimal number : ";
       cin>>n;
       cout<<n<<" in decimal = "<<decimal_binary(n)<<" in binary";
   }
getch();
}

int decimal_binary(int n)  /* Function to convert decimal to binary.*/
{   int rem, i=1, binary=0;
    while (n!=0)
    {   rem=n%2;
    n/=2;
    binary+=rem*i;
    i*=10;
    }
    return binary;
}

int binary_decimal(int n) /* Function to convert binary to decimal.*/
{  int decimal=0, i=0, rem;
    while (n!=0)
    {   rem = n%10;
    n/=10;
    decimal += rem*pow(2,i);
    ++i;
    }
    return decimal;
}

Saturday, 15 March 2014

Check number is Even or Odd

C++ Program to check the number is Even or Odd:Numbers perfectly divisible by 2 are known even numbers and numbers which are not divisible by 2 are called odd numbers.This program takes an integer from user and checks whether that number is even or odd and displays the result.In this program, user is asked to enter an integer which is stored in variable a. Then, the remainder is found when that number is divided by 2 and checked whether remainder is 0 or not.
If remainder is 0 then, that number is even otherwise that number is odd. This task is performed using if...else statement in C programming and the result is displayed accordingly. 


Source Code:-

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

void main()
{clrscr();
 int a;
 cout<<"Check Number is Even or Odd by-Tarun Rawar\n";
 cout<<"\nEnter number to  check : ";
 cin>>a;
 if(a%2==0)
  cout<<"\n"<<a<<" is a Even number ";
 else cout<<"\n"<<a<<" is a Odd number ";
getch();
}


check Armstrong number

 C++ Program to check the input number is Armstrong number or not.
Armstrong number c++ program:  A number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are: 0, 1, 153, 370, 407. 


Source Code:-


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

void main()
{ clrscr();
  int temp,num,sum=0,r;
  cout<<"Program to check Armstrong condition by-Tarun Rawat\n\n";
  cout<<"Enter a number : ";
  cin>>num;
  temp=num;
  while(num!=0)
       {r=num%10;
       num=num/10;
       sum=sum+(r*r*r);
       }
  if(sum==temp)
    cout<<endl<<temp<<" is a Armstrong number. \n";
  else cout<<endl<<temp<<" is not a Armstrong number./n";
getch();
}