Saturday, 30 March 2013

De-coders: Solutions for 2nd round problem statements.

Q 1.    C program to print the pattern.

                                                                               *
                                                                             *  *
                                                                           *  *  *
                                                                         *  *  *  *
                                                                       *  *  *  *  *

#include<stdio.h>
#include<conio.h>
void main()
{
char x='*';
int i,k,l;
clrscr();
for(i=0;i<6;i++)
{
for(k=0;k<5-i;k++)
printf(" ");
for(l=0;l<i;l++)
printf(" %c",x);
printf("\n");
}
getch();
}
                                     

  Q 2.    C program to revers an integer. 

#include<stdio.h>
#include<conio.h>
void main()
{
int x,n;
clrscr();
printf("Enter the number:");
scanf("%d",&n);

while(n)
{
x=n%10;
printf("%d",x);
n=n/10;
}
getch();
}

 

  Q 3.    C program to solve two liner equations.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,l,m,n;
float x,y;
clrscr();
printf("Enter a ,b, c:");
scanf("%d%d%d",&a,&b,&c);
printf("Enter l ,m, n:");
scanf("%d%d%d",&l,&m,&n);
x=(float)(c*m-n*b)/(m*a-l*b);
y=(float)(c*l-n*a)/(b*l-m*a);
printf("x= %f\ny=%f\n",x,y);
getch();
}

  Q 4.    C program to check whether given number is some power of 2 without using any loop or % operator. 

#include<stdio.h>
#include<conio.h>
int main()
{
int a;
clrscr();
printf("Enter the number:");
scanf("%d",&a);
if(!(a&(a-1)))
printf("It is some power of 2");
else
printf("It is not some power of 2");
getch();
return(0);
}


Q 5.    C program to check whether given string is anagram of first string or not .

#include<stdio.h>
#include<process.h>
#include<conio.h>
#include<string.h>
char S[20];
void swap(char * x,char *y);
void pert(char *a,int i,int n);
void main()
{
char a[20];
int l;
clrscr();
printf("Enter the string:");
scanf("%s",a);
printf("Enter the other string:");
scanf("%s",S);
l=strlen(a);
pert(a,0,l);
printf("NO");
getch();
}

void swap(char *x , char *y)
{
char z;
z=*x;
*x=*y;
*y=z;
}

void pert(char *a,int i,int n)
{
int j;
if(i==n)
 {
    if(strcmp(a,S)==0)
    {
    printf("YESS");
    getch();
    exit(0);
    }
 }

else
  {
    for(j=i;j<n;j++)
    {
    swap((a+i),(a+j));
    pert(a,i+1,n);
    swap((a+i),(a+j));
    }

   }
}
                 

Q 6. C program to print hello world without using semicolon.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
if(printf("Hello word"))
{}
getch();
}



Saturday, 2 March 2013

Program to convert Roman number to Decimal number

#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0;
char c[20];
static int p,r;
clrscr();
printf("Enter the Roman number \n");
gets(c);
for(i=0;c[i]!='\0';i++)
{
switch(c[i])
{
case 'M': sum+=1000,p=1000;break;
case 'D': sum+=500,p=500;break;
case 'C': sum+=100,p=100;break;
case 'L': sum+=50,p=50;break;
case 'X': sum+=10,p=10;break;
case 'V': sum+=5,p=5;break;
case  'I': sum+=1,p=1;break;
default:printf("Invalid entry\n");
getch();
exit(0);
}
if(r<p)
sum=sum-2*r;
r=p;
}
printf("%s is %d",c,sum);
getch();
}

OUT PUT.......?

//CODE 1
#include<iostream>
using namespace std;
int main()
{
float a=0.7;
if(a==0.7)
cout<<"C++\n";
else
cout<<"C\n";
return(0);
}


When we execute the above code we get the o/p as :"C",
But excepted o/p is :"C++" 

//CODE 2
#include<iostream>
using namespace std;
int main()
{
float a=0.7;
if(a==(float)0.7)
cout<<"C++\n";
else
cout<<"C\n";
return(0);
}



