SET -II
1. WRITE A C PROGRAM TO SPERATE ODD AND EVEN IN SPERATE ARRAY ?
#include <stdio.h>
void main()
{
int arr1[10], arr2[10], arr3[10];
int i,j=0,k=0,n;
printf("\n\nSeparate odd and even integers in separate arrays:\n");
printf("------------------------------------------------------\n");
printf("Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
for(i=0;i<n;i++)
{
if (arr1[i]%2 == 0)
{
arr2[j] = arr1[i];
j++;
}
else
{
arr3[k] = arr1[i];
k++;
}
}
printf("\nThe Even elements are : \n");
for(i=0;i<j;i++)
{
printf("%d ",arr2[i]);
}
printf("\nThe Odd elements are :\n");
for(i=0;i<k;i++)
{
printf("%d ", arr3[i]);
}
printf("\n\n");
}
2. FIND THE SUM OF ELEMENTS IN ARRAY IN C PROGRAM
#include<stdio.h> int main() { //let's assume the maximum array size as 100. //initialize sum as 0. Otherwise, it will take some garbage value. int arr[100], size, i, sum = 0; //Get size input from user printf("Enter array size\n"); scanf("%d",&size); //Get all elements using for loop and store it in array printf("Enter array elements\n"); for(i = 0; i < size; i++) scanf("%d",&arr[i]); //add all elements to the variable sum. for(i = 0; i < size; i++) sum = sum + arr[i]; // same as sum += arr[i]; //print the result printf("Sum of the array = %d\n",sum); return 0; }
SET-IV
1. WRITE A C PROGRAM TO SPERATE ODD AND EVEN IN SPERATE ARRAY ?
#include <stdio.h>
void main()
{
int arr1[10], arr2[10], arr3[10];
int i,j=0,k=0,n;
printf("\n\nSeparate odd and even integers in separate arrays:\n");
printf("------------------------------------------------------\n");
printf("Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
for(i=0;i<n;i++)
{
if (arr1[i]%2 == 0)
{
arr2[j] = arr1[i];
j++;
}
else
{
arr3[k] = arr1[i];
k++;
}
}
printf("\nThe Even elements are : \n");
for(i=0;i<j;i++)
{
printf("%d ",arr2[i]);
}
printf("\nThe Odd elements are :\n");
for(i=0;i<k;i++)
{
printf("%d ", arr3[i]);
}
printf("\n\n");
}
2. WRITE A PROGRAM TO SORT ELEMENT IN ARRAY
- #include <stdio.h>
- int main()
- {
- //Initialize array
- int arr[] = {5, 2, 8, 7, 1};
- int temp = 0;
- //Calculate length of array arr
- int length = sizeof(arr)/sizeof(arr[0]);
- //Displaying elements of original array
- printf("Elements of original array: \n");
- for (int i = 0; i < length; i++) {
- printf("%d ", arr[i]);
- }
- //Sort the array in ascending order
- for (int i = 0; i < length; i++) {
- for (int j = i+1; j < length; j++) {
- if(arr[i] > arr[j]) {
- temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- }
- }
- printf("\n");
- //Displaying elements of array after sorting
- printf("Elements of array sorted in ascending order: \n");
- for (int i = 0; i < length; i++) {
- printf("%d ", arr[i]);
- }
- return 0;
- }
SET- III
1. WRITE A C PROGRAM TO FIND FIRST AND SECOND LARGEST NUMBER IN ARRAY ?
}
2. WRITE IS ARRAY ? HOW TO DECLARE THE ARRAY ?
An array in C/C++ or be it in any programming language is a collection of similar data items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They can be used to store collection of primitive data types such as int, float, double, char, etc of any particular type. To add to it, an array in C
An array in C/C++ or be it in any programming language is a collection of similar data items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They can be used to store collection of primitive data types such as int, float, double, char, etc of any particular type. To add to it, an array in C