-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path40th_program.c
More file actions
34 lines (34 loc) · 846 Bytes
/
Copy path40th_program.c
File metadata and controls
34 lines (34 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Write a program using pointer to compute the sum of all elements stored in an array.
#include<stdio.h>
int main()
{
int size,n,m;
a:
printf("Enter size of an array :");
n=scanf("%d",&size);
if(n!=1 || size<1)
{
printf("Invalid Input! Enter postive no only \n");
scanf("%*s");
goto a;
}
int arr[size],*ptr=arr,sum=0;
for(int i=0;i<size;i++)
{
b:
printf("Enter elements %d :",i+1);
m=scanf("%d",&arr[i]);
if(m!=1)
{
printf("Invalid Input! Enter any number \n"); //Ensure that user not enter any letter or special characters.
scanf("%*s");
goto b;
}
else
{
sum+=ptr[i];
}
}
printf("Sum of all %d elements in array :%d \n",size,sum);
return 0;
}