Make Your Search Easy ! :) Use me

Friday, August 11, 2017

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

   }

Program to find the greatest number and its frequency from a series of inputs, without storing all the inputs.


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

void main()
{
         int max=0,num,i,j=0,flag=0;
         cout<<"Enter the number of inputs to be entered :";
         cin>>i;
         do
          {
          cout<<"Enter number "<<(j+1)<<"  : ";
            cin>>num;
            if(num>max)
            {
            max=num;
                  flag=0;
               }
            if(num==max)
            {
                flag++;
               }
            j++;
          }while(j<i);
         cout<<"\n\nGreatest number is : "<<max<<endl;
         cout<<"Frequency of greatest number is : "<<flag;
         getch();

   }

Wednesday, August 9, 2017

Program to print the following alphabetical pattern:-

Alphabetical Pattern


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

void main()
    {
      char j,k;
      int h,i;
      for(j='A';j<'H';j++)
      {  cout<<j;      }
      for(k=j-2;k>='A';k--)
        {  cout<<k;      }
      for(i=1;i<8;i++)
      {
          cout<<endl;
      for(j='A';j<'H'-i;j++)
      {
          cout<<j;
         }
            for(h=1;h<2*(i);h++)
            {
                cout<<" ";
               }
            for(k=j-1;k>='A';k--)
            {
                cout<<k;
               }
         }
      getch();

   }

Program to calculate age.


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

struct date
{ int dd,mm,yyyy;
      void get()
      {
          cout<<"Enter Date: (do not enter zero befor significant digit) ";
            cin>>dd;
            cout<<"Enter Month: (do not enter zero befor significant digit) ";
            cin>>mm;
            cout<<"Enter Year: (do not enter zero befor significant digit)";
            cin>>yyyy;
         }
      void show()
      {
          cout<<endl<<yyyy<<" Years "<<mm<<" months "<<dd<<" days";
         }
    };

void calc(date a, date b)
{
    date age;
      if(b.dd>a.dd)
          {
            a.dd+=30;
               a.mm--;
            }
         if(b.mm>a.yyyy)
          {
            a.mm+=12;
               a.yyyy--;
            }
      age.dd=a.dd-b.dd;
      age.mm=a.mm-b.mm;
      age.yyyy=a.yyyy-b.yyyy;
    cout<<"\nYour age is :\n";
      age.show();
   }

void main()
 {
         date dob,today;
         cout<<"Enter today's date :\n";
         today.get();
         cout<<"\n\nEnter you date of birth:\n";
         dob.get();
         calc(today,dob);
         getch();

 }

Triangle related problem

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


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


void main()
 {
        int flag1,flag2,flag3;
              flag1=flag2=flag3=0;
              float angle1,angle2, angle3;
              cout<<"Enter 1st Angle : ";
              cin>>angle1;
              cout<<"Enter 2nd Angle : ";
              cin>>angle2;
              cout<<"Enter 2nd Angle : ";
              cin>>angle3;
              if(angle1+angle2+angle3==180)
              {
                if(angle1==angle2&&angle2==angle3)
                  { cout<<"\n\nGiven angles are of equilateral triangle."; }
                  if(angle1==angle2||angle2==angle3||angle1==angle3)
                     { flag1=1; }
                  if(angle1!=angle2 && angle1!=angle3)
                     { flag2=1; }
                  if(angle1==90||angle2==90||angle3==90)
                  {  flag3=1;}
              if(flag1==flag3)
            { cout<<"\n\nGiven angles are of a right angle isosceles triangle."; }
            if(flag2==flag3)
                  { cout<<"\n\nGiven angles are of a right angle scalene triangle."; }
               }
              else
              { cout<<"\n\nGiven angles are not of a triangle."; }
              getch();

 }

Fibonacci Series

Program to find the Fibonacci value of any given number from 1 to 100.


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

void main()
{
    int num,f[100];
      cout<<"Enter a number to find Fibonacci value : ";
      cin>>num;
      f[0]=0;
      f[1]=1;
      for(int i=2;i<=num;i++)
      { f[i]=f[i-1]+f[i-2];}
      cout<<f[num];
      getch();

   }

Number Conversions

Program to convert decimal numbers into hexadecimal, octal and binary.

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

//function to obtain hexadecimal value
void show_hex(int a)
{
    int decnum=a,i=0,temp;
      char hex[100];
      while(decnum>0)
      {
          temp=decnum%16;
            //to convert the obtained result into characters
            if(temp<10)
            { hex[++i]=temp+48; }
            else
            {  hex[++i]=temp+55; }
            decnum/=16;
         }
      cout<<"\n\nEquivalent Hexadecimal value for "<<a<<" is : ";
      for(i;i>0;i--)
      {
          cout<<hex[i];
         }
   }

//function to obtain octadecimal value
void show_octal(int a)
{
    int decnum=a,i=0,temp;
      char octal[100];
      while(decnum>0)
      {
          temp=decnum%8;
            //to convert the obtained result into characters
            octal[++i]=temp+48;
            decnum/=8;
         }
      cout<<"\nEquivalent Octadecimal value for "<<a<<" is : ";
      for(i;i>0;i--)
      {
          cout<<octal[i];
         }
   }

//function to obtain binary value
void show_binary(int a)
   {
    int decnum=a,i=0,temp;
      char binary[100];
      while(decnum>0)
      {
          temp=decnum%2;
            //to convert the obtained result into characters
            binary[++i]=temp+48;
            decnum/=2;
         }
      cout<<"\nEquivalent binary value for "<<a<<" is : ";
      for(i;i>0;i--)
      {
          cout<<binary[i];
         }
   }

//main program
void main()
{
    int a;
    cout<<"Enter any decimal number :";
      cin>>a;
      show_hex(a);
      show_octal(a);
      show_binary(a);
      getch();
   }