Skip to main content

Calculate the sum of elements in an array.

 HOW TO CALCULATE THE SUM OF THE ELEMENT IN A GIVEN ARRAY: -

We will learn how to find the sum of elements in an array is an easy task when you know how to iterate through array elements. In this problem, we will explain your C approach to find the sum of array elements inputted by the user.
Here is the solution to this query in C Languageguage.
 
ALGORITHMS:

STEP 1:- Input the size of the array and store the value in m and arr[m].
STEP 2: -To store the sum of array elements, initialize a variable sum = 0.
STEP 3: -To find the sum of all elements, iterate through each element 
and add the current element to the sum.

STEP 4: -Inside the loop add the current array element to sum i.e.

                sum = sum + arr[i] or sum += arr[i].



CODE:-

#include<stdio.h>
int main()
{
    int i,num,a[100],sum=0;
    printf("Enter the value of number: ");
    scanf("%d",&num);
    printf("Enter value of array: ");
    for(i=0;i<num;i++)
    {
        scanf("%d",&a[i]);
        sum=sum+a[i];
    }
    printf("The sum is %d",sum);
    return 0;
}



OUTPUT:-









FOLLOW THIS WEBPAGE FOR MORE NEW PROGRAMS AND CODING SOLUTIONS.

Comments

Popular posts from this blog

Data Structure Multiple Choice Questions and Answers

  Data Structure Multiple Choice Questions and Answers Our 1000+ multiple choice questions and answers (MCQs) on "Data Structure - I" (along with 1000+ MCQs on "Data Structure - II (Algorithms)") focus on all areas of Data Structure covering 200+ topics. One can read MCQs on Data Structure - II (Algorithms)                 Array and Array Operations This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Array and Array Operations”. 1. Which of these best describes an array? a) A data structure that shows a hierarchical behavior b) Container of objects of similar types c) Arrays are immutable once initialised d) Array is not a data structure View Answer Answer: b Explanation: Array contains elements only of the same type. 2. How do you initialize an array in C? a) int arr[3] = (1,2,3); b) int arr(3) = {1,2,3}; c) int arr[3] = {1,2,3}; d) int arr(3) = (1,2,3); View Answer Answer: c Explanation: This is the syntax ...

sWAP cASE

  sWAP cASE: You are given a string and your task is to  swap cases . In other words, convert all lowercase letters to uppercase letters and vice versa. For Example: Www.HackerRank.com → wWW.hACKERrANK.COM Pythonist 2 → pYTHONIST 2 Function Description Complete the  swap_case  function in the editor below. swap_case  has the following parameters: string s:  the string to modify Returns string:  the modified string Input Format A single line containing a string  . Sample Input 0 HackerRank.com presents "Pythonist 2". Sample Output 0 hACKERrANK.COM PRESENTS "pYTHONIST 2". def   swap_case ( s ):      result =  s . swapcase ()      return ( result ) if  __ name__  ==  '__main__' :      s  =  input ()      result  =  swap_case ( s )      print ( result )

Program to find Smallest element in an array using C

  Program to find Smallest element in an array using C We will find the smallest or minimum element in an array using the C concept. We apply an algorithm that assumes the first element as the smallest element and then compares it with other elements if an element is smaller than it then, it becomes the new smallest element, and this process is repeated till the complete array is scanned. ALGORITHMS:-  STEP 1:-    Initialize the required variables. STEP 2:-  Take the input from the user. STEP 3:-   Assume the smallest element is present at the first position. STEP 4:-    Scan each element with the help of for a loop. STEP 5: -    Check if the element Scanned is new smaller than small. STEP 6:-    If it is smallest than small, change the value of small. STEP 7:-    Print small as it stores the smallest element of the array. CODE:- #include <stdio.h> int  main() {      int  a[ 50 ],p,...