3.22.2012

Insert element in array

Q. Write a C program to insert or add new element in array, give with output of program.


Ans.


/*c program for insert new element in array*/
#include<stdio.h>
#include<conio.h>
#define SIZE 50
int main()
{
 int arr[SIZE];
 int i,index,tmp,num;
 printf("Enter total number of elements in array : ");
 scanf("%d", &num);
 for(i=0; i<num; i++)
 {
   printf("Enter %d element : ",i+1);
   scanf("%d", &arr[i]);
 }
 printf("\nEnter element number before to insert new element : ");
 scanf("%d", &index);
 printf("Enter new element : ");
 scanf("%d", &tmp);
 for(i=num-1; i>=index-1; i--)
    arr[i+1] = arr[i];
 arr[index-1] = tmp;
 printf("\n-- After insertion an element, new array list --\n\n");
 for(i=0; i<=num; i++)
   printf("\t%d\n",arr[i]);
 getch();
 return 0;
}


/**************** Output ******************/
Screen shot of insert element in array




Related programs:

  1. How to create array in C?
  2. How to calculate length of array?
  3. Display array in reverse order
  4. Delete an exist element in array
  5. Example of array C program

Array in reverse order

Q. Write a C program to display the array in reverse order.


Ans.


/*c program for display the array in reverse order*/
#include<stdio.h>
#include<conio.h>
#define SIZE 50
int main()
{
 int arr[SIZE];
 int i,index,tmp,num;
 printf("Enter total number of element in array : ");
 scanf("%d", &num);
 for(i=0; i<num; i++)
 {
  printf("Enter %d element : ",i+1);
  scanf("%d", &arr[i]);
 }
 printf("\n-- Array in reverse order --\n\n");
 for(i=0; i<num; i++);
 for(i--; i>=0; i--)
    printf("\t%d\n",arr[i]);
 getch();
 return 0;
}


/************* Output *********************/
Screen shot of  display array in reverse order




Related Programs:

  1. How to create array in C?
  2. How to calculate length of array?
  3. Insert new element in array
  4. Delete an exist element in array
  5. Example of array C program

Delete element in array

Q. Write a C program to delete a value or element in array and user should guide which value is deleted.


Ans.


/*c program for delete a number in array*/
#include<stdio.h>
#include<conio.h>

#define SIZE 50
int main()
{
 int arr[SIZE];
 int i,num,index;
 printf("Enter total number of element in array : ");
 scanf("%d", &num);
 for(i=0; i<num; i++)
 {
  printf("Enter %d element : ",i+1);
  scanf("%d",&arr[i]);
 }
 printf("\nEnter element number which you want to delete : ");
 scanf("%d", &index);
 if(index >= num+1)
   printf("\nElement not found!!");
 else
 {
  for(i=index-1; i<num-1; i++)
     arr[i]=arr[i+1];
  printf("\n-- After deletion new array list --\n\n");
  for(i=0; i<num-1; i++)
    printf("\t%d\n",arr[i]);
 }
 getch();
 return 0;
}


/*************** Output *****************/
Screen shot of delete element in array




Related Programs:

  1. How to create array in C?
  2. How to calculate length of array?
  3. Insert new element in array
  4. Display array in reversing order
  5. Example of array C program

3.21.2012

Length of array

Q. Write a C program to calculate the length of a given array.


arr[] = {12, 7 , 9 , 4 , 22 , 63 , 74 , 5 , 1 }


Ans.


/*c program for calculate the length of array*/
#include<stdio.h>
int main()
{
 int arr[]={ 12,7,9,4,22,63,74,5,1 };
 int length;
 length = sizeof(arr)/sizeof(int);
 printf("Length of given array = %d",length);
 getch();
 return 0;
}


/*************** Output *****************/
Screen shot for calculate length of array c program




Related programs:

  1. How to create array in C?
  2. Display array in reverse order
  3. Insert new element in array
  4. Delete an exist element in array
  5. Example of array C program

3.20.2012

Create array

Q. Write a program to create array.


Ans.


/*c program for creation of array or list of numbers*/
#include<stdio.h>
#include<conio.h>
#define SIZE 50
int main()
{
 int arr[SIZE];
 int i,num;
 printf("Enter total number of elements : ");
 scanf("%d", &num);
 for(i=0; i<num; i++)
 {
   printf("Enter %d element : ",i+1);
   scanf("%d", &arr[i]);
 }
 printf("\n-- Creation of array --\n\n");
 for(i=0; i<num; i++)
   printf("\t%d\n", arr[i]);
 getch();
 return 0;
}


/**************** Output ******************/
Screen shot for creation of array


Related programs:

  1. Display array in reverse order
  2. Insert new element in array
  3. Delete an exist element in array
  4. Length of the array
  5. Example of array C program

