Make Your Search Easy ! :) Use me

Showing posts with label CPP Codes. Show all posts
Showing posts with label CPP Codes. Show all posts

Tuesday, August 29, 2017

Triangle related problem.

Program to accept 3 sides and check if is it of a triangle. If yes, also find type of the triangle.


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

void main()
  {
    float a, b, c, s, tmp, base, h, max;
    cout<<"Enter 1st side : ";
    cin>>a;
    cout<<"Enter 2nd side : ";
    cin>>b;
    cout<<"Enter 3rd side : ";
    cin>>c;
    max=a;
    base=b;
    h=c;
    if(b>max)
     {
          max=b;
  base=a;
  h=c;
}
    else if(c>max)
        { 
   max=c;
   base=a;
       h=b;
      }
    s=(a+b+c)/2;
    tmp=(s-a)*(s-b)*(s-c)*s;
    if(tmp > 0)
        {
  if(a==b && b==c)
    {
      cout<<"\nThe given triangle is a equilateral triangle";
            }
          else if(pow(max,2)==pow(base,2)+pow(h,2))
            {
      if(a==b || b==c || a==c)
        {
  cout<<"\nThe given triangle is a isosceles right angled triangle";
}
      else if(a!=b  && b!=c)
{
  cout<<"\nThe given triangle is a scalene right angled triangle";
}
           }
 else if(a==b || b==c || a==c)
   {
     cout<<"\nThe given triangle is a isoceles triangle";
   }
 else if(a!=b  && b!=c)
   {
     cout<<"\nThe given triangle is a scalene triangle";
   }
      }
    else
      {
        cout<<"\nThe gives sides donot form a triangle.";
      }
    getch();
  }

Sunday, August 27, 2017

Fibonacci Series

Program to find the Fibonacci value of any given number from 0 to infinity.


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

void main()
 {
    double num,f0,f1,f2;
      cout<<"Enter a number to find Fibonacci value : ";
      cin>>num;
      if(num==0)
      {
          cout<<"0";
          getch();
          return;
         }
      if(num==1)
      {
          cout<<"1";
          getch();
          return;
         }
      f0=0;
      f1=1;
      for(int i=2;i<=num;i++)
       { 
         f2=f0+f1;
         f0=f1;
         f1=f2;
       }
      cout<<f2;
      getch();
   }

Tuesday, August 22, 2017

Infix to Postfix

Program to convert an expression from infix notation into postfix.


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>


void main()
{
    char exp[100],inf[100],op[50];
      int len,j=0,k=-1;
      cout<<"Enter the equation : \n";
      gets(exp);
      len=strlen(exp);
      for(int i=0;i<len;i++)
      {
          if(isalnum(exp[i]))
            {
                inf[j]=exp[i];
                  j++;
               }
            else if(!isdigit(exp[i])&&exp[i]!=')')
            {
                  if(exp[i]=='+' || exp[i]=='-')
                  {
                      while(k!=-1 && op[k]!='(')
                        {
                      inf[j]=op[k];
                        j++;
                        k--;
                      }
                        if(op[k]=='(')
                        {
                            k--;
                           }
                     }
                  if(exp[i]=='*'&&op[k]=='/' || exp[i]=='/'&&op[k]=='*')
                  {
                      inf[j]=op[k];
                        j++;
                        k--;
                     }
                  k++;
                op[k]=exp[i];

               }
            else if(exp[i]==')')
            {
                while(op[k]!='(')
                  {

                      inf[j]=op[k];
                        j++;
                        k--;

                     }
                  k--;
               }
         }
      inf[j]='\0';
      cout<<"PostFix Expression is : \t";
      cout<<inf;
      getch();

   }


Thursday, August 17, 2017

Number to Roman Numberals

Program to take numbers (up to 4 digits) as input and give equivalent Roman Numerals as output.


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

