R2:Function&Recursion

Write a program to read a string through keyboard and sort it using bubble sort

With Function:
  1. #include<stdio.h>
  2. #include<string.h>
  3. void bubbleSort(char str[25][25], char temp[25],int n)
  4. {
  5.     int i,j;
  6.     for(i=0; i<=n; i++) {
  7.         for (j = i + 1; j <= n; j++) {
  8.             if (strcmp(str[i], str[j]) > 0) {
  9.                 strcpy(temp, str[i]);
  10.                 strcpy(str[i], str[j]);
  11.                 strcpy(str[j], temp);
  12.             }
  13.         }
  14.     }
  15. }
  16. int main(){
  17.     int i, j, n;
  18.     char str[25][25], temp[25];
  19.     printf("Enter the number of string : ");
  20.     scanf("%d", &n);

  21.     printf("\nEnter %d strings : \n", n);
  22.     for(i=0; i<=n; i++)
  23.         gets(str[i]);

  24.     bubbleSort(str,temp,n);

  25.     printf("\n\nSorted Strings :");
  26.     for(i=0; i<=n; i++)
  27.         puts(str[i]);

  28.     return 0;
  29. }


With Recursion:

  1. #include<stdio.h>
  2. #include<string.h>
  3. void bubbleSort(char str[25][25], char temp[25],int n)
  4. {
  5.     if (n == 0)
  6.         return;
  7.     int i,j;
  8.     for(i=1; i< n; i++) {
  9.             if (strcmp(str[i], str[i+1]) > 0) {
  10.                 strcpy(temp, str[i]);
  11.                 strcpy(str[i], str[i+1]);
  12.                 strcpy(str[i+1], temp);
  13.             }
  14.         }
  15.     bubbleSort(str,temp, n-1);
  16. }
  17. int main(){
  18.     int i, j, n;
  19.     char str[25][25], temp[25];
  20.     printf("Enter the number of string : ");
  21.     scanf("%d", &n);

  22.     printf("\nEnter %d strings : \n", n);
  23.     for(i=0; i<=n; i++)
  24.         gets(str[i]);

  25.     bubbleSort(str,temp,n);

  26.     printf("\n\nSorted Strings :");
  27.     for(i=0; i<=n; i++)
  28.         puts(str[i]);

  29.     return 0;
  30. }

Write a program to search a character in a string using binary search

with function:
  1. #include <stdio.h>
  2. #include <string.h>

  3. int func(char str[100][100],int start,int end,char key[40])
  4. {
  5.     while(start <= end)
  6.     {
  7.         int mid = (start+end)/2;
  8.         if (strcmp(key, str[mid])==0)
  9.         {
  10.             printf("key found at the position %d\n", mid+1);
  11.             return 0;
  12.         }
  13.         else if
  14.                 (strcmp(key, str[mid])>0)
  15.         {
  16.             end = end;
  17.             start = mid+1;
  18.         }

  19.         else
  20.         {
  21.             start=start;
  22.             end = mid-1;
  23.         }
  24.     }
  25.     return -1;

  26. }
  27. int main()
  28. {
  29.     int i, n, start, end, mid;
  30.     char str[100][100], key[40];

  31.     printf("Please enter the number of string to be stored : ");
  32.     scanf("%d",&n);

  33.     printf("Enter the string in ascending order\n");

  34.     for(i=0; i<=n-1; i++)
  35.     {
  36.         scanf("%s", &str[i]);
  37.     }

  38.     printf("Enter the string to be searched : ");
  39.     scanf("%s", &key);

  40.     start = 0;
  41.     end = n-1;
  42.     if(func(str,start,end,key)==-1)
  43.         printf("Failed\n");
  44.     return 0;
  45. }


with recursion:
  1. #include <stdio.h>
  2. #include <string.h>

  3. int recursion(char str[100][100],int start,int end,char key[40])
  4. {
  5.     if(start>end)
  6.         return -1;
  7.     int mid = (start+end)/2;
  8.     if(strcmp(key, str[mid])==0)
  9.     {
  10.         printf("key found at the position %d\n", mid+1);
  11.         return 0;
  12.     }
  13.     else if(strcmp(key, str[mid])>0)
  14.     {
  15.         recursion(str,mid+1,end,key);
  16.     }
  17.     else
  18.     {
  19.         recursion(str,start,mid-1,key);
  20.     }

  21. }
  22. int main()
  23. {
  24.     int i, n, start, end, mid;
  25.     char str[100][100], key[40];

  26.     printf("Please enter the number of string to be stored : ");
  27.     scanf("%d",&n);

  28.     printf("Enter the string in ascending order\n");

  29.     for(i=0; i<=n-1; i++)
  30.     {
  31.         scanf("%s", &str[i]);
  32.     }

  33.     printf("Enter the string to be searched : ");
  34.     scanf("%s", &key);

  35.     start = 0;
  36.     end = n-1;
  37.     if(recursion(str,start,end,key)==-1)
  38.         printf("Failed\n");
  39.     return 0;
  40. }

Write a program to check whether a number is prime or perfect number

Write a program to check whether a number is prime or perfect number.
Used function

  1. #include <stdio.h>
  2. #include <math.h>

  3. int Prime(int num);
  4. int Perfect(int num);


  5. int main()
  6. {
  7.     int num;
  8.     
  9.     printf("Enter a number to check Prime or Perfect number : ");
  10.     scanf("%d", &num);
  11.     
  12.     if(Prime(num)) {
  13.         printf("%d is Prime number.\n", num);
  14.     }
  15.     else {
  16.         printf("%d is not Prime number.\n", num);
  17.     }
  18.     
  19.     if(Perfect(num)) {
  20.         printf("%d is Perfect number.\n", num);
  21.     }
  22.     else {
  23.         printf("%d is not Perfect number.\n", num);
  24.     }
  25.     
  26.     return 0;
  27. }

  28. int Prime(int num) 
  29. {
  30.     int i;
  31.     
  32.     for(i=2; i<=num/2; i++) {
  33.         if(num%i == 0)  
  34.         {
  35.             return 0;
  36.         }  
  37.     } 
  38.     
  39.     return 1; 
  40. }

  41. int Perfect(int num) 
  42. {
  43.     int i, sum, n;
  44.     sum = 0;
  45.     n = num;
  46.     
  47.     for(i=1; i<n; i++) {
  48.         if(n%i == 0) {  
  49.             sum += i;  
  50.         }  
  51.     }
  52.     
  53.     return (num == sum);
  54. }
Used recursion

  1. #include <stdio.h>
  2. #include <math.h>

  3. int Prime(int i, int num);
  4. int Perfect(int i, int num,int sum);

  5. int main()
  6. {
  7.     int num;

  8.     printf("Enter a number to check Prime or Perfect number : ");
  9.     scanf("%d", &num);

  10.     if(Prime(2,num)) {
  11.         printf("%d is Prime number.\n", num);
  12.     }
  13.     else {
  14.         printf("%d is not Prime number.\n", num);
  15.     }

  16.     if(Perfect(1,num,0)) {
  17.         printf("%d is Perfect number.\n", num);
  18.     }
  19.     else {
  20.         printf("%d is not Perfect number.\n", num);
  21.     }
  22.     return 0;
  23. }

  24. int Prime(int i, int num)
  25. {
  26.     if(i > num / 2){
  27.         return 1;
  28.     }
  29.     if(num % i == 0){
  30.         return 0;
  31.     }
  32.     Prime(i+1,num);
  33. }
  34. int Perfect(int i, int num,int sum)
  35. {
  36.     if(i == num){
  37.         return (num == sum);
  38.     }
  39.     if(num % i == 0){
  40.         Perfect(i+1,num,sum+i);
  41.     }else{
  42.         Perfect(i+1,num,sum);
  43.     }
  44. }

Write a program to print even or odd numbers in given range

Write a program to print even or odd numbers in given range.
Using function

  1. #include <stdio.h>

  2. int even(int lowerLimit, int upperLimit)
  3. {
  4.     int i, r, count=0;
  5.     printf("\nDisplay the even numbers between %d and %d are: ", lowerLimit, upperLimit);
  6.     for(i=lowerLimit; i<=upperLimit; i++){
  7.         r = i%2;
  8.         if(r == 0) {
  9.            printf("%3d", i);
  10.            count++;
  11.         }
  12.     }
  13.     //printf("\n\nTotal even numbers between %d and %d are: %d", lowerLimit, upperLimit, count);
  14. }

  15. int odd(int lowerLimit, int upperLimit)
  16. {
  17.     int i,r,count=0;
  18.       printf("\n\nDisplay the odd numbers between %d and %d are: ",lowerLimit, upperLimit);
  19.     for(i=lowerLimit; i<=upperLimit; i++){
  20.         r=i%2;
  21.         if(r==1) {
  22.             printf("%3d",i);
  23.             count++;
  24.             }
  25.     }
  26.     //printf("\n\nTotal odd numbers between %d and %d are:  %d", lowerLimit, upperLimit, count);
  27. }
  28. int main()
  29. {
  30.     int lowerLimit, upperLimit;
  31.     printf("Enter lower limit : ");
  32.     scanf("%d", &lowerLimit);
  33.     
  34.     printf("Enter upper limit : "); //Enter the second number for range
  35.     scanf("%d", &upperLimit);
  36.     
  37.     even(lowerLimit, upperLimit);
  38.     odd(lowerLimit, upperLimit);
  39.     return 0;
  40. }


Recursion

  1. #include <stdio.h>

  2. void printEvenOdd(int num, int limit);
  3. int main()
  4. {
  5.     int lowerLimit, upperLimit;
  6.     printf("Enter lower limit : ");
  7.     scanf("%d", &lowerLimit);  

  8.     printf("Enter upper limit : ");
  9.     scanf("%d", &upperLimit);

  10.     printf("Even or odd numbers from %d to %d given range are : ", lowerLimit, upperLimit);
  11.     printEvenOdd(lowerLimit, upperLimit);

  12.     return 0;
  13. }

  14. void printEvenOdd(int num, int limit)//recursive function
  15. {
  16.     if(num > limit)
  17.         return;
  18.     printf("%4d",num);

  19.     printEvenOdd(num+2, limit);
  20. }

Write a program to display all array elements

Write a program to display all array elements.
using function

  1. #include <stdio.h>

  2. int input(int *arr, int n)
  3.     int i;
  4.     
  5.     for(i=0; i<n; i++) {
  6.         scanf("%d", &arr[i]);
  7.     }
  8. }
  9.      
  10. int display(int *arr, int n)
  11.     int i;
  12.     
  13. for(i=0; i<n; i++) {
  14.         printf("%5d", arr[i]);
  15.     }
  16. }
  17.  
  18. int main()
  19. {
  20.     int arr[100], i, n;  
  21.  
  22.     printf("Enter the size of array: ");
  23.     scanf("%d", &n);
  24.  
  25.     printf("Enter %d elements in the array : ", n);
  26.     input(arr, n);
  27.     printf("\nElements in array are : ");
  28.     
  29.     display(arr, n);
  30.     return 0;
  31. }

Recursion recursion

  1. #include <stdio.h>

  2. int input(int *arr,int i, int n)
  3.      
  4.    if(i<n) {
  5. scanf("%d", &arr[i]);
  6.         input(arr, i+1, n);
  7.     }
  8. }
  9.      
  10. int display(int *arr,int i,int n)
  11.      if(i<n) {
  12.         printf("%d  ", arr[i]);
  13.         display(arr,i+1,n);
  14.     }
  15. }

  16. int main()
  17. {
  18.     int arr[100],i,n;  
  19.  
  20.     printf("Enter the size of array: ");
  21.     scanf("%d", &n);
  22.  
  23.     printf("Enter %d elements in the array : ", n);
  24.     input(arr,0,n);
  25.     printf("\nElements in array are: ");
  26.     display(arr, 0, n);
  27.  
  28.     return 0;
  29. }

