mathajax

Java programming Integer Odd or Even and Year is Leap or not


The java code finds a given number whether it  is Odd or Even number.

While executing the code, the user has to enter an input ,"positive integer" from the keyboard and then following steps used to find the given input is Odd or Even.      

  1. Read an input , num
  2. Reminder = num % 2
  3. check Reminder ==1 , print 'Odd Number'
  4. else, print 'Even Number'




import java.util.Scanner;

public class OddEven 
{
 public static void main(String[] args) 
 {
     
 Scanner sc= new Scanner(System.in);
 System.out.println("Find a given number is Odd or Evenn");
 System.out.println("Enter a integer");
 int num=sc.nextInt();  
 sc.close();
  
 if ( num%2 ==1 ) {   
  System.out.println( num + " is Odd number" );
 } else  {
  System.out.println( num + " is Even number" );
 }

 }

}


Output
Find a given number is Odd or Even

Enter a integer
5
5 is Odd number

Output2
Find a given number is Odd or Even

Enter a integer
6
6 is Even number



The java code finds a given year whether it  is leap year or not.

While executing the code, the user has to enter an input ,"positive integer" from the keyboard and then following steps to find the given the input is leap year or not.   


  1. Read  an input , year 
  2. Reminder = year % 4
  3. check Reminder ==0 , print 'leap year'
  4. else, print 'not leap year'



package cljavabasics;

import java.util.Scanner;

public class Leapyear 
{
 public static void main(String[] args) 
 {
    
  System.out.println("Find Given a year is leap year or not n");
  Scanner sc= new Scanner(System.in);
  System.out.println("Enter a year");
  int year=sc.nextInt();  
  sc.close();
  
     
  if ( (year%4) ==0 ) {   
   System.out.println( year + " is leap year" );
  } else {
   System.out.println( year + " is not leap year" );
  }

 }

}


Output
Find Given a year is leap year or not 

Enter a year
2018
2018 is not leap year

Output2
Find Given a year is leap year or not 

Enter a year
2020
2020 is leap year

Comments

Popular posts from this blog

Matrix Forward and Back Substitution

Solving System of Linear Equations by Gauss Jordan Elimination

Solve System of Linear Equations by LU Decompose

Distance Metric - Euclidean Distance

Matrix in Java