Java challeges with if-else
1.Write a program to check if a number is positive or negative .
class example1 {
public static void main(String[] args) {
int a=-2;
if(a>0) {
System.out.println("A is positive");
}
else{
System.out.println("A is nagative");
}
}
}
OUTPUT:
public class even {
public static void main (String[] args) {
int number=11;
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:
OUTPUT:
OUTPUT:
OUTPUT:
7. Write a program to check if a number is three-digit number.
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.
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.
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.
import java.util.Scanner;
public class leapyear {
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.
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.
import java.util.Scanner;
class smallestnum {
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){
}
else{
System.out.println("c is small");
}
}
}
OUTPUT: