Download System.out.println() - se3-informatics

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
JAVA – Programming Language
RHHS – SE3
Problem 1
Write a java program that calculates the Area or the Perimeter of a square.
-
The user should enter the side (integer number)
-
The user should enter one letter A (for area) or P (for perimeter) to perform the
needed function.
The output of the program should appear as the following form:
Enter the side: 5
Enter a letter (A for area – P for perimeter): P
Perimeter = 20
int side, a, p;
char c;
System.out.print("Enter the square : ");
side=scan.nextInt();
System.out.print("Enter a letter (A for area – P for perimeter): ");
c=scan.next().charAt(0);
if(c=='a') {
a=side*side;
System.out.println("Area = " + a);
}
else {
p=side*4;
System.out.println("Perimeter = " + p);
}
Informatics
PROGRAMMING
-1-
JAVA – Programming Language
RHHS – SE3
Problem 1 - extra
Write a java program that calculates the Area or the Perimeter of a square.
-
The user should enter the side (integer number)
-
The user should enter one letter A (for area) or P (for perimeter) to perform the
needed function otherwise the program should display (The letter is not accepted).
The output of the program should appear as the following form:
Enter the side: 5
Enter a letter (A for area – P for perimeter): P
Perimeter = 20
int side, a, p;
char c;
System.out.print("Enter the square : ");
side=scan.nextInt();
System.out.print("Enter a letter (A for area – P for perimeter): ");
c=scan.next().charAt(0);
if(c=='a') {
a=side*side;
System.out.println("Area = " + a);
}
else if(c=='p') {
p=side*4;
System.out.println("Perimeter = " + p);
}
else { System.out.println("The letter is not Accepted");
}
Informatics
PROGRAMMING
-2-
RHHS – SE3
JAVA – Programming Language
Problem2
Write a Java program that calculates the revenue from a sale based on the unit price and
quantity of a product input by the user.
The discount rate is 10% for the quantity purchased between 100 and 120 units, and 15%
for the quantity purchased greater than 120 units. If the quantity purchased is less than 100
units, the discount rate is 0%.
The output of the program should appear as the following form:
Enter unit Price: 25
Enter quantity: 110
Total: 2750
Discount: 275.0$
After discount: 2475.0$(10.0%)
int price, quantity, total;
float discount, afterdiscount;
System.out.print("Enter unit Price: ");
price=scan.nextInt();
System.out.print("Enter quantity : ");
quantity=scan.nextInt();
total=price*quantity;
System.out.println("Total : "+total);
if(quantity >=1 && quantity<=99) {
discount=total*0/100;
Informatics
PROGRAMMING
-3-
RHHS – SE3
JAVA – Programming Language
afterdiscount=total-discount;
System.out.println("Discount : " + discount + "$");
System.out.println("After Discount : " + afterdiscount + "$(0.0%)");
}
else if(quantity >=100 && quantity<=120) {
discount=total*10/100;
System.out.println("Discount : " + discount+ "$");
System.out.println("After Discount : " + (total-discount) + "$(10.0%)");
}
else {
discount=total*15/100;
System.out.println("Discount : " + discount+ "$");
System.out.println("After Discount : " + (total-discount) + "$(15.0%)");
}
}
}
Informatics
PROGRAMMING
-4-
RHHS – SE3
JAVA – Programming Language
Problem 3
Write a java program that enters an integer number by the user (it must be less than 1000)
and calculates and displays the summation of them. (The program must stop when user
enter a number >=1000)
int sum=0;
int nb;
do {
System.out.print("Enter a number : ");
nb=scan.nextInt();
sum=sum+nb;
} while (nb < 1000);
System.out.println("summation = " + sum);
Informatics
PROGRAMMING
-5-
RHHS – SE3
JAVA – Programming Language
Problem 4
Write a java program that enters N integer number by the user (N must be between 5 and
10), and then counts and displays the number of even numbers entered by the user.
Write a java program that enters integer number by the user (the number must be
between 1 and 500), and then counts and displays the number of even numbers entered by
the user.
int count=0;
int nb;
do {
System.out.print("Enter a number : ");
nb=scan.nextInt();
if(nb%2==0) count++;
}while(nb>=1 && nb<=500);
System.out.println();
System.out.println("Count = " + count);
Informatics
PROGRAMMING
-6-
RHHS – SE3
JAVA – Programming Language
Problem 5
Write a java program that enters integer number by the user and calculate the
multiplication of them. The program must stop when the multiplication become over
than 5000.
int m=1;
int nb;
do {
System.out.print("Enter a number : ");
nb=scan.nextInt();
m=m*nb;
}while(m<=5000);
System.out.println();
System.out.println("Multiplication = " + m);
Informatics
PROGRAMMING
-7-
RHHS – SE3
JAVA – Programming Language
Problem 6
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 or 2, the program should
display "Invalid number"
The output of the program should appear as the following form
Enter nb1: 25
Enter nb2: 4
1- Find the maximum square root of nb1 and nb2
2- Check if nb1 is divisible by nb2.
Choose from 1 or 2 : 1
Maximum = 5.0
Note: For the second choice display: 9 25 is not divisible by 4
int a,b,c;
System.out.print("Enter a nb1 : ");
a=scan.nextInt();
System.out.print("Enter a nb2 : ");
b=scan.nextInt();
System.out.println("1-Find the maximum square root of nb1 and nb2");
System.out.println("2-Check if nb1 is divisible by nb2");
System.out.print("Choose from 1 or 2 : ");
c=scan.nextInt();
Informatics
PROGRAMMING
-8-
RHHS – SE3
JAVA – Programming Language
switch (c) {
case 1: if(a>b) {
System.out.println("Maximum = " +Math.sqrt(a));
}
else {
System.out.println("Maximum = " +Math.sqrt(b));
}
break;
case 2: if(a%b==0) {
System.out.println(a + " is divisible by " + b);
}
else {
System.out.println(a + " is not divisible by " + b);
}
break;
default: System.out.println("Invalid number");
}
Informatics
PROGRAMMING
-9-
RHHS – SE3
JAVA – Programming Language
Problem 7
Write a Java program that prompts the user to choose the correct answer from a list of
answer choices of a question.
The user can choose to continue answering the question or stop answering it.
See the example below:
What is the command keyword to exit a loop in Java?
a. int
b. continue
c. break
d. exit
Enter your choice: b
Incorrect!
Again? press y to continue:
char choice, repeat;
repeat='y';
System.out.println("What is the command keyword to exit a loop in java?");
System.out.println("a.int");
System.out.println("b.continue");
System.out.println("c.break");
System.out.println("d.exit");
do {
System.out.print("Enter your choice : ");
choice=scan.next().charAt(0);
Informatics
PROGRAMMING
- 10 -
JAVA – Programming Language
RHHS – SE3
switch (choice) {
case 'a' : System.out.println("Incorect!");
break;
case 'b': System.out.println("Incorect!");
break;
case 'c': System.out.println("Correct answer");
break;
case 'd': System.out.println("Incorect!");
break;
default : System.out.println("wrong character");
break;
}
System.out.print("Again? press y to continue: ");
repeat=scan.next().charAt(0);
}while (repeat=='y');
Informatics
PROGRAMMING
- 11 -
RHHS – SE3
JAVA – Programming Language
Problem 8 - 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 a digit; and n = n / 10 to discard the last digit
int a,b;
System.out.print("Enter a number : ");
a=input.nextInt();
do {
b=a%10;
System.out.print(b + ", ");
a=a/10;
} while(a>0);
Informatics
PROGRAMMING
- 12 -
RHHS – SE3
Informatics
JAVA – Programming Language
PROGRAMMING
- 13 -
RHHS – SE3
JAVA – Programming Language
Problem 9 - Reverse Digits extra:
Write a java program that reverses the number 1234 becomes 4321.
int a = 1234;
int t=1000;
int s=0;
int x;
do{
x=a%10;
s=s+(x*t);
t=t/10;
a=a/10;
}while(a>0);
System.out.println("Reverse of a = " + s);
Informatics
PROGRAMMING
- 14 -
RHHS – SE3
JAVA – Programming Language
Problem 9 - Reverse Digits:
Write a java program that reverses digits within an integer number given by the user.
Example: 1234 becomes 4321.
Solution 2
int a,t;
System.out.print("Enter a number : ");
a=scan.nextInt();
if(a<100) {t=10;}
else if(a<1000) {t=100;}
else if(a<10000) {t=1000;}
else if(a<100000) {t=10000;}
else {t=100000;}
int s=0;
int x;
do{
x=a%10;
s=s+(x*t);
t=t/10;
a=a/10;
}while(a>0);
System.out.println("Reverse of a = " + s);
}
Informatics
PROGRAMMING
- 15 -
JAVA – Programming Language
RHHS – SE3
Problem 11
Write a java program that prints the following sequence of numbers from -100 to 100.
The output of the program should be as the following form
-100
-95
-90
…
90
95
100
int i;
for(i=-100;i<=100;i=i+5) {
System.out.print(i + " ");
}
Informatics
PROGRAMMING
- 16 -
RHHS – SE3
JAVA – Programming Language
Problem 12
Write a java program that enters 5 integer numbers by the user and then 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
……
Scanner input = new Scanner (System.in);
int i;
int nb, square;
for(i=1;i<=5;i=i+1) {
System.out.print("Enter a number : ");
nb=input.nextInt();
square=nb*nb;
System.out.println("The square of " + nb + " is " + square);
}
Informatics
PROGRAMMING
- 17 -
JAVA – Programming Language
RHHS – SE3
Problem 13
Write a java program that calculates and prints the sum of a harmonic series as
shown below where n <1000.
Harmonic (n) =
1
1
1
1
2
3
4
𝑛
1+ + + +⋯
float i;
float s, d;
s=0;
for (i=1;
i<1000; i++) {
d=1/i;
s=s+d;
}
System.out.println("Total = " + s);
Informatics
PROGRAMMING
- 18 -
JAVA – Programming Language
RHHS – SE3
Problem 14
Write a java program that enters 10 integer numbers by the user, and display the
number of occurrence (count) of even numbers.
int nb, i;
int c=0;
for (i=1;i<=10;i=i+1) {
System.out.print("Enter a number : ");
nb=scan.nextInt();
if(nb%2==0) {
c++;
(or you can use c=c+1)
}
}
System.out.println("Total = " + c);
Informatics
PROGRAMMING
- 19 -
RHHS – SE3
JAVA – Programming Language
Problem 15
Write a java program that enters integer numbers by the user,
and then displays the total of them. The program must stop
when the user enter the number 0.
Example:
Enter a number : 5
Enter a number : 8
Enter a number : 2
Enter a number : 10
Enter a number : 0
Total = 25
int nb;
int s=0;
do{
System.out.print("Enter a number : ");
nb=scan.nextInt();
s=s+nb;
}while( nb != 0);
System.out.println("Total = " + s);
Informatics
PROGRAMMING
- 20 -
JAVA – Programming Language
RHHS – SE3
Problem 16
Write a java program that enters 5 integer numbers by the user, and then displays
the multiplication of them.
input
Scanner
= new Scanner (System.in);
int i;
int nb, m;
m=1;
for(i=1; i<=5; i=i+1) {
System.out.print("Enter a number : ");
nb=input.nextInt();
m=m*nb;
}
System.out.println("Multiplication = " + m);
Informatics
PROGRAMMING
- 21 -
JAVA – Programming Language
RHHS – SE3
Problem 17
Write a java program that calculates the total of numbers from 1 to 50 and displays
the result.
The output of the program should be as the following form
The total of numbers from 1 to 50 is 1275
int i, s;
s=0;
for(i=1;i<=50;i++) {
s=s+i;
}
System.out.println("The total of numbers from 1 to 50 is " + s);
Solution 2: using do while.
Int s=0;
Int i=1;
Do {
S=s+i;
I++;
} while (i<=50);
Informatics
PROGRAMMING
- 22 -
JAVA – Programming Language
RHHS – SE3
Problem 18
Write a java program that displays the following sequence:
2
4
8
16
32
64
128
256
512
1024
int I;
for(i=2; i<=1024; i=i*2) {
System.out.print(" " + i);
}
Solution 2 Using do.. while..:
int i=2;
do {
System.out.print(" " + i);
i=i*2;
} while(i<=1024);
Informatics
PROGRAMMING
- 23 -
JAVA – Programming Language
RHHS – SE3
Problem 19
Write a Java program that enters an integer number by the user and then displays the
divisible numbers of it.
Example: The divisible numbers of 16 are: 1, 2, 4, 8, 16.
int i;
int nb;
System.out.print("Enter a number : ");
nb=scan.nextInt();
for(i=1; i<=nb; i++) {
if (nb % i==0) System.out.println(i);
}
Informatics
PROGRAMMING
- 24 -
JAVA – Programming Language
RHHS – SE3
Problem 20
What is the output of the following program?
Int i,c;
c=0;
For (i=2;i<=6;i++) {
c=c+i;
c--;
}
System.out.println(c);
Solution
15
i
C=c+i
c--
2
0+2=2
1
3
1+3=4
3
4
3+4=7
6
5
6+5=11
10
6
10+6=16
15
Informatics
PROGRAMMING
- 25 -
RHHS – SE3
JAVA – Programming Language
Problem 23
Write a java program that calculates XN. X and N are an integer numbers given by the user.
XN = X * X * X … * X
N
The output of the program should appear as the following form
Enter a number: 5
Enter the Power: 3
Result = 125
Solution
int i, x, n, p;
p=1;
System.out.print("Enter a number : ");
x=scan.nextInt();
System.out.print("Enter the power : ");
n=scan.nextInt();
for (i=1;i<=n;i++){
p=p*x;
}
System.out.println("Result = " + p);
Informatics
PROGRAMMING
- 26 -
JAVA – Programming Language
RHHS – SE3
Problem 24
Write a Java program that calculates the following sequence.
R = 1 + X1 + X2 + X3 + X4 + X5 + …. + X10
X is an integer number given by the user.
Solution
int x, p=1, i, s=1, n;
System.out.print("Enter a number :");
x=scan.nextInt();
System.out.print("Enter a power :");
n=scan.nextInt();
for(i=1;i<=n;i++) {
p=p*x;
s=s+p;
}
System.out.println("The sum is " + s);
Informatics
PROGRAMMING
- 27 -
RHHS – SE3
JAVA – Programming Language
Problem 26
Write a java program that calculates the factorial of an integer number given by the user.
The output of the program should appear as the following form
Enter a number: 5
The factorial of 5 is 120
Solution
int nb, f, i;
f=1;
System.out.print("Enter a number :");
nb=scan.nextInt();
for(i=1; i<=nb; i++) {
f=f*i; }
System.out.println (“The factorial of “ + nb + “ is “ + f”);
Informatics
PROGRAMMING
- 28 -
JAVA – Programming Language
RHHS – SE3
Extra question
Write a java program that calculates and prints the result of the following expression.
X=
1
1
1
1
2
4
6
100
1+ + + +⋯
Int I;
Double d, s;
S=1;
for(i=1; i<=100; i=i+2) {
d=1/i;
s=s+d;
}
System.out.println("X = " + s);
Informatics
PROGRAMMING
- 29 -
RHHS – SE3
JAVA – Programming Language
Problem 26
Write a java program that:

