Wednesday, January 19, 2011

pattern






/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/*
* two numbers are entered. Both are of 4 digits.
* For each digit there can be three parts
* ( eg for 1234
* 1. 123 and 4
* 2. 12 and 34
* 3. 1 and 234)
*
* We need to find perfect suare number between first number and second number
* where both parts of any one part (of 3 parts) are also perfect square
*
*/



#include<stdio.h>
#define TRUE 1
#define FALSE 0


int
perfectSquare(int n1)
{

long
i;
int
return_val = FALSE;

if
(n1 == 1)
{

return
TRUE;
}


for
(i = 1; i < n1; i++)
{


if
((i * i) == n1)
{

return_val = TRUE;
}
}


return
return_val;
}


void
main()
{

int
n1, n2;

int
i;
int
temp;
int
div = 1000;

printf("\nEnter first number: ");
scanf("%d", &n1);
printf("\nEnter second number: ");

scanf("%d", &n2);
for
(i = n1; i < n2; i++)
{


div = 1000;
while
(div >= 10)
{


temp = i % div;
if
(perfectSquare(temp) == TRUE)
{


temp = i / div;
if
(perfectSquare(temp) == TRUE)
{


if
(1 == perfectSquare(i))
{

printf("%d ", i);
}
}
}


div = div / 10;
}
}

printf("\n");

}

String pattern






/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for string pattern
*/

/**
* Enter string: Please validate my Input
* Output: Jnpvt my vbljdbtf Plfbsf
*/

/**
* here all the words of a string are printed in reverse order (the last word comes first, second last wor comes second and so on)
* here the order of words in the word is not changed.
* whenever we encounter A, E , I , O or U replace it with the next character (eg A with B, E with F
* and so on)
* extra spaces in the program are also ignored
*/




#include<stdio.h>

#include<string.h>

void
main()
{

char
arr[100][100] = {0};

/* this trick also works with the structure
* we can initialize whole structure using this technique
*/

int
x = 0, y = 0;

char
str[100];
int
i = 0;

printf("\nEnter string: ");
gets(str);
/*asusual warnings with gets*/

i = 0;

while
(str[i] != '\0' && i < strlen(str))
{


if
(str[i] == ' ' || str[i] == '\0')
{


while
(str[i] == ' ' && str[i] != '\0')
{


i++;
}
}

else

{

if
(str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U' || str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u')
{


arr[x][y] = str[i] +1 ;
y++;

i++;
}

else

{

arr[x][y] = str[i];

y++;
i++;
}

if
(str[i] == ' ' || str[i] == '\0')
{


arr[x][y] = '\0';
x++;
y = 0;
}

}
}


printf("Output:");
for
(i = x; i >= 0; i--)
{


printf(" %s", arr[i]);
}

printf("\n");

}


pattern






/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for pattern
*/


/**
* input: 18
* output: 21 11 1
*
* 18 = 1 2 3 6 9
* now sum of 1 2 3 6 9 is 21
* 21 = 1 3 7
* now sum of 1 3 7 is 11
* 11 = 1
* // we stop at 1
*
*/



#include<stdio.h>


int
fun(int n)
{

int
sum = 0;

int
i;

for
(i = 1; i < n; i++)
{


if
(n % i == 0)
{

sum = sum + i;
}
}


return
sum;

}


void
main()
{

int
n;

int
temp;
printf("\nEnter number: ");
scanf("%d", &n);

/* n is greater than 0*/

temp = n;
while
(temp > 1)
{


temp = fun(temp);
printf("%d ", temp);
}


printf("\n");
}

pattern








/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for pattern
*/



#include<stdio.h>


void
fun1(int arr[100][100], int n)
{


int
i, j;
for
(i = 0; i < n; i++)
{


for
(j = 0; j < n; j++)
{


printf("%d ", arr[i][j]);
}

for
(j = 0; j < n; j++)
{


printf("* ");
}

for
(j = 0; j < n; j++)
{


printf("%d ", arr[i][j]);
}


printf("\n");
}

}


void
fun2(int arr[100][100], int n)
{


int
i, j;
for
(i = 0; i < n; i++)
{


for
(j = 0; j < n; j++)
{


printf("* ");
}

for
(j = 0; j < n; j++)
{


printf("%d ", arr[i][j]);
}

for
(j = 0; j < n; j++)
{


printf("* ");
}

printf("\n");
}

}


void
main()
{


int
arr[100][100] = {0};
/* this is a trick to assign all the values whether
they are float integer or char this will do all the appropriate assignments.
eg 0 to integer and long, 0.0f to float and null to a character
:refer iso standards
*/

int
n;

int
i, j;
int
count;
printf("Enter the number: ");

/* the number should be between 1 to 9*/

scanf("%d", &n);
count = n;

for
(i = 0; i < n; i++)
{


for
(j = 0; j < n; j++)
{


arr[i][j] = count;
count++;
if
(count > 9)
{


count = 0;
}
}
}

fun1(arr, n);

fun2(arr, n);
fun1(arr, n);


}

