Question

In: Computer Science

I am coding with NetBeans LDE Java. Write a program that reads a positive integer n...

I am coding with NetBeans LDE Java.

Write a program that reads a positive integer n , prints all sums from 1 to any integer m 1≤mn . For example, if n=100, the output of your program should be

The sum of positive integers from 1 to 1 is 1;

      The sum of positive integers from 1 to 2 is 3;

      The sum of positive integers from 1 to 3 is 6;

      The sum of positive integers from 1 to 4 is 10;

      The sum of positive integers from 1 to 5 is 15;

*

*

*

*

      The sum of positive integers from 1 to 100 is 5050.

(The program should include a main method, and no tester class is needed)

Solutions

Expert Solution

import java.util.Scanner;
public class Main
{
   public static void main(String[] args)
   {
   Scanner sc = new Scanner(System.in);
   int n = sc.nextInt();
   int total=0;
   for(int i=1;i<=n;i++)
   {
   total=total+i;
       System.out.print("The sum of positive integers from 1 to "+i+" is "+total);     
       if(i==n)
       System.out.println(".");
       else
       System.out.println(";");
   }
   }
}



Related Solutions

Write a java program that asks the user for a positive integer N and then calculates...
Write a java program that asks the user for a positive integer N and then calculates a new value for N based on whether it is even or odd: if N is even, the new N is N/2. (Use integer division.) if N is odd, the new N is 3*N + 1. Repeat with each new N until you reach the value 1. For example, say the initial value is 12. Then the successive values are: 12 (even, next value...
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
Write a program that reads a positive integer n , prints all sums from 1 to...
Write a program that reads a positive integer n , prints all sums from 1 to any integer m 1≤m≤n . For example, if n=100, the output of your program should be The sum of positive integers from 1 to 1 is 1;       The sum of positive integers from 1 to 2 is 3;       The sum of positive integers from 1 to 3 is 6;       The sum of positive integers from 1 to 4 is 10;      ...
I am using NetBeans IDE Java for coding. I would like the code to be commented...
I am using NetBeans IDE Java for coding. I would like the code to be commented for a better understanding. 1. Implement a class Robot that simulates a robot wandering on an infinite plane. The robot is located at a point with integer coordinates and faces north, east, south, or west. Supply methods: public void turnLeft() public void turnRight() public void move() public Point getLocation() public String getDirection() The turnLeft and turnRight methods change the direction but not the location....
I am using NetBeans IDE Java for coding. I would like the code to be commented...
I am using NetBeans IDE Java for coding. I would like the code to be commented for a better understanding. 1. Implement a class Robot that simulates a robot wandering on an infinite plane. The robot is located at a point with integer coordinates and faces north, east, south, or west. Supply methods: public void turnLeft() public void turnRight() public void move() public Point getLocation() public String getDirection() The turnLeft and turnRight methods change the direction but not the location....
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
In c++, write a program that reads a string consisting of a positive integer or a...
In c++, write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format.
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer value n and returns an approximation of e as (1 + 1/n)^n I am not even sure what the question is asking me to do but I think it is asking me to code that function. Does anyone know how to do this?
Write a Java program (name it LargestOccurenceCount) that reads from the user positive non-zero integer values,...
Write a Java program (name it LargestOccurenceCount) that reads from the user positive non-zero integer values, finds the largest value, and counts it occurrences. Assume that the input ends with number 0 (as sentinel value to stop the sentinel loop). The program should ignore any negative input and should continue to run. Hint: to remember/save entered (good) values, you can concatenate them into a string (separated by spaces) that you can output later on. Sample runs showing input prompts and...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT