Wednesday, July 2, 2014

Space Program

Space Program: Asked me in AthenaHealth, Novell Software development, Aspire Systems
/* Input: abcd
    Output: abcd
                a bcd
                ab cd
                abc d
                a b cd
                a bc d
                ab c d
                a b c d
*/
#include<stdio.h>
void space(char *,int,char *,int);
main()
{
char a[10],b[10];
scanf("%s",a);
space(a,0,b,0);
return 0;
}
void space(char *a,int x,char *b,int y)
{
if(a[x]=='\0')
{
b[y]='\0';
printf("%s",b);
return;
}
b[y]=a[x];
space(a,x+1,b,y+1);
if(y>0 && b[y-1]!=' ')
{
b[y]=' ';
space(a,x,b,y+1);
}
}

Square Root Of A Number Without Using Library Function sqrt()

#include<stdio.h>
float mysqrt(float);
main()
{
int n=36;
printf("%d",mysqrt(n));
return 0;
}
float mysqrt(float n)
{
float prev=0,cur=1;
while(prev!=cur)
{
prev=cur;
cur=0.5*(prev+(n/prev));
}
return cur;
}

Strong Number

/* StrongNumber: Sum of factorial of digits in a number is equal to the number
i.e. 145 = 1 ! + 4 ! + 5 !
     145 = 1 + 24 + 125
     145 = 145
*/
#include<stdio.h>
int fact(int);
main()
{
int n,s,i;
for(i=0;i<200000;i++)
{
n=i;
s=0;
while(n)
{
s+=fact(n%10);
n/=10;
}
if(s==i)
printf("Strong Number:%d\n",i);
}
return 0;
}
int fact(int a)
{
if(a==0)
return 1;
else
return a*fact(a-1);
}