3.12.2012

Character triangle

Q. Write a C program to print the following character triangle:


EDCBA
DCBA
CBA
BA
A


Ans:


/*c program for display the character triangle*/
#include<stdio.h>
#include<conio.h>
int main()
{
 char ch,r,c;
 int sp;
 printf("Enter last character of triangle : ");
 scanf("%c",&ch);
 if(ch>='a' && ch<='z')
    ch=ch-32;
 printf("\n");
 for(r='A'; r<=ch; ch--)
 {
   for(c=ch; c>=r; c--)
       printf("%c",c);
   printf("\n");
 }
 getch();
 return 0;
}


/****************OUTPUT****************/
Screen shot for character triangle

3.03.2012

kg to pound convert

Q. Write a C program to convert kilograms to pounds.


Ans.


Formula for conversion kg to pounds:


1 kilogram = 2.20462262184878 pounds
Hence, pounds = 2.2 * kilograms


/*c program for conversion to kg to pound*/
#include<stdio.h>
#include<conio.h>
int main()
{
  float kg,pound;
  printf("Enter value of Kilograms : ");
  scanf("%f",&kg);
  pound=2.20462262184878*kg;
  printf("\n\t-- Convert Kilograms to pounds --\n");
  printf("\n%f kg = %f pound",kg,pound);
  getch();
  return 0;
}


The output of above program would be:

Output of Convert Kilograms to pounds C program
Figure: Screen shot for Convert Kilograms to pounds C program




Related programs:


Length conversion programs
  1. Convert meters to feet and feet to meter
  2. Convert meters to inches and Inches to meter
  3. Convert inches to feet and feet to inches
  4. Convert feet to yards and feet to yards

Width and mass Conversion programs
  1. Convert pounds to kilograms
  2. Convert tons to kilograms
  3. Convert kilograms to  tons

Area Conversion programs
  1. Convert square inches to square feet
  2. Convert square feet to square inches
  3. Convert acres to square feet
  4. Convert square feet to  acres


Number System conversion programs
  1. Convert decimal to binary program
  2. Convert decimal to octal program
  3. Convert decimal to hexadecimal program 
  4. Convert binary to decimal program 
  5. Convert binary to hexadecimal program
  6. Convert binary to octal program 
  7. Convert octal to decimal program
  8. Convert octal to binary program
  9. Convert hexadecimal to binary program
  10. Convert hexadecimal to decimal program


Other Conversion programs
  1. Convert digits of number to word program
  2. Convert roman to number program
  3. Convert number to roman program

3.02.2012

Linear Search

Q. Write a C program to search an element in a given list of elements using Liner Search.


Ans.


/*c program for linear search*/
#include<stdio.h>
#include<conio.h>
int main()
{
  int arr[20];
  int i,size,sech;
  printf("\n\t-- Linear Search --\n\n");
  printf("Enter total no. of elements : ");
  scanf("%d",&size);
  for(i=0; i<size; i++)
  {
    printf("Enter %d element : ",i+1);
    scanf("%d",&arr[i]);
  }
  printf("Enter the element to be searched: ");
  scanf("%d",&sech);
  for(i=0; i<size; i++)
  {
    if(sech==arr[i])
    {
      printf("Element exits in the list at position : %d",i+1);
      break;
    }
  }
  getch();
  return 0;
}


/**************** OUTPUT ****************/


Searching element through Linear Search

3.01.2012

Singly linked list

Q. Write a C program that create a singly linked list.


Ans.


/*c program for creating singly linked list*/
#include<stdio.h>
#include<conio.h>
struct single_link_list
{
  int age;
  struct single_link_list *next;
};
typedef struct single_link_list node;
node *makenode(int );
int main()
{
  int ag;
  node *start,*last,*nn;   //nn=new node
  start=NULL;
  while(1)
  {
     printf("Enter your age : ");
     scanf("%d",&ag);
     if(ag==0)
        break;
     nn=makenode(ag);
     if(start==NULL)
     {
        start = nn;
        last = nn;
     }   
     else
     {
        last->next = nn;
        last = nn;
     }
  }
  printf("\n\t****Single linked list****\n\n");
  for(; start!=NULL; start=start->next)
     printf("%d\t",start->age);
  getch();
  return 0;
}


/*creation of node*/


node *makenode(int tmp)
{
  node *nn;
  nn = (node *)malloc(sizeof(node));
  nn->age = tmp;
  nn->next = NULL;
  return nn;
}

/**************** OUTPUT *****************/
Singly linked list


Related post:
  1. What is linked list in C?
  2. Traversing the list
  3. Search item in linked list
  4. Insert an item
  5. Deleting an item
  6. All linked list operation in program