When we execute the above code we get the o/p as :"C++",

 Bcoz: Computers use a binary (0's and 1's) system to store decimal numbers. This leads to some inaccuracy, since some decimal values can't be stored exactly in binary....0.7 is stored as the value 0.69999999.Since that value isn't 0.7, the condition fails..

The float function takes care of this problem -
float(0.7)
it rounds the value 0.69999999 to 0.7. Many decimal values are stored accurately in binary, for example 0.5, but many are not.



Tuesday, 26 February 2013

Implementation of STACK in C++


#include<iostream>
using namespace std;
int main()
{
int stack[20],top=-1,ch;
//MAX. SIZE IS 5.
while(1)
{
cout<<"PRESS 1 TO PUSH\nPRESS 2 TO POP\nPRESS 3 TO DISPLAY\nPRESS 4 TO EXIT\n";
cin>>ch;
switch(ch)
{
case 1: if(top==4) 
       cout<<"OVERFLOW\n";
       else
{
         cout<<"ENTER THE ELEMENT TO BE PUSHED:";
         cin>>stack[++top];

break;
case 2:if(top==-1)
       cout<<"UNDERFLOW\n";
else
cout<<"ELEMENT POPED:"<<stack[top--]<<endl;
break;
case 3: if(top==-1)
cout<<"EMPTY STACK\n";
else
{ for(int i=top;i>=0;i--)
 cout<<stack[i]<<"\t";
 cout<<endl;

break;

case 4 :break;
default:cout<<"INVALID ENTRY\n";
}//END OF SWITCH
if(ch==4)
break;
}//END OF WHILE
return(0);
}

Monday, 25 February 2013

C++ program :Array of all prime numbers less then or equal to n (SIEVEN)


#include<iostream>
using namespace std;
int main()
{
int A[20],B[20],i,n,j;
cout<<"Enter the positive intiger:";
cin>>n;
for(i=2;i<=n;i++)
B[i]=i;
for(i=2;i<=n;i++)
{
if(B[i]!=0)
{   j=i*i;
   while(j<=n)
  {  B[j]=0;
     j+=i;
   }
}
}

j=0;
for(i=2;i<=n;i++)

if(B[i])
{
A[j++]=B[i];
cout<<B[i]<<"\t";
}
}
cout<<endl;
return(0);
}

Saturday, 23 February 2013

Swap two numbers

Here a,b are two variables of type int.

Method 1:
                          a=a+b;
                          b=a-b;
                          a=a-b;

Method 2:
                          a=a*b;
                          b=a/b;
                          a=a/b;

Method 3:
                          a=a^b;
                          b=a^b;
                          a=a^b;

Method 4:
                          a=(a+b)-(b=a);


Method 5:
                 /*With temp. variable*/
                          temp=a;
                          a=b;
                          b=temp;


                         
                         
                     

Tuesday, 19 February 2013

A Trick to trace tower of Hanoi manually


  • What procedure suggests:               

         If n=1 move the disk from source to destination.
         Otherwise
(i) Move n-1 disks from source to temp.  peg by using destination peg as  auxiliary.
(ii)Move nth  disk from temp. to destination then move n-1 disks using source peg as c   auxiliary.

  • An alternative for easy manual tracing.
           Construct a tree a shown in fig.
         (I) If n!=1then lftchild ={n-1,S,D,T} and rtchild={n-1,T,S,D}
         Do this till n=1;
         For n=3 you will get tree as shown in fig. to get moves go for inorder traversal of tree,
         ( Move nth disk from source to destination.{n, s, t, d} )  
Inorder Traversal
1. Traverse the left sub tree in inorder.  
2. Visit the root.
3. Traverse the right sub tree in inorder.

Do the Inorder traversal for the above problem   
Move disk 1 from A to C.
Move disk 2 from A to B.
Move disk 1 from C to B.
Move disk 3 from A to C.
Move disk 1 from B to A.
Move disk 2 from B to C.
Move disk 1 from A to C.