CoineTre

JF-ExcBasic04.Print and Sum

Jan 14th, 2021 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1. /*Write a program to display numbers from given start to given end and their sum. All the numbers will be integers. On the first line you will receive the start number, on the second the end number.
  2. */
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class Ex4PrintAndSum {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         int n1 = Integer.parseInt(scanner.nextLine());
  10.         int n2 = Integer.parseInt(scanner.nextLine());
  11.         int sum = 0;
  12.         for (int i = n1; i <= n2 ; i++) {
  13.             sum +=i;
  14.             System.out.printf("%d ",i);
  15.         }
  16.         System.out.println();
  17.         System.out.println("Sum: " + sum);
  18.     }
  19. }
Add Comment
Please, Sign In to add comment