Knowee
Questions
Features
Study Tools

#include<stdio.h>int main(){ int arr[100],size,i,sum=0,n; printf("Enter the size:"); scanf("%d",&size); for(i=0;i<n;i++) { printf("Enter %d elements,:\t"); scanf("%d",&arr[i]); } for(i=0;i<n;i++) { sum+=arr[i]; } printf("Sum of element=%d",sum); return 0;}

Question

#include<stdio.h>int main(){ int arr[100],size,i,sum=0,n; printf("Enter the size:"); scanf("%d",&size); for(i=0;i<n;i++) { printf("Enter %d elements,:\t"); scanf("%d",&arr[i]); } for(i=0;i<n;i++) { sum+=arr[i]; } printf("Sum of element=%d",sum); return 0;}

🧐 Not the exact question you are looking for?Go ask a question

Solution

This is a C program that calculates the sum of elements in an array. However, there is a small mistake in the code. You are using the variable 'n' in your for loop, but you have not initialized it. You should use 'size' instead of 'n' because you have already initialized 'size' with the size of the array. Here is the corrected code:

#include<stdio.h>
int main(){
    int arr[100],size,i,sum=0;
    printf("Enter the size:");
    scanf("%d",&size);
    for(i=0;i<size;i++)
    {
        printf("Enter %d elements,:\t");
        scanf("%d",&arr[i]);
    }
    for(i=0;i<size;i++)
    {
        sum+=arr[i];
    }
    printf("Sum of element=%d",sum);
    return 0;
}

This program works as follows:

  1. It first asks the user to enter the size of the array.
  2. Then it asks the user to enter the elements of the array one by one.
  3. It calculates the sum of the elements in the array.
  4. Finally, it prints the sum of the elements.

This problem has been solved

Similar Questions

0/0

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.