Program Odd Number Calculator to n number ~ IN JAVA
PROGRAME THAT CALULATED THE ODD NUMBER IN JAVA
import java.util.Scanner;
public class SumOfOddMNumber {
public static void main(String[] args) {
int input = input("Enter you number: ");
if (checkNumberIsOdd(input)) {
System.out.println("Your odd number " + input + " sum is " + sumOfOddNumber(input));
} else {
System.out.println("You number is not a even number ");
}
}
// These function use for taking input from user
private static int input(String command) {
Scanner input = new Scanner(System.in);
System.out.print(command);
return input.nextInt();
}
// This function help to check the number was odd or not
private static boolean checkNumberIsOdd(int number) {
return number % 2 != 0;
}
// RETURN SUM Of odd number
private static int sumOfOddNumber(int limit) {
int sum = 0, i = 0;
while (i <= limit) {
if (i % 2 != 0) {
sum += i;
}
i++;
}
return sum;
}
}
Comments
Post a Comment