The document contains a series of Java programming exercises, including finding the sum and average of ten numbers using arrays, reversing a digit number, printing the Fibonacci series, implementing method overriding, and checking if a number is prime. Each exercise is accompanied by code examples and sample outputs. The document serves as a practical guide for learning basic programming concepts in Java.
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 ratings0% found this document useful (0 votes)
15 views11 pages
Bca-Ii Year Java Programming Practical
The document contains a series of Java programming exercises, including finding the sum and average of ten numbers using arrays, reversing a digit number, printing the Fibonacci series, implementing method overriding, and checking if a number is prime. Each exercise is accompanied by code examples and sample outputs. The document serves as a practical guide for learning basic programming concepts in Java.
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/ 11
INDEX
1.Write A Program to Find Sum & Average Of 10number Using Arrays.
2.Write A Program to Display Reverse of a Digit Number Using Array.
3.Write A Program to Print Fibonacci Series. With Output.
4.Write A Program to Implement Method Overriding.
5.Write A Program to Check the Number Prime or Not.
1.Write A Program to Find Sum & Average Of 10number Using Arrays? Ans: import java.util.Scanner;
public class SumAverage {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] numbers = new int[10]; int sum = 0;
// Taking input for 10 numbers
System.out.println("Enter 10 numbers:"); for (int i = 0; i < 10; i++) { numbers[i] = sc.nextInt(); sum += numbers[i]; }
// Calculating average double average = sum / 10.0;
// Displaying sum and average
System.out.println("Sum of the numbers: " + sum); System.out.println("Average of the numbers: " + average); } } Output: Enter 10 numbers: 10 20 30 40 50 60 70 80 90 100 Sum of the numbers: 550 Average of the numbers: 55.0 2.Write A Program to Display Reverse of A Digit Number Using Array? Ans: import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) { Scanner sc = new Scanner(System.in);
// Taking number as input
System.out.print("Enter a number: "); int number = sc.nextInt();
// Convert number to string to easily reverse the digits
for (int i = digits.length - 1; i >= 0; i--) { reversed += digits[i]; }
// Display the reversed number
System.out.println("Reversed number: " + reversed); } } Output: Enter a number: 12345 Reversed number: 54321 3.Write A Program to Print Fibonacci Series. Ans: import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) { Scanner sc = new Scanner(System.in);
// Taking the number of terms as input
System.out.print("Enter the number of terms: "); int n = sc.nextInt();
int a = 0, b = 1;
System.out.print("Fibonacci series: ");
for (int i = 0; i < n; i++) {
System.out.print(a + " "); int nextTerm = a + b; a = b; b = nextTerm; } } } Output: Enter the number of terms: 10 Fibonacci series: 0 1 1 2 3 5 8 13 21 34 4.Write A Program to Implement Method Overriding? Ans: class Animal { void sound() { System.out.println("Animals make sounds"); } }
public static void main(String[] args) { Animal animal = new Animal(); Dog dog = new Dog();
animal.sound(); // Calls Animal's sound method
dog.sound(); // Calls Dog's sound method } } Output: Animals make sounds Dog barks 5.Write A Program to Check the Number Prime or Not? Ans: import java.util.Scanner;
public class PrimeNumber {
public static void main(String[] args) { Scanner sc = new Scanner(System.in);
// Taking number as input
System.out.print("Enter a number: "); int number = sc.nextInt();
boolean isPrime = true;
// Check if the number is prime
for (int i = 2; i <= number / 2; i++) { if (number % i == 0) { isPrime = false; break; } }
if (isPrime && number > 1) {
System.out.println(number + " is a prime number."); } else { System.out.println(number + " is not a prime number."); } } }