Review 103
Review 103
if ( num % 2 == 0) {
System . out . println ( num + " is an even number . " ) ;
} else {
System . out . println ( num + " is an odd number . " ) ;
}
1
}
}
System . out . println ( " The sum of the first " + N + " numbers
is : " + sum ) ;
}
}
System . out . println ( " The factorial of " + num + " is : " +
fact ) ;
}
}
2
Exercise 5: Multiplication table
Write a program that asks the user to input an integer, then displays the mul-
tiplication table of that number up to 10 using a for loop.
Solution:
import java . util . Scanner ;
public class M u l t i p l i c a t i o n T a b l e {
public static void main ( String [] args ) {
Scanner input = new Scanner ( System . in ) ;
System . out . println ( " Enter a number : " ) ;
int num = input . nextInt () ;
3
public static void main ( String [] args ) {
Scanner input = new Scanner ( System . in ) ;
int positiveCount = 0;
int negativeCount = 0;
public class C a l c u l a t e A v e r a g e {
public static void main ( String [] args ) {
Scanner input = new Scanner ( System . in ) ;
int total = 0;
4
public class PrimeCheck {
public static void main ( String [] args ) {
Scanner input = new Scanner ( System . in ) ;
System . out . println ( " Enter a number : " ) ;
int num = input . nextInt () ;
boolean isPrime = true ;
if ( num <= 1) {
isPrime = false ;
} else {
for ( int i = 2; i <= num / 2; i ++) {
if ( num % i == 0) {
isPrime = false ;
break ;
}
}
}
if ( isPrime ) {
System . out . println ( num + " is a prime number . " ) ;
} else {
System . out . println ( num + " is not a prime number . " ) ;
}
}
}
if ( sum == num ) {
System . out . println ( num + " is a perfect number . " ) ;
} else {
System . out . println ( num + " is not a perfect number . " ) ;
}
}
}
5
Exercise 12: Print a triangle of stars
Use a for loop to display a triangle of stars where the height is defined by the
user.
Solution:
import java . util . Scanner ;
public class P ow er Ca l cu la to r {
public static void main ( String [] args ) {
Scanner input = new Scanner ( System . in ) ;
System . out . println ( " Enter the base : " ) ;
int base = input . nextInt () ;
System . out . println ( " Enter the exponent : " ) ;
int exp = input . nextInt () ;
int result = 1;
System . out . println ( base + " to the power of " + exp + " is :
" + result ) ;
}
}
6
Solution:
import java . util . Scanner ;
if ( years > 5) {
salary += salary * 0.05;
}
System . out . println ( " The final salary with bonus is : " +
salary ) ;
}
}