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.
- Read an input , num
- Reminder = num % 2
- check Reminder ==1 , print 'Odd Number'
- 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.
- Read an input , year
- Reminder = year % 4
- check Reminder ==0 , print 'leap year'
- 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
Post a Comment