Fills an array V [10] with integer numbers

Displays all the elements.

Displays all the elements in reverse order.
Scanner scan = new Scanner (System.in);
int v[];
v = new int[10];
int i;
for (i=0; i<10; i++) {
System.out.print("Enter a value ");
v[i]=scan.nextInt();
}
for (i=0; i<v.length; i++) {
System.out.println("v[" + i + "]=" + v[i]);
}
for (i=9; i>=0; i--) {
System.out.println("v[" + i + "]=" + v[i]);
}
Note: V.length is the length of the array; you can use it instead 10
To display it in reverse you can also write:
for (i=v.length-1 ; i>=0; i--) {
System.out.println("v[" + i + "]=" + v[i]);
}
Informatics
PROGRAMMING
- 30 -
RHHS – SE3
JAVA – Programming Language
Problem 27
Write a java program that creates an array T [] of N elements:

The number of elements N is given by the user (N must be less than 100)

Fills the array T[] with integer numbers less than 200

Calculates and displays the summation of even numbers (even elements)

Calculates and displays the multiplication of odd numbers (odd elements)

Enters an integer Number named Val, and then finds and displays the number of
occurrences (count) of Val.
int n;
do {
System.out.print("Enter the length of the Array: ");
n=scan.nextInt();
}while (n>=100);
int t[];
t=new int[n];
int i, s, m, c;
s=0;
m=1;
c=0;
for (i=0; i<n; i++) {
do {
System.out.print("Enter a number : ");
t[i] = scan.nextInt();
} while (t[i]>=200);
if (t[i] % 2 ==0) s+=t[i];
// the same as s=s+t[i];
else m*=t[i];
// the same as m=m*t[i];
}
System.out.println("Summation = " + s);
System.out.println("Multiplication = " + m);
int val;
System.out.print("Enter Val : ");
val=scan.nextInt();
for (i=0; i<n; i++) {
if (t[i] == val) c++;
}
System.out.println(val + " exists " + c + " times ");
Informatics
PROGRAMMING
- 31 -
RHHS – SE3
JAVA – Programming Language
Problem 28
Write a java program that fills an array V [10] with integer numbers, and verifies if the user
entered minimum 3 numbers which are divisible with 5.
int t[];
t=new int[10];
int i;
int c=0;
for(i=0; i<t.length; i++) {
System.out.print("Enter a number :");
t[i]=scan.nextInt();
if (t[i] %5 == 0) c++;
}
if (c>=3) System.out.println("Yes he did");
else System.out.println("No he did not");
Informatics
PROGRAMMING
- 32 -
RHHS – SE3
JAVA – Programming Language
Problem 29
Write a java program that fills an array V [10] with integer numbers, and then displays the
maximum of them.
int t[];
t=new int[10];
int i;
int max;
//i<10;
for(i=0; i<t.length; i++) {
System.out.print("Enter a number :");
t[i]=scan.nextInt();
}
max=t[0];
for (i=1; i<t.length; i++) {
if (t[i]>max) max=t[i];
}
System.out.println("The maximum is " + max);
Informatics
PROGRAMMING
- 33 -
RHHS – SE3
JAVA – Programming Language
Problem 30
In mathematics, the Fibonacci numbers or Fibonacci series or Fibonacci sequence are the
numbers in the following integer sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144
By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each
subsequent number is the sum of the previous two.
Display this sequence, take into consideration the length of the table is 13.
int [] t;
t=new int[13];
int i;
t[0]=0;
t[1]=1;
for (i=2;i<13;i++){
t[i]=t[i-1]+t[i-2];
}
for (i=0;i<13;i++){
System.out.print(" " + t[i]);
}
Informatics
PROGRAMMING
- 34 -
JAVA – Programming Language
RHHS – SE3
Problem 31
Write a Java program that fills an array V [20] by the double of the indices (i)
Example:
Element
0
2
4
6
8
-----
38
Indices (i)
0
1
2
3
4
-----
19
int t[];
t = new int[20];
for(int i = 0; i < 20; i++){
t[i] = i*2;
}
for(int i = 0; i < t.length; i++){
System.out.println("t["+i+"] = " + t[i]);
}
Note: You can write t.length instead 20
for(int i = 0; i < t.length; i++){
t[i] = i*2;
}
Informatics
PROGRAMMING
- 35 -
RHHS – SE3
JAVA – Programming Language
Problem 32
Write a java program that creates an array V1 of 8 integer elements. Then reverse
this array into another array V2 and at the end, then displays the elements of V2.
int [] v1, v2;
v1=new int[8];
v2=new int[8];
int i;
for (i=0;i<=7;i++){
System.out.print("Enter a number : ");
v1[i]=scan.nextInt();
v2[i]=v1[7-i];
}
System.out.println("Print v2");
for (i=0;i<=7;i++){
System.out.println(v2[i]);
}
Informatics
PROGRAMMING
- 36 -
JAVA – Programming Language
RHHS – SE3
Problem 33
Write a java program that:


Creates the array VA of 8 integer numbers.
Fills the array VA by numbers between 0 and 255 (include 0 and 255).
Creates from the values of array VA the array VB containing in each of its cases the
sum of two successive cases of VA.
Only the first value of VB corresponds to the first value of VA.
Example:
Array VA
6
2
4
8
0
5
3
9
Array VB

6
8
6
12
8
5
8
12
=
6
=
6
=
2
=
4
=
8
=
0
=
5
=
3
+
2
+
4
+
8
+
0
+
5
+
3
+
9
Displays the contents of array VB in 1 row, separated with a space.
int va[], vb[];
va= new int[8];
vb=new int[8];
int i;
for(i=0; i<8; i++) {
do{
System.out.print("va[" + i + "]= ");
va[i]=scan.nextInt();
}while (va[i]<0 || va[i]>255 );
}
vb[0]=va[0];
for(i=1; i<8; i++) {
vb[i]=va[i] + va[i-1];
}
for(i=0; i<8; i++) {
System.out.print(vb[i] + " | ");
}
Informatics
PROGRAMMING
- 37 -
Related documents