Skip to content
15 changes: 8 additions & 7 deletions Recursion programs/Divisibility by 11 and 9.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
/*Program that tests whether a number is divisible by 11 and 9 or not */

#include<stdio.h>
#include<stdlib.h>
int divisibleBy9(long int x);
int divisibleBy11(long int x);

main( )
int main( )
{
long int num;
printf("Enter the number to be tested : ");
scanf("%ld", &num);

if(divisibleBy9(num))
if((num == 0) || divisibleBy9(labs(num)))
printf("The number is divisible by 9\n");
else
printf("The number is not divisible by 9\n");

if(divisibleBy11(num))
if(divisibleBy11(labs(num)))
printf("The number is divisible by 11\n");
else
printf("The number is not divisible by 11\n");
Expand All @@ -34,12 +35,12 @@ int divisibleBy9( long int n )
sumofDigits += n%10;
n/=10;
}
divisibleBy9(sumofDigits);
return divisibleBy9(sumofDigits);
}/*End of divisibleBy9()*/

int divisibleBy11( long int n )
{
int s1=0, s2=0,diff;
int s1=0, s2=0;

if(n == 0)
return 1;
Expand All @@ -53,7 +54,7 @@ int divisibleBy11( long int n )
s2 += n%10;
n /= 10;
}
diff = s1>s2 ? (s1-s2) : (s2-s1);
divisibleBy11(diff);
return divisibleBy11(labs(s1-s2));
}/*End of divisibleBy11()*/


4 changes: 2 additions & 2 deletions Recursion programs/Even Sum.C
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* Sum of all even numbers in an array */
#include<stdio.h>
int sumEven(int arr[], int size);
main( )
int main( )
{
int arr[6]={1,2,3,4,8,10};
printf("%d\n",sumEven(arr,6));
printf("%d\n",sumEven(arr,sizeof(arr)/sizeof(int)));
}
int sumEven(int arr[], int size)
{
Expand Down
19 changes: 9 additions & 10 deletions Recursion programs/Factorial.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
/*Program to find the factorial of a number by recursive method*/
/*Program to find the factorial of a number by recursive and iterative methods*/

#include<stdio.h>
#include<stdlib.h>
long int fact(int n);
long int Ifact(int n);

main( )
int main( )
{
int num;
printf("Enter a number : ");
scanf("%d", &num);
if(num<0)
if(num<0) {
printf("No factorial for negative number\n");
else
printf("Factorial of %d is %ld\n", num, fact(num) );
exit(1);
}

if(num<0)
printf("No factorial for negative number\n");
else
printf("Factorial of %d is %ld\n", num, Ifact(num) );
printf("Factorial of %d is %ld\n", num, fact(num) );

printf("Factorial of %d is %ld\n", num, Ifact(num) );
}/*End of main()*/

/*Recursive*/
Expand All @@ -39,4 +39,3 @@ long int Ifact(int n)
}
return fact;
}/*End of ifact()*/

11 changes: 8 additions & 3 deletions Recursion programs/Input and add n numbers.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
/* Input and add n numbers*/
#include<stdio.h>
#include<stdlib.h>
int InputAndAdd(int n);
main()
int main()
{
int n;
printf("Enter n :");
scanf("%d",&n);
if (n < 1)
{
printf("Input must be a positive integer\n");
exit(1);
}
printf("%d\n",InputAndAdd(n));
}
int InputAndAdd(int n)
{
int a;
printf("Enter a number : ");
scanf("%d",&a);
scanf("%d",&a);
if (n == 1)
return a;
else
return a + InputAndAdd(n-1);
}

16 changes: 3 additions & 13 deletions Recursion programs/Number in Words.C
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include<stdio.h>
void f(int n);

