answersLogoWhite

0


Best Answer

int myArray[N]; //where N is a constant

int mySum = 0;

...

for (int arrayIndex1 = 0; arrayIndex1 < N; arrayIndex1++)

{

cin >> myArray[arrayIndex1];

}

...

for (int int arrayIndex2 = 0; arrayIndex2 < N; arrayIndex2++)

{

mySum +=myArray[arrayIndex2];

}

...

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

//Sum of n numbers.

#include

void main()

{

int a[],n,sum=0;

printf("enter total number of numbers you want to sum up");

scanf("%d",&n);

printf("Enter the numbers one by one");

for(int i=0;i

{

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

}

for(int j=0;j

{

sum+=a[j];

}

printf("The Sum of %d numbers is = %d",n,sum);

}

IF YOU MEANT "SUM OF FIRST N NUMBERS THEN THIS IS THE PROGRAM IN c"

#include

main()

{

int n;

printf (" enter the number upto which you want the sum : ");

scanf ("%d",&n);

int res;

res = (n * (n+1)/2);

printf ("sum of first %d numbers is %d", n,res);

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Use a valarray rather than an array. A valarray is an array specifically tailored for numeric values, and provides a valarray::sum() method to sum the elements.

#include<iostream>

#include<valarray>

#include<cassert>

int main()

{

std::valarray<int> a {4, 8, 15, 16, 23, 42};

assert (a.sum() 108);

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

If the numbers are consecutive, use a for loop, and just add the counter variable. The following example (Java language) will add the numbers 1, 2, ... 100:

int sum = 0;
for (int i = 1; i <= 100; i++)
sum += i;

If you want to add numbers in an array, loop through the array elements. I will assume the array already exists:

int sum = 0;
for (int i = 1; i <= 100; i++)
sum += myArray[i];


This answer is:
User Avatar

User Avatar

Wiki User

14y ago

In Java: // This program adds all numbers in the array int sum = 0; for (int i = 0; i < myArray.length(); i++) { sum += myArray[i]; }

In Java: // This program adds all numbers in the array int sum = 0; for (int i = 0; i < myArray.length(); i++) { sum += myArray[i]; }

In Java: // This program adds all numbers in the array int sum = 0; for (int i = 0; i < myArray.length(); i++) { sum += myArray[i]; }

In Java: // This program adds all numbers in the array int sum = 0; for (int i = 0; i < myArray.length(); i++) { sum += myArray[i]; }

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

The answer is with using recursion.....]

the code follows

import java.io.*;

import java.util.Scanner;

// This program computes sum 1 to N numbers using recursion

public class SumRecursion

{

public static void main(String[] args)

{

System.out.println("Enter N");

try

{

BufferedReader br = new BufferedReader( new InputStreamReader(System.in));

int number= Integer.parseInt(br.readLine()); // reads number as a string from keyboard and converts to number

/* Scanner sc = new Scanner(System.in); // We can also use this lines for getting N

int number = sc.nextInt(); */

int sum = sumR(number); // calling sumR function

System.out.println("Sum of " + number + " Numbers using Recursion is : " + sum);

}

catch (Exception e)

{

System.out.println ("Give Proper Input i.e. Enter Numeric value only and with in range");

}

}

// Computes a Sum of N numbers by recursion.

public static int sumR(int num)

{

int sum;

if(num==1) return(1) ; else sum=num + sumR(num-1); // function calls itself

return sum;

}

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

write algorithem to find the sum of given n number

This answer is:
User Avatar
User Avatar

Basi Reddy

Lvl 1
1y ago
I am improved

User Avatar

Wiki User

14y ago

In Java: // This program adds all numbers in the array int sum = 0; for (int i = 0; i < myArray.length(); i++) { sum += myArray[i]; }

This answer is:
User Avatar

User Avatar

Zoheb S

Lvl 2
2y ago

NA

This answer is:
User Avatar
User Avatar

Loraine Quigley

Lvl 1
2y ago
I think this is the right answer.

Add your answer:

Earn +20 pts
Q: Write a program to calculate the sum of n numbers using array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write a program to read set of numbers using by an array and display the ascending order of the given input numbers?

To write a C++ program to display the student details using class and array of object.


How do you write an assembly language program to find the sum of n numbers using array?

write an assembly language program to find sum of N numbers


Could you Write a program for 8086 microprocessor that displays on the monitor the average of 2 numbers from an array?

How to write a program for mouse in microprocessor?


How do you write a program which reads a list of ten numbers and print the list in reserve order in c program?

The simplest way is probably to read the numbers into an array and then prints each element of the array starting at the last one and moving backwards.


Write a c program to find the maximum of two numbers and print out with its position?

maxValue = function (array) {mxm = array[0];for (i=0; i&lt;array.length; i++) {if (array[i]&gt;mxm) {mxm = array[i];}}return mxm;}; i don't know


Program to count the number of numbers in an array using 8085 microprocessor?

A program which is used to count the number of numbers in an array using a 8085 microprocessor is known as a assembly language program.


Write a java program to find sum of even and odd numbers in given array?

for(int i = 1; i &lt; 100; i+=2) { System.out.println(i); }


How do you write a program in java to read ten numbers and print sum of ten integers?

Add the numbers into one variable as you read them in. But if you prefer, you can read the numbers into an array and then use a loop to add the numbers together.


How can you write a c program to display numbers in ascending order in array?

I know how to do this and you need to know how to do this. Why don't you do your best at writing this program and if it does not work then ask for help. You will not learn anything if I give you the answer.


Program to print all the even numbers of an array in visual basic?

find even number in array


How do you write a C Program to fill up an Integer Array?

Reference:cprogramming-bd.com/c_page1.aspx# array programming


How do you find sum of 100 numbers using fortran programme?

The following is for F95 and later (due to the use of intrinsic SUM ): My assumptions: -Your numbers are integers -Your numbers are stored in an array -The numbers you are describing are 0-100 program findSum !I assumed integer, replace this with your data type integer, dimension(100) :: numbers integer :: sumOfNumbers !We populate an array with our numbers !Replace this with your numbers do i=1,(size(numbers)+1) numbers = i end do !We find the sum of those numbers sumOfNumbers = sum(numbers) !We write out the sum to prompt write(*,*) 'Sum is: ', sumOfNumbers end program findSum