void main()
{
      char roman['z'];
    int year,temp;
      cout<<"Enter a year to convert into Roman Numeral: ";
      cin>>year;
      strcpy(roman,"Roman Numeral: ");
      temp=year/1000;
      year%=1000;
      switch(temp)
      {
          case 1: strcat(roman,"M");break;
            case 2: strcat(roman,"MM");break;
            case 3: strcat(roman,"MMM");break;
            case 4: strcat(roman,"MMMM");break;
            case 5: strcat(roman,"MMMMM");break;
            case 6: strcat(roman,"MMMMMM");break;
            case 7: strcat(roman,"MMMMMMM");break;
            case 8: strcat(roman,"MMMMMMMM");break;
            case 9: strcat(roman,"MMMMMMMMM");break;
         }
      temp=year/100;
      year%=100;
      switch(temp)
      {
          case 1: strcat(roman,"C");break;
            case 2: strcat(roman,"CC");break;
            case 3: strcat(roman,"CCC");break;
            case 4: strcat(roman,"CD");break;
            case 5: strcat(roman,"D");break;
            case 6: strcat(roman,"DC");break;
            case 7: strcat(roman,"DCC");break;
            case 8: strcat(roman,"DCCC");break;
            case 9: strcat(roman,"CM");break;
         }
      temp=year/10;
      year%=10;
      switch(temp)
      {
          case 1: strcat(roman,"X");break;
            case 2: strcat(roman,"XX");break;
            case 3: strcat(roman,"XXX");break;
            case 4: strcat(roman,"XL");break;
            case 5: strcat(roman,"L");break;
            case 6: strcat(roman,"LX");break;
            case 7: strcat(roman,"LXX");break;
            case 8: strcat(roman,"LXXX");break;
            case 9: strcat(roman,"XC");break;
         }
      temp=year/1;
      year%=1;
      switch(temp)
      {
          case 1: strcat(roman,"I");break;
            case 2: strcat(roman,"II");break;
            case 3: strcat(roman,"III");break;
            case 4: strcat(roman,"IV");break;
            case 5: strcat(roman,"V");break;
            case 6: strcat(roman,"VI");break;
            case 7: strcat(roman,"VII");break;
            case 8: strcat(roman,"VIII");break;
            case 9: strcat(roman,"IX");break;
         }
      cout<<endl<<roman;
      getch();

   }

Friday, August 11, 2017

ASCII Values and Equivalent Characters

Program to print all ASCII values and equivalent characters.


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

void main()
{
      int i=0;
      cout<<"List of all ASCII values and equivalent characters\n";
      while(i<256)
      {
          cout<<i<<"  ->  "<<(char)i<<endl;
          i++;
         }
    getch();

   }



A complete table of ASCII values and its equivalent characters is given below for reference.




ASCII Table 1
ASCII values and equivalent characters

ASCII Table 2
Extended ASCII values and equivalent characters

Perfect Number

Program to check given input is a perfect number or not.


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

void main()
{
      int num,sum=0,i;
      cout<<"Enter a number to check perfect or not : ";
      cin>>num;
      for(i=1;i<num;i++)
      {
          if(num%i==0)
            { sum+=i; }
         }
      if(num==sum)
      { cout<<endl<<num<<" is a perfect number."; }
      else
      { cout<<endl<<num<<" is not a perfect number."; }
    getch();

   }



OR


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

bool perfect(int a)
{
    int sum=0;
    for(int i=1;i<a;i++)
      {
          if(a%i==0)
            {
                sum+=i;
               }
         }
      if(sum==a)
      {return(1);}
      else
      {return(0);}
   }


void main()
{
    int num;
      cout<<"Enter a number to check is it Perfect or not :";
      cin>>num;
      if(perfect(num)==1)
      {
          cout<<"\nEntered Number is a Perfect Number";
         }
      else
      {
          cout<<"\nEntered Number is not a Perfect Number";
         }
      getch();
   }

Number to Number Name

Program to accept a number as input and print number in words as output. 

(The program has limit from 1 to 9999, which can be edited and increased)

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

