Question

In: Computer Science

find the summation of the sequence of number 1,2,3...n,where n ranges from 1 to 100.use type...

find the summation of the sequence of number 1,2,3...n,where n ranges from 1 to 100.use type long.Display the results in a tabular format that shows n and the corresponding sum. If this were a product instead of sum, what difficulty might you encounter with the variable that accumulates the product?

Solutions

Expert Solution

import java.util.Scanner;

public class DisplaySum {

        public static void main(String[] args) {

                Scanner in = new Scanner(System.in);

                System.out.println("Enter value of n: ");
                int n = in.nextInt();

                long sum = 0;

                System.out.printf("\n\n%-10s%-20s\n", "Terms", "Sum");
                for (int i = 1; i <= n; i++) {
                        sum += i;
                        System.out.printf("%-10d%-20d\n", i, sum);
                        
                }
        }

}
**************************************************
If instead of sum, we take product, then the value of long will very easily be filled.. Like if we take n=20, We will have around 15-16 zeroes at the end of the product.. which will easily fill the range of the long values

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

1. Use induction to prove that Summation with n terms where i=1 and Summation 3i 2...
1. Use induction to prove that Summation with n terms where i=1 and Summation 3i 2 − 3i + 1 = n^3 for all n ≥ 1. 2. Let X be the set of all natural numbers x with the property that x = 4a + 13b for some natural numbers a and b. For example, 30 ∈ X since 30 = 4(1) + 13(2), but 5 ∈/ X since there’s no way to add 4’s and 13’s together to...
Determine the number of permutations of {1,2,3,...,n-1,n} where n is any positive integer and no even...
Determine the number of permutations of {1,2,3,...,n-1,n} where n is any positive integer and no even integer is in its natural position.
Given the sequence {an}∞ n=1 where an = 3ne−6n A) Justify whether the sequence is increasing...
Given the sequence {an}∞ n=1 where an = 3ne−6n A) Justify whether the sequence is increasing or decreasing. B) Is the sequence bounded? If yes, what are the bounds? C) Determine whether the sequence converges or diverges. State any reason (i.e result, theorems) for your conclusion.
Let S = {1,2,3,...,10}. a. Find the number of subsets of S that contain the number...
Let S = {1,2,3,...,10}. a. Find the number of subsets of S that contain the number 5. b. Find the number of subsets of S that contain neither 5 nor 6. c. Find the number of subsets of S that contain both 5 and 6. d. Find the number of subsets of S that contain no odd numbers. e. Find the number of subsets of S that contain exactly three elements. f. Find the number of subsets of S that...
Evaluate the following expressions. a. Summation from k equals 1 to 18 k b. Summation from...
Evaluate the following expressions. a. Summation from k equals 1 to 18 k b. Summation from k equals 1 to 7 left parenthesis 4 k plus 1 right parenthesis c. Summation from k equals 1 to 6 k squared d. Summation from n equals 1 to 8 left parenthesis 1 plus n squared right parenthesis e. Summation from m equals 1 to 4 StartFraction 5 m plus 5 Over 7 EndFraction f. Summation from j equals 1 to 3 left...
Python #Remember that Fibonacci's sequence is a sequence of numbers #where every number is the sum...
Python #Remember that Fibonacci's sequence is a sequence of numbers #where every number is the sum of the previous two numbers. # #Joynernacci numbers are similar to Fibonacci numbers, but #with two differences: # # - Fibonacci numbers are famous, Joynernacci numbers are # not (yet). # - In Joynernacci numbers, even-indexed numbers are the # sum of the previous two numbers, while odd-indexed # numbers are the absolute value of the difference # between the previous two numbers. #...
Find the first four terms of the sequence (an)n≥1 with the given definition. Determine if they...
Find the first four terms of the sequence (an)n≥1 with the given definition. Determine if they are potentially arithmetic or geometric. (a) an is the number of n-bit strings which have more 1’s than 0’s. (Also, write down the strings for n ≤ 4.) (b) an is the number of n-bit strings in which the number of 1’s is greater than or equal to the number of 0’s in every prefix. For example, 010111 would not qualify, since the prefix...
def seq3np1(n): """ Print the 3n+1 sequence from n, terminating when it reaches 1. args: n...
def seq3np1(n): """ Print the 3n+1 sequence from n, terminating when it reaches 1. args: n (int) starting value for 3n+1 sequence return: None """ while(n != 1): print(n) if(n % 2) == 0: # n is even n = n // 2 else: # n is odd n = n * 3 + 1 print(n) # the last print is 1 def main(): seq3np1(3) main() Using the provided code, alter the function as follows: First, delete the print statements...
Find the value of ∑(−1)^n/(3n+1) from n=0 to ∞
Find the value of ∑(−1)^n/(3n+1) from n=0 to ∞
Find the 10-point DFT sequence of the x [n] sequence given below. ?[?] = cos (...
Find the 10-point DFT sequence of the x [n] sequence given below. ?[?] = cos ( 3??/ 5 ) . sin( 4??/ 5 )
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT