Search Logic Blocks

Saturday, January 18, 2020

Java: Let's Work on Some Problems

Problem : Write a program which asks user to enter the number and checks if number is divisible by 2 and 5. Print appropriately for four conditions -
  1. If number is divisible by only 2
  2. if number is divisible by only 5
  3. If number is divisible by both 2 and 5
  4. if number is not divisible by any of them
Class DivBy2Or5 (DivBy2Or5.java)
import java.util.Scanner;

public class DivBy2Or5 {
    public static void main(String args[]) {
            Scanner sc = new Scanner(System.in);
            System.out.println("Please enter a number:");
            int i = sc.nextInt();
            if((i % 10) == 0) {
                System.out.println("Number " + i + " is divisible by both 2 and 5");
            } else if ((i % 2) == 0) {
                System.out.println("Number " + i + " is divisible by 2 only");
            } else if ((i % 5) == 0) {
                System.out.println("Number " + i + " is divisible by 5 only");
            } else {
                System.out.println("Number " + i + " is not divisible by both 2 and 5");
            }
    }
}
Problem : Write an program in which ask the user to enter his full name and print "Your name is [Entered Name]."

Class FullName (FullName.java)
import java.util.Scanner;

public class FullName {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter your full name:");
        String fullName = sc.nextLine();
        System.out.println("Your name is " + fullName);
    }
}
Problem : Write a program to ask user to enter two double values and also an operator ('+', '-', '*', '/', '%'). Then the program should perform the operation chosen on two entered values.

Class SwitchOper (SwitchOper.java)
import java.util.Scanner;

public class SwitchOper {
    public static void main(String args[]) throws java.io.IOException {
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter two numbers: ");
        double val1 = sc.nextDouble();
        double val2 = sc.nextDouble();
        double finalVal = 0;

        System.out.println("Choose the operator '+', '-', '*', '/', '%': ");
        char oper = (char) System.in.read();
        switch(oper) {
            case '+':
                finalVal = val1 + val2;
                break;
            case '-':
                finalVal = val1 - val2;
                break;
            case '*':
                finalVal = val1 * val2;
                break;
            case '/':
                finalVal = val1 / val2;
                break;
            case '%':
                finalVal = val1 % val2;
                break;
            default:
                finalVal = 0;
                System.out.println("Default");
        }
        System.out.println("Final Value is " + finalVal);
    }
}
Problem : Write a program to print if a number is even or odd using the conditional operator '? :'.

Class EvenOddCond (EvenOddCond.java)
import java.util.Scanner;

public class EvenOddCond {
    public static void main(String args[]) {
        String strEvenOdd;
        int num;
        System.out.println("Please enter the number:");
        Scanner sc = new Scanner(System.in);
        num = sc.nextInt();
        strEvenOdd = ((num % 2) == 0) ? "Even" : "Odd";
        System.out.println(num + " is an " + strEvenOdd + " number.");
    }
}
Problem : Write a program to get sum of even numbers between 0 and 20. 

Class SumEven20 (SumEven20.java)
public class SumEven20 {
    public static void main(String args[]) {
        int i, sum = 0;
        for(i = 0; i <= 20; i += 2) {
            sum += i;
        }
        System.out.println("Sum of even numbers between 0 and 20 is " + sum);
    }
}

No comments: