Saturday 29 August 2020

odd even(using do-while loop)

Write a program to find Number is a Odd or Even from 1 to N using do-while loop.
import java.util.Scanner;
public class EvenOdd {
	public static void main(String[] args) 
	{
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the N: ");
		int n=scan.nextInt();
		int i=1;
		//condition used to identify number is odd or even
		System.out.println("Even Numbers:");
		do	
		{
			if(i%2==0)
			{
				System.out.println(i);
			}
			i++;
		}while(i<=n);
		System.out.println("Odd Numbers:");
		i=1;
		do
		{
			if(i%2!=0)
			{
				System.out.println(i);
			}
			i++;
		}	while(i<=n);
    }
}


Output:
Enter the N: 
6
Even Numbers:
2
4
6
Odd Numbers:
1
3
5
 
  

No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32