Write a program to find the factorial of any number

Write a program to find the factorial of any number.
using function 

  1. #include <stdio.h>
  2. int fact(int);
  3.  
  4. void main()
  5. {
  6.  int num, factorial;
  7.  
  8.   printf("Please enter any number to find factorial : ");
  9.   scanf("%d",&num);
  10.   factorial=fact(num);
  11.     printf("The number %d factorial is = %d\n",num,factorial);
  12.     //printf("Factorial of the number (%d) = %d\n",num,fact(num));
  13. }
  14.  
  15. int fact(int n)
  16. {
  17.     int i,f=1;
  18.     for(i=1;i<=n;i++) {
  19.         f=f*i;
  20.     }
  21.     return f;
  22. }

Recursion:

  1. #include <stdio.h>

  2. int fact(int n)
  3. {
  4.     if(n==0)
  5.         return 1;
  6.     else 
  7.         return (n*fact(n-1));
  8. }
  9.  
  10. void main()
  11. {
  12.     int num;
  13.     printf("Please enter any number to find factorial : ");
  14.     scanf("%d", &num);
  15.     printf("The number %d factorial is = %d\n",num,fact(num));
  16. }

Write a program to find LCM of two number

Write a program to find LCM of two number
using function

  1. #include <stdio.h>
  2. int gcd(int, int);

  3. int main() {
  4.   int n1, n2, lcm_gcd, lcm;

  5.   printf("Enter two numbers to find LCM : ");
  6.   scanf("%d %d", &n1, &n2);

  7.   lcm_gcd = gcd(n1, n2);
  8.   lcm = (n1*n2)/lcm_gcd;

  9.   //printf("\n%d and %d of GCD is = %d\n", n1, n2, lcm_gcd);
  10.   printf("\n%d and %d of LCM is = %d\n", n1, n2, lcm);

  11.   return 0;
  12. }

  13. int gcd(int n1, int n2) {
  14.   if (n1 == 0) {
  15.     return n2;
  16.   }

  17.   while (n2 != 0) {
  18.     if (n1 > n2) {
  19.       n1 = n1-n2;
  20.     }
  21.     else {
  22.       n2 = n2-n1;
  23.     }
  24.   }
  25.   return n1;
  26. }


Recursion recursion

  1. #include <stdio.h>
  2. int gcd(int, int);

  3. int main() {
  4.   int n1, n2, lcm_gcd, lcm;

  5.   printf("Enter two numbers to find LCM : ");
  6.   scanf("%d %d", &n1, &n2);

  7.   lcm_gcd = gcd(n1, n2);
  8.   lcm = (n1*n2)/lcm_gcd;

  9.   //printf("\n%d and %d of GCD is = %d\n", n1, n2, lcm_gcd);
  10.   printf("\n%d and %d of LCM is = %d\n", n1, n2, lcm);

  11.   return 0;
  12. }

  13. int gcd(int m, int n) {
  14.   if (n == 0) {
  15.     return m;
  16.   }
  17.   else {
  18.     return gcd(n, m%n);
  19.   }
  20. }

Write a program to find GCD of two number

Write a program to find GCD of two number.
using function

  1. #include<stdio.h>
  2. int find_gcd(int n1, int n2);

  3. int main() {
  4.     int n1, n2;
  5.     printf("Enter two numbers to find GCD : ");
  6.     scanf("%d %d", & n1, & n2);
  7.     
  8.     printf("\n%d and %d of GCD is: %d\n\n", n1, n2, find_gcd(n1, n2));
  9.  
  10.     return 0;
  11.  }
  12. int find_gcd(int n1, int n2) {
  13.   if (n2 == 0)
  14.     return n1;
  15.   else
  16.     return find_gcd(n2, n1 % n2);
  17. }

