Monday, January 25, 2016

Java Core Exercise 4: if then else Program

Java Programming Exercise 1:  Write Java program to allow the user to input his/her age. Then the program will show if the person is el... thumbnail 1 summary
Java Programming
Exercise 1: Write Java program to allow the user to input his/her age. Then the program will show if the person is eligible to vote. A person who is eligible to vote must be older than or equal to 18 years old.
Enter your age: 18
You are eligible to vote.
Solution:
import java.util.*;
public class JavaExercises
{
public static void main(String[] args)
{
checkEligibility();
}
static void checkEligibility(){
int age;
Scanner sc=new Scanner(System.in);
System.out.print("What is your age?");
age=sc.nextInt();
if(age>=18)
System.out.println("You are eligible to vote.");
else
System.out.println("You are not eligible to vote.");
}
}
Exercise 2: Write a Java program to determine whether an input number is an even number.
Solution:

import java.util.*;
public class JavaExercises
{
public static void main(String[] args)
{
determineEven();
}
static void determineEven(){
int num=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number:");
num=sc.nextInt();
if(num%2==0)
System.out.println("It is an even number.");
else System.out.println("It is an odd number.");
  }
}

No comments

Post a Comment