08Sep/17

Insert an element at the given index of an array

Program:

#include<iostream>;
#define MAX 6
using namespace std;

int main()
{
    int a[MAX] = {10,20,30,40,50};
    int N = 5; //no. of elements
    int k = 2; //index position to insert data
    int item = 100; //item to be inserted in index position

    for(int i = N-1; i &gt;= k; i--){
        a[i+1] = a[i];
    }
    a[k] = item;

    for(int j = 0; j &lt; MAX; j++){
        cout &lt;&lt; a[j] &lt;&lt; "\t";
    }
}

Output:

10 20 100 30 40 50
03Dec/16

Program to swap using call by reference

Program:

#include<stdio.h>
int swap(int *a, int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}
int main(){
    int x = 20, y = 50;
    printf("Before Swap: x= %d y= %d", x, y);
    swap(&x, &y);
    printf("\nAfter Swap: x= %d y= %d\n", x, y);
}

Output:

Before Swap: x = 20  y = 50 
After Swap:  x = 50  y = 20 
03Dec/16

Find the Nth Fibonacci Number using Recursion

Program:

#include<stdio.h>
int fibo(int n){
    int f;
    if(n == 1 || n == 0) return n;
    f = fibo(n-1) + fibo(n-2);
    return f;
}
int main()
{
    int k,x;
    printf("Enter a number: ");
    scanf("%d",&x);
    k = fibo(x);
    printf("The %d number in fibonacci series is: %d\n",x,k);
    return 0;
}

Output:

Enter a number: 6
The 6 number in fibonacci series is: 8
03Dec/16

Factorial of a number using recursion

Program:

#include<stdio.h>
int fact(int n){
    int f;
    if(n == 1) return 1;
    f = n * fact (n-1);
    return f;
}
int main()
{
    int k,x;
    printf("Enter a number: ");
    scanf("%d",&x);
    k = fact(x);
    printf("Factorial of %d is: %d\n",x,k);
    return 0;
}

Output:

Enter a number: 5
Factorial of 5 is: 120
03Dec/16

Find power of a number (a^b) using recursion

Program:

#include<stdio.h>
int getPower(int base,int exponent)
{
    if(exponent == 0) return 1;
    return base * getPower(base,exponent-1);
}
int main()
{
    int base, exponent,z;
    printf("Enter base and exponent: ");
    scanf("%d %d", &base, &exponent);
    z = getPower(base,exponent);
    printf("Result = %d",z);
}

Output:

Enter base and exponent: 2 5
Result = 32
22Oct/16

Program to print full pyramid using star (*)

Program:

#include<stdio.h>
int main(){
    int i,rows,k=0,space;
    printf("Enter the no. of rows:");
    scanf("%d",&rows);
    for(i=1; i<=rows; ++i){
        for(space=1;space<=rows-i;++space){
                printf(" ");
            }
        while(k != 2*i-1){
            printf("*");
            ++k;
        }
        k=0;
        printf("\n");
    }
    return 0;
}

Output:

Enter the no. of rows:5
    *
   ***
  *****
 *******
*********
15Aug/16

Example of 1-dimension Array Summation

Program

#include<stdio.h>
int main()
{
    int a[5],i,sum=0;
    for(i=0;i<5;i++)
    {
        printf("Enter number %d:",i+1);
        scanf("%d",&a[i]);
        sum=sum+a[i];
    }
    printf("Sum=%d",sum);
    return 0;
}

Output

Enter number 1: 10
Enter number 2: 30
Enter number 3: 20
Enter number 4: 50
Enter number 5: 40
Sum = 150