diff --git a/Recursion programs/Divisibility by 11 and 9.c b/Recursion programs/Divisibility by 11 and 9.c index 3ac77f1..db985ef 100644 --- a/Recursion programs/Divisibility by 11 and 9.c +++ b/Recursion programs/Divisibility by 11 and 9.c @@ -1,21 +1,22 @@ /*Program that tests whether a number is divisible by 11 and 9 or not */ #include +#include 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"); @@ -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; @@ -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()*/ + diff --git a/Recursion programs/Even Sum.C b/Recursion programs/Even Sum.C index badc4eb..fcb5210 100644 --- a/Recursion programs/Even Sum.C +++ b/Recursion programs/Even Sum.C @@ -1,10 +1,10 @@ /* Sum of all even numbers in an array */ #include 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) { diff --git a/Recursion programs/Factorial.c b/Recursion programs/Factorial.c index 4ff1776..14e684a 100644 --- a/Recursion programs/Factorial.c +++ b/Recursion programs/Factorial.c @@ -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 +#include 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*/ @@ -39,4 +39,3 @@ long int Ifact(int n) } return fact; }/*End of ifact()*/ - diff --git a/Recursion programs/Input and add n numbers.c b/Recursion programs/Input and add n numbers.c index e58b548..670171a 100644 --- a/Recursion programs/Input and add n numbers.c +++ b/Recursion programs/Input and add n numbers.c @@ -1,21 +1,26 @@ /* Input and add n numbers*/ #include +#include 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); } - diff --git a/Recursion programs/Number in Words.C b/Recursion programs/Number in Words.C index b0c3716..96d690c 100644 --- a/Recursion programs/Number in Words.C +++ b/Recursion programs/Number in Words.C @@ -3,6 +3,8 @@ #include void f(int n); +const char* words[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; + int main() { int num = 12340; @@ -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]); } diff --git a/Recursion programs/Reverse of text line.C b/Recursion programs/Reverse of text line.C index c92aeaa..1bc52ac 100644 --- a/Recursion programs/Reverse of text line.C +++ b/Recursion programs/Reverse of text line.C @@ -1,16 +1,31 @@ /* Enter a line of text and Reverse it*/ #include +#include + 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); } diff --git a/Sorting/Sort Bubble.c b/Sorting/Sort Bubble.c index 7b4a1eb..37f70e9 100644 --- a/Sorting/Sort Bubble.c +++ b/Sorting/Sort Bubble.c @@ -1,15 +1,22 @@ /*Program of sorting using bubble sort*/ #include +#include #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 #include +#include #include +#include +#define SLEN 5 struct node { @@ -15,31 +18,44 @@ 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"); @@ -47,9 +63,21 @@ main( ) 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) @@ -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;