Answer to Question #164804 in Java | JSP | JSF for Neil

Question #164804

Create a sample (Java) program that displays the sum of integers from 1 to 50.


1
Expert's answer
2021-02-18T14:37:38-0500
import java.util.Scanner;

public class SumOfIntegers {

    public static void main(String[] args) {
        int sum=0;
        Scanner sc=new Scanner(System.in);
        System.out.println("Please enter the first term of the series");
        
        int num=sc.nextInt();
        
        System.out.println("The entered number is:"+num);
        System.out.println("Please enter toal number of terms:");
        
        int last_term=sc.nextInt();
        sc.close();
        System.out.println("The entered number is:"+last_term);
        for(int i=0;i<last_term;i++){
            
            sum=sum+num;
            num++;
        }
        System.out.println("The required sum of the series will be : "+sum);

    }

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog