#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;}
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:
- It first asks the user to enter the size of the array.
- Then it asks the user to enter the elements of the array one by one.
- It calculates the sum of the elements in the array.
- Finally, it prints the sum of the elements.
Similar Questions
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.