-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfOddNo.java
More file actions
30 lines (28 loc) · 912 Bytes
/
SumOfOddNo.java
File metadata and controls
30 lines (28 loc) · 912 Bytes
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
30
import java.util.*;
public class SumOfOddNo {
public static void SumOfOddno(int number){
int sum = 0;
for(int i = 1 ; i <= number; i++){
if (i % 2 != 0) {
sum = sum + i;
}
}
System.out.println("Sum of odd number betwen 1 to n is " + sum);
}
public static void SumOfEven(int number){
int sum = 0;
for(int i = 1 ; i <= number; i++){
if (i % 2 == 0) {
sum = sum + i;
}
}
System.out.println("Sum of even number betwen 1 to n is " + sum);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number for finding the sum of odd and even number in the range(1 to number): ");
int n = sc.nextInt();
SumOfOddno(n);
SumOfEven(n);
}
}