void main()
{
      char num_name['z'];
    int num,temp;
      cout<<"Enter the number : ";
      cin>>num;
      strcpy(num_name,"Number Name : ");
if(num>=100)
      {
          temp=num/1000;
            switch(temp)
            {
                case 1: strcat(num_name," One Thousand");break;
                case 2: strcat(num_name," Two Thousand");break;
                case 3: strcat(num_name," Three Thousand");break;
                case 4: strcat(num_name," Four Thousand");break;
                case 5: strcat(num_name," Five Thousand");break;
                case 6: strcat(num_name," Six Thousand");break;
                case 7: strcat(num_name," Seven Thousand");break;
                case 8: strcat(num_name," Eight Thousand");break;
                case 9: strcat(num_name," Nine Thousand");break;
               }
            num%=1000;
         }
if(num>=100)
        {
          temp=num/100;
             switch(temp)
              {
                 case 1: strcat(num_name," One Hundred");break;
                 case 2: strcat(num_name," Two Hundred");break;
                 case 3: strcat(num_name," Three Hundred");break;
                 case 4: strcat(num_name," Four Hundred");break;
                 case 5: strcat(num_name," Five Hundred");break;
                 case 6: strcat(num_name," Six Hundred");break;
                 case 7: strcat(num_name," Seven Hundred");break;
                 case 8: strcat(num_name," Eight Hundred");break;
                 case 9: strcat(num_name," Nine Hundred");break;
                }
             num%=100;
          }
      if(num>=20)
      {
          temp=num/10;
            switch(temp)
            {
                case 2: strcat(num_name," Twenty");break;
                  case 3: strcat(num_name," Thirty");break;
                  case 4: strcat(num_name," Forty");break;
                  case 5: strcat(num_name," Fifty");break;
                  case 6: strcat(num_name," Sixty");break;
                  case 7: strcat(num_name," Seventy");break;
                  case 8: strcat(num_name," Eighty");break;
                  case 9: strcat(num_name," Ninety");break;
               }
            num%=10;
         }
      if(num>=10)
      {
          temp=num%10;
            switch(temp)
            {
                case 1: strcat(num_name," Eleven");break;
                case 2: strcat(num_name," Twelve");break;
                  case 3: strcat(num_name," Thirteen");break;
                  case 4: strcat(num_name," Forteen");break;
                  case 5: strcat(num_name," Fifteen");break;
                  case 6: strcat(num_name," Sixteen");break;
                  case 7: strcat(num_name," Seventeen");break;
                  case 8: strcat(num_name," Eighteen");break;
                  case 9: strcat(num_name," Nineteen");break;
               }
             goto show;
         }
      if(num<10)
      {
          temp=num/1;
            switch(temp)
            {
                case 1: strcat(num_name," One");break;
                case 2: strcat(num_name," Two");break;
                  case 3: strcat(num_name," Three");break;
                  case 4: strcat(num_name," Four");break;
                  case 5: strcat(num_name," Five");break;
                  case 6: strcat(num_name," Six");break;
                  case 7: strcat(num_name," Seven");break;
                  case 8: strcat(num_name," Eight");break;
                  case 9: strcat(num_name," Nine");break;
               }
            num%=1;
         }
      show:
      cout<<num_name;
    getch();

   }

Finding LCM and GCD (HCF)

Menu driven program to obtain LCM or HCF of a given number of inputs.


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


void lcm()
{
    int i,j,n,lcm,lo,hi=1,arg['z'];     //lo= loop operator
      cout<<"\nEnter No. Terms of Which LCM is Desired....(2 or more):\n";
      cin>>n;
      cout<<"\nEnter the Nos. of Which LCM is to be Found:\n";
      for(i=0;i<n;++i)
        {
            cin>>arg[i];//take input of all numbers
         }
      for(i=0;i<n;++i)
        {
            hi=hi*arg[i]; //find the product of all the inputs (highest possibility for LCM)
         }
      for(i=1,lo=arg[0];i<n;++i)
        {
        if(lo<arg[i])
        {
                lo=arg[i];//to find the lowest possibility for LCM
               }
         }
      for(lo;lo<=hi;++lo)
         {
          for(j=0;j<n;++j)
          {
      if(lo%arg[j]!=0) //to check divisibility of each number starting form lowest possibility to highest to confirm LCM
        {
        break;  //even a single no. is indivisible process stops and restarts
          }
               }
    if(j==n)   // complete cyle of j loop means the no. is divisible by all hence LCM.
        {
        cout<<"The LCM is : "<<lo;
            getch();
            exit(0);
        }
      }
 }

/*----------------------------------------------------*/


