Sample Programs in java

Java example program to count number of odd numbers between 1 to 100

[java]
/* Program for odd numbers*/
/**
*@author:Candidjava.com
*@Description: To check a given number is Odd
*/
public class Odd
{
public static void main(String[] args) {
int count = 0;
int n;
for (int i = 1; i <= 100; i++) {
if (i % 2 != 0) {
count = count + 1;
System.out.println(i);// display the odd value
}
}
System.out.println("count:" + count);// display the count value
}
}
[/java]

Table of Contents

Output:

1
3
5
.
.
.
99