Monday 14 March 2016

Sum of 'n' numbers


Write a C program to find the sum of 'n' numbers using a for loop.

Input Format: 
Input consists of n+1 integers. The first integer corresponds to n. The next n integers correspond to the numbers to be added.

Output Format: 
Refer Sample Input and Output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]

Sample Input and Output: 
Enter the value of n
4
Enter the number
5
Enter the number
2
Enter the number
1
Enter the number
6
The sum is 14

Code:
  #include<stdio.h>
int main(){
  int a[10],n,sum=0,i;
  printf("Enter the value of n\n");
  scanf("%d",&n);
  for (i=0;i<n;i++)
  {
    printf("Enter the number\n");
    scanf("%d",&a[i]);
    sum=sum+a[i];
  }
  printf("The sum is %d",sum);
  return 0;
}

1 comment:

  1. format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
    what to do in case of this error

    ReplyDelete