C Program to Sum of all Elements in an Array

C Program to Sum of all Elements in an Array

C program to sum of all elements in an array; Through this tutorial, we will learn how to calculate sum of all elements in an array using standard method, function and recursion in c programs.

Programs to Sum of all Elements in an Array in C

To calculate sum of all elements in an array using standard method, function and recursion in c programs:

  • C Program to Sum of all Elements in an Array using Standard Method
  • C Program to Sum of all Elements in an Array using Function
  • C Program to Sum of all Elements in an Array using Recursion

C Program to Sum of all Elements in an Array using Standard Method

#include <stdio.h>
 
int main()
{
    int a[1000],i,n,sum=0;
   
    printf("Enter size of the array : ");
    scanf("%d",&n);
 
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
 
    
    for(i=0; i<n; i++)
    {
         
        sum+=a[i];
    }
     printf("sum of array is : %d",sum);
 
    return 0;
}

The output of the above c program; as follows:

Enter size of the array : 5
Enter elements in array : 1 2 3 4 5
sum of array is : 15

C Program to Sum of all Elements in an Array using Function

#include <stdio.h>
 
 int sumofarray(int a[],int n)
 {
 	int i,sum=0;
 
    for(i=0; i<n; i++)
    {
         sum+=a[i];
         
    }
 	return sum;
 }
int main()
{
    int a[1000],i,n,sum;
   
    printf("Enter size of the array : ");
    scanf("%d", &n);
 
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
 
    sum=sumofarray(a,n);
    printf("sum of array is :%d",sum);
 
    
}

The output of the above c program; as follows:

Enter size of the array : 5
Enter elements in array : 4 5 6 7 8
sum of array is :30

C Program to Sum of all Elements in an Array using Recursion

#include <stdio.h>
 
 int sumofarray(int a[],int n,int i)
 {
 	   if(i<n)
        return a[i]+sumofarray(a,n,++i);
	   
        return 0;
 	
 }
int main()
{
    int a[1000],i,n,sum;
   
    printf("Enter size of the array : ");
    scanf("%d", &n);
 
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d", &a[i]);
    }
 
    sum=sumofarray(a,n,0);
     printf("sum of array is :%d",sum);
 
    
}

The output of the above c program; as follows:

Enter size of the array : 5
Enter elements in array : 7 5 1 2 5
sum of array is :20

Recommended C Programs

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *