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.