Program to print odd numbers bewtween two number given by user using method in java language.
import java.util.*;
class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("enter the first and last no. in between you want to print odd numbers=");
int a=sc.nextInt();
int b=sc.nextInt();
// create object
Print obj=new Print();
// calling method
obj.oddNumber(a,b);
}
}
class Print{
void oddNumber(int x, int y)
{
System.out.println("odd numbers are=");
for(int i=x+1; i<=y-1; i++){
if(i%2!=0)
System.out.print(i+" ");
else
continue;
}
}
}
■》output->
enter the first and last no. in between you want to print odd numbers=3
26
odd numbers are=
5 7 9 11 13 15 17 19 21 23 25
Comments
Post a Comment