System.out.print("Enter a number : ") - se2

advertisement
JAVA – Programming Language
RHHS – SE2
Problem 1
What are the outputs of the following program?
int a; int b; int c; String name;
a=10;b=20;
System.out.print("a = " + a);
System.out.println(" b = " + b);
c=a+b;
c++;
System.out.println(c);
a=a*2;
a=a-- ;
c=a+b;
System.out.print(a + " - ");
System.out.println(c);
name="java";
System.out.println(name);
name="End";
System.out.println(name);
Solution
A = 10 b=20
31
19 – 39
Java
End
Informatics
PROGRAMMING
-1-
RHHS – SE2
JAVA – Programming Language
Problem 2
Write a java program that calculates the area, and the perimeter of a circle.
The radius of the circle is equal to 10.
The output of the program should appear as the following form
Area and Perimeter
-----------------Radius = 10
Area
= 314
Perimeter = 62.8
int r;
double a, p;
System.out.println("Area and Perimeter");
System.out.println("-------------------");
r=10;
System.out.println("radius = " + r);
a=3.14*r*r;
System.out.println("area = " + a);
p = 2*3.14*r;
System.out.println("Perimeter = " + p);
Informatics
PROGRAMMING
-2-
RHHS – SE2
JAVA – Programming Language
Problem 3
Write a java program that calculates the Square of an integer number.
The number is given by the user.
The output of the program should appear as the following form
Enter a number: 10
Square number of 10 is 100
int a , s ;
System.out.print("Enter a number : ");
a=scan.nextInt();
s=a*a;
System.out.println("Square of " + a + " is " + s);
Informatics
PROGRAMMING
-3-
RHHS – SE2
JAVA – Programming Language
Problem 4
Write a java program that calculates the area and the perimeter of a rectangle.
The width and the height are given by the user.
The output of the program should appear as the following form
Area - Perimeter of a Rectangle
------------------------------Enter the width : 10
Enter the Height : 20
Area = 200
Perimeter = 60
int w , h, a, p ;
System.out.println("Area - Perimeter of a Rectangle");
System.out.println("--------------------------------);
System.out.print("Enter the width : ");
w=scan.nextInt();
System.out.print("Enter the height : ");
h=scan.nextInt();
a=w*h;
System.out.println("Area = " + a);
p=(w+h)*2;
System.out.println("Perimeter = " + p);
Informatics
PROGRAMMING
-4-
RHHS – SE2
JAVA – Programming Language
Problem 5
Write a java that calculates the absolute value of a given number.
The output of the program should appear as the following form
Enter a number: -5
Absolute value = 5
int nb;
System.out.print("Enter a number : ");
nb=scan.nextInt();
int abs;
if(nb>=0) abs=nb;
else abs=nb*(-1);
System.out.println("Absolute value = " + abs);
Informatics
PROGRAMMING
-5-
RHHS – SE2
JAVA – Programming Language
Problem 6
Write a java program that calculates the average grade of a student and displays whether the
student has passed or failed.
The output of the program should appear as the following form
Enter the name: Adnan
Enter the Midyear: 85
Enter the Final: 87
Adnan Has passed, the average is 86
double m, f, avg;
String name;
System.out.print("Enter the name : ");
name=scan.next();
System.out.print("Enter the midyear : ");
m=scan.nextDouble();
System.out.print("Enter the final : ");
f=scan.nextDouble();
avg = (m+f)/2;
if(avg>=65)
System.out.println(name + " has passed, the average is " + avg);
else
System.out.println(name + " has failed, the average is " + avg);
Informatics
PROGRAMMING
-6-
RHHS – SE2
JAVA – Programming Language
Problem 7
Write a java program that displays the remainder of the division between 2 numbers given
by the user
The output of the program should appear as the following form
Enter number 1 : 10
Enter number 2 : 3
The remainder is: 1
int a,b,c;
System.out.print("Enter number 1: ");
a=scan.nextInt();
System.out.print("Enter number 2: ");
b=scan.nextInt();
c=a%b;
System.out.println("The remainder is " + c);
Informatics
PROGRAMMING
-7-
RHHS – SE2
JAVA – Programming Language
Problem 8
Write a java program that determines if a given number by the user is ODD or EVEN.
The output of the program should appear as the following form
Enter a number:10
10 is even
int a,b,c;
System.out.print("Enter number : ");
a=scan.nextInt();
if(a%2 == 0) System.out.println(a + " is even");
else System.out.println(a + " is ODD");
Informatics
PROGRAMMING
-8-
RHHS – SE2
JAVA – Programming Language
Problem 9 solution 1
Write a java program that enters the name and the salary of an employee and then
calculates the new salary which is salary + bonus.
The Salary is given by the user.
The Bonus is calculated depending on the following conditions:
 If the Salary <= 1000 then the bonus is 100
 Salary >=1001 and <=2000 then the bonus is 200
 Salary >=2001 and <=3000 then the bonus is 300
 Salary >=3001 then the bonus is 400
The output of the program should appear as the following form
Enter the name: Ahmad
Enter the salary: 2500
Bonus = 300
New salary of Ahmad is 2800
String name;
int s, ns;
System.out.print ("Enter the name : ");
name=scan.next();
System.out.print ("Enter the salary : ");
s=scan.nextInt();
if (s<=1000) {
System.out.println("Bonus = 100");
System.out.println("New salary of " + name + " is " + (s+100) );
}
else if (s>=1001 && s<= 2000) {
System.out.println("Bonus = 200");
ns=s+200;
System.out.println("New salary of " + name + " is " + ns);
}
else if (s>=2001 && s<= 3000) {
System.out.println("Bonus = 300");
ns=s+300;
System.out.println("New salary of " + name + " is " + ns);
}
else {
System.out.println("Bonus = 400");
ns=s+400;
System.out.println("New salary of " + name + " is " + ns);
}
Informatics
PROGRAMMING
-9-
RHHS – SE2
JAVA – Programming Language
Problem 9 solution 2
String name;
int s, b, ns;
System.out.print ("Enter the name : ");
name=scan.next();
ayman
.
System.out.print ("Enter the salary : ");
s=scan.nextInt();
3200
if (s<=1000) {
b=100;
}
else if (s>=1001 && s<= 2000) {
b=200;
}
else if (s>=2001 && s<= 3000) {
b=300;
}
else {
b=400;
}
System.out.println("Bonus = " + b);
Ns=s+b;
System.out.println("New salary of " + name + " is " + ns );
Informatics
PROGRAMMING
- 10 -
JAVA – Programming Language
RHHS – SE2
Extra Problem – Switch values
Write a java program that displays the vote of the user.
In case user entered number different from 1, 2, 3, or 4 the program should display “Invalid
number”
The output of the program should appear as the following form
Enter your name: Ahmad
Vote for
-------1- Instagram
2- Twitter
3- WhatsApp
4- Snapchat
Choose your opinion: 2
Ahmad voted for twitter
String name;
int choose;
System.out.print("Enter your name : ");
name=scan.next();
System.out.println("Vote for ");
System.out.println("---------");
System.out.println("1-Instagram");
System.out.println("2-Twitter");
System.out.println("3-WhatsApp");
System.out.println("4-Snapchat");
System.out.println();
Informatics
PROGRAMMING
- 11 -
RHHS – SE2
JAVA – Programming Language
System.out.print("Choose your opinion : ");
choose=scan.nextInt();
switch (choose) {
case 1: System.out.println(name + "voted for Instagram");
break;
case 2: System.out.println(name + "voted for Twitter");
break;
case 3: System.out.println(name + "voted for WhatsApp");
break;
case 4: System.out.println(name + "voted for Snapchat");
break;
default: System.out.println("Invalid number");
}
Informatics
PROGRAMMING
- 12 -
RHHS – SE2
JAVA – Programming Language
Problem 10
Write a java program that calculates the needed operation between two integer numbers
given by the user.
In case user entered number different from 1, 2 or 3, the program should display “Invalid
number”
The output of the program should appear as the following form
Enter a number: 5
Enter a number: 3
Operations:
1- Addition
2- Multiplication
3- Subtraction
Choose from 1 to 3 : 2
The multiplication is 15
int a,b, choice;
System.out.print("Enter a number : ");
a=scan.nextInt();
System.out.print("Enter a number : ");
b=scan.nextInt();
System.out.println("Operations:");
System.out.println("1-Addition:");
System.out.println("2-Multiplication:");
System.out.println("3-Subtraction:");
Informatics
PROGRAMMING
- 13 -
RHHS – SE2
JAVA – Programming Language
System.out.print("Choose from 1 to 3 : ");
choice=scan.nextInt();
switch (choice) {
case 1: System.out.println("The addition is " + (a+b));
break;
case 2: System.out.println("The Multiplication is " + (a*b));
break;
case 3: System.out.println("The subtraction is " + (a-b));
break;
default: System.out.println("Invalid number ");
}
Informatics
PROGRAMMING
- 14 -
JAVA – Programming Language
RHHS – SE2
Extra Problem – Replace the values of 2 variables
Write a java program that enters 2 integer numbers by the user and then replaces the
values of the 2 variables as the following:
- The variable a should contain the value of b
- The variable b should contain the value of a
Example 1: if a = 1, and b = 2, after the execution of the program, the variable a
should contain 2 and the variable b should contain 1.
Remark:
- Don’t write instructions like: SOPLln ("a = " + b); OR
SOPLln ("b = " + a);
int a,b, c;
System.out.print ("Enter A : ");
a=scan.nextInt ();
System.out.print("Enter B : ");
b=scan.nextInt();
c=a;
a=b;
b=c;
System.out.println("A =
" + a);
System.out.println("B =
" + b);
Informatics
PROGRAMMING
- 15 -
JAVA – Programming Language
RHHS – SE2
Extra Problem – do.. while
Write a java program that enters integer numbers by the user
The program must stop when the number entered by the user becomes 0.
int a;
do {
System.out.print("Enter a number : ");
a=scan.nextInt();
}while(a != 0);
Informatics
PROGRAMMING
- 16 -
JAVA – Programming Language
RHHS – SE2
Extra Problem– do.. while
Write a java program that enters integer numbers by the user and calculates and
displays the square of each number.
The program must stop when the number entered by the user becomes 0.
Enter numbers
int nb;
do{
System.out.print("Enter a number : ");
nb=scan.nextInt();
System.out.println("Square = " + (nb*nb));
} while(nb != 0);
Informatics
PROGRAMMING
- 17 -
JAVA – Programming Language
RHHS – SE2
Extra Problem– do.. while
Write a java program that enters integer numbers by the user and calculates the
total of them. The program must stop when the number entered by the user
becomes >100.
int a;
int s=0;
do {
System.out.print("Enter a number : ");
a=scan.nextInt();
s=s+a;
}while(a <=100);
System.out.println("----------");
System.out.println("Total = " + s);
}
}
Informatics
PROGRAMMING
- 18 -
RHHS – SE2
JAVA – Programming Language
Problem 11
Write a java program that enters integer numbers by the user and calculates and displays
the square root of every number. The program must stop when user enter the number 0.
int nb;
do{
System.out.print("Enter a nb: ");
nb=scan.nextInt();
System.out.println("Square root = " + Math.sqrt(nb));
} while (nb != 0);
Informatics
PROGRAMMING
- 19 -
JAVA – Programming Language
RHHS – SE2
Problem 12
Write a java program that enters integer numbers by the user and calculates the
total of them. The program must stop when the total become greater than 5000.
int a, s=0;
do {
System.out.print("Enter a number : ");
a = scan.nextInt ();
s=s+a;
} while (s<=5000);
System.out.println("Total = " + s);
}
do{
System.out.print("Enter a nb: ");
nb=scan.nextInt();
s=s+nb;
}while(s<=5000);
s=s-nb;
System.out.println("Total = " + s);
Informatics
PROGRAMMING
- 20 -
JAVA – Programming Language
RHHS – SE2
Extra problem
Write a java program that enters integer numbers by the user and calculates the
multiplication of them.
The program must stop when the user enters the number 0.
int nb;
int p=1;
do{
System.out.print("Enter a nb: ");
nb=scan.nextInt();
if (nb!=0) p=p*nb;
} while (nb !=0);
System.out.println("Multiplication = " + p);
Informatics
PROGRAMMING
- 21 -
JAVA – Programming Language
RHHS – SE2
Problem 13 (modified) - 1
Write a java program that enters integer numbers between 5 and 500 by the user, and then
counts and displays the number of even numbers.
(The program stop if the number is <5 or >500)
int a, c=0;
do {
System.out.print("Enter a number : ");
a = scan.nextInt ();
if (a%2 == 0) c=c+1;
// c++;
} while (a>=5 && a <=500);
System.out.println("Total = " + c);
Informatics
PROGRAMMING
- 22 -
JAVA – Programming Language
RHHS – SE2
Problem 13 (modified) - 2
Write a java program that enters integer numbers between 5 and 500 by the user, and then
counts only the even numbers between 5 and 500.
(The program stop if the number is <5 or >500)
int a, c=0;
do {
System.out.print("Enter a number : ");
a = scan.nextInt ();
if (a%2 == 0) c=c+1;
// c++;
} while (a>=5 && a <=500);
if (a%2==0) c--;
System.out.println("Total = " + c);
Informatics
PROGRAMMING
- 23 -
JAVA – Programming Language
RHHS – SE2
Extra problem - Extracting Digits:
Write a java program that extracts each digit from an integer number in the reverse order.
For example, if the number is 1542, the output shall be "2,4,5,1", with a comma separating
the digits.
Hints: Use n % 10 to extract the last digit;
n = n / 10 to discard the last digit
example: int n=152;
x=n%10; so x=2;
r=n/10; so r=15;
System.out.print("Enter a nb: ");
nb=scan.nextInt();
do{
x=nb%10;
System.out.print(x + " , ");
nb=nb/10;
} while (nb !=0);
Informatics
PROGRAMMING
- 24 -
JAVA – Programming Language
RHHS – SE2
Problem 14
Write a java program that prints the following sequence of numbers from 0 to 100.
The output of the program should be as the following form
0
2
4
6
…
100
int i;
for(i=0; i<=100; i=i+2) {
System.out.print (i + " ");
}
Informatics
PROGRAMMING
- 25 -
RHHS – SE2
JAVA – Programming Language
Problem 15
Write a java program that displays the following sequence of numbers from 100 to 0.
The output of the program should be as the following form
100
95
90
…
0
int i;
for(i=100; i>=0; i=i-5) {
System.out.println(i + " ");
}
Informatics
PROGRAMMING
- 26 -
RHHS – SE2
JAVA – Programming Language
Problem 16
Write a java program that enters 5 integer numbers by the user and prints the result of the
square of each number.
The output of the program should be as the following form
Enter a number : 10
The square of 10 is 100
Enter a number : 5
The square of 5 is 25
……
int i, s, nb;
for(i=1; i<=5; i++) {
System.out.print("Enter a number : ");
nb=scan.nextInt();
s=nb*nb;
System.out.println("The sqaure of " + nb + " is " + s);
}
Informatics
PROGRAMMING
- 27 -
JAVA – Programming Language
RHHS – SE2
Problem 20
Write a java program that calculates the total of numbers from 1 to 50 and displays the
result. (s=1+2+3+4+5+..50)
The output of the program should be as the following form
The total of numbers from 1 to 50 is 1275
int i;
int s=0;
for(i=1; i<=50; i=i+1) {
s=s+i;
}
System.out.println("total = " + s);
Informatics
PROGRAMMING
- 28 -
Download