Amer Assighnment

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

1)

import java.util.Scanner;
class Amer1{
//1- Read two numbers x and y, then print the sum, the difference, the multiplications and the divisions of these
two numbers.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
double x ;
double y ;
double op;
System.out.println("please enter the first (x) number;");
x=scan.nextInt();
System.out.println("please enter the second (y) number;");
y=scan.nextInt();
op=x+y;
System.out.println("The sum of two numbers is: "+op);
op=x-y;
System.out.println("The difference of two numbers is: "+op);
op=x*y;
System.out.println("The multiplication of two numbers is: "+op);
op=x/y;
System.out.println("The division of two numbers is: "+op);
}
}

2)
import java.util.Scanner;
class Amer2{
//2- Find the perimeter of the rectangle (‫)جد محيط المستطيل‬.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
double x ;
double y ;
double p;
System.out.println("please enter the length of the rectangle:");
x=scan.nextInt();
System.out.println("please enter the width of the rectangle:");
y=scan.nextInt();
p=2*(x+y);
System.out.println("The perimeter of the rectangle is :"+p);
}
}
3)
import java.util.Scanner;
class Amer3{
//3- Read two number then find the biggest one and the smallest one.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int n1 ;
int n2 ;
System.out.println("Welcome, this program will detect which number is the biggest please enter two
numbers:");
System.out.println("please enter a number:");
n1=scan.nextInt();
System.out.println("please enter another number:");
n2=scan.nextInt();
if (n1>n2){
System.out.println("The biggest number is :"+n1);
System.out.println("The smallest number is :"+n2);}
else {
System.out.println("The biggest number is :"+n2);
System.out.println("The smallest number is :"+n1);}
}
}

4) H
import java.util.Scanner;
class Amer4{
//4- Read a number then find if that number is positive or negative.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int n1 ;
int n2 ;
System.out.println("please enter a number:");
n1=scan.nextInt();
if (n1>0)
System.out.println("The number is positive");
else
System.out.println("The number is negative");
}
}
5)
import java.util.Scanner;
class Amer5{
//5- Read a number then find if that number is odd or even.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int n1 ;
int n2 ;
System.out.println("please enter a number:");
n1=scan.nextInt();
if (n1%2==0)
System.out.println("The number is even");
else
System.out.println("The number is odd");
}
}

6)
import java.util.Scanner;
class Amer6{
//6- Find the area of a circle, if you enter a negative radius, print “this is
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
double n1 ;
double a;
System.out.println("please enter the Radius:");
n1=scan.nextInt();
if (n1>=0){
a=3.14*n1;
System.out.println("The area of the circle is: " +a);}
else
System.out.println("this is negative radius and we cannot find the area for the negative radius");
}
}

7)
import java.util.Scanner;
class Amer7{
//7- Count the human temperature. If he is normal, advise him study and then sleep. If he is ill tell him to
take a rest and drink orange (normal temperature is 37 degree).
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
double TP ;
double a;
System.out.println("please enter your temperature:");
TP=scan.nextInt();
if ((TP<37)&&(TP>=36)){
System.out.println("Your temperature is good study and then you can sleep " );}
else
System.out.println("your temperature is not normal take a rest and drink orange");
}
}
8)
import java.util.Scanner;
class Amer8{
//8- Read 5 numbers then sum all of these numbers using for loop.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int n ;
int sum=0;
int i;
for(i=1;i<6;i++){
System.out.println("please enter the number");
n=scan.nextInt();
sum=sum+n;}
System.out.println("The sum of those numbers is: "+sum );
}
}

9) F
import java.util.Scanner;
class Amer9{
//9- Read 10 numbers then find the average of these 10 numbers.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int n ;
int sum=0;
double avg;
int i;
for(i=1;i<11;i++){
System.out.println("please enter the number");
n=scan.nextInt();
sum=sum+n;}
avg=sum/10;

System.out.println("The average of those numbers is: "+avg );


}
}
10)
import java.util.Scanner;
class Amer9{
//9- Read 10 numbers then find the average of these 10 numbers.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int n ;
int sum=0;
double avg;
int i;
for(i=1;i<11;i++){
System.out.println("please enter the number");
n=scan.nextInt();
sum=sum+n;}
avg=sum/10;

System.out.println("The average of those numbers is: "+avg );


}
}

11)
import java.util.Scanner;
class Amer11{
//11-Read 30 numbers then find how many number is odd and how many even.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int n ;
int even=0;
int odd=0;
int i;
for(i=1;i<31;i++){
System.out.print("please enter the mark");
n=scan.nextInt();
if (n%2==0){
even=even+1;
System.out.println("the number is even");}
else{
System.out.println("the number is odd");
odd=odd+1;}}
System.out.println("number of even numbers is : "+even );
System.out.println("number of even numbers is : "+odd );
}
}
12)
import java.util.Scanner;
class Amer12{
//12- Find the factorial of a number you enter from the keyboard.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int n ;
int f=1;
int i;
System.out.print("please a number");
n=scan.nextInt();
for(i=1;i<=n;i++)
f=f*i;

System.out.println("The factorial of "+n+ "is: "+f );


}
}

13)
import java.util.Scanner;
class Amer13{
//13- Find the power of a number you enter from the keyboard
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int x;
int y;
int i;
int p=1;
System.out.print("please enter the value of x");
x=scan.nextInt();
System.out.print("please enter the value of y");
y=scan.nextInt();
for(i=1;i<=y;i++)
p=p*x;

System.out.println("The power of " +x+" to "+y+" is : "+p );


}
}
14)
import java.util.Scanner;
class Amer14{
//14- Find the sum of all the numbers from 1 to n.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int i;
int n;
int sum=0;
System.out.print("please enter a number to sum from 1 to that number:");
n=scan.nextInt();
for(i=1;i<=n;i++)
sum=sum+i;
System.out.println("The result is: " +sum );
}
}

15)
import java.util.Scanner;
class Amer15{
//15- Using 1D array, read 10 score marks, then find the average.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int A[]=new int[10];
int i;
int avg;
int sum=0;
for(i=0;i<10;i++){
System.out.print("please enter your mark:");
A[i]=scan.nextInt();
sum=sum+A[i];}
avg=sum/10;
System.out.println("The result is: " +avg );
}
}
16)
import java.util.Scanner;
class Amer16{
//16- Using 1D array read 30 student marks then find the largest and lowest mark.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int A[]=new int[30];
int i;
int larg=A[0];
int low=A[0];
for(i=0;i<30;i++){
System.out.print("please enter your mark:");
A[i]=scan.nextInt();
if(A[i]>larg)
larg=A[i];
if(A[i]<low)
low=A[i];}
System.out.println("The largest mark is: " +larg );
System.out.println("The lowest mark is: "+low);
}
}

17)
import java.util.Scanner;
class Amer17{
//17- Read an array of 20 numbers, then find if the array are symmetric in the middle.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int A[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int s=20;
int i;
boolean symmetric=true;
for(i=0;i<(s/2+1);i++){
if (A[i]!=A[(s/2)])
symmetric=false;}
if(symmetric==true)
System.out.println("The array is Symmetric in the middle" );
else
System.out.println("The array is not Symmetric in the middle ");
}
}
18)
import java.util.Scanner;
class Amer19{
//19- Find the maximum number in an array of 10 numbers and then replace it with the last number or the
first number.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int A[]={1,2,3,4,5,6,7,8,9,10};
int i;
int max=A[0];
int j;
int r1=0;
for(i=0;i<10;i++){
if(A[i]>max)
max=A[i];
r1=i;}
System.out.println("The largest mark is: " +r1 );
j =A[r1];
A[r1]=A[0];
A[0]=j;
System.out.println("The largest mark is: " +max );
}
}

19) F
import java.util.Scanner;
class Amer18{
//18- Find the maximum number in an array of 30 numbers and then replace it with the minimum number.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int A[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
int i;
int larg=A[0];
int low=A[0];
int j;
int r1=0;
int r2=0;
for(i=0;i<30;i++){
if(A[i]>larg)
larg=A[i];
r1=i;
if(A[i]<low)
low=A[i];
r2=i;}
j =A[r1];
A[r1]=A[r2];
A[r2]=j;
System.out.println("The largest mark is: " +larg );
System.out.println("The lowest mark is: "+low);
}
}
20)
import java.util.Scanner;
class Amer20{
//20- Read a 2D array of 4 x 4 numbers, then find the sum of each row and the sum of the whole matrix.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int A[][]=new int[4][4];
int i;
int j;
int t ;
int r=4;
int c=4;
int Row=0;
int sum=0;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
A[j][i]=scan.nextInt();
Row=Row+A[j][i];
sum=sum+A[i][j];}
t=Row;
System.out.println("The sum of row:"+i+" is "+t);}
System.out.println("The sum of whole matrix is :"+sum);
}
}
21) H
import java.util.Scanner;
class Amer21{
//21- Read a 2D array of 4 x 4 numbers, then find the lower triangle and the diagonal and the upper triangle.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int A[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
int i;
int j;
int t ;
int r=4;
int c=4;
int Row=0;
int sum=0;
int su=0;
int sl=0;
int firstd=0;
for(j=0;j<c;j++)
for(i=0;i<r;i++)
firstd=firstd+A[i][i];
System.out.println("The diagonal is"+firstd);
for(j=0;j<c;j++)
for(i=0;i<r;i++)
if (i>j)
sl=sl+A[i][j];
System.out.println("The lower Triangle is: "+sl);
for(j=0;j<c;j++)
for(i=0;i<r;i++)
if (i<j)
su=su+A[i][j];
System.out.println("The Uper Triangle is: "+su);
}
}
22)
import java.util.Scanner;
class Amer22{
//22- Read a 2D array of 4 x 4 numbers, then replace the second row with the forth row.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int A[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
int i;
int j;
int t=0;
int r1=0;
int r3=0;
int r=4;
int c=4;
for(j=0;j<c;j++)
for(i=0;i<r;i++){
if((j==1)||(j==3))
System.out.println(" ");
t=A[1][i];
A[1][i]=A[3][i];
A[3][i]=t;
for(j=0;j<c;j++)
for(i=0;i<r;i++)
if(j==1)
System.out.println("The 11: " +A[1][i]);
for(j=0;j<c;j++)
for(i=0;i<r;i++)
if(j==3)
System.out.println("The 22: " +A[3][i]);}
for(j=0;j<c;j++)
for(i=0;i<r;i++)
if(j==1)
System.out.println("The resultyy is: " +A[1][i]);
for(j=0;j<c;j++)
for(i=0;i<r;i++)
if(j==3)
System.out.println("The result is: " +A[3][i]);
}
}
23)
class Amer23{
//23- Read a 2D array of 4 x 4 numbers, then find the maximum number in the matrix.
public static void main(String[]args){
int A[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
int i;
int j;
int max=A[0][0];
int c=4;
int r=4;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
if(A[i][j]>max)
max=A[i][j];

}}
System.out.print("The maximum number in the matrix is :"+max);

}
}

24)
import java.util.Scanner;
class Amer24{
//24- Write a factorial function and power function.
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int x;
int y;
int i;
int p=1;
int n ;
int f=1;
System.out.print("please enter a number");
n=scan.nextInt();
for(i=1;i<=n;i++)
f=f*i;
System.out.println("The factorial is : "+f );
System.out.println(" ");
System.out.println("Please Enter two numbers to find their power");
System.out.print("please enter the value of x");
x=scan.nextInt();
System.out.print("please enter the value of y");
y=scan.nextInt();
for(i=1;i<=y;i++)
p=p*x;
System.out.println("The power of " +x+" to "+y+" is : "+p );
}
}
25)
import java.util.Scanner;
class Amer25{
//25- Write the series 1/1! + 3/3! + 5/5! + 7/5!.
public static void main(String[]args){
double h=0 ;
double f=1;
double b=1;
double d=1;
double a=4;
int j;
int i;
for(i=0;i<a;i++){
f=1;
for(j=1;j<=d;j++){
if(d<=5)
f=f*j;
else{
d=5;
f=f*j;}}
d=d+2;
h= h+b/f;
b=b+2;}
System.out.println("The ttt : "+h );

}
}

You might also like