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];
}
...
//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();
}
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);
}
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];
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]; }
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;
}
}
write algorithem to find the sum of given n number
In Java: // This program adds all numbers in the array int sum = 0; for (int i = 0; i < myArray.length(); i++) { sum += myArray[i]; }
NA
To write a C++ program to display the student details using class and array of object.
How to write a program for mouse in microprocessor?
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.
maxValue = function (array) {mxm = array[0];for (i=0; i<array.length; i++) {if (array[i]>mxm) {mxm = array[i];}}return mxm;}; i don't know
find even number in array
To write a C++ program to display the student details using class and array of object.
write an assembly language program to find sum of N numbers
How to write a program for mouse in microprocessor?
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.
maxValue = function (array) {mxm = array[0];for (i=0; i<array.length; i++) {if (array[i]>mxm) {mxm = array[i];}}return mxm;}; i don't know
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.
for(int i = 1; i < 100; i+=2) { System.out.println(i); }
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.
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.
find even number in array
Reference:cprogramming-bd.com/c_page1.aspx# array programming
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