Program to print Odd Numbers till "n" numbers (Java)
So today we are going to write program that print all the odd number till n numbers. So this is a very simple program and it does not require much logic to write this program, as even numbers are those which are divisible by 2 that's it.
So lets get started...
Q. Write a program to print all Odd Number till n numbers. The integer n is to be entered by the user.
[Any integer that cannot be divided exactly by 2 is an Odd Number.]
For Example: 1, 3, 7, 9, etc...
Solution:
import java.util.*; class PrintOddNumbers { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number: "); int n = sc.nextInt(); System.out.println("Odd Numbers are: "); for(int i = 1;i <= n;i++) { if(i%2 != 0) { System.out.println(i); } } } }
Output:
Enter a number: 10 Odd Numbers are: 1 3 5 7 9
Variable description used in program:
int n - It is used to accept "n" number from the user.
So that is it for the print Odd Number till "n" numbers program. Hope you find it useful and if you have any query do comment in the comment box and I will try to solve your query. Will see you on next program.
Thank You.
Program to print Odd Numbers till "n" numbers (Java)
Reviewed by Get2Know
on
September 19, 2020
Rating:

No comments: