import java.io.*;
class aeven
{
public static void main(String[] args)
{
try{
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter 1st number : ");
int num1 = Integer.parseInt(br1.readLine());
System.out.println("Enter 2nd number : ");
int num2 = Integer.parseInt(br1.readLine());
System.out.println("Odd Numbers : ");
for (int i=num1;i <=num2 ; i++)
{
if(i%2!=0 )
{
System.out.print(i+ ", ");
}
}
}
catch(Exception e){}
}
}
Chat with our AI personalities
int sum = 0; for (int i = 51; i < 200; i = i + 2){ sum += i; } return sum;
Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.
public class apples { public static void main(String argc[]) { for(int i = 0; i <= 50; i++){ if(i % 2 != 0){ System.out.println(i); } } } }
It is actually quite easy to write a program in java to do this. The easiest way to do this that I can think of is to use the remainder operator (%) to test whether a number is odd or not. Here is a simple program that will print out all the odd numbers between 1 and 50. public class OddNumbers { public static void main(String[] args) { int i=1; while(i < 50) { if(i%2 != 0) { System.out.println(i); } i++; } } }
You can use the symbol % to see the remainder of the division problem. eg. document.write(11 % 3 + "") will display the number 2 since 3 goes into 11 three times with a remainder of 2. So here's some sample code: function oddEven(num){ if(num % 2 == 0){ return "even" } else{ return "odd" } }