Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.lang.*;
- public class EX_01{
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //EX_01
- {
- int arr[] = new int[3];
- Scanner sc = new Scanner(System.in);
- System.out.println("Hello!\nPlease enter 3 numbers:");
- for(int i=0;i<3;i++)
- {
- arr[i]=sc.nextInt();
- }
- int pow1=arr[0]*arr[0], pow2=arr[1]*arr[1], pow3=arr[2]*arr[2];
- if (pow1==pow2+pow3||pow2==pow1+pow3||pow3==pow1+pow2)
- {
- System.out.println("Pythagorean triplet!");
- }
- else
- {
- System.out.println("NO!");
- }
- }
- //EX_02
- {
- int num,print=1,counter=0;
- System.out.println("\nHello!\nPlease enter a number (bigger then 1):");
- Scanner sc = new Scanner(System.in);
- num= sc.nextInt();
- for (int i=0;i<num*num;i++)
- {
- System.out.print(print+"\t");
- print++;
- counter++;
- if (counter==num)
- {
- counter=0;
- System.out.println();
- }
- }
- }
- //EX_03
- {
- System.out.println("\nHello!\nPlease give me a string:");
- Scanner sc= new Scanner(System.in);
- String str= sc.nextLine();
- int begin=0, end=str.length()-1;
- while(begin<end)
- {
- if (str.charAt(begin)!=str.charAt(end))
- {
- System.out.println("Not a palindrome!");
- break;
- }
- begin++;
- end--;
- }
- if (begin==end)
- {
- System.out.println("Palindrome with middle!");
- }
- if (begin>end)
- {
- System.out.println("Palindrome without middle!");
- }
- }
- //EX_04
- {
- int num, counter=0;
- float sum=0;
- Scanner sc= new Scanner(System.in);
- System.out.println("\nHello!\nPlease enter a size for the array: ");
- num=sc.nextInt();
- int[] arr= new int[num];
- System.out.println("Please enter " + num + " numbers to the array:");
- for (int i=0;i<num;i++)
- {
- arr[i]=sc.nextInt();
- if (arr[i]%2==0)
- {
- sum+=arr[i];
- counter++;
- }
- }
- System.out.println("The avarage of the even numbers in the array is: " + sum/counter);
- }
- //EX_05
- {
- int[][] arr;
- int num,sum=0;
- Scanner sc = new Scanner(System.in);
- System.out.println("\nHello!\nPlease enter a number: ");
- num= sc.nextInt();
- arr= new int[num][num];
- System.out.println("The Matrix is:");
- for(int i=0;i<num;i++)
- {
- for(int j=0;j<num;j++)
- {
- arr[i][j]=(int) (Math.random()*100+1);
- System.out.print(arr[i][j]+ "\t");
- if(i==j)
- {
- sum+=arr[i][j];
- }
- }
- System.out.println();
- }
- System.out.println("\nThe Sum of the main diagonal is: " + sum);
- }
- }
- }
- /*
- ------OUTPUT EXAMPLE------
- Hello!
- Please enter 3 numbers:
- 5
- 3
- 4
- Pythagorean triplet!
- Hello!
- Please enter a number (bigger then 1):
- 3
- 1 2 3
- 4 5 6
- 7 8 9
- Hello!
- Please give me a string:
- BlalB
- Palindrome with middle!
- Hello!
- Please enter a size for the array:
- 4
- Please enter 4 numbers to the array:
- 3
- 5
- 6
- 6
- The avarage of the even numbers in the array is: 6.0
- Hello!
- Please enter a number:
- 4
- The Matrix is:
- 31 28 82 21
- 98 54 52 12
- 15 57 46 27
- 22 60 48 80
- The Sum of the main diagonal is: 211
- -------------------------------------------------------------
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement