Monday, January 7, 2013

Program to Convert Decimal to Roman Number



C/C++ Program to Convert Decimal to Roman Number



#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int a;

     cout<<"      \" This program will convert decimal number into roman number. \""<<endl<<endl;

     xx:
    cout<<"What is the Decimal number ?. Give a integer number."<<endl;
    cin>>a;

    if(a<0) {
    cout<<"Negative number is not allow.Please give a integer number."<<endl;
    goto xx;
    }

     cout<<endl;
     cout<<"You have given decimal "<<a<<endl;
     cout<<"In Roman number is : ";

     while(a>=1000){
    cout<<"M";
    a=a-1000;
    }

     while(a>=500){
    cout<<"D";
    a=a-500;
    }

     while(a>=100){
    cout<<"C";
    a=a-100;
    }

     while(a>=50){
    cout<<"L";
    a=a-50;
    }

     while(a>=10){
    cout<<"X";
    a=a-10;
    }

     if((a>=5) && (a<9)){
    cout<<"V";
    a=a-5;
    }

     switch(a){
     case 0 :
        break;

     case 1 : cout<<"I"  ;
        break;

     case 2 : cout<<"II" ;
        break;

     case 3 : cout<<"III";
        break;

     case 4 : cout<<"IV" ;
        break;

     case 9 : cout<<"IX" ;
        break;
     }

getch();
}




For more please visit cybarlab.com

Program for Case Conversion



C/C++ Program for Case Conversion



#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
int main(){
clrscr();
int i,j,n,k;
char *s;

      cout<<" \" This program will convert upper case to lower or vice virsa. \""<<endl<<endl;

      cout<<"What is the string ?. Give a String/Sentence. :"<<endl;
      gets(s);

      k=strlen(s);
      cout<<endl<<"The string that you have given is :"<<endl;
      cout<<s;

      cout<<endl<<"After case conversion it is :"<<endl;

      for(i=0;i<=k;i++) {
      n=int(s[i]);

      if(n==32) {
      s[i]=char(n);
      cout<<s[i];
      }

      else if((n>=48) && (n<=57)){
      s[i]=char(n);
      cout<<s[i];
      }

      else if((n>=97) && (n<=122)) {
      n=n-32;
      s[i]=char(n);
      cout<<s[i];
      }

      else if((n>=65) && (n<=90)) {
      n=n+32;
      s[i]=char(n);
      cout<<s[i];
      }
      }

getch();
}

Program to Calculate Are of a Triangle



C/C++ Program to Calculate Are of a Triangle



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

int main(){
clrscr();
float b,h,x;

    cout<<"             \" This program will show area of a Triangle. \""<<endl<<endl;

    cout<<"What is the Base ?. Give a number."<<endl;
       cin>>b;
    cout<<"What is the Hight ?. Give a number."<<endl;
       cin>>h;
       x=(b*h)/2;

    cout<<endl;
    cout<<"You have given Base   :"<<b<<endl;
    cout<<"You have given Hight  :"<<h<<endl;
    cout.precision(2);
    cout<<"So area of this Triangle :"<<x;

getch();
}

Sunday, January 6, 2013

Program for Binary Search



C/C++ Program for Binary Search



#include<iostream.h>
#include<conio.h>
int main(){
clrscr();
int n,i,beg,end,mid,data[20],loc,item;

     cout<<"     \" This program will performs Binary search. \""<<endl<<endl;

     xx:
    cout<<"How many numbers ?. Give a integer number."<<endl;
    cin>>n;

    if(n<0) {
    cout<<"Negative number is not allow, Please give a integer number"<<endl<<endl;
    goto xx;
    }

    else if(n==0) {
    cout<<"0 is not allow"<<endl<<endl;
    goto xx;
    }

     yy:
    cout<<"What are the elements ?. Give some sorted data"<<endl;
    for(i=1;i<=n;i++)
    cin>>data[i];

    for(i=1;i<n;i++)
    if(data[i] > data[i+1]) {
    cout<<"Non sorted data is not allow.Please give sorted data."<<endl;
    goto yy;
    }

     cout<<"Give a data that you want to search."<<endl;
     cin>>item;

     beg=1;
     end=n;
     mid=(beg+end)/2;

     while(data[mid]!=item && beg<=end) {
    if(item<data[mid])
    end=mid-1;
    else beg=mid+1;
    mid=(beg+end)/2;
    }

     cout<<endl;
     if(data[mid]==item) {
    loc=mid;
     cout<<item<<" is present here ( ";
     for(i=1;i<=n;i++)
     cout<<data[i]<<" ";
     cout<<") and it's position is : "<<loc;
     }

     else {
     cout<<item<<" is not present here ( ";
     for(i=1;i<=n;i++)
     cout<<data[i]<<" ";
     cout<<")";
     }

getch();
}


For more please visit cybarlab.com