CORE JAVA
1.Write a program to check if a number is positive or negative.
Program:
import java.util.Scanner;
public class q1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
double g= scanner.nextDouble();
if (g> 0) {
System.out.println("The g is positive.");
} else if (g < 0) {
System.out.println("The g is negative.");
} else {
System.out.println("The g is zero.");
}
scanner.close();
}
}
Output:
2.Write a program to check if a number is even or odd.
Program:
public class q2 {
public static void main (String[] args) {
int number=77;
if(number%2==0){
System.out.println("Number is even");
}else if(number%2==1){
System.out.println("Number is odd");
}else{
System.out.println("Number is not zero");
}
}
}
Output:
3.Write a program to find the largest of three numbers.
Program:
import java.util.Scanner;
public class q3{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter 3 numbers: ");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int largest;
if (a >= b && a >= c) {
largest = a;
} else if (b >= a && b >= c) {
largest = b;
} else {
largest = c;
}
System.out.println("Largest number is: " + largest);
sc.close();
}
}
Output:
4.Write a program to check if a number is divisible by 5.
Program:
class q4 {
public static void main(String[]args) {
int number=20;
if(number%5==0){
System.out.println("Num = "+number + " is divisible by 5");
}
else{
System.out.println("Num = "+number + " is not divisible by 5");
}
}
}
Output:
5.Write a program to check if a number is divisible by 3 and 5.
Program:
class q5 {
public static void main(String[] args) {
int a=15;
if(a%3==0 && a%5==0) {
System.out.println("a="+a + "is divisible by 3");
}
else{
System.out.println("a is not divisible by 3,5");
}
}
}
Output:
6.Write a program to check if a person is eligible to vote (age ≥ 18).
Program:
import java.util.Scanner;
class age {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int age;
System.out.println("Enter your age");
age=sc.nextInt();
if(age>=18){
System.out.println("Person is eligible vote.");
}else{
System.out.println("Person is not eligible vote.");
}
sc.close();
}
}
Output:
7.Write a program to check if a number is three-digit number.
Program:
import java.util.Scanner;
class threedigit{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int number;
System.out.println("Enter a number:");
number=sc.nextInt();
if(number>=100 && number<999){
System.out.println("Number = " +number + " is a three digit number");
}
else{
System.out.println("Number = " +number + " is not a three digit number");
}
sc.close();
}
}
Output:
8.Write a program to check if two numbers are equal or not.
Program:
class equals {
public static void main(String[] args) {
int a=10, b=20, c=10;
System.out.println(" a="+a +"\n b="+b +"\n c="+c);
if(a==b && b==c){
System.out.println("a and b is equal");
}else if(a==c ){
System.out.println("a and c is equal");
}
else{
System.out.println("a and c is not equal");
}
}
}
Output:
9.Write a program to display grade (A/B/C/F) based on marks.
Program:
import java.util.Scanner;
class grade {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int marks,grade;
System.out.println("Enter the marks:");
marks=sc.nextInt();
if(marks>=90 && marks < 100){
System.out.println("Grage: A");
}
else if(marks>=65 && marks<90){
System.out.println("Grade: B");
}
else if(marks>=35 && marks<65){
System.out.println("Grade: C");
}
else if(marks>0 && marks<35){
System.out.println("Grade: Fail");
}
else{
System.out.println("Invaild marks");
}
sc.close();
}
}
Output:
10.Write a program to check if a year is a leap year or not.
Program:
import java.util.Scanner;
class q10 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int year;
System.out.println("Enter a year:");
year=sc.nextInt();
if(year%4==0){
System.out.println("It is leap year");
}
else{
System.out.println("It is not a leap year");
}
}
}
Output:
11.Write a program to find the smallest of two numbers.
Program:
import java.util.Scanner;
class smallest2 {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int a,b;
System.out.println("Enter a number a:");
a=sc.nextInt();
System.out.println("Enter a number b:");
b=sc.nextInt();
if(a<b){
System.out.println("a is small");
}
else{
System.out.println("b is small");
}
sc.close();
}
}
Output:
12.Write a program to find the smallest of three numbers.
Program:
import java.util.Scanner;
class q12 {
public static void main (String[] args){
Scanner sc=new Scanner(System.in);
int a,b,c;
System.out.println("Enter the number a:");
a=sc.nextInt();
System.out.println("Enter the number b:");
b=sc.nextInt();
System.out.println("Enter the number c:");
c=sc.nextInt();
if(a<=b && a<=c){
System.out.println("a is small");
}else if(b<=a && b<=c){
System.out.println("b is small");
}
else{
System.out.println("c is small");
}
}
}
Output:
Comments
Post a Comment