You are on page 1of 9

Name : Anurag Rai

Registration number : 180909428


Section : L
Re-Registered

LAB MANUAL 5

1. Write a C program to find the largest and smallest element in a 1D


array.

#include <stdio.h>
int main()
{
int n;
printf("Enter the size of the array:\n");
scanf("%d",&n);
int A[n];
printf("Enter elements into the array:\n");
for (int i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
int s=A[0];
int g=A[n-1];
for (int i=0;i<n;i++)
{
if (s>A[i])
s=A[i];
if (g<A[i])
g=A[i];
}
printf("The Smallest number in the array is: %d\nThe Greatest number
in the array is: %d\n",s,g);
printf("My name is Anurag Rai and the date is 04-06-21.\n");
return 0;
}

2. Write a C program to print all the prime numbers in a given 1D array.

#include <stdio.h>
int main()
{
int n;
printf("Enter size of the array:\n");
scanf("%d",&n);
int A[n];
printf("Enter numbers in the array:\n");
for (int i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
printf("The Prime Numbers are:\n");
for (int i=0;i<n;i++)
{
int c=0;
for (int j=1;j<=A[i];j++)
{
if (A[i]%j==0)
c++;
}
if (c==2)
printf("%d\n",A[i]);
}
printf("My name is Anurag Rai and the date is 04-06-21.\n");
return 0;
}

3. Write a C program to arrange the given elements in a 1D array in


ascending and descending order using bubble sort method. [Hint: use
switch case (as case ‘a’ and case ‘d’) to specify the order].

#include <stdio.h>
int main()
{
int n;
printf("Enter size of the array:\n");
scanf("%d",&n);
int A[n];
printf("Enter numbers in the array:\n");
for (int i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
char c;
printf("Enter \'a\' for arranging the array in ascending order\nEnter \'d\'
for arranging the array in descending order\n");
fflush(stdin);
scanf("%c",&c);
switch (c)
{
case 'a':
for (int i=0;i<n;i++)
{
for (int j=0;j<n-1-i;j++)
{
if (A[j]>A[j+1])
{
int t=A[j];
A[j]=A[j+1];
A[j+1]=t;
}
}
}
printf("The array in the Ascending order is:\n");
for (int i=0;i<n;i++)
{
printf("%d ",A[i]);
}
break;
case 'd':
for (int i=0;i<n;i++)
{
for (int j=0;j<n-1-i;j++)
{
if (A[j]<A[j+1])
{
int t=A[j];
A[j]=A[j+1];
A[j+1]=t;
}
}
}
printf("The array in the Descending order is:\n");
for (int i=0;i<n;i++)
{
printf("%d ",A[i]);
}
break;
default:
printf("Invalid input\n");
break;
}
printf("\nMy name is Anurag Rai and the date is 04-06-21.\n");
return 0;
}
4. Write a C program to insert an element into a 1D array by getting an
element and the position from the user.

#include <stdio.h>
int main()
{
int n;
printf("Enter the size of the array:\n");
scanf("%d",&n);
int A[n+1];
printf("Enter elements into the array:\n");
for (int i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
int ele,p;
printf ("Enter the element to be inserted:\n");
scanf("%d",&ele);
printf("Enter the position at which the element is to be entered:\n");
scanf("%d",&p);
for (int i=n-1;i>=p-1;i--)
{
A[i+1]=A[i];
}
A[p-1]=ele;
printf("The Array after inserting the element is :\n");
for (int i=0;i<=n;i++)
{
printf("%d ",A[i]);
}
printf("\nMy name is Anurag Rai and the date is 04-06-21.\n");
return 0;
}
5. Write a C program to search the position of the number that is entered
by the user and delete that number from the array and display the
resultant array elements.

#include <stdio.h>
int main()
{
int n;
printf("Enter the size of the array:\n");
scanf("%d",&n);
int A[n];
printf("Enter elements into the array:\n");
for (int i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
int ele,p;
int f=0;
printf("Enter the number to be deleted:\n");
scanf("%d",&ele);
for (int i=0;i<n;i++)
{
if (ele ==A[i] )
{
p=i;
f=1;
break;
}
}
if (f==1)
{
printf("The resultant array after deleting %d from the array is:\n",ele);
for (int i=p;i<n-1;i++)
{
A[i]=A[i+1];
}
for (int i=0;i<n-1;i++)
{
printf("%d ",A[i]);
}
}
else
{
printf("The element is not present in the array.\n");
}
printf("\nMy name is Anurag Rai and the date is 04-06-21.\n");
return 0;
}

You might also like