Monday, September 24, 2012

Insertion sort in c++

#include<iostream.h>

void main()
{
    int i,j,k,tmp;
    //take input from user
    cout<<"Enter size of array\n";
    cin>>k;
    int a[100];
    cout<<"Enter integer in array \n";
    for(i=0;i<k;i++)
    cin>>a[i];
   
    //sorting program start
    for(i=1;i<k;i++)
    {
        tmp=a[i];
        j=i-1;


        while(j>=0)
        {
            if(a[j]<tmp)
                break;
            a[j+1]=a[j];
            j--;
        }
        a[j+1]=tmp;
    }
    //output of sorted array
    for(i=0;i<k;i++)
        cout<<a[i]<<"   ";
    cout<<endl;
}

Thursday, September 13, 2012

Guss game in c++

#include<iostream.h>
#include<time.h>
#include<stdlib.h>
int main ()
{
    int guess , secret , i;
    char ans;
    bool flag;
    do
    {
        flag=true;
        i=0;
        srand(time(0));
        secret=rand()%11;

  
        cout<<secret;
        while(i<3 && flag==true)
    {
    cout<<"Enter your guess from 0 to 10:";
    cin>>guess;

    {
    if(guess>secret)
  
        cout<<"Your guess is too big "<<endl;


    else if (guess<secret)
  
        cout<<"Your guess is too samll  "<<endl;

  
    else
    {
        cout<<"Congratulations !! Your guess is right "<<endl;
        flag=false ;
    }
    }
    i++;
  

    }
      
    cout<<"Do you want to play again [y/n] ";
    cin>>ans ;
    }
    while (ans=='y' || ans=='Y');
    return 0;
}

calculator code in c++ without using + , - , x , / and % operator