Tuesday, January 18, 2011

word combination / string combination








#include<stdio.h>
#include<string.h>
#define MAX 100


void
swap(char* src, char* dst)
{

char
ch = *dst;
*
dst = *src;
*
src = ch;
}


int
permute(char *set, int begin, int end)
{


int
i;
int
range = end - begin;

if
(range == 1)
{

printf("%s\n", set);
}


else


{

for
(i = 0; i < range; i++)
{


swap(&set[begin], &set[begin + i]);

permute(set, begin + 1, end);

swap(&set[begin], &set[begin + i]); /*set back*/

}
}
}


void
main()
{

char
str[MAX];

printf("Enter the string: ");

/*as ussual warnings with gets*/

gets(str);
permute(str, 0, strlen(str));

}

Monday, January 10, 2011

power using recursion / recursive function

/*****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for finding power using recursion
*/


#include<stdio.h>

int
fun(int a,int b)
{


if
(b>1)
{

return
a*fun(a,b-1);
}


else

{

return
a;
}
}


void
main()
{


int
a =3 ;
int
b = 2;

printf("\nEnter a: ");
scanf("%d",&a);
printf("Enter b: ");

scanf("%d",&b);
printf("%d^%d is: %d\n",a,b,fun(a,b));
}

factorial using recursion

/*****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for finding factorial using recursion
*/


#include<stdio.h>

int
fun(int n)
{


if
(n>1)
{

return
n*fun(n-1);
}


else

{

return
1;
}
}


void
main()
{


int
n =3 ;
printf("\nEnter a number to find factorial: ");
scanf("%d",&n);

printf("Factorial is: %d\n",fun(n));
}

infix to prefix with bracket

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for infix to prefix with bracket
*/


#include<stdio.h>
#include<string.h>

#define MAX 100

/*
* this will not catch errors
* eg if you enter (a*b]+c then it will output
*/


/* OP:
* Enter expression: (a*(c+d)+(e+f)*g)/(j+i)
* prefix of entered (infix) expression is:
* /+*a+cd*+efg+ji
*
*/




// personally, i believe that declaring TOP and S here is bad practice

void
main()
{

int
TOP = -1, PTOP = 0;

char
s[MAX];
char
pref[MAX] = {'\0'};

int
pre(char);
int
push(char[], int, char);

int
pop(char *, int *);

char
c[MAX] = {'\0'}, symb, x;

int
i, j;
char
temp;
printf("Enter expression: ");

gets(c);

/*
* gets will give error when compiling on modern compiler
*/


/*
* here i don't have strrev function on my compiler (gcc) so i am doing this manually
* but you can write strrev(c); if your compiler supports it
*/

i = 0;

j = strlen(c) - 1;
while
(i < j)
{


temp = c[i];
c[i++] = c[j];

c[j--] = temp;
}


// x = pop(s,&TOP);
// TOP = push(s,TOP,symb);

for
(i = 0; c[i] != '\0'; i++)
{


symb = c[i];
if
(isalpha(symb) > 0)
{


pref[PTOP] = symb;
PTOP++;
}

else if
(symb == '+' || symb == '-' || symb == '*' || symb == '/' || symb == '\\' || symb == '^' || symb == '$')
{


while
((pre(symb)) < (pre(s[TOP])))
{


x = pop(s, &TOP);
pref[PTOP] = x;

PTOP++;
}

TOP = push(s, TOP, symb);
}


else if
(symb == '}' || symb == ']' || symb == ')')
{


TOP = push(s, TOP, symb);
}

else


{

x = '\0';
while
(x != '}' && x != ']' && x != ')')
{


x = pop(s, &TOP);
if
(x != '}' && x != ']' && x != ')')
{


pref[PTOP] = x;
PTOP++;
}
}
}
}

while
(TOP >= 0)
{


x = pop(s, &TOP);
pref[PTOP] = x;

PTOP++;
}

pref[PTOP] = '\0';




i = 0;
j = strlen(pref) - 1;


while
(i < j)
{

temp = pref[i];

pref[i++] = pref[j];
pref[j--] = temp;
}




printf("\nprefix of entered (infix) expression is:\n%s\n", pref);


// getch(); // win not work on linux
}

int
push(char *s, int top, char ele)
{


int
i;
if
(top >= MAX)
{


printf("\nStack Overflow");
}

else

{

s[++top] = ele;
}


return
top;
}


int
pop(char *a, int *top)
{


if
((*top) >= 0)
{
(*
top) = (*top) - 1;

return
a[(*top) + 1];
}

else

{


printf("Stack underflow\n");
return
0;
}
}


