#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);
}
Output :
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
Computer Science » C Program » Find the Number of Elements in An Array Program : [php]#include <stdio.h> int main() { int array[] = {10, 20, 30, 40, 50, 60, 70,80,90,100}; int n,m; n = sizeof(array); m = n/sizeof(int); printf("Size of the given array is : %d", m); return 0; }[/php] Output : [php]Size of the given array is : 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 » Print Alternate Numbers in an Array Program : [php] int main() { int array[9]; int i; printf("Enter the element of the array : "); for (i = 0; i < 9; i++) scanf("%d", &array[i]); printf("nAlternate elements of the entered array : n"); for (i = 0; i < 9; i += 2) printf( "%dn", array[i]) ; }[/php] Output : [php]Enter the element of the array : 1 2 3 4 5 6 7 8 9 Alternate elements of the entered array : 1 3 5 7 9[/php]
Computer Science » C Program » Calculate Sum of Odd and Even Numbers Program : [php]#include <stdio.h> int main() { int i, n; int sum_odd = 0, sum_even = 0; printf("Enter the number up to which you want to calculate sum : "); scanf("%d", &n); for (i = 1; i <= n; i++) { if (i % 2 == 0) sum_even = sum_even + i; else sum_odd = sum_odd + i; } printf("nSum of all even numbers is : %d", sum_even); printf("nSum of all odd numbers is : %d", sum_odd); }[/php] Output : [php]Enter the number up to which you want to calculate sum : 100 Sum of all even numbers is : 2550 Sum of all odd numbers is : 2500[/php]
Computer Science » C Program » Even Number Pyramid Program : [php]#include<stdio.h> int main() { int i, j, num = 2; for (i = 0; i < 4; i++) { num = 2; for (j = 0; j <= i; j++) { printf("%dt", num); num = num + 2; } printf("n"); } return (0); }[/php] Output : [php]2 2 4 2 4 6 2 4 6 8 [/php]