[go: up one dir, main page]

0% found this document useful (0 votes)
18 views7 pages

Review 103

Uploaded by

Sara Haiji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

Review 103

Uploaded by

Sara Haiji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Java exercises - Exercises with Solutions

Dr. Dhai eddine SALHI

Exercise 1: Find the largest number


Write a Java program that asks the user to input two numbers, then displays
the largest of the two. Use an if-else structure to compare the two numbers.
Solution:
import java . util . Scanner ;

public class LargestNumber {


public static void main ( String [] args ) {
Scanner input = new Scanner ( System . in ) ;
System . out . println ( " Enter the first number : " ) ;
int num1 = input . nextInt () ;
System . out . println ( " Enter the second number : " ) ;
int num2 = input . nextInt () ;

if ( num1 > num2 ) {


System . out . println ( " The largest number is : " + num1 ) ;
} else {
System . out . println ( " The largest number is : " + num2 ) ;
}
}
}

Exercise 2: Check if a number is even or odd


Ask the user to input a number, then use an if-else statement to check if the
number is even or odd and display a corresponding message.
Solution:
import java . util . Scanner ;

public class EvenOdd {


public static void main ( String [] args ) {
Scanner input = new Scanner ( System . in ) ;
System . out . println ( " Enter a number : " ) ;
int num = input . nextInt () ;

if ( num % 2 == 0) {
System . out . println ( num + " is an even number . " ) ;
} else {
System . out . println ( num + " is an odd number . " ) ;
}

1
}
}

Exercise 3: Sum of the first N numbers


Write a Java program that asks the user to input a positive integer N, then
calculates and displays the sum of the first N numbers using a for loop.
Solution:
import java . util . Scanner ;

public class SumOfNumbers {


public static void main ( String [] args ) {
Scanner input = new Scanner ( System . in ) ;
System . out . println ( " Enter a number : " ) ;
int N = input . nextInt () ;
int sum = 0;

for ( int i = 1; i <= N ; i ++) {


sum += i ;
}

System . out . println ( " The sum of the first " + N + " numbers
is : " + sum ) ;
}
}

Exercise 4: Calculate factorial


Ask the user to input a positive integer and use a for loop to calculate and
display the factorial of that number.
Solution:
import java . util . Scanner ;

public class Factorial {


public static void main ( String [] args ) {
Scanner input = new Scanner ( System . in ) ;
System . out . println ( " Enter a number : " ) ;
int num = input . nextInt () ;
int fact = 1;

for ( int i = 1; i <= num ; i ++) {


fact *= i ;
}

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 () ;

for ( int i = 1; i <= 10; i ++) {


System . out . println ( num + " * " + i + " = " + ( num * i ) )
;
}
}
}

Exercise 6: Find the multiples of a number


Ask the user to input two numbers, then use a for loop to display all the
multiples of the first number that are less than the second number.
Solution:
import java . util . Scanner ;

public class Multiples {


public static void main ( String [] args ) {
Scanner input = new Scanner ( System . in ) ;
System . out . println ( " Enter the number : " ) ;
int num = input . nextInt () ;
System . out . println ( " Enter the limit : " ) ;
int limit = input . nextInt () ;

for ( int i = num ; i < limit ; i += num ) {


System . out . println ( i ) ;
}
}
}

Exercise 7: Count positive and negative numbers


Ask the user to input a series of 10 integers. Use a for loop to count how many
of these numbers are positive and how many are negative.
Solution:
import java . util . Scanner ;

public class CountPosNeg {

3
public static void main ( String [] args ) {
Scanner input = new Scanner ( System . in ) ;
int positiveCount = 0;
int negativeCount = 0;

System . out . println ( " Enter 10 numbers : " ) ;


for ( int i = 1; i <= 10; i ++) {
int num = input . nextInt () ;
if ( num > 0) {
positiveCount ++;
} else if ( num < 0) {
negativeCount ++;
}
}

System . out . println ( " Positive numbers : " + positiveCount ) ;


System . out . println ( " Negative numbers : " + negativeCount ) ;
}
}

Exercise 9: Calculate average


Write a program that asks the user to input 5 grades, then calculates and
displays the average.
Solution:
import java . util . Scanner ;

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;

System . out . println ( " Enter 5 grades : " ) ;


for ( int i = 0; i < 5; i ++) {
int grade = input . nextInt () ;
total += grade ;
}

double average = total / 5.0;


System . out . println ( " The average is : " + average ) ;
}
}

Exercise 10: Prime number check


Ask the user to input an integer. Use a for loop and an if-else statement to
check if this number is prime.
Solution:
import java . util . Scanner ;

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 . " ) ;
}
}
}

Exercise 11: Perfect number check


A perfect number is a number equal to the sum of its proper divisors. Ask the
user to input a number and check if it is perfect.
Solution:
import java . util . Scanner ;

public class PerfectNumber {


public static void main ( String [] args ) {
Scanner input = new Scanner ( System . in ) ;
System . out . println ( " Enter a number : " ) ;
int num = input . nextInt () ;
int sum = 0;

for ( int i = 1; i < num ; i ++) {


if ( num % i == 0) {
sum += i ;
}
}

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 StarTriangle {


public static void main ( String [] args ) {
Scanner input = new Scanner ( System . in ) ;
System . out . println ( " Enter the height of the triangle : " ) ;
int height = input . nextInt () ;

for ( int i = 1; i <= height ; i ++) {


for ( int j = 1; j <= i ; j ++) {
System . out . print ( " * " ) ;
}
System . out . println () ;
}
}
}

Exercise 13: Calculate powers


Write a program that asks the user to input a base and an exponent, then
calculates the power using a for loop.
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;

for ( int i = 1; i <= exp ; i ++) {


result *= base ;
}

System . out . println ( base + " to the power of " + exp + " is :
" + result ) ;
}
}

Exercise 14: Salary with bonus


Ask the user to input an employee’s salary and the number of years of service.
If the employee has more than 5 years of service, they receive a 5% bonus.

6
Solution:
import java . util . Scanner ;

public class SalaryBonus {


public static void main ( String [] args ) {
Scanner input = new Scanner ( System . in ) ;
System . out . println ( " Enter the employee ’s salary : " ) ;
double salary = input . nextDouble () ;
System . out . println ( " Enter the number of years of service : " )
;
int years = input . nextInt () ;

if ( years > 5) {
salary += salary * 0.05;
}

System . out . println ( " The final salary with bonus is : " +
salary ) ;
}
}

Exercise 20: Count words in a sentence


Ask the user to input a sentence, then use a loop to count how many words the
sentence contains.
Solution:
import java . util . Scanner ;

public class WordCounter {


public static void main ( String [] args ) {
Scanner input = new Scanner ( System . in ) ;
System . out . println ( " Enter a sentence : " ) ;
String sentence = input . nextLine () ;

String [] words = sentence . split ( " \\ s + " ) ;


System . out . println ( " Number of words : " + words . length ) ;
}
}

You might also like