int
pre(char x)
{


int
a;
switch
(x)
{

case
'(':

case
'{':
case
'[':
a = -5;

break
;
case
'+':
case
'-':
a = 2;

break
;
case
'/':
case
'*':
case
'\\':

case
'%':
a = 5;
break
;

case
'$':
case
'^':
a = 10;

break
;
}

return
a;
}

infix to prefix without bracket

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for infix to prefix without bracket
*/


// the actual algorithm is quiet difficult so doing this simple way

/* OP:
* Enter expression: a*b+d/e
* prefix of entered (infix) expression is:
* +*ab/de
*/


// I will be following the BSD style indentation in all the programs because it feels more readable to me


#include<stdio.h>
#include<string.h>

#define MAX 100

void
main()
{


int
TOP = -1, PTOP = 0;
char
s[MAX];

char
pref[MAX] = {'\0'};

int
pre(char);

int
push(char[], int, char);
int
pop(char *, int *);

char
c[MAX] = {'\0'}, symb, x;

char
temp;

int
i, j;

printf("Enter expression: ");
gets(c);


/*
* here i don't have strrev function on my compiler (gcc) so i am doing this manually
* but you can write strrev(c); if your compiler supports it
*/


i = 0;
j = strlen(c) - 1;

while
(i < j)
{

temp = c[i];

c[i++] = c[j];
c[j--] = temp;
}


/*
* gets will give error when compiling on modern compiler
*/


for
(i = 0; c[i] != '\0'; i++) // NULL will give error on iso compiler

{
symb = c[i];
if
(isalpha(symb) > 0)
{


pref[PTOP] = symb;
PTOP++;
}

else

{


while
((pre(symb))<(pre(s[TOP])) && TOP >= 0)
{


x = pop(s, &TOP);
pref[PTOP] = x;

PTOP++;
}

TOP = push(s, TOP, symb);
}
}


while
(TOP >= 0)
{

x = pop(s, &TOP);

pref[PTOP] = x;
PTOP++;
}

pref[PTOP] = '\0';


//strrev(pref);
// use strrev if it is there in your compiler
// if not available then use following code
i = 0;
j = strlen(pref) - 1;



while
(i < j)
{

temp = pref[i];

pref[i++] = pref[j];
pref[j--] = temp;
}





// now print the reverse of the string
printf("\nprefix of entered (infix) expression is:\n %s\n", pref);
// getch(); // win not work on linux

}

int
push(char *s, int top, char ele)
{


int
i;
if
(top >= MAX)
{


printf("\nStack Overflow");
}

else

{

s[++top] = ele;
}


return
top;
}


int
pop(char *a, int *top)
{


if
((*top) >= 0)
{
(*
top) = (*top) - 1;

return
a[(*top) + 1];
}

else

{


printf("Stack underflow\n");
return
0;
}
}


int
pre(char x)
{


int
a;
switch
(x)
{

case
'+':

case
'-':
a = 2;
break
;

case
'/':
case
'*':
case
'\\':

case
'%':
a = 5;
break
;

case
'$':
case
'^':
a = 10;

break
;
}

return
a;
}

infix to postfix with bracket

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for infix to postfix with bracket
*/


#include<stdio.h>
#define MAX 100


/*
* this will not catch errors
* eg if you enter (a*b]+c then it will output ab*c+
*/

/* OP:
* Enter expression: (a*(c+d)+(e+f)*g)/(j+i)
* postfix of entered (infix) expression is:
* acd+*ef+g*+ji+/
*/



// personally, i believe that declaring TOP and S here is bad practice

void
main()
{

int
TOP = -1, PTOP = 0;

char
s[MAX];
char
post[MAX] = {'\0'};

int
pre(char);
int
push(char[], int, char);

int
pop(char *, int *);

char
c[MAX] = {'\0'}, symb, x;

int
i;
printf("Enter expression: ");
gets(c);

/*
* gets will give error when compiling on modern compiler
*/


// x = pop(s,&TOP);
// TOP = push(s,TOP,symb);

for
(i = 0; c[i] != '\0'; i++)
{


symb = c[i];
if
(isalpha(symb) > 0)
{


post[PTOP] = symb;
PTOP++;
}

else if
(symb == '+' || symb == '-' || symb == '*' || symb == '/' || symb == '\\' || symb == '^' || symb == '$')
{


while
((pre(symb)) <= (pre(s[TOP])) && TOP >= 0)
{


x = pop(s, &TOP);
post[PTOP] = x;

PTOP++;
}

TOP = push(s, TOP, symb);
}


else if
(symb == '(' || symb == '[' || symb == '{')
{


TOP = push(s, TOP, symb);
}

else


{

x = '\0';
while
(x != '(' && x != '{' && x != '[')
{


x = pop(s, &TOP);
if
(x != '(' && x != '{' && x != '[')
{


post[PTOP] = x;
PTOP++;
}
}
}
}

while
(TOP >= 0)
{


x = pop(s, &TOP);
post[PTOP] = x;

PTOP++;
}

post[PTOP] = '\0';
printf("\npostfix of entered (infix) expression is:\n%s\n", post);


// getch(); // win not work on linux
}

