Question

I am making a java program that's supposed to store each solution in a variable and...

I am making a java program that's supposed to store each solution in a variable and then use that variable when printing the results. I've figured out the other stuff (diameter of a circle, average of three numbers, etc. but can't figure out a quicker way to do the question below other than having a variable add every number. Would like a suggestion on how to do this. Thanks!

"The sum of a series of numbers from 1 to 100
(Hint: Is there a quicker way to do this than 1 + 2 + 3 + 4 + etc…)"

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Method-1:

Source code:

import java.util.*;
public class Main
{
   public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=100,sum=0;
        sum=n*(n+1)/2;
        /*There is a formula for first n numbers Sum
            sum=(n)*(n+1)/2
        */
        System.out.println("Sum of number 1-100:"+sum);
   }
}

Code screenshot:

OUTPUT:

Method-2

SourceCode:

import java.util.*;
public class Main
{
   public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=100,sum=0,i=0;
  
        for (i=1;i<=n;i++)
        {
            sum=sum+i;
        }
        System.out.println("Sum of numbers 1-100:"+sum);
   }
}

Code Screenshot:

Output:

Add a comment
Know the answer?
Add Answer to:
I am making a java program that's supposed to store each solution in a variable and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am taking a computer science course and my professor teaches much too fast. I am...

    I am taking a computer science course and my professor teaches much too fast. I am feeling so overwhelmed and lost. Very worried as well, as I am trying my very hardest in this class but just cant seem to grasp it. I have just been given this assignment.... Can someone explain what I am supposed to do? Is there one class? two? are there supposed to be multiple files? :( thanks! Write a Java class called a Circle that...

  • This is what I am supposed to do: I think he wants us to do this in Java. Thank you! Fractals (a)...

    This is what I am supposed to do: I think he wants us to do this in Java. Thank you! Fractals (a) Write a function in Racket to create a Koch curve fractal. The Koch curve can be constructed by starting with a line (segment), then recursively altering each line segment as follows: divide the line segment into three segments of equal length. draw an equilateral triangle that has the middle segment from step 1 as its base and points...

  • java : Given an int variable n that has been initialized to a positive value and,...

    java : Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a do...while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Use no variables other than n, k, and total.

  • Write a complete Java program, including comments in both the main program and in each method,...

    Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...

  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

  • I am working on my java but I keep getting this one wrong. Method Name: arrayContains...

    I am working on my java but I keep getting this one wrong. Method Name: arrayContains Access modifier: Private Parameters: 1 integer named number Return type: boolean Purpose: This method returns a true or a false indicating if the number is present in an array or not. In order for this method to work, there must be an array already declared. Therefore, in the CLASS BLOCK, declare an array as follows: static int[] listof Numbers = {1, 2, 3, 4,...

  • Need some guidance with the following problems as I am new to Java programming. Any assistance...

    Need some guidance with the following problems as I am new to Java programming. Any assistance is greatly appreciated. Thanks Using printf and specifiers for all print instructions: 1.            Code the following: a.            10% is stored in a variable called discount when the customer is a student; otherwise, it stores 0%. Code this if … else control structure using the conditional operator (ternary operator). The variables discount and student have already been declared. Assume student is a boolean variable. b.           ...

  • JAVA Primitive Editor (Please help, I am stuck on this assignment which is worth a lot...

    JAVA Primitive Editor (Please help, I am stuck on this assignment which is worth a lot of points. Make sure that the program works because I had someone answer this incorrectly!) The primary goal of the assignment is to develop a Java based primitive editor. We all know what an editor of a text file is. Notepad, Wordpad, TextWrangler, Pages, and Word are all text editors, where you can type text, correct the text in various places by moving the...

  • So I am having probelms with figuring out how to do this program the part in...

    So I am having probelms with figuring out how to do this program the part in bold is the part I dont know how to do. This is in JAVA. What I have so far is shown below the question, this is right upto what I have to do next. Choose Your Operation Write a program that uses a WidgetViewer object to do the following: Generate two random integers between 1 and 9 (inclusive). Name one of them x, the...

  • This question is about java program. Please show the output and the detail code and comment.And...

    This question is about java program. Please show the output and the detail code and comment.And the output (the test mthod )must be all the true! Thank you! And the question is contain 7 parts: Question 1 Create a Shape class with the following UML specification: (All the UML "-" means private and "+" means public) +------------------------------------+ | Shape | +------------------------------------+ | - x: double | | - y: double | +------------------------------------+ | + Shape(double x, double y) | |...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT