Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Program {
- public static int reverseNumber(int num) {
- String str = "";
- while (num != 0) {
- str += num % 10;
- num /= 10;
- }
- return Integer.parseInt(str);
- }
- public static double average(int[] arr) {
- double sum = 0;
- for (int i = 0; i < arr.length; i++) {
- sum += arr[i];
- }
- return sum / arr.length;
- }
- public static double findX(int a, int b) {
- double x = (double) -b / a;
- return x;
- }
- public static int reverseNum2(int num) {
- StringBuilder sb = new StringBuilder();
- sb.append(num);
- sb.reverse();
- return Integer.parseInt(sb.toString());
- }
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- while (true) {
- System.out.println("1. Reverse number");
- System.out.println("2. Find average");
- System.out.println("3. Find x");
- System.out.println("4. Exit");
- char menu = scan.nextLine().charAt(0);
- if (menu == '1') {
- System.out.print("Enter your number: ");
- int num = Integer.parseInt(scan.nextLine());
- if (num < 0) {
- System.out.println("Negative number!");
- } else {
- System.out.println("Reversed: " + reverseNumber(num));
- }
- } else if (menu == '2') {
- System.out.print("Enter your numbers: ");
- String[] nums = scan.nextLine().split(" ");
- if (nums.length == 0) {
- System.out.println("Empty row!");
- } else {
- int[] arr = new int[nums.length];
- for (int i = 0; i < nums.length; i++) {
- arr[i] = Integer.parseInt(nums[i]);
- }
- System.out.printf("Average: %.2f\n", average(arr));
- }
- } else if (menu == '3') {
- System.out.print("a = ");
- int a = Integer.parseInt(scan.nextLine());
- System.out.print("b = ");
- int b = Integer.parseInt(scan.nextLine());
- if (a == 0) {
- System.out.println("No real roots!");
- } else {
- System.out.printf("x = %.2f\n", findX(a, b));
- }
- } else {
- break;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment