0% found this document useful (0 votes)
30 views

Programming Home Work 3

This lab manual covers several Java programs including programs to check if a number is even or odd, check if a year is a leap year, calculate powers of a number using loops and math functions, calculate the average of array elements, check if an array contains a specific value, and check if a number is prime. The document provides the code and expected output for each program to demonstrate how to complete the programming tasks.

Uploaded by

Elham Hotak
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Programming Home Work 3

This lab manual covers several Java programs including programs to check if a number is even or odd, check if a year is a leap year, calculate powers of a number using loops and math functions, calculate the average of array elements, check if an array contains a specific value, and check if a number is prime. The document provides the code and expected output for each program to demonstrate how to complete the programming tasks.

Uploaded by

Elham Hotak
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

LAB MANUAL 1

OBJECT OREINTED PROGRAMMING LECTURER: SAYED NAJMUDDIN “SADAAT”

Java Simple Programs


//Java Program to check Even or Output 1:
Odd number import
java.util.Scanner; Enter an Integer number:
class CheckEvenOdd{ 78
public static void main(String
Entered number is even
args[]) { int num; Output 2:
System.out.println("Enter an Integer Enter an Integer number:
number:"); 77
//The input provided by user is Entered number is odd
stored in num Scanner input = new
Scanner(System.in);
num = input.nextInt();
/* If number is divisible by 2 then it's
an even number * else odd number*/
if ( num % 2 == 0 )
System.out.println("Entered number
is even"); else
System.out.println("Entered number is
odd");
}
}
//Program to check whether the input year is Here we will write a java
leap or not import java.util.Scanner; program to check whether
public class Demo {
public static void main(String[] the input year is a leap year
args) { int year; or not. Before we see the
Scanner scan = new program, lets see how to
Scanner(System.in); determine whether a year is
System.out.println("Enter any a leap year mathematically:
Year:"); year = scan.nextInt();
scan.close(); To determine whether a
boolean isLeap = year is a leap year, follow
false; if(year % 4 == these steps:
0) 1. If the year is evenly divisible
by 4, go to step
{ 2. Otherwise, go to step 5.
if( year % 100 == 0) 2. If the year is evenly
divisible by 100, go to step 3.
{ if ( year % 400 == 0)
isLeap =
Otherwise, go to step 4.
3. If the year is evenly
true; else
isLeap = false; divisible by 400, go to step 4.
Page 1 of 2
} Otherwise, go to step 5.
else 4. The year is a leap year (it has
isLeap = true; 366 days).
}else {
isLeap = false; 5. The year is not a leap year (it
} has 365 days).
if(isLeap==true)
System.out.println(year + " is a Leap
Year."); else Output:
System.out.println(year + " is not a Leap
Year.");
}
} Enter any Year:
2001
2001 is not a Leap Year.
//Program to calculate power of a number In this program we are
using for loop
public class JavaExample { calculating the power of a
public static void main(String[] args) {
//Here number is the base and p is the given number using for loop.
exponent
int number = 2, p = 5; Here number is the base and p
long result = 1;
//Copying the exponent value to the is the power (exponent). So we
loop counter are calculating the result of
int i = p;
for (;i != 0; --i) number^p.Program to
{
result *= number; calculate power of a number
}
//Displaying the output using pow() function In the
System.out.println(number+"^"+ above two programs, we have
p+" = "+result);
} seen how to calculate power of
}
a number using loops. In the
following program, we are
using pow() function to
calculate power.
public class JavaExample {
public static void
main(String[] args) { int
number = 10, p = 3;
double result =
Math.pow(number, p);
System.out.println(num
ber+"^"+p+" =
"+resu
lt);
}
}

Page 2 of 2
Java Simple Programs
Fibonacci Number Output 1:
Write a method that returns the nth
element of the Fibonacci Sequence The 0 1 1 2 3 5 8 13 21 …….
Fibonacci Sequence is the series of
numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21 The next
number is found by adding up the two
numbers before it.
Assume that indexes start at zero, e.g.,
fib(0) = 0, fib(1) = 1, ...

Write a Java program to calculate the average


value of array elements.
Output:
public class Exercise4 {
public static void main(String[] args) {
Average value of the
int[] numbers = {20, 30, 25, 35, -16, 60, -
100};
array elements is : 7.0
//calculate sum of all array
elements int sum = 0;
for(int i=0; i < numbers.length ; i++)
sum = sum + numbers[i];
//calculate average value
double average = sum /
numbers.length;
System.out.println("Average is :"+
average);
}
}
Write a Java program to test if an array
contains a specific value. Output:
public class Exercise5 { true
Page 3 of 2
public static boolean contains(int[] arr, int
item) { fals
for (int n : arr) {
if (item == n) { e
return true;
}
}
return false;
Prime Number
}
public static void main(String[] args) {
A prime number is a
int[] my_array1 = {
1789, 2035, 1899, 1456, 2013,
natural number greater
than 1 that has no
positive divisors other
than 1 and itself.
Write a method that
checks if
a number is a prime
number.
1458, 2458, 1254, 1472, 2365, boolean isPrime
1456, 2265, 1457, 2456};
System.out.println(contains(my_array = true; for (int i
1, 2013)); = n - 1; i > 1; i--)
System.out.println(contains(my_array
1, 2015)); {
}
} if (n % i ==
0) {
isPrime =
false;
break;
}
}

Page 4 of 2

You might also like