You are on page 1of 5

// Program to take 5 values from the user and store them in an array

// Print the elements stored in the array


#include <stdio.h>

int main()
{
int values[5];

printf("Enter 5 integers: ");

// taking input and storing it in an array


for(int i = 0; i < 5; ++i)
{
scanf("%d", &values[i]);
}

printf("Displaying integers: ");

// printing elements of an array


for(int i = 0; i < 5; ++i)
{
printf("%d\n", values[i]);
}
return 0;
}

//Program for inserting an element in an array.

#include <stdio.h>

int main()
{
int array[50], position, c, n, value;

printf("Enter number of elements in the array\n");


scanf("%d", &n);

printf("Enter %d elements\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);
printf("Please enter the location where you want to insert an new element\n");
scanf("%d", &position);

printf("Please enter the value\n");


scanf("%d", &value);

for (c = n - 1; c >= position - 1; c--)


array[c+1] = array[c];

array[position-1] = value;

printf("Resultant array is\n");

for (c = 0; c <= n; c++)


printf("%d\n", array[c]);

return 0;
}

//Program for searching an element in an Array

#include <stdio.h>

int main() {
int nbr, i, r, arr[30];

printf("Enter the number of elements in the array: ");


scanf("%d", &nbr);

printf("Enter the array elements: ");


for (i = 0; i < nbr; i++)
{
scanf("%d", &arr[i]);
}

printf("Enter the item to be searched: ");


scanf("%d", &r);

//Research starts from the index 0


i = 0;
while (i < nbr && r != arr[i])
{
i++;
}

if (i < nbr)
{
printf("The element is found in the position = %d", i + 1);
}
else
{
printf("Element not found!");
}

return 0;
}

//Program to delete elements in an array.

#include <stdio.h>

int main()
{
int array[100], position, c, n;

printf(“Enter the number of elements of the array : “);


scanf(“%d”, &n);

printf(“\nInput the array elements : “);

for (c = 0; c < n; c++)


scanf(“%d”, &array[c]);

printf(“\nEnter the position : “);


scanf(“%d”, &position);

if (position >= n+1)


printf(“\nDeletion not possible.\n”);
else
{
for (c = position – 1; c < n – 1; c++)
array[c] = array[c+1];

printf(“\nArray after deletion : “);


for (c = 0; c < n – 1; c++)
printf(“%d\n”, array[c]);
}

return 0;
}

//Program to insert an elements in 2-D array at run time.


#include<stdio.h>
int main(){
int i=0,j=0;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
//traversing 2D array
for(i=0;i<4;i++){
for(j=0;j<3;j++){
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
}//end of j
}//end of i
return 0;
}

//Program to insert an elements in 2-D array at run time.

#include <stdio.h>
void main ()
{
int arr[3][3],i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("\n printing the elements ....\n");
for(i=0;i<3;i++)
{
printf("\n");
for (j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
}
}

// C program to read strings

#include<stdio.h>

int main()
{
// declaring string
char str[50];

// reading string
scanf("%s",str);

// print string
printf("%s",str);

return 0;
}

// C program to illustrate how to pass string to functions


#include<stdio.h>

void printStr(char str[])


{
printf("String is : %s",str);
}

int main()
{
// declare and initialize string
char str[] = "BCA";

// print string by passing string


// to a different function
printStr(str);

return 0;
}

You might also like