Here, the program is written to find the sum of ‘n’ numbers. The numbers are taken as input. Then using a ‘for’ loop the numbers are added.
For example, if the number of elements is 2 and the given inputs are: 1, 1.3.
Then the sum will be: 1+1.3=2.3
INPUT: The number of elements and the numbers to be added
OUTPUT: Sum of the given numbers
PROCESS:
Step 1: [Taking the input]
Read n [number of elements]
For i=0 to n-1 repeat
Read arr[i]
[End of ‘for’ loop]
Step 2: [Adding the numbers]
Set sum<-0
For i=0 to n-1 repeat
Set sum<-sum+arr[i]
[End of ‘for’ loop]
Print "The sum of the numbers: sum"
Step 3: Stop.
for(i=0;i<n;i++)-------------------------------------------O(n)
{
sum=sum+arr[i];
}
The time complexity of this program is O(n) where ‘n’ is the number of elements to be added.
The space complexity of this program is O(n) where ‘n’ is the number of elements to be added.