More Exercises

Exercise - Swap Numbers

Arrow Swap Icons - Download Free Vector Icons | Noun Project

ื›ืชื‘ื• ืชื•ื›ื ื™ืช ื”ืžื—ืœื™ืคื” ืืช ื”ืขืจื›ื™ื ืฉืœื”ื

ื“ืจืš ื

public class Main {
    public static void main(String[] args) {
        int num1 = MyConsole.readInt("enter 1st int: ");
        int num2 = MyConsole.readInt("enter 2nd int: ");

        int temp = num1;
        num1 = num2;
        num2 = temp;

        System.out.println("After swaping:" + " num1 = " + num1 + ", num2 = " + num2);
    }
}
enter 1st int:  100
enter 2nd int:  15
After swaping: num1 = 15, num2 = 100

ื“ืจืš ื‘ - ื‘ืœื™ ืžืฉืชื ื” ืฉืœื™ืฉื™

public class Main {
    public static void main(String[] args) {
        int num1 = MyConsole.readInt("enter 1st int: ");
        int num2 = MyConsole.readInt("enter 2nd int: ");

        num1 = num1 + num2;
        num2 = num1 - num2;
        num1 = num1 - num2;
        System.out.println("After swaping:" + " num1 = " + num1 + ", num2 = " + num2);
    }
}
enter 1st int:  100
enter 2nd int:  15
After swaping: num1 = 15, num2 = 100

ื”ืกื‘ืจ:

input: num1=100, num2=15

num1 = num1 + num2; //(num1: 115) = 100 + 15
num2 = num1 - num2; //(num2: 100) = 115 - 15
num1 = num1 - num2; //(num1: 15)  = 115 - 100

Exercise - Print Digits

Numeral - Math Definitions - Letter N

ื›ืชื•ื‘ ืชื›ื ื™ืช

  • ื”ืงื•ืœื˜ืช ืžืกืคืจ ืฉืœื ืชืœืช-ืกืคืจืชื™ ื•ืžื“ืคื™ืกื” ืืช ื›ืœ ื”ืกืคืจื•ืช ืฉืœื• ื‘ื ืคืจื“

  • ืžื“ืคื™ืกื” ืืช ืกื›ื•ื ื”ืกืคืจื•ืช ืฉืœื•.

public class Main {
    public static void main(String[] args) {
        int num = MyConsole.readInt("enter 3-digit int: ");

        if (num>100 && num<1000){
            int singles = num % 10;
            int tens = num / 10 % 10;
            int hundreds = num / 100;

            System.out.println(hundreds);
            System.out.println(tens);
            System.out.println(singles);
            System.out.println("sum is: "+ (singles+tens+hundreds));
        } else {
            System.out.println("not valid number");
        }

    }
}
enter 3-digit int:  567
5
6
7
sum is: 18

ืžื•ืžืœืฅ ืœืฉื™ื ื›ืืŸ while ืขืœ ืžื ืช ืฉืžื”ืฉืžืชืฉ ื™ื›ื•ืœ ืœื”ื–ื™ืŸ ืขื•ื“ ืžืกืคืจ ืื ื”ื•ื ืœื ื‘ืชื•ื•ืš

Exercise - Clock

Magis Tempo Wall Clock by Lumens - Dwell

ื›ืชื•ื‘ ืชื›ื ื™ืช ืฉืงื•ืœื˜ืช ืžืกืคืจ ืฉืœื ืฉืžื”ื•ื•ื” ืžืกืคืจ ืฉื ื™ื•ืช ื•ื”ื•ืคื›ืช ืื•ืชื• ืœืฉืขื•ืช, ื“ืงื•ืช ื•ืฉื ื™ื•ืช. ื•ืžื“ืคื™ืกื” ืืช ื•ืžื“ืคื™ืกื” ืื•ืชื. ืœื“ื•ื’ืžื”, ืžืกืคืจ 3669 ื”ื•ืคืš ืฉืขื” 1 ,ื“ืงื” 1 ,9 ืฉื ื™ื•ืช.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter seconds: ");
        int seconds_input = scanner.nextInt();

        int hours = seconds_input / 3600;
        int minutes = (seconds_input / 60) % 60 ;
        int seconds = seconds_input % 60;

        System.out.print("hours: " + hours);
        System.out.print(", minutes: " + minutes);
        System.out.print(", seconds: " + seconds + "\n");
    }
}
Enter seconds: 3669
hours: 1, minutes: 1, seconds: 9

Exercise - Roots of a quadratic equation

Graphs of Quadratic Functions | Boundless Algebra

ื ื•ืกื—ืช ื”ืฉื•ืจืฉื™ื

x=โˆ’bยฑb2โˆ’4ac2ax = \frac{-b\pm\sqrt[]{b^2-4ac}}{2a}
determinant=b2โˆ’4acdeterminant = \sqrt[]{b^2-4ac}
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        double secondRoot = 0, firstRoot = 0;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the value of a: ");
        double a = sc.nextDouble();

        System.out.print("Enter the value of b: ");
        double b = sc.nextDouble();

        System.out.print("Enter the value of c: ");
        double c = sc.nextDouble();

        double determinant = (b*b)-(4*a*c);
        double sqrt = Math.sqrt(determinant);

        if(determinant>0){
            firstRoot = (-b + sqrt)/(2*a);
            secondRoot = (-b - sqrt)/(2*a);
            System.out.println("Roots are: "+ firstRoot +" and "+secondRoot);
        }else if(determinant == 0){
            System.out.println("Root is : "+(-b + sqrt)/(2*a));
        }
    }
}
Enter the value of a: 1
Enter the value of b: -1
Enter the value of c: -6

Roots are: 3.0 and -2.0
(x+2)(xโˆ’3)=0x2โˆ’1xโˆ’6=0(x + 2)(x - 3) = 0\\ x^2 -1x - 6 = 0

Exercise -Check if point is in radius of (x,y)

Hint: use Pythagorean theorem ืคื™ืชื’ื•ืจืก

Radius, diameter, & circumference | Circles (article) | Khan Academy
distance
distance<radiusdistance < radius
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        //circle
        System.out.print("Enter x of circle: ");
        double x = sc.nextDouble();

        System.out.print("Enter y of circle: ");
        double y = sc.nextDouble();

        System.out.print("Enter radius: ");
        double radius = sc.nextDouble();

        //point
        System.out.print("Enter x of point: ");
        double x_point = sc.nextDouble();

        System.out.print("Enter y of point: ");
        double y_point = sc.nextDouble();

        //calculation
        double delta_x = x - x_point;
        double delta_y = y - y_point;
        double distance = Math.sqrt(Math.pow(delta_x, 2) + Math.pow(delta_y, 2));

        if (distance < radius)
            System.out.println("Point is inside circle");
        else if (distance == radius)
            System.out.println("Point is on the circles perimeter");
        else
            System.out.println("Point is outside circle");
    }
}
Enter x of circle: 6
Enter y of circle: 0
Enter radius: 5
Enter x of point: 3
Enter y of point: -4
Point is on the circles perimeter

another run

Enter x of circle: 0
Enter y of circle: 0
Enter radius: 5
Enter x of point: 5.1
Enter y of point: 0
Point is outside circle

Exercise - Rotate Square

Last updated

Was this helpful?