int
push(char *s, int top, char ele)
{


int
i;
if
(top >= MAX)
{


printf("\nStack Overflow");
}

else

{

s[++top] = ele;
}


return
top;
}


int
pop(char *a, int *top)
{


if
((*top) >= 0)
{
(*
top) = (*top) - 1;

return
a[(*top) + 1];
}

else

{


printf("Stack underflow\n");
return
0;
}
}


int
pre(char x)
{


int
a;
switch
(x)
{

case
'(':

case
'{':
case
'[':
a = -5;

break
;
case
'+':
case
'-':
a = 2;

break
;
case
'/':
case
'*':
case
'\\':

case
'%':
a = 5;
break
;

case
'$':
case
'^':
a = 10;

break
;
}

return
a;
}

infix to postfix without bracket

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for infix to postfix without bracket
*/


#include<stdio.h>
#define MAX 100


void
main()
{

int
TOP = -1, PTOP = 0;

char
s[MAX];
char
post[MAX] = {'\0'};

int
pre(char);
int
push(char[], int, char);

int
pop(char *, int *);

char
c[MAX] = {'\0'}, symb, x;

int
i;
printf("Enter expression: ");
gets(c);

/*
* gets will give error when compiling on modern compiler
*/


for
(i = 0; c[i] != '\0'; i++) // NULL will give error on iso compiler

{
symb = c[i];
if
(isalpha(symb) > 0)
{


post[PTOP] = symb;
PTOP++;
}

else

{


while
((pre(symb)) <= (pre(s[TOP])) && TOP >= 0)
{


x = pop(s, &TOP);
post[PTOP] = x;

PTOP++;
}

TOP = push(s, TOP, symb);
}
}


while
(TOP >= 0)
{

x = pop(s, &TOP);

post[PTOP] = x;
PTOP++;
}

post[PTOP] = '\0';

printf("\nPostfix of entered (infix) expression is:\n %s\n", post);
// getch(); // win not work on linux
}

int
push(char *s, int top, char ele)
{


int
i;
if
(top >= MAX)
{


printf("\nStack Overflow");
}

else

{

s[++top] = ele;
}


return
top;
}


int
pop(char *a, int *top)
{


if
((*top) >= 0)
{
(*
top) = (*top) - 1;

return
a[(*top) + 1];
}

else

{


printf("Stack underflow\n");
return
0;
}
}


int
pre(char x)
{


int
a;
switch
(x)
{

case
'+':

case
'-':
a = 2;
break
;

case
'/':
case
'*':
case
'\\':

case
'%':
a = 5;
break
;

case
'$':
case
'^':
a = 10;

break
;
}

return
a;
}

infix to postfix without bracket

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for infix to postfix without bracket
*/


#include<stdio.h>
#define MAX 100


void
main()
{

int
TOP = -1, PTOP = 0;

char
s[MAX];
char
post[MAX] = {'\0'};

int
pre(char);
int
push(char[], int, char);

int
pop(char *, int *);

char
c[MAX] = {'\0'}, symb, x;

int
i;
printf("Enter expression: ");
gets(c);

/*
* gets will give error when compiling on modern compiler
*/


for
(i = 0; c[i] != '\0'; i++) // NULL will give error on iso compiler

{
symb = c[i];
if
(isalpha(symb) > 0)
{


post[PTOP] = symb;
PTOP++;
}

else

{


while
((pre(symb)) <= (pre(s[TOP])) && TOP >= 0)
{


x = pop(s, &TOP);
post[PTOP] = x;

PTOP++;
}

TOP = push(s, TOP, symb);
}
}


while
(TOP >= 0)
{

x = pop(s, &TOP);

post[PTOP] = x;
PTOP++;
}

post[PTOP] = '\0';

printf("\nPostfix of entered (infix) expression is:\n %s\n", post);
// getch(); // win not work on linux
}

int
push(char *s, int top, char ele)
{


int
i;
if
(top >= MAX)
{


printf("\nStack Overflow");
}

else

{

s[++top] = ele;
}


return
top;
}


int
pop(char *a, int *top)
{


if
((*top) >= 0)
{
(*
top) = (*top) - 1;

return
a[(*top) + 1];
}

else

{


printf("Stack underflow\n");
return
0;
}
}


int
pre(char x)
{


int
a;
switch
(x)
{

case
'+':

case
'-':
a = 2;
break
;

case
'/':
case
'*':
case
'\\':

case
'%':
a = 5;
break
;

case
'$':
case
'^':
a = 10;

break
;
}

return
a;
}