-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram39.java
29 lines (24 loc) · 893 Bytes
/
Program39.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* Program 39
Write a program to pass 2 int values to a function such that the first number is lower than the second number. The integer values will be the range and the function returns the sum of the numbers in the range.
1/3/24 */
import java.util.Scanner;
public class Program39 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter lower number: ");
int l = sc.nextInt();
System.out.print("Enter higher number: ");
int u = sc.nextInt();
if (l > u) {
System.out.println("Lower number should be less than the higher number.");
System.exit(1);
}
System.out.println("Sum: " + sum(l, u));
}
static int sum(int l, int u) {
int sum = 0;
for (int i = l; i <= u; i++)
sum += i;
return sum;
}
}