#include<iostream.h>
int main ()
{
    char ch;
    do
    {
int a,b,result=0,count=0,num,c=0,d=0;
char op;
do
{
cout<<"\t      \"Enter an operator from following options\" "<<endl;
cout<<"\t\t    Enter '!' to Invert sign"<<endl;
cout<<"\t\t    Enter '+' for Addition"<<endl;
cout<<"\t\t    Enter '-' for Subtraction"<<endl;
cout<<"\t\t    Enter '*' for Multiplication"<<endl;
cout<<"\t\t    Enter '/' for Division"<<endl;
cout<<"\t\t    Enter '%' for Remainder"<<endl;
cout<<endl<<"Please enter your option from list given above : ";
cin>>op;
if(op!='!' && op!='+' && op!='-' && op!='*' && op!='/' && op!='%')
cout<<endl<<endl<<"\a\"Please enter a valid character\" "<<endl<<endl;
}
while(op!='!' && op!='+' && op!='-' && op!='*' && op!='/' && op!='%');
switch (op)
{
case '!':
    cout<<endl<<"Enter a number to invert the sign :";
    cin>>a;
    if(a>0)
    {
        while(a)
        {
            result--;
            a--;
        }
        cout<<"The inverted number is "<<result<<endl;
    }
    else if (a<0)
    {
        while(a)
        {
            result++;
            a++;
        }
        cout<<"The inverted number is "<<result<<endl;
    }
    break ;
case '+':
    cout<<endl<<"Enter first number :";
    cin>>a;
    cout<<"Enter second number :";
    cin>>b;
    if(b==0)
        cout<<endl<<"The sum is "<<a<<endl;
    /*else if (a==0)
        cout<<endl<<"The sum is "<<b<<endl;*/
    if(b>0)
    {
        while (b)
        {
            a++;
            b--;
        }
        cout<<endl<<"The sum is "<<a<<endl;
    }
    else if( b<0)
    {
        while (b)
        {
            a--;
            b++;
        }
        cout<<endl<<"The sum is "<<a<<endl;
    }
    break;
case '-':
    cout<<endl<<"Enter first number :";
    cin>>a;
    cout<<"Enter second number :";
    cin>>b;
    if(b==0)
        cout<<endl<<"The difference is "<<a<<endl;
    if( b>0)
    {
        while (b)
        {
            a--;
            b--;
        }
        cout<<endl<<"The difference is "<<a<<endl;
    }
    else if( b<0)
    {
        while (b)
        {
            a++;
            b++;
        }
        cout<<endl<<"The difference is "<<a<<endl;
    }
    break;
case '*':
    cout<<endl<<"Enter first number :";
    cin>>a;
    cout<<"Enter second number :";
    cin>>b;
    if(a>=0 && b>=0)
    {
        for(a;a>0;a--)
        {
            num=b;
            for(;num>0;num--)
            {
                result++;
            }
        }
        cout<<endl<<"The product is "<<result<<endl;
    }
    else if (a<0 && b<0)
    {
        while(a)
        {
            c++;
            a++;
        }
        while(b)
        {
            d++;
            b++;
        }
        for(c;c>0;c--)
        {
            num=d;
            for(;num>0;num--)
            {
                result++;
            }
        }
        cout<<endl<<"The product is "<<result<<endl;
    }
    else if (a>=0 && b<0)
    {
        while(b)
        {
            d++;
            b++;
        }
        for(a;a>0;a--)
        {
            num=d;
            for(;num>0;num--)
            {
                result++;
            }
        }
        while(result)
        {
            result--;
            c--;
        }
        cout<<endl<<"The product is "<<c<<endl;
    }
    else if (a<0 && b>=0)
    {
        while(a)
        {
            c++;
            a++;
        }
        for(c;c>0;c--)
        {
            num=b;
            for(;num>0;num--)
            {
                result++;
            }
        }
        while(result)
        {
            result--;
            d--;
        }
        cout<<endl<<"The product is "<<d<<endl;
    }
    break;
case '/':
    cout<<endl<<"Enter first number :";
    cin>>a;
    cout<<"Enter second number :";
    cin>>b;
    if(b==0)
        cout<<endl<<"Division is not possible "<<endl;
    else if(a==0)
        cout<<endl<<"The quotient is 0 "<<endl;
    if(a>0 && b>0)
    {
    while (a)
    {
        num=b;
        while(num>0 && a>=num)
        {
            num--;
            a--;
        }
        if (a>0 && a<num)
        {
            a=0;
            count--;
        }
        count++;
    }
    cout<<endl<<"The quotient is "<<count<<endl;
    }
    else if (a<0 && b>0)
    {
       
        while(a)   
        {       
            c++;       
            a++;       
        }
        while(c)
        {
        num=b;   
        while(num>0 && c>=num)
        {
            num--;
            c--;
        }
        if (c>0 && c<num)
        {
            c=0;
            count--;
        }
        count++;
        }
        while(count)
        {
            count--;
            d--;
        }
    cout<<endl<<"The quotient is "<<d<<endl;
    }
    else if (a>0 && b<0)
    {
       
        while(b)   
        {       
            d++;       
            b++;       
        }
        while(a)
        {
        num=d;   
        while(num>0 && a>=num)
        {
            num--;
            a--;
        }
        if (a>0 && a<num)
        {
            a=0;
            count--;
        }
        count++;
        }
        while(count)
        {
            count--;
            c--;
        }
    cout<<endl<<"The quotient is "<<c<<endl;
    }
    else if (a<0 && b<0)
    {
       
        while(b)   
        {       
            d++;       
            b++;       
        }
        while(a)   
        {       
            c++;       
            a++;       
        }
        while(c)
        {
        num=d;   
        while(num>0 && c>=num)
        {
            num--;
            c--;
        }
        if (c>0 && c<num)
        {
            c=0;
            count--;
        }
        count++;
        }
       
       
    cout<<endl<<"The quotient is "<<count<<endl;
    }
    break;
case '%':
    cout<<endl<<"Enter first number :";
    cin>>a;
    cout<<"Enter second number :";
    cin>>b;
    if(b==0)
        cout<<endl<<"Division is not possible "<<endl;
    else if(a==0)
        cout<<endl<<"The remainder is "<<b<<endl;
    if(a>0 && b>0)
    {
    while (a)
    {
        num=b;
        while(num>0 && a>=num)
        {
            num--;
            a--;
        }
        if (a>0 && a<num)
        {
            a=0;
            count--;
        }
        if(a<b)
            break;
    }
        cout<<endl<<"The remainder is "<<a<<endl;
    }
    if(a<0 && b>0)
    {
        while(a)
        {
            c++;
            a++;
        }
    while (c)
    {
        num=b;
        while(num>0 && c>=num)
        {
            num--;
            c--;
        }
        if (c>0 && c<num)
        {
            c=0;
            count--;
        }
        if(c<b)
            break;
    }
    while(c)
        {
            d++;
            c++;
        }
        cout<<endl<<"The remainder is "<<d<<endl;
    }
    if(a>0 && b<0)
    {
        while(b)
        {
            c++;
            b++;
        }
    while (a)
    {
        num=c;
        while(num>0 && a>=num)
        {
            num--;
            a--;
        }
        if (a>0 && a<num)
        {
            a=0;
            count--;
        }
        if(a<c)
            break;
    }
        cout<<endl<<"The remainder is "<<a<<endl;
    }
        if(a<0 && b<0)
    {
        while(b)
        {
            d++;
            b++;
        }
        while(a)
        {
            c++;
            a++;
        }
    while (c)
    {
        num=d;
        while(num>0 && c>=num)
        {
            num--;
            c--;
        }
        if (c>0 && c<num)
        {
            c=0;
            count--;
        }
        if(c<d)
            break;
    }    while(c)
        {
            result--;
            c--;
        }
        cout<<endl<<"The remainder is "<<result<<endl;
    }
    break;
}
cout<<endl<<"Do you want to calculate again [y/n] ";
cin>>ch;
}
while(ch=='y' || ch=='Y');
cout<<endl<<endl<<"Take Care ... Goodbye :)"<<endl;
return 0;
}

Monday, September 10, 2012

Comparing four number in javascript.

<html>



<body>

<script type="text/javascript">


a=prompt("Enter Number1: ");
b=prompt("Enter Number2: ");
c=prompt("Enter Number3: ");
d=prompt("Enter Number4: ");
a=parseInt(a);
b=parseInt(b);
c=parseInt(c);
d=parseInt(d);

if (a>b && a>c && a>d)
{
document.write(+a+"<b> Is Greatest Number</b><br/>");
}
else 
if (a>b && a>c || a>c && a>d || a>b && a>d)
{
document.write(+a+"<b> Is at Second Mid</b><br/>");
}
else
if (a>b || a>c || a>d)
{
document.write(+a+"<b>Is at First Mid </b><br/>");
}
else
{
document.write(+a+"<b>Is Least Number</b><br/>");
}



if (b>a && b>c && b>d)
{
document.write(+b+"<b> Is Greatest Number</b><br/>");
}
else
if (b>a && b>c || b>c && b>d || b>d && b>d)
{
document.write(+b+"<b>Is at Second Mid Position</b><br/>");
}
else
if (b>a || b>c || b>d)
{
document.write(+b+"<b>Is at First Mid Position</b><br/>");
}
else
{
document.write(+b+"</b>Is Least Number</b><br/>");
}



if (c>a && c>b && c>d)
{
document.write(+c+"<b>Is Greatest Number</b> <br/>");
}
else
if (c>a && c>b || c>b && c>d || c>a && c>d)
{
document.write(+c+"<b>Is at Second Mid Position</b><br/>");
}
else
if (c>a || c>b || c>d)
{
document.write(+c+"<b>Is at First Mid Position</b><br/>");
}
else
{
document.write(+c+"<b>Is Least Number </b><br/>");
}



if (d>a && d>c && d>b)
{
document.write(+d+"<b>Is Greatest Number</b> <br/>");
}
else
if (d>a && d>b || d>b && d>c || d>a && d>c)
{
document.write(+d+"<b>Is at Second Mid Position</b><br/>");
}
else
if (d>a || d>b || d>c)
{
document.write(+d+"<b>Is at First Mid Position</b><br/>");
}
else
{
document.write(+d+"<b>Is Least Number</b><br/>");
}


</script>

</body>

</html>