Brijmohan lal Sahu - Facebook
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Monday, October 19, 2015

Inserting elements into integer array in C

/*Program to create an integer array and printing
elements of array*/

#include<conio.h>
void main()
{
int a[10],i;
clrscr();
printf("\nEnter 5 numbers:\n");

//inserting elements in array
for(i=0;i<5;i++)
{
printf("\nElement %d =>",i+1);
scanf("%d",&a[i]);
}

//printing elements in array
printf("\nElements in array:");
for(i=0;i<5;i++)
{
printf("\nElement %d =>",i+1);
printf("%d",a[i]);
}
getch();
}

-------------------------------------------------------------------------
Explanation:
--------------------------------------------------------------------------
In above program we have created an array of integer type with size(maximum 10) so our system
automatically index it as...

And we are inserting values using for loop with loop variable i=0 to i<5
that means 5 values  0, 1, 2, 3, 4.



Similarly after successful inserting values into array we are printing those values using again another
for loop.
--------------------------------------------------------------------------------------------------------------------------








Sunday, October 18, 2015

Creating integer Array in C and inserting values


Array

Syntax :

Data_Type Array_Name[Size];

A collection or group of similar type of datatypes.

Means if you create an array of integer type that means it can hold only integer values.

An array can be..
1. Integer type
2. Float type
3. Character type
4. Pointer type ( to store pointers)

In C we use array to create string or we can say an array of character is treated as a string.

Note: C does not provide any exception or error on array out of bound.
That means if you try to insert values more than the specified size of your array in program, during run time it will accept all. But it holds only up to its actual size.

* Array always occupy continuous space while allocation in memory.

-----------------------------------------------------------------------
Just a basic program to create an integer type array with size 10 and inserting values through for loop.

/*Program to create an integer array */
#include<conio.h>
void main()
{
int a[10];  //integer type array
int i;
clrscr();

printf("\nEnter 5 numbers:\n");

//Inserting values into array
for(i=0;i<5;i++)
{
printf("\nElement %d =>",i+1);
scanf("%d",&a[i]);
}
getch();
}



Thursday, September 24, 2015

Program to reverse any string in C

// Program to reverse a string
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char name[20],rev[20];
int i,len,j;

clrscr();
printf("\nEnter a string:");                                        

scanf("%s",&name);


printf("\n name entered is: %s",name);

len=strlen(name);

printf("\nLength of string is: %d",len);
i=len-1;
j=0;
while(i>=0)
{
rev[j]=name[i];
printf("\n j=%d  i= %d of %c",j,i,rev[j]);
j=j+1;
i=i-1;
}
printf("\n reverse of string is: ");
for(j=0;j<len;j++)
{
printf("%c",rev[j]);
}

getch();
}

Thursday, November 6, 2014

Program to calculate sum of numbers in a list in C.

list Sum


Code Box
------------------------------------------

/*program to sum list of numbers
*/

#include<stdio.h>
#include<conio.h>

void main()
{
int num[10];
int i,sum;
sum=0;
clrscr();
printf("\nEnter 10 number:");
for(i=0;i<10;i++)
{
scanf("%d",&num[i]);
}
printf("\nDone");
clrscr();
printf("\nYou entered:\n");

for(i=0;i<10;i++)
{
printf("  %d",num[i]);
}
printf("\n\nSum of the numbers are:\n");
for(i=0;i<10;i++)
{sum=sum+num[i];
}

printf("Sum :%d",sum);


getch();
}

Program to find largest number in list in C.

Largest Number In List.


Code Box
--------------------------------------

/*Program to find largest number in list
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int num[10];
int i,k,large;
clrscr();

printf("\nEnter 10 number:");
for(i=0;i<10;i++)
{
scanf("%d",&num[i]);
}
printf("\nDone");
clrscr();
printf("\nYou entered:\n");

for(i=0;i<10;i++)
{
printf("  %d",num[i]);
}
printf("\n\nLargest numbers in list:\n");
large=num[0];
for(i=0;i<10;i++)
{
if(large<=num[i])
{
large=num[i];
}
}
printf(" %d",large);

getch();
}


Program to delete an element from list in C.

List out put.


Code Box
-------------------------------------------

/* Program to delete an element from list
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,l,k,ins,temp,n,del,pos;
int num[50];
clrscr();

printf("\nEnter List Size(max 50):");
scanf("%d",&n);
printf("\nEnter elements :");
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}

clrscr();
printf("\nElements in list :");
for(i=0;i<n;i++)
{
printf(" %d",num[i]);
}
printf("\nSelect :\n1.for insert into list\n2.for delete from list :");
scanf("%d",&l);
switch(l)
{
case 1:

printf("\nEnter element :");
scanf("%d",&ins);
printf("\nPosition :");
scanf("%d",&pos);
pos=pos-1;
n=n+1;
for(i=pos;i<n;i++)
{ temp=num[i];
  num[i]=ins;
  ins=temp;
}

break;

case 2:

printf("\nSelect \n1.To enter number for delete\n2.for position of number :");
scanf("%d",&j);
switch(j)
{
case 1:
printf("\nEnter element :");
scanf("%d",&del);
for(i=0;i<n;i++)
{
if(num[i]==del)
{for(k=i+1;k<n;k++)
 {
  num[k-1]=num[k];
 }
}
}
break;

case 2:
printf("\nPosition :");
scanf("%d",&pos);
pos=pos-1;
for(i=pos;i<n;i++)
{
num[i]=num[i+1];
}
break;

default :
printf("\nerror:check entry!!!!");
}
n=n-1;

break;
default :
printf("\nerror:check entry!!!!");

}

printf("\nElements in new list :");
for(i=0;i<n;i++)
{
printf(" %d",num[i]);
}

getch();
}

program to sort list of numbers in increasing order in C.

Increasing Order



Code Box
--------------------------------

/*program to sort list of numbers in increasing order
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int num[10];
int i,j,k,l;
clrscr();
printf("\nEnter 10 number:");
for(i=0;i<10;i++)
{
scanf("%d",&num[i]);
}
printf("\nDone");
clrscr();
printf("\nYou entered:\n");

for(i=0;i<10;i++)
{
printf(" %d ",num[i]);
}
printf("\n\nNumbers in increasing order:\n");
for(i=0;i<10;i++)
{

for(l=0;l<10;l++)
{
if(num[i]<=num[l])
{
j=num[i];
num[i]=num[l];
num[l]=j;
}
}
}
for(i=0;i<10;i++)
{
printf("  %d",num[i]);
}


getch();
}

Sunday, November 2, 2014

Program to print Student information using array in C.


Student Information


Code Box
---------------------------------------

/* Program to print Student information */

#include<stdio.h>
#include<conio.h>
void main()
{
int age;
char name[25],city[20],clas[20];
clrscr();

printf("\nEnter name:");
scanf("%s",name);

printf("\nEnter  age:");
scanf("%d",&age);

printf("\nEnter  class:");
scanf("%s",clas);

printf("\nEnter City:");
scanf("%s",city);

printf("\n------------------------------------------");
printf("\n\t Student Information ");
printf("\n\t Name : %s",name);
printf("\n\t Age  : %d",age);
printf("\n\t Class: %s",clas);
printf("\n\t City : %s",city);

printf("\n------------------------------------------");



getch();
}