Calculate Addition of All Elements in Array : C Program

Computer Science » C Program » Calculate Addition of All Elements in Array

Program :

#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
Subject Name : Computer Science
Exam Name : IIT GATE, UPSC ESE, RRB, SSC, DMRC, NMRC, BSNL, DRDO, ISRO, BARC, NIELIT
Posts Name : Assistant Engineer, Management Trainee, Junior Engineer, Technical Assistant
Computer Science Books

GATE 2023 Total InfoENGG DIPLOMAUGC NET Total Info
IES 2023 Total InfoPSUs 2022 Total InfoCSIR UGC NET Total Info
JAM 2023 Total InfoM TECH 2023 Total InfoRAILWAY 2022 Total Info

Related Posts

  • Find the Number of Elements in An Array : C ProgramComputer 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]
    Tags: program, elements, array, computer, science, output, programs
  • Searching Element in an Array : C ProgramComputer 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]
    Tags: program, array, output, computer, science, programs
  • Print Alternate Numbers in an Array : C ProgramComputer 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]
    Tags: program, array, computer, science, output, programs
  • Calculate Sum of Odd and Even Numbers : C ProgramComputer 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]
    Tags: program, calculate, computer, science, output, programs
  • Even Number Pyramid : C ProgramComputer Science » C Program » Even Number Pyramid Program : [php]#include&lt;stdio.h&gt; int main() { int i, j, num = 2; for (i = 0; i &lt; 4; i++) { num = 2; for (j = 0; j &lt;= i; j++) { printf(&quot;%dt&quot;, num); num = num + 2; } printf(&quot;n&quot;); } return (0); }[/php] Output : [php]2 2 4 2 4 6 2 4 6 8 [/php]
    Tags: program, computer, science, output, programs

LEAVE A REPLY

Please enter your comment!
Please enter your name here