Deepika java

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

RAM CHAMELI CHADHA VISHVAS GIRLS COLLEGE

Meerut Road, Ghaziabad

PRACTICAL FILE

Subject: JAVA PROGRAMMING AND DYNAMIC WEBPAGE DESIGN (BCA-506)

Semester: BCA-3rd Year (5th Semester)

Submitted By : Submitted To :

Ms. Deepika Solanki Ms. Gunjan Singh


Roll No.: 220953106011
INDEX
S.NO TITLE TEACHER’S
SIGN
1. WAP to Print 10 Natural Numbers

2. WAP to Print Sum of Numbers up 1 to n

3. WAP to Print Palindrome of a Number

4. WAP to Print Armstrong Number

5. WAP to Print Factorial of a Given Number

6. WAP to Check if a Number is Prime or Not

7. WAP to Print Reverse of a Number

8. WAP to Print Even Numbers up 1 to n

9. WAP to Print Fibonacci Series

10. WAP to Print Average of First 10 Natural Numbers

11. WAP to Print Maximum Element in an Array

12. WAP to Print Second Largest Element in an Array

13. WAP to Check Whether a Number is Even or Odd

14. WAP to Concatenate Two Strings

15. WAP to Print Multiplication Table in an Array


1. WAP to Print 10 Natural Numbers

public class NaturalNumbers {

public static void main(String[] args) {

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

System.out.print(i + " ");


}

}
2. WAP to Print Sum of Numbers up 1 to n

import java.util.Scanner;

public class SumUpToN {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int n = sc.nextInt();

int sum = 0;

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

sum += i;

System.out.println("Sum of numbers up to " + n + ": " + sum);

}
3. WAP to Print Palindrome of a Number

import java.util.Scanner;

public class Palindrome {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt();

int original = num, reverse = 0;

while (num != 0) {

int digit = num % 10;

reverse = reverse * 10 + digit;

num /= 10;

if (original == reverse) {

System.out.println(original + " is a palindrome.");

} else {

System.out.println(original + " is not a palindrome.");

}
4. WAP to Print Armstrong Number

import java.util.Scanner;

public class Armstrong {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt();

int original = num, sum = 0;

while (num != 0) {

int digit = num % 10;

sum += Math.pow(digit, 3);

num /= 10;

}
if (original == sum) {

System.out.println(original + " is an Armstrong number.");

} else {

System.out.println(original + " is not an Armstrong number.");

}
5. WAP to Print Factorial of a Given Number

import java.util.Scanner;

public class Factorial {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int n = sc.nextInt();

long factorial = 1;

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

factorial *= i;

System.out.println("Factorial of " + n + " is: " + factorial);

}
6. WAP to Check if a Number is Prime or Not

import java.util.Scanner;

public class PrimeCheck {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int n = sc.nextInt();

boolean isPrime = true;

if (n <= 1) {

isPrime = false;
} else {

for (int i = 2; i <= n / 2; i++) {

if (n % i == 0) {

isPrime = false;

break;

}
}

if (isPrime) {

System.out.println(n + " is a prime number.");

} else {
System.out.println(n + " is not a prime number.");

}
7. WAP to Print Reverse of a Number

import java.util.Scanner;

public class ReverseNumber {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt();

int reverse = 0;

while (num != 0) {

int digit = num % 10;

reverse = reverse * 10 + digit;

num /= 10;

}
System.out.println("Reversed number is: " + reverse);

}
8. WAP to Print Even Numbers up 1 to n

import java.util.Scanner;

public class EvenNumbers {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the value of n: ");

int n = sc.nextInt();

System.out.println("Even numbers up to " + n + ":");

for (int i = 2; i <= n; i += 2) {

System.out.print(i + " ");


}

}
9. WAP to Print Fibonacci Series

import java.util.Scanner;

public class Fibonacci {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the number of terms: ");

int n = sc.nextInt();

int a = 0, b = 1, next;

System.out.println("Fibonacci series:");

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

System.out.print(a + " ");

next = a + b;

a = b;
b = next;

}
10. WAP to Print Average of First 10 Natural Numbers

public class Average {

public static void main(String[] args) {

int sum = 0;

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

sum += i;

double average = sum / 10.0;

System.out.println("Average of first 10 natural numbers is: " + average);

}
11. WAP to Print Maximum Element in an Array

import java.util.Scanner;

public class MaxInArray {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the number of elements: ");

int n = sc.nextInt();

int[] arr = new int[n];


System.out.println("Enter the elements of the array:");

for (int i = 0; i < n; i++) {

arr[i] = sc.nextInt();

int max = arr[0];

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

if (arr[i] > max) {

max = arr[i];

System.out.println("Maximum element in the array is: " + max);

}
12. WAP to Print Second Largest Element in an Array

import java.util.Scanner;

public class SecondLargest {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the number of elements: ");

int n = sc.nextInt();

int[] arr = new int[n];


System.out.println("Enter the elements of the array:");

for (int i = 0; i < n; i++) {

arr[i] = sc.nextInt();

int largest = Integer.MIN_VALUE, secondLargest = Integer.MIN_VALUE;

for (int i = 0; i < n; i++) {

if (arr[i] > largest) {

secondLargest = largest;

largest = arr[i];

} else if (arr[i] > secondLargest && arr[i] != largest) {

secondLargest = arr[i];

}
}
if (secondLargest == Integer.MIN_VALUE) {

System.out.println("No second largest element.");

} else {

System.out.println("Second largest element in the array is: " +


secondLargest);

}
13. WAP to Check Whether a Number is Even or Odd

import java.util.Scanner;

public class EvenOrOdd {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt();

if (num % 2 == 0) {

System.out.println(num + " is an even number.");

} else {

System.out.println(num + " is an odd number.");

}
14. WAP to Concatenate Two Strings

import java.util.Scanner;

public class ConcatenateStrings {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the first string: ");

String str1 = sc.nextLine();

System.out.print("Enter the second string: ");

String str2 = sc.nextLine();

String result = str1 + str2;

System.out.println("Concatenated string is: " + result);

}
15. WAP to Print Multiplication Table in an Array

import java.util.Scanner;

public class MultiplicationTable {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the number for the multiplication table: ");

int num = sc.nextInt();

System.out.print("Enter the number of terms: ");

int n = sc.nextInt();

int[] table = new int[n];

for (int i = 0; i < n; i++) {

table[i] = num * (i + 1);


}

System.out.println("Multiplication table of " + num + ":");

for (int i = 0; i < n; i++) {

System.out.println(num + " x " + (i + 1) + " = " + table[i]);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy