#include <stdio.h>
int main()
{
int Arr[50];
int n, i, sum = 0;
float average;
printf("Enter the number of elements in Array : ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("nEnter element %d : ", i + 1);
scanf("%d", &Arr[i]);
sum = sum + Arr[i];
}
average = (float)sum/n;
printf("nThe sum of the elements array is : %d", sum);
printf("nThe average of the elements array is : %0.2f", average);
return 0;
}
Output :
Enter the number of elements in Array : 5
Enter element 1 : 20
Enter element 2 : 30
Enter element 3 : 40
Enter element 4 : 50
Enter element 5 : 60
The sum of the elements array is : 200
The average of the elements array is : 40.00
Computer Science » C Program » Insert Element in An Array Program : [php]#include<stdio.h> int main() { int arr[30], element, num, i, location; printf("nEnter no of elements : "); scanf("%d", &num); for (i = 0; i < num; i++) { scanf("%d", &arr[i]); } printf("nEnter the element to be inserted : "); scanf("%d", &element); printf("nEnter the location : "); scanf("%d", &location); //Create space at the specified location for (i = num; i >= location; i--) { arr[i] = arr[i - 1]; } num++; arr[location - 1] = element; //Print out the result of insertion for (i = 0; i < num; i++) printf("n %d", arr[i]); return (0); }[/php] Output : [php]Enter no of elements : 5 10 20 30 40 50 Enter the element to be inserted : 60 Enter the location : 3 10 20 60 30 40 50[/php]
Computer Science » C Program » Reversing an Array Elements Program : [php]#include<stdio.h> int main() { int arr[30], i, j, num, temp; printf("nEnter no of elements : "); scanf("%d", &num); //Read elements in an array for (i = 0; i < num; i++) { scanf("%d", &arr[i]); } j = i - 1; // j will Point to last Element i = 0; // i will be pointing to first element while (i < j) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; // increment i j--; // decrement j } //Print out the Result of Insertion printf("nArray after reversal : "); for (i = 0; i < num; i++) { printf("%d t", arr[i]); } return (0); }[/php] Output : [php]Enter no of elements : 5 10 20 30 40 50 Array after reversal : 50 40 30 20 10 [/php]
Computer Science » C Program » Searching Element in an Array Program : [php]#include<stdio.h> int main() { int a[30], element, num, i; printf("nEnter no of elements : "); scanf("%d", &num); printf("nEnter the values : "); for (i = 0; i < num; i++) { scanf("%d", &a[i]); } //Read the element to be searched printf("nEnter the element to be searched : "); scanf("%d", &element); //Search starts from the zeroth location i = 0; while (i < num && element != a[i]) { i++; } //If i < num then Match found if (i < num) { printf("nNumber found at the location : %d", i + 1); } else { printf("nNumber not found."); } return (0); }[/php] Output 1: [php]Enter no of elements : 5 Enter the values : 10 20 30 40 50 Enter the element to be searched : 30 Number found at the location : 3[/php] Output 1: [php]Enter no of elements : 5 Enter the values : 10 20 30 40 50 Enter the element to be searched : 60 Number not found.[/php]
Computer Science » C Program » Find Smallest Element in Array Program : [php]#include<stdio.h> int main() { int a[30], i, num, smallest; printf("nEnter no of elements : "); scanf("%d", &num); //Read n elements in an array for (i = 0; i < num; i++) scanf("%d", &a[i]); //Consider first element as smallest smallest = a[0]; for (i = 0; i < num; i++) { if (a[i] < smallest) { smallest = a[i]; } } // Print out the Result printf("nSmallest element is : %d", smallest); return (0); }[/php] Output : [php]Enter no of elements : 5 10 20 30 40 50 Smallest element is : 10[/php]
Computer Science » C Program » Find Largest Element in Array Program : [php]#include<stdio.h> int main() { int a[30], i, num, largest; printf("nEnter no of elements : "); scanf("%d", &num); //Read n elements in an array for (i = 0; i < num; i++) scanf("%d", &a[i]); //Consider first element as largest largest = a[0]; for (i = 0; i < num; i++) { if (a[i] > largest) { largest = a[i]; } } // Print out the Result printf("nLargest element is : %d", largest); return (0); }[/php] Output : [php]Enter no of elements : 5 10 20 30 40 50 Largest element is : 50[/php]