Saturday 29 August 2020

Odd Even numbers from 1 to N

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

Output:
Enter the N: 
10
1 is odd number
2 is Even number
3 is odd number
4 is Even number
5 is odd number
6 is Even number
7 is odd number
8 is Even number
9 is odd number
10 is Even number
Even count:  5
Odd count:  5


  

No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32