Using recursion 

  1. #include<stdio.h>

  2. int find_gcd(int , int );

  3. int main()
  4. {
  5.     int n1, n2, gcd;
  6.     printf("Enter two numbers to find GCD : ");
  7.     scanf("%d%d", &n1, &n2);
  8.     
  9.     gcd = find_gcd(n1, n2);
  10.     printf("\n%d and %d of GCD is: %d\n\n", n1, n2, gcd);

  11.     return 0;
  12. }

  13. int find_gcd(int x, int y)
  14. {
  15.     if(x > y)
  16.         find_gcd(x-y, y);

  17.     else if(y > x)
  18.         find_gcd(x, y-x);
  19.     else
  20.         return x;
  21. }

Write a program to find nth Fibonacci term

Write a program to find nth Fibonacci term
Used function

  1. #include<stdio.h> 
  2.   
  3. int fibo(int num) 
  4. {
  5.   int fibonacci[num+2];
  6.   int i; 
  7.   
  8.   fibonacci[0] = 0; 
  9.   fibonacci[1] = 1; 
  10.   
  11.   for (i = 2; i <= num; i++) { 
  12.       fibonacci[i] = fibonacci[i-1] + fibonacci[i-2]; 
  13.   } 
  14.   
  15.   return fibonacci[num]; 
  16.   
  17. int main() { 
  18.     int num; 
  19.     printf("Enter the number to find nth fiboacci term : ");
  20.     scanf("%d", &num);

  21.     int first=0, second=1, third;
  22.     printf("%4d%4d", first, second);
  23.     for(int i=3; i <= num; i++) {
  24.         third=first+second;
  25.         first=second;
  26.         second=third;
  27.         printf("%4d", third);
  28.     }

  29.     printf("\n\n%dᵗʰ fibonacci term is : %d", num, fibo(num));
  30.     return 0; 
  31. }

Recursion

  1. #include <stdio.h>

  2. int fibo(int num);

  3. int main()
  4. {
  5.     int num;
  6.     int fibonacci;
  7.   
  8.     printf("Enter the number to find nth fiboacci term : ");
  9.     scanf("%d", &num);
  10.    
  11.     int first=0, second=1, third;
  12.     printf("%4d%4d", first, second);
  13.     for(int i=3; i <= num; i++) {
  14.         third=first+second;
  15.         first=second;
  16.         second=third;
  17.         printf("%4d", third);
  18. }

  19.     if(num < 0) {
  20.         printf("Fibonacci of negative number is not possible.\n");
  21.     }
  22.     else {
  23.         fibonacci = fibo(num);
  24.         printf("The %dᵗʰ fibonacci term is : %d\n", num, fibonacci);
  25.     }
  26.    /* //or (this one is also correct) 
  27.     fibonacci = fibo(num); 
  28.     printf("%dᵗʰ fibonacci term is : %d", num, fibonacci);
  29.     */
  30.     return 0;
  31. }

  32. int fibo(int num) 
  33. {
  34.     if(num == 0)
  35.         return 0;
  36.     else if(num == 1)
  37.         return 1;
  38.     else 
  39.         return fibo(num-1) + fibo(num-2); 
  40. }

Write a program to find the sum of all elements of the array

Write a program to find the sum of all elements of the array.
Used function

  1. #include <stdio.h>

  2. int element(int arr[], int n)
  3. {
  4.     int sum = 0;
  5.   
  6.     for(int i = 0; i < n; i++) {
  7.         sum = sum + arr[i];
  8.     }
  9.   
  10.     return sum;
  11. }
  12.   
  13. int main()
  14. {
  15.     int arr[100];
  16.     int total = 0;
  17.     int i, n;
  18.     
  19.     printf("Enter the number of elements to be stored in the array : ");
  20.     scanf("%d", &n);

  21.     printf("Enter %d elements in the array :\n", n);
  22.     for(i=0; i<n; i++) {
  23.           printf("Element - %d : ", i);
  24.           scanf("%d", &arr[i]);
  25.         }
  26.   
  27.     total = element(arr, n);
  28.   
  29.     printf("Sum of all elements of array is : %d", total);
  30.   
  31.     return 0;
  32. }

Used recursion

  1. #include <stdio.h>

  2. int element(int arr[], int n, int i)
  3. {
  4.     if(i<n)
  5.         return arr[i]+element(arr, n, ++i);
  6.    
  7.         return 0;
  8. }
  9.   
  10. int main()
  11. {
  12.     int arr[100];
  13.     int total = 0;
  14.     int i, n;
  15.     
  16.     printf("Enter the number of elements to be stored in the array : ");
  17.     scanf("%d", &n);

  18.     printf("Enter %d elements in the array :\n", n);
  19.     for(i=0; i<n; i++) {
  20.           printf("Element - %d : ", i);
  21.           scanf("%d", &arr[i]);
  22.         }
  23.   
  24.     total = element(arr, n, 0);
  25.   
  26.     printf("Sum of all elements of array is : %d", total);
  27.   
  28.     return 0;
  29. }

Write a program to find cube of a number

Write a program to find cube of a number.
Used Function

  1. #include <stdio.h>

  2. int cube(int num);

  3. int main()
  4. {
  5.     int num;
  6.     int c;
  7.     
  8.     printf("Please enter a number: ");
  9.     scanf("%d", &num);
  10.     
  11.     c = cube(num);

  12.     printf("Cube of %d is : %d", num, c); 
  13.     
  14.     return 0;
  15. }

  16. int cube(int num) {
  17.     return (num*num*num);
  18. }

Used Recursion
#include<stdio.h>

int iterator = 2;
int givenValue=0;
int cube(int num)
{
  if(!iterator)
  {
    printf("Cube of %d is : %d ", givenValue, num/givenValue);
    return;
  }
  else
  {
    iterator--;
    cube(num*num);
  }
}

int main()
{
  int n=0;
  printf("Enter the number to find the cube\n");
  scanf("%d",&givenValue);
  cube(givenValue);
}



//maybe the below program is incorrect
  1. #include <stdio.h>

  2. int cube(int);

  3. int main()
  4. {
  5.     int n;
  6.     int c;
  7.     
  8.     printf("Please enter a number: ");
  9.     scanf("%d", &n);
  10.     
  11.     c = cube(n);

  12.     printf("Cube of %d is : %d", n, c); 
  13.     
  14.     return 0;
  15. }

  16. int cube(int num) {
  17.     return (num*num*num);
  18. }

Write a program to find the power of a number

Write a program to find the power of a number.
Used Function
  1. #include<stdio.h>
  2. void power(int,int);
  3.  
  4. void main() 
  5. {
  6.     int base, expo;
  7.     printf("Enter the base : ");
  8.     scanf("%d",&base);
  9.     
  10.     printf("Enter the exponent : ");
  11.     scanf("%d",&expo);
  12.     
  13.     power(base, expo);
  14. }
  15.  
  16. void power(int base, int expo)
  17. {
  18.     int power=1;
  19.     while(expo>0)
  20.     {
  21.         power=power*base;
  22.         expo--;
  23.     }
  24.     printf("\nPower of the number is = %d", power);
  25. }

Recursion
  1. #include <stdio.h>
  2.  
  3. int power(int base, int expo)
  4. {
  5.     if(expo==0)
  6.         return 1;
  7.     else 
  8.         return (base*power(base, expo-1));
  9. }
  10.  
  11. void main()
  12. {
  13.     int base, expo;
  14.     printf("Enter the base : ");
  15.     scanf("%d", &base);
  16.     
  17.     printf("Enter the exponent : ");
  18.     scanf("%d", &expo);
  19.     
  20.     printf("\nPower of the number is (%d^%d) = %d", base, expo, power(base, expo));
  21. }