#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);
}
Output :
Enter no of elements : 5
10 20 30 40 50
Smallest element is : 10
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]
Computer Science » C Program » C Program Examples "How to print hello, world! using c program ? How to find area of a circle using c program ? How to find smallest element in an array using c program ? You all must have this kind of questions in your mind. Below article will solve this puzzle of yours. Just take a look.” - Here we have listed all the programs in C programming language, categorised under different progamming concepts like Basic, Area, Mathematical, Number, Array etc. Check the respective category to view all the programs under that category. C Programs - Basic C Programs C Programs - Number Programs C Programs - Area Programs C Programs - Mathematical Programs C Programs - Array Programs C Programs - Electrical Programs C Programs - Pyramid Programs
Computer Science » C Program » Find Sum of Digits of a Number Program : [php]#include <stdio.h> int main() { long number, digit, sum = 0; printf("Enter the number :"); scanf("%ld", &number); while (number > 0) { digit = number % 10; sum = sum + digit; number = number / 10; } printf("nSum of the digits of given number is : %ld", sum); }[/php] Output : [php]Enter the number : 2882 Sum of the digits of given number is : 20[/php]
Computer Science » C Program » Delete an Element from Specified Location from Array Program : [php]#include<stdio.h> int main() { int arr[30], num, i, loc; printf("nEnter no of elements : "); scanf("%d", &num); //Read elements in an array printf("nEnter %d elements : ", num); for (i = 0; i < num; i++) { scanf("%d", &arr[i]); } //Read the location printf("nLocation of the element to be deleted : "); scanf("%d", &loc); /* loop for the deletion */ while (loc < num) { arr[loc - 1] = arr[loc]; loc++; } num--; // No of elements reduced by 1 //Print Array for (i = 0; i < num; i++) printf("n %d", arr[i]); return (0); }[/php] Output : [php]Enter no of elements : 5 Enter 5 elements : 10 20 30 40 50 Location of the element to be deleted : 3 10 20 40 50[/php]
Computer Science » C Program » Calculate Addition of All Elements in Array Program : [php]#include<stdio.h> int main() { int i, arr[50], sum, num; printf("nEnter number of elements :"); scanf("%d", &num); //Reading values into Array printf("nEnter the values :"); for (i = 0; i < num; i++) scanf("%d", &arr[i]); //Computation of total sum = 0; for (i = 0; i < num; i++) sum = sum + arr[i]; //Printing of all elements of array for (i = 0; i < num; i++) printf("na[%d]=%d", i, arr[i]); //Printing of total printf("nSum of all elements : %d", sum); return (0); }[/php] Output : [php]Enter number of elements : 4 Enter the values : 10 20 30 40 a[0]=10 a[1]=20 a[2]=30 a[3]=40 Sum of all elements : 100[/php]