Write a C program to add a series of odd numbers i.e. 1+3+5+7+……..+n

This program will give you an idea of adding a series having only odd numbers. Let a series  1+3+5+7+……..25. Now Define the first odd number by a variable and last odd number by another variable. Then use a while loop for performing sum operation. Here I used variable i as loop counter as well as putting each odd number of the series on it. While loop will stop when i>lastODDnumber 
 

//1+3+5+..............+n
#include <stdio.h> 
main(){
    int i,n1,n2,SUM=0;
    printf("Insert the first ODD number: ");
    scanf("%d",&n1);
    printf("Insert the last ODD number: ");
    scanf("%d",&n2);
    i=n1; //initialize i by first odd number
    while(i<=n2){
        SUM=i+SUM;
        i=i+2;
    }
    printf("Total: %d",SUM);
    return 0;
}

Write a C program to calculate a series of integer numbers as 1+2+3+......+n

Here 1+2+3+…….+n is a simple series where each number can be added using a single loop. At first take a variable SUM and initialize it by Zero. Then add each number of the series successively. Then print the SUM.


//1+2+3+..............+n

#include <stdio.h>

main(){
  int i,n,SUM=0;
  printf("Insert the last number: ");
  scanf("%d",&n);
  for(i=1;i<=n;i++)
       SUM=SUM+i;
  printf("Total: %d",SUM);

  return 0;
 }

Algorithm of Insertion sort and a program of insertion sort in C

Insertion sort:

It is simple to sort a set of integer number or character set using insertion algorithm. Let you have unsorted array A[] of 10 elements.
A[]={23, 17, 12, 25, 33, 14, 17, 26, 21, 19}
I am showing you first six elements sorting in below.
  • 23, 17, 12, 25, 33, 14, 17, 26, 21, 19  [initially]
  • 17, 23,12, 25, 33, 14, 17, 26, 21, 19  [key= A[1] or 17 according to previous line that was sorted in this line]
  • 12, 17, 23, 25, 33, 14, 17, 26, 21, 19   [key= A[2] or 12 according to previous line]
  • 12, 17, 23, 2533, 14, 17, 26, 21, 19   [key= A[3] or 25 according to previous line, but here after first comparison of 12 with previous element we see no need to sort ]
  • 12, 17, 23, 25, 33, 14, 17, 26, 21, 19   [key=A[4] or 33 according to previous line, but here after first comparison of  with previous element we see no need to sort ]
  • 12, 14, 17, 23, 25, 33, 17, 26, 21, 19 and that’s way all elements are sorted.
You have seen that every time key is placed in proper location in the array. We have seen that before all elements of a key have already been sorted

A program for function: Search, Insert, Show, Push, Pop || Array and Stack using Function call

Use this program to run certain function with array and stack. Function: search, insert, show, push and pop. Use a menu to make choice for run operation. I give you a code based on my algorithm. Try to find another algorithm and then make code. Before start learn about Array and Stack and difference between them.


#include<stdio.h>
#include<stdlib.h>
#define max 20
int ARRAY();
int STACK();
int SEARCH(int LA[max], int N);
int INSERT(int LA[max], int N);
int SHOW(int LA[max], int N);
int PUSH(int stk[max], int TOP);
int POP(int stk[max], int TOP);

int main()
    {
        char ch;
        for(;;)
         {
            printf("\nMake a choice of your operation:")  ;
            printf("\nA: for ARRAY operation");
            printf("\nS: for STACK operation");
            printf("\nX: for EXIT\n");

Program of Searching and inserting a item in a linear array - using function

#include<stdio.h>
#include<stdlib.h>
int SEARCH(int LA[100], int N);
int INSERT(int LA[100], int N);
int SHOW(int LA[100], int N);

int main()
    {
        int LA[100],i, N;
        char ch;
        printf("\nEnter how many array element: ");
        scanf("%d", &N);
        printf("\nEnter array elements:\n");
        for(i=0;i<N;i++)
          {
              printf("\nElement no %d: ", i+1);
              scanf("%d", &LA[i]);
          }
        menu:
        {

        printf("\nSelect your operation:\n") ;
        printf("\n1: SEARCH an item");
        printf("\n2: INSERT an element ");
        printf("\n0: Exit\n ")