Friday, September 30, 2016

Factorial of given number using recursive function.

Recursive Function

The function which calls itself is called recursive function.


Factorial of given number using recursive function.



#include <stdio.h>
int factorial(int);

int  main() {
   int number;
  
   printf("Enter your number:");
   scanf("%d", &number);
  
   printf("Factorial of %d is %d\n", number, factorial(number));
   return 0;
}
int factorial(int i)
{
   if(i<=1) {
      return 1;
   }
   return (i*factorial(i-1));
}


Output of the program

Enter your number:5
Factorial of 5 is 120


Use of built-in C string library function.


// Use of built-in C string library function.

#include<stdio.h>
int main()
{
     int len;
     char str1[10], str2[10];
    
     puts("Enter String 1:");
     gets(str1);
    
     puts("Enter String 2:");
     gets(str2);
    
     //Function to find length of given string 1.
     len = strlen(str1);
     printf("\nLength of String 1 is %d.\n", len);
    
     //Function to compare two strings
     if(strcmp(str1,str2)==0)
           printf("\nString 1 and String 2 are same.\n");
     else
           printf("\nString 1 and String 2 are not same.\n");
          
     //Function to copy string 1 into string 2
     strcpy(str2,str1); //this will copy str1 into str2
     printf("\nString 2 after execution of strcpy = %s", str2);
    
     return 0;
}


Output of the program:

/*Output
Enter String 1:
abc
Enter String 2:
xyz

Length of String 1 is 3.

String 1 and String 2 are not same.

String 2 after execution of strcpy = abc
*/

Thursday, September 29, 2016

Square of given number using function with an argument and a return value.

/* C program to find square of given number using function.
Use of function with an argument and a return value.
*/
#include<stdio.h>

int square(int); // function prototype declaration.

void main()
{
     int number, answer;
    
     printf("Enter your number:");
     scanf("%d", &number);
    
     answer = square(number);  //Call function.
    
     printf("Square of %d is %d.", number, answer);
}

int square(int n)
{
     //function logic is written here..
     return(n*n); //This will return answer to main function.
}



Output of the Program:

Enter your number:5
Square of 5 is 25. 

C program to print Standing Triangle pattern.

C program to print standing triangle pattern. For n = 3, generate pattern as shown below:

*
* *
* * *
* *
*

#include<stdio.h>
int main()
{
     int i, j, N=5;
     for(i=0; i<N; i++)  
     {
           for(j=0; j<=i; j++)
                printf("* ");
           printf("\n");       
     }
     for(i=0; i<N; i++)  
     {
           for(j=N-1; j>i; j--)
                printf("* ");
           printf("\n");       
     }
     return 0;
}


Output of the Program:

*
* *
* * *
* *
*



Tuesday, September 27, 2016

Algorithm to print Fibonacci series up to given number N.

About Fibonacci series 
Each new number in a series is addition of its previous two numbers.

Example of Fibonacci series(for N = 8)
0, 1, 1, 2, 3, 5, 8, 13

Algorithm to print Fibonacci series up to given number N.

Step 1: Start
Step 2: Declare variables N, N1, N2, N3, i.
Step 3: Read value of N from user.
Step 4: if N < 2, display message “Enter number > 2” and go to step 9.
Step 5: Initialize variables N1 = 0, N2 = 1, i = 0
Step 6: Display N1, N2  
Step 7: Compute N3 = N1 + N2
Step 8: Repeat following statements until i <  N - 2
Display N3
N1 = N2
N2 = N3  
N3 = N1 + N2
i  = i + 1
Step 9: Stop



* * * * *

Algorithm to find factorial of a given number.

Factorial of number n is defined as follow:

      n! = 1 * 2 * 3 * 4 * ....* n

Algorithm to find factorial of a given number N:

Step 1: Start
Step 2: Declare variables: N, fact,  i.
Step 3: Initialize variables
           fact = 1
           i = 1
Step 4: Read value of N from user.
Step 5: Repeat following steps until i = N
           fact =  fact * i
           i = i + 1
Step 6: Print fact variable.
Step 7: Stop




Saturday, September 24, 2016

Use of pointer to access the Array elements.



Use of pointer to access the Array elements.

#include <stdio.h>

int main ()
{
       int marks[] = {60, 70, 80, 90, 100};
       int i, *p1;
       /* let us use pointer to refer array elements */
       p1 = marks;
       for ( i = 0; i < 5; i++)
       {
              printf("Value stored at marks[%d] = %d\n", i, *p1 );
              p1++;
       }
       return 0;
}



Output of the Program:


Value stored at marks[0] = 60
Value stored at marks[1] = 70
Value stored at marks[2] = 80
Value stored at marks[3] = 90
Value stored at marks[4] = 100