const char* words[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

int main()
{
int num = 12340;
Expand All @@ -16,17 +18,5 @@ void f(int n)

f(n/10);

switch(n%10)
{
case 0: printf("zero ");break;
case 1: printf("one ");break;
case 2: printf("two ");break;
case 3: printf("three ");break;
case 4: printf("four ");break;
case 5: printf("five ");break;
case 6: printf("six ");break;
case 7: printf("seven ");break;
case 8: printf("eight ");break;
case 9: printf("nine ");break;
}
printf("%s ", words[n%10]);
}
23 changes: 19 additions & 4 deletions Recursion programs/Reverse of text line.C
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
/* Enter a line of text and Reverse it*/
#include<stdio.h>
#include<stdlib.h>

void func(void);
main( )

int main( )
{
printf("Enter text :\n");
func();
printf("\n");
}/*End of main()*/

void func(void)
{
char c;
if((c=getchar())!='\n')
int c;

c = getchar();
switch (c)
{
case EOF:
case '\n':
break;
default:
func();
putchar(c);
break;
}

if (c != EOF)
putchar(c);
}
10 changes: 8 additions & 2 deletions Sorting/Sort Bubble.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
/*Program of sorting using bubble sort*/

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

main()
int main()
{
int arr[MAX],i,j,temp,n,xchanges;

printf("Enter the number of elements : ");
scanf("%d",&n);

if(n > MAX)
{
printf("number of elements must not exceed %d\n", MAX);
exit(1);
}

for(i=0; i<n; i++)
{
printf("Enter element %d : ",i+1);
Expand Down Expand Up @@ -39,4 +46,3 @@ main()
printf("%d ",arr[i]);
printf("\n");
}/*End of main()*/

2 changes: 1 addition & 1 deletion Stacks and Queues/Stack using Array.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ main()
display();
break;
case 5:
exit(1);
exit(0);
default:
printf("Wrong choice\n");
}/*End of switch*/
Expand Down
2 changes: 1 addition & 1 deletion Stacks and Queues/Stack using Linked List.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ main()
display();
break;
case 5:
exit(1);
exit(0);
default :
printf("Wrong choice\n");
}/*End of switch */
Expand Down
50 changes: 41 additions & 9 deletions Trees/IsBST.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* Find whether a binary tree is binary search tree or not*/
/* Find out whether a binary tree is a binary search tree or not*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
#include<unistd.h>
#define SLEN 5

struct node
{
Expand All @@ -15,41 +18,66 @@ void inorder(struct node *ptr);
struct node *Node(int item);
int IsBST(struct node *ptr, int MIN, int MAX);

main( )
int main( )
{
struct node *root1, *root2;

struct node *root1, *root2, *root3;
char str[SLEN];

srandom(getpid());

/* BST */
root1 = Node(32);
root1->lchild = Node(23);
root1->rchild = Node(36);
root1->lchild->rchild = Node(25);
root1->rchild->lchild = Node(33);

/* not a BST */
root2 = Node(42);
root2->lchild = Node(60);
root2->rchild = Node(19);
root2->lchild->rchild = Node(36);
root2->rchild->lchild = Node(41);

/* random test data */
root3 = Node(random() % INT_MAX);
root3->lchild = Node(random() % INT_MAX);
root3->rchild = Node(random() % INT_MAX);
root3->lchild->rchild = Node(random() % INT_MAX);
root3->rchild->lchild = Node(random() % INT_MAX);

display(root1,1);
printf("\n\n");
inorder(root1);
printf("\n\n");

if( IsBST(root1,INT_MIN,INT_MAX) )
printf("Tree 1 is a BST\n");
if( IsBST(root1,INT_MIN,INT_MAX) )
str[0] = '\0';
else
printf("Tree 1 is not a BST\n");
strncpy(str, "not ", SLEN);
printf("Tree 1 is %sa BST\n", str);

display(root2,1);
printf("\n\n");
inorder(root2);
printf("\n\n");

if( IsBST(root2,INT_MIN,INT_MAX) )
printf("Tree 2 is a BST\n");
str[0] = '\0';
else
printf("Tree 2 is not a BST\n");
strncpy(str, "not ", SLEN);
printf("Tree 2 is %sa BST\n", str);

display(root3,1);
printf("\n\n");
inorder(root3);
printf("\n\n");

if( IsBST(root3,INT_MIN,INT_MAX) )
str[0] = '\0';
else
strncpy(str, "not ", SLEN);
printf("Tree 3 is %sa BST\n", str);
}/*End of main( )*/

int IsBST(struct node *ptr, int MIN, int MAX)
Expand All @@ -64,6 +92,10 @@ int IsBST(struct node *ptr, int MIN, int MAX)
struct node *Node(int item)
{
struct node* tmp = (struct node *)malloc(sizeof(struct node));
if (tmp == NULL) {
perror("malloc");
exit(1);
}
tmp->info = item;
tmp->lchild = tmp->rchild = NULL;
return tmp;
Expand Down