Here is a program to find the sum of n numbers entered .. I have done this programme in c++ but now I was asked to do it using c.. there is some error with the program.The basic array input ,display thing itself is not working.. Kindly help me .. I hope the while loop part is correct

int main()
{
int i,a[10],sum=0,n;
printf("Enter the number of numbers " );
scanf("%d",&n);
printf("Enter the numbers\n");
for(i=0;i<n;i++)
   {
  scanf("%d",a[i]);
   }
   for(i=0;i<n;i++)
   printf("%d",a[i]);


while(n>0)
  {sum=sum+a[i];
i++;
  }
printf("sum = \t%d ",sum);
return 0;
getch();
}

Recommended Answers

All 3 Replies

Check with your silly mistakes...
Add & for the scanf :

scanf("%d", &a[i]);

Also your hope for the while loop went wrong, since its not correct.
Make the changes as :

//while(n>0)
for(i=0; i<n; i++)
{
     sum=sum+a[i];
     //i++;
}

I think it will work fine...;)

U forgot to decrement n in while loop..

Thanks a million.! working perfectly... Thanks for making me understand my flaws

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.