/* For more exercises, visit shegertech.blogspot.com or shegertech.com Author: Eyasu M. Email: eyasu@india.com */ public class FactorialAverage { public static void main(String[] args) { int factorial; double sum = 0; int[] x = new int[8]; for (int i = 3, k = 0; i <= 10; i++, k++) { factorial = 1; for (int y = 1; y <= i; y++) { factorial *= y; } x[k] = factorial; sum += x[k]; } System.out.println(sum / x.length); } }
Monday, August 28, 2017
Wednesday, July 19, 2017
Exponent Program
/* A program that displays the value of a number raised to the power of a number.Created by Eyasu Mulugeta A.K.A BlackDiamond0014 on 6/23/2017. eyasu@india.com */ import java.util.Scanner; import static java.lang.Math.pow; public class ExponentProgram { public static void main(String[] args) { Scanner in = new Scanner(System.in); double base, exponent; System.out.print("Enter the base : "); base=in.nextDouble(); System.out.println(); System.out.print("Enter the exponent : "); exponent=in.nextDouble(); System.out.println("\n"+base+" raised to "+exponent+" is "+ pow(base,exponent)); } }
Factor Program
/*A program that displays the factors of a number. Created by Eyasu Mulugeta A.K.A BlackDiamond0014 on 6/23/2017. eyasu@india.com */ import java.util.ArrayList; import java.util.Scanner; public class FactorProgram { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num=0, size; System.out.print("Enter a number to find its factors : "); num = in.nextInt(); ArrayList<Integer> factors = new ArrayList<Integer>(); System.out.println(); System.out.print("The factors of " + num + " are "); for (int i = 1, j = 0; i <= num; i++, j++) { if (num % i != 0) continue; factors.add(i); } for (int k:factors) { System.out.print(k); if(k==num){ break; } System.out.print( ", "); } } }
Multiplication Table Program
/*A program that displays the multiplication table. Created by Eyasu Mulugeta A.K.A BlackDiamond0014 on 6/23/2017. eyasu@india.com*/ public class MultiplicationTable { public static void main(String[] args) { int j, k; System.out.print("x"); for (int m = 0; m <= 12; m++) System.out.print("\t" + m + "\t"); for (int i = 0; i <= 12; i++) { System.out.print("\n" + i); for (j = 0; j <= 12; j++) { System.out.print("\t" + (i * j) + "\t"); } } } }
Square Root Program
/* A program that displays the square root of a number. Created by Eyasu Mulugeta A.K.A BlackDiamond0014 on 6/23/2017. eyasu@india.com */
import java.util.Scanner; import static java.lang.Math.sqrt; public class SquareRoots { public static void main(String[] args) { //we used the Scanner class to create an object to input dataScanner in = new Scanner(System.in); int num=0; System.out.print("Please input a number : "); num = in.nextInt(); System.out.println("The square root of "+num+" is "+sqrt(num)); } }
Difference Program
/*A program that displays the difference between two integer numbers. Created by Eyasu Mulugeta A.K.A BlackDiamond0014 on 6/23/2017. eyasu@india.com
*/import java.util.Scanner; public class SubtractTwoNumbers { public static void main(String[] args) { int num1, num2; Scanner s = new Scanner(System.in); System.out.println("\n\n\t\t\t# # # # Subtraction Program # # # #\n\n"); System.out.print("Please enter the first number : "); num1=s.nextInt(); System.out.print("Please enter the second number : "); num2=s.nextInt(); System.out.println("\nThe difference of "+num1+" and "+num2+ " is "+(num1-num2)+"."); } }
Sum Program
/* A program that displays the sum of two integer numbers. Created by Eyasu Mulugeta A.K.A BlackDiamond0014 on 6/23/2017. eyasu@india.com */
import java.util.Scanner; public class AddTwoNumbers { public static void main(String[] args) { int num1, num2; Scanner s = new Scanner(System.in); System.out.println("\n\n\t\t\t# # # # Sum Program # # # #\n\n"); System.out.print("Please enter the first number : "); num1=s.nextInt(); System.out.print("Please enter the second number : "); num2=s.nextInt(); System.out.println("\nThe sum of "+num1+" and "+num2+ " is "+(num1+num2)+"."); } }
Sunday, July 16, 2017
Write a program that checks whether a string is palindrome or not.
/**
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
*/
import java.util.Scanner;
public class Q2Solution {
public static void main(String[] args){
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.print("Enter a string to check if it is a palindrome : ");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
*/
import java.util.Scanner;
public class Q2Solution {
public static void main(String[] args){
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.print("Enter a string to check if it is a palindrome : ");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}
Find all twin prime numbers less than 100.
/**
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 8, Example 3
*/
public class Q3Solution {
public boolean is_Prime(long n) {
if (n < 2)
return false;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0)
return false;
}
return true;
}
public static void main(String[] args) {
Q3Solution q3 = new Q3Solution();
for (int i = 2; i < 100; i++) {
if (q3.is_Prime(i) && q3.is_Prime(i + 2)) {
System.out.printf("(%d, %d)\n", i, i + 2);
}
}
}
}
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 8, Example 3
*/
public class Q3Solution {
public boolean is_Prime(long n) {
if (n < 2)
return false;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0)
return false;
}
return true;
}
public static void main(String[] args) {
Q3Solution q3 = new Q3Solution();
for (int i = 2; i < 100; i++) {
if (q3.is_Prime(i) && q3.is_Prime(i + 2)) {
System.out.printf("(%d, %d)\n", i, i + 2);
}
}
}
}
Write a Java method to check whether a year (integer) entered by the user is a leap year or not.
/**
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 8, Exercise 2
*/
public class Q2Solution {
public static void main(String[] args){
int year = 2008;
if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
System.out.println("Year " + year + " is a leap year");
else
System.out.println("Year " + year + " is not a leap year");
}
}
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 8, Exercise 2
*/
public class Q2Solution {
public static void main(String[] args){
int year = 2008;
if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
System.out.println("Year " + year + " is a leap year");
else
System.out.println("Year " + year + " is not a leap year");
}
}
Write a Java method to find the smallest number among three numbers using arrays and methods.
/**
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 8, Exercise 1
*/
public class Q1Solution {
void smallestNumber(int[] arr){
int smallestNumber =arr[0];
for (int i=1;i< arr.length;i++){
if (smallestNumber>arr[i]){
smallestNumber=arr[i];
}
}
System.out.println("The smallest number in tbe array is "+ smallestNumber);
}
public static void main(String[] args) {
int[] arr={55, 34, 11, 87, 29};
Q1Solution q1 = new Q1Solution();
q1.smallestNumber(arr);
}
}
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 8, Exercise 1
*/
public class Q1Solution {
void smallestNumber(int[] arr){
int smallestNumber =arr[0];
for (int i=1;i< arr.length;i++){
if (smallestNumber>arr[i]){
smallestNumber=arr[i];
}
}
System.out.println("The smallest number in tbe array is "+ smallestNumber);
}
public static void main(String[] args) {
int[] arr={55, 34, 11, 87, 29};
Q1Solution q1 = new Q1Solution();
q1.smallestNumber(arr);
}
}
Write a guess number game.
/**
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
*/
import java.util.Random;
import java.util.Scanner;
public class Q2Solution {
public static void main(String[] args){
System.out.println("Pick a number: ");
Scanner inputnum = new Scanner(System.in);
int maxnum;
maxnum = inputnum.nextInt();
Random rand = new Random();
int number = rand.nextInt(maxnum);
int tries = 0;
Scanner input = new Scanner(System.in);
int guess;
boolean win = false;
while (win == false){
System.out.println("Guess a number between 1 and "+ maxnum +": ");
guess = input.nextInt();
tries++;
if (guess == number){
win = true;
}
else if(guess < number){
System.out.println("Number is to low, tray again");
}
else if(guess > number){
System.out.println("Number is to high, try again");
}
}
System.out.println("You win!");
System.out.println("It took you "+ tries + " tries.");
}
}
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
*/
import java.util.Random;
import java.util.Scanner;
public class Q2Solution {
public static void main(String[] args){
System.out.println("Pick a number: ");
Scanner inputnum = new Scanner(System.in);
int maxnum;
maxnum = inputnum.nextInt();
Random rand = new Random();
int number = rand.nextInt(maxnum);
int tries = 0;
Scanner input = new Scanner(System.in);
int guess;
boolean win = false;
while (win == false){
System.out.println("Guess a number between 1 and "+ maxnum +": ");
guess = input.nextInt();
tries++;
if (guess == number){
win = true;
}
else if(guess < number){
System.out.println("Number is to low, tray again");
}
else if(guess > number){
System.out.println("Number is to high, try again");
}
}
System.out.println("You win!");
System.out.println("It took you "+ tries + " tries.");
}
}
Write a program that generates numbers between the range 0-25 and display the average of the prime numbers that are generated.
/**
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 7, Exercise 1
*/
public class Q1Solution {
double averageOfPrimeNumbers() {
double average = 0.0d;
boolean flag;
int half = 0, sum = 0, count=0;
for (int i = 2; i <= 10; i++) {
half = i / 2;
flag = true;
for (int j = 2; j <= half; j++) {
if (i % j == 0) {
flag = false;
}
}
if (flag) {
sum += i;
count++;
}
}
average = (double) sum / count;
return average;
}
public static void main(String[] args) {
Q1Solution q1 = new Q1Solution();
double average = q1.averageOfPrimeNumbers();
System.out.println("The average is " + average);
}
}
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 7, Exercise 1
*/
public class Q1Solution {
double averageOfPrimeNumbers() {
double average = 0.0d;
boolean flag;
int half = 0, sum = 0, count=0;
for (int i = 2; i <= 10; i++) {
half = i / 2;
flag = true;
for (int j = 2; j <= half; j++) {
if (i % j == 0) {
flag = false;
}
}
if (flag) {
sum += i;
count++;
}
}
average = (double) sum / count;
return average;
}
public static void main(String[] args) {
Q1Solution q1 = new Q1Solution();
double average = q1.averageOfPrimeNumbers();
System.out.println("The average is " + average);
}
}
Write a program that will ask the user to input three integer values from the keyboard. Then it will print the smallest and largest of those numbers using functions.
/**
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
*/
public class Q2Solution {
void compare(int num1, int num2, int num3) {
int smallest = 0, largest = 0;
if(num1>num2 && num1>num3)
largest = num1;
else if (num2>num1 && num2>num3)
largest= num2;
else
largest = num3;
System.out.println("Largest number = " + largest);
if(num1<num2 && num1<num3)
smallest = num1;
else if (num2<num1 && num2<num3)
smallest = num2;
else
smallest =num3;
System.out.println("Smallest number = " + smallest);
}
public static void main(String[] args) {
int n1 = 9, n2 = 5, n3 = 4;
Q2Solution q2 = new Q2Solution();
q2.compare(n1, n2, n3);
}
}
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
*/
public class Q2Solution {
void compare(int num1, int num2, int num3) {
int smallest = 0, largest = 0;
if(num1>num2 && num1>num3)
largest = num1;
else if (num2>num1 && num2>num3)
largest= num2;
else
largest = num3;
System.out.println("Largest number = " + largest);
if(num1<num2 && num1<num3)
smallest = num1;
else if (num2<num1 && num2<num3)
smallest = num2;
else
smallest =num3;
System.out.println("Smallest number = " + smallest);
}
public static void main(String[] args) {
int n1 = 9, n2 = 5, n3 = 4;
Q2Solution q2 = new Q2Solution();
q2.compare(n1, n2, n3);
}
}
Write a Java basic calculator.
/**
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 6, Exercise 1
*/
import java.util.Scanner;
public class Q1Solution {
private float firstOperannd;
private float secondOperannd;
private float result;
private int operator;
private Scanner input = new Scanner(System.in);
private boolean exitCalculator = false;
public void startCalculator() {
while (!exitCalculator) {
System.out.print("Enter 1 for addition \n"
+ "Enter 2 for subtraction \n"
+ "Enter 3 for multiplication \n"
+ "Enter 4 for division \n" + "Enter 0 for Exit"+"\n\nInput Menu number : ");
operator = input.nextInt();
switch (operator) {
case 1:
result = add();
System.out.println("\nResult is " + result+"\n");
break;
case 2:
result = subtract();
System.out.println("\nResult is " + result+"\n");
break;
case 3:
result = multiply();
System.out.println("\nResult is " + result+"\n");
break;
case 4:
result = divide();
System.out.println("\nResult is " + result+"\n");
break;
case 0:
exitCalculator = true;
System.out.println("Calculator program Terminated \n");
break;
default:
System.out.println("Please provide proper input \n");
}
}
}
private float add() {
System.out.print("\nEnter first number : ");
firstOperannd = input.nextInt();
System.out.print("\nEnter second number : ");
secondOperannd = input.nextInt();
return firstOperannd + secondOperannd;
}
private float subtract() {
System.out.print("\nEnter first number : ");
firstOperannd = input.nextInt();
System.out.print("\nEnter second number : ");
secondOperannd = input.nextInt();
return firstOperannd - secondOperannd;
}
private float multiply() {
System.out.print("\nEnter first number : ");
firstOperannd = input.nextInt();
System.out.print("\nEnter second number : ");
secondOperannd = input.nextInt();
return firstOperannd * secondOperannd;
}
private float divide() {
System.out.print("\nEnter first number : ");
firstOperannd = input.nextInt();
System.out.print("\nEnter second number : ");
secondOperannd = input.nextInt();
return firstOperannd / secondOperannd;
}
public static void main(String[] args) {
Calculation c= new Calculation();
c.startCalculator();
}
}
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 6, Exercise 1
*/
import java.util.Scanner;
public class Q1Solution {
private float firstOperannd;
private float secondOperannd;
private float result;
private int operator;
private Scanner input = new Scanner(System.in);
private boolean exitCalculator = false;
public void startCalculator() {
while (!exitCalculator) {
System.out.print("Enter 1 for addition \n"
+ "Enter 2 for subtraction \n"
+ "Enter 3 for multiplication \n"
+ "Enter 4 for division \n" + "Enter 0 for Exit"+"\n\nInput Menu number : ");
operator = input.nextInt();
switch (operator) {
case 1:
result = add();
System.out.println("\nResult is " + result+"\n");
break;
case 2:
result = subtract();
System.out.println("\nResult is " + result+"\n");
break;
case 3:
result = multiply();
System.out.println("\nResult is " + result+"\n");
break;
case 4:
result = divide();
System.out.println("\nResult is " + result+"\n");
break;
case 0:
exitCalculator = true;
System.out.println("Calculator program Terminated \n");
break;
default:
System.out.println("Please provide proper input \n");
}
}
}
private float add() {
System.out.print("\nEnter first number : ");
firstOperannd = input.nextInt();
System.out.print("\nEnter second number : ");
secondOperannd = input.nextInt();
return firstOperannd + secondOperannd;
}
private float subtract() {
System.out.print("\nEnter first number : ");
firstOperannd = input.nextInt();
System.out.print("\nEnter second number : ");
secondOperannd = input.nextInt();
return firstOperannd - secondOperannd;
}
private float multiply() {
System.out.print("\nEnter first number : ");
firstOperannd = input.nextInt();
System.out.print("\nEnter second number : ");
secondOperannd = input.nextInt();
return firstOperannd * secondOperannd;
}
private float divide() {
System.out.print("\nEnter first number : ");
firstOperannd = input.nextInt();
System.out.print("\nEnter second number : ");
secondOperannd = input.nextInt();
return firstOperannd / secondOperannd;
}
public static void main(String[] args) {
Calculation c= new Calculation();
c.startCalculator();
}
}
Write a Java program that will print the pattern as shown below.
****
*****
*******
*********
*********
*******
*****
****
/**
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 5, Exercise 4
*/
public class Q4Solution {
public static void main(String[] args) {
int i=0;
int j=0;
for(i=1;i<=9;i=i+2){
for(j=1;j<=i;++j)
System.out.print("*");
System.out.print("\n");
}
for(i=9;i>=1;i=i-2){
for(j=i;j>=1;--j)
System.out.print("*");
System.out.print("\n");
}
System.out.print("\n");
}
}
*****
*******
*********
*********
*******
*****
****
/**
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 5, Exercise 4
*/
public class Q4Solution {
public static void main(String[] args) {
int i=0;
int j=0;
for(i=1;i<=9;i=i+2){
for(j=1;j<=i;++j)
System.out.print("*");
System.out.print("\n");
}
for(i=9;i>=1;i=i-2){
for(j=i;j>=1;--j)
System.out.print("*");
System.out.print("\n");
}
System.out.print("\n");
}
}
Write a program that accepts a number from the user and tells whether the number is a prime number.
/**
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 5, Exercise 3
*/
import java.util.Scanner;
public class Q3Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = 0, half = 0;
boolean flag = false;
System.out.print("Enter a positive integer number : ");
num = in.nextInt();
half = num / 2;
if (num > 0) {
for (int i = 2; i <= half; i++) {
if (num % i == 0) {
flag = true;
}
}
if (flag) {
System.out.println(num + " is not a prime number.");
} else {
System.out.println(num + " is a prime number.");
}
} else {
System.out.println("Please enter a non-negative integer number.");
}
}
}
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 5, Exercise 3
*/
import java.util.Scanner;
public class Q3Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = 0, half = 0;
boolean flag = false;
System.out.print("Enter a positive integer number : ");
num = in.nextInt();
half = num / 2;
if (num > 0) {
for (int i = 2; i <= half; i++) {
if (num % i == 0) {
flag = true;
}
}
if (flag) {
System.out.println(num + " is not a prime number.");
} else {
System.out.println(num + " is a prime number.");
}
} else {
System.out.println("Please enter a non-negative integer number.");
}
}
}
Write a program that displays the numbers between 1 and 10 in reverse order(descending order).
/**
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 5, Exercise 2
*/
import java.util.Scanner;
public class Q2Solution {
public static void main(String[] args) {
for (int i=10; i>0;i--){
System.out.println(i);
}
}
}
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 5, Exercise 2
*/
import java.util.Scanner;
public class Q2Solution {
public static void main(String[] args) {
for (int i=10; i>0;i--){
System.out.println(i);
}
}
}
Write a java program that accepts a number from the user and tells whether it is a perfect number or not.
/**
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 5, Exercise 1
*/
import java.util.Scanner;
public class Q1Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num1 = 0, sum=0;
System.out.print("Enter the number : ");
num1 = in.nextInt();
for (int i=1; i<num1;i++){
if (num1%i==0){
sum+=i;
}
}
if(sum==num1){
System.out.println(num1+" is a perfect number");
}
else {
System.out.println(num1+ " is not a perfect number");
}
}
}
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 5, Exercise 1
*/
import java.util.Scanner;
public class Q1Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num1 = 0, sum=0;
System.out.print("Enter the number : ");
num1 = in.nextInt();
for (int i=1; i<num1;i++){
if (num1%i==0){
sum+=i;
}
}
if(sum==num1){
System.out.println(num1+" is a perfect number");
}
else {
System.out.println(num1+ " is not a perfect number");
}
}
}
Write a Java program that prompts the user to input three integer values and find the greatest value of the three values.
/**
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 4, Exercise 3
*/
import java.util.Scanner;
public class Q3Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num1 = 0, num2 = 0, num3 = 0, max = 0;
System.out.print("First value : ");
num1 = in.nextInt();
System.out.print("Second value : ");
num2 = in.nextInt();
System.out.print("Third value : ");
num3 = in.nextInt();
if (num1 > num2 && num1 > num3) {
max = num1;
System.out.println("The maximum value is " + max);
} else if (num2 > num1 && num2>num3) {
max= num2;
System.out.println("The maximum value is " + max);
}
else{
max = num3;
System.out.println("The maximum value is " + max);
}
}
}
* Created by Jo on 7/10/2017.
* For more exercises and lessons, visit http://shegertech.blogspot.com/
* Chapter 4, Exercise 3
*/
import java.util.Scanner;
public class Q3Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num1 = 0, num2 = 0, num3 = 0, max = 0;
System.out.print("First value : ");
num1 = in.nextInt();
System.out.print("Second value : ");
num2 = in.nextInt();
System.out.print("Third value : ");
num3 = in.nextInt();
if (num1 > num2 && num1 > num3) {
max = num1;
System.out.println("The maximum value is " + max);
} else if (num2 > num1 && num2>num3) {
max= num2;
System.out.println("The maximum value is " + max);
}
else{
max = num3;
System.out.println("The maximum value is " + max);
}
}
}