void gcd()
{
      int i,j,n,gcd,hi,arg['z'];     //lo= loop operator
      cout<<"\nEnter No. Terms of Which LCM is Desired....(2 or more):\n";
      cin>>n;
      cout<<"\nEnter the Nos. of Which LCM is to be Found:\n";
      for(i=0;i<n;++i)
        {
            cin>>arg[i];//take input of all numbers
         }
      for(i=1,hi=arg[0];i<n;++i)
        {
        if(hi>arg[i])
        {
           hi=arg[i];//to find the highest possibility for GCD
           }
         }
      for(i=hi;i>=hi;i--)
        {
           for(j=0;j<n;j++)
              {
                if(arg[j]%i!=0)
                  {
                      break;
                   }
              }
           if(j==n)
              {
                cout<<"The HCF is : "<<i;
                getch();
                exit(0);
              }
         }
   }
/*----------------------------------------------------*/


void main()
{
    int opt;
      menu:
      clrscr();
      cout<<"Choose from one of the option:-\n1.Find LCM <Press 1>\n2.Find GCD <Press 2>\n";
      cin>>opt;
      switch(opt)
      {
          case 1: lcm();break;
            case 2: gcd();break;
          default: goto menu;
         }
      getch();

   }

Thursday, August 10, 2017

Unit Conversion (Length)

Program to convert the distance from kilometer into meter, feets, inches, and centimeters.


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

void main()
{
    float dist,dist_m,dist_ft,dist_in,dist_cm;
      cout<<"\t\t\tUNIT CONVERTER"<<endl;
      cout<<"Enter distance (in KM) : ";
      cin>>dist;
      dist_m=dist*1000;
      dist_ft=dist*(100000/30.48);
      dist_in=dist_ft*12;
      dist_cm=dist_m*100;
      cout<<"\n\nDistance in Meters is : "<<dist_m<<" m ";
      cout<<"\nDistance in Feets is : "<<dist_ft<<" feets ";
      cout<<"\nDistance in Inches is : "<<dist_in<<" inches ";
      cout<<"\nDistance in Centimeters is : "<<dist_cm<<" cm ";
      getch();

   }

Program to find out a minimum number of notes and coins of various denominations needed for given amount.


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

void main()
{
   int amount, note2000, note500, note100, note50, note20, note10, coin5, coin2, coin1;
         note2000=note500=note100=note50=note20=note10=coin5=coin2=coin1=0;

         cout<<"Enter the amount :";
         cin>>amount;
         if(amount>=2000)
          {
          note2000=amount/2000;
            amount%=2000;
          }
         if(amount>=500)
            {
            note500=amount/500;
                  amount%=500;
               }
         if(amount>=100)
            {
                note100=amount/100;
                  amount%=100;
               }
         if(amount>=50)
            {
                note50=amount/50;
                  amount%=50;
               }
         if(amount>=20)
            {
                note20=amount/20;
                  amount%=20;
               }
         if(amount>=10)
            {
                note10=amount/10;
                  amount%=10;
               }
//Remove the coin section if only notes is needed

         if(amount>=5)
            {
                coin5=amount/5;
                  amount%=5;
               }
         if(amount>=2)
            {
                coin2=amount/2;
                  amount%=2;
               }
         if(amount>=1)
            {
                coin5=amount/1;
                  amount%=1;
               }
         cout<<"\n\nMinimum No. of notes requeired for "<<amount<<" is : "<<endl;
         cout<<"\nNo. of Rs 2000 Note : "<<note2000;
         cout<<"\nNo. of Rs 500 Note : "<<note500;
         cout<<"\nNo. of Rs 100 Note : "<<note100;
         cout<<"\nNo. of Rs 50 Note : "<<note50;
         cout<<"\nNo. of Rs 20 Note : "<<note20;
         cout<<"\nNo. of Rs 10 Note : "<<note10;

//Remove the following 3 statements if only notes is needed

         cout<<"\nNo. of Rs 5 coin : "<<coin5;
         cout<<"\nNo. of Rs 2 Coin : "<<coin2;
         cout<<"\nNo. of Rs 1 Coin : "<<coin1;
         cout<<"\nTotal No. of Notes : "<<(note2000+note500+note100+note50+note20+note10);
         cout<<"\nTotal No. of Coins : "<<(coin5+coin2+coin1);
         getch();

   }