#include<stdio.h>
int main()
{
int get()
{
int n;
int sum=0;
int i;
int a[i];
printf("enter the value of n");
scanf("%d",&n);
if(n==5)
{
printf("ok");
}
else
{
printf("not ok dont enter next value re run the program");
}
printf("enter the values of array");
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("the sum of array is ");
for(int i=1;i<=n;i++)
{
sum=sum+a[i];
printf("%d\n",sum);
}
}
get();
return 0;
}
but when i write below code it doesnot give the output as i am expecting
#include<stdio.h>
int main()
{
int get()
{
int n;
int sum=0;
int i;
int a[i];
printf("enter the value of n");
scanf("%d",&n);
if(n==5)
{
printf("ok");
}
else
{
printf("not ok dont enter next value re run the program");
}
printf("enter the values of array");
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the values of array");
for(int i=1;i<=n;i++)
{
printf("%d",a[i]);
}
printf("the sum of array is ");
for(int i=1;i<=n;i++)
{
sum=sum+a[i];
printf("%d\n",sum);
}
}
get();
return 0;
}
it does not give the expected output please help
Problem
You are given an array A consisting of N integers.
Task
Print the sum of the elements in the array.
Note: Some of the integers may be quite large.
Input Format
The first line contains a single integer N denoting the size of the array.
The next line contains space-separated integers denoting the elements of the array.
Output format
Print a single value representing the sum of the elements in the array.
Constraints
1<=N<=10
0<=a[i]<=10^10
Sample Input
5
1000000001 1000000002 1000000003 1000000004 1000000005
Sample Output
5000000015
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long int list;
int main() {
/* Enter your code here. Read input from STDIN. Print output to
STDOUT */
list n,sum=0;
cin>>n;
while(n--)
{
list a;
cin>>a;
sum+=a;
}
cout<<sum;
return 0;
}
this program gave the correct solution.its quite complex to understand but it works according to the expectations.
#include <stdio.h>
int main()
{
int i, n, value;
long long sum = 0;
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%d",&value);
sum =sum + value;
}
printf("%lld", sum);
return 0;
}
this c program is accepted by machine which is much simpler form except how variable value is acting as an array without [] these symbols