Dsa Lab Assignment 01
Dsa Lab Assignment 01
Output:
Q2. WAP to find out the sum of the numbers stored in an array of integers.
Code:
#include <stdio.h>
#include <stdio.h>
void main()
{
int arr[50];
int i, n, sum=0;
printf("Enter the number of elements you want to enter:");
scanf("%d", &n);
return 0;
}
Output:
Q3. WAP to find largest and smallest element stored in an array.
Code:
#include<stdio.h>
int main()
{
int a[50],i,n,large,small;
printf("\nEnter the number of elements : ");
scanf("%d",&n);
printf("\nInput the array elements : ");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("\nThe smallest element is %d\n",small);
printf("\nThe largest element is %d\n",large);
return 0;
}
Output:
Q4. WAP to display the array elements in ascending order.
Code:
#include <stdio.h>
void main (){
int num[20];
int i, j, a, n;
printf("enter number of elements in an array\n");
scanf("%d", &n);
printf("Enter the elements\n");
for (i = 0; i < n; ++i)
scanf("%d", &num[i]);
for (i = 0; i < n; ++i){
for (j = i + 1; j < n; ++j){
if (num[i] > num[j]){
a = num[i];
num[i] = num[j];
num[j] = a;
}
}
}
printf("The numbers in ascending order is:\n");
for (i = 0; i < n; ++i){
printf("%d\n", num[i]);
}
}
Output:
Q5. WAP Input N element into an array.find out sum of all even number and multiply all
odd number.
Code:
#include <stdio.h>
void main()
{
int i,n,oddmultiplication=1,evenSum=0;
printf("Enter the number of elements you want to input: ");
scanf("%d",&n);
int a[n];
for(int i=0; i<n; i++) {
printf("Enter an integer for index %d: ", i);
scanf("%d",&a[i]);
}
Output:
Q6. WAP to search a particular number from the array.
Code:
#include <stdio.h>
int main() {
int a, i, r, arr[50];
printf("Enter the length of the array: ");
scanf("%d", &a);
i = 0;
while (i < a && r != arr[i]) {
i++;
}
if (i < a) {
printf("The element is found in the position = %d", i + 1);
} else {
printf("Element not found!");
}
return 0;
}
Output:
Q7. WAP to remove a specific element from the array.
Code:
#include<stdio.h>
void main()
{
int key,n, i, index = -1;
printf("Enter number of element in array:");
scanf("%d",&n);
int a[n];
for(int i=0; i<n; i++)
{
printf("Enter an integer for index %d: ", i);
scanf("%d",&a[i]);
}
printf("Enter element to delete\n");
scanf("%d",&key);
}
Output:
Q8. WAP to insert a new element in a specified position in the array.
Code:
#include<stdio.h>
int main()
{
int n, i, pos, num;
printf("Enter number of elements:");
scanf("%d",&n);
int a[n];
printf("Enter integer numbers\n", (n));
for(i = 0; i < (n); i++)
scanf("%d", &a[i]);
if(pos < n)
{
printf("Enter a new number to be inserted at position %d\n", pos);
scanf("%d", &num);
for(i = n; i > pos; i--)
a[i] = a[i - 1];
a[pos] = num;
printf("\n");
return 0;
}
Output:
Q9. WAP to remove duplicates from the Array.
Code:
#include <stdio.h>
int main ()
{
int i, j, k,n;
printf ("Enter the number of elements in an array: ");
scanf (" %d", &n);
int a[n];
printf (" \n Enter elements of an array: \n ",n);
for ( i = 0; i <n; i++)
{
scanf (" %d", &a[i]);
}
for ( i = 0; i <n; i ++)
{
for ( j = i + 1; j <n; j++)
{
if ( a[i] == a[j])
{
for ( k = j; k <- 1; k++)
{
a[k] = a [k + 1];
}
n--;
j--;
}
}
}
printf (" \n Array elements after deletion of the duplicate elements: ");
for ( i = 0; i <n; i++)
{
printf (" %d \t", a[i]);
}
return 0;
}
Output:
Q10. WAP to store numbers into an array of n integers, where the array must contain some
duplicates. Find out the most repeating element in the array.
Code:
#include <stdio.h>
int main()
{
int arr[] = {23, 42, 54, 28, 22, 78, 23, 87, 54};
Output: