SELF PROFILE
NAME: Delisha Dutta
CLASS: IX
SECTION: Science
ROLL NO: 09
SUBJECT: Computer Applications
1
INDEX
SERIA TITLE PAG
L NO. E
NO.
1. INTRODUCTION 3
2. PROGRAM NO. 1 4-6
3. PROGRAM NO.2 7-9
4. PROGRAM NO. 3 9-11
5. PROGRAM NO. 4 11-13
6. PROGRAM NO. 5 13-14
7. PROGRAM NO. 6 15-16
2
Introduction:
Java is a popular programming language, created in 1995.
It is owned by Oracle, and more than 3 billion devices run Java.
It is used for:
Mobile applications (especially Android apps)
Desktop applications
Web applications
Web servers and application servers
Games
Database connection
And much, much more!
Uses of Java:
Java works on different platforms (Windows, Mac, Linux,
Raspberry Pi, etc.)
It is one of the most popular programming languages in the world
It has a large demand in the current job market
It is easy to learn and simple to use
It is open-source and free
It is secure, fast and powerful
It has huge community support (tens of millions of developers)
Java is an object-oriented language which gives a clear structure to
programs and allows code to be reused, lowering development costs
As Java is close to C++ and C#, it makes it easy for programmers to
switch to Java or vice versa
The six programming tasks are written in Java, a versatile and widely-
used programming language known for its portability, performance, and
robust ecosystem. Java's object-oriented principles, platform independence
through the Java Virtual Machine (JVM), and extensive standard libraries
make it a popular choice for various applications, from web development to
enterprise solutions and mobile applications. The language's strong typing
and automatic memory management contribute to its reliability and ease of
maintenance, making it a preferred option for many developers and
organizations worldwide.
3
Program no.: 1
Program Definition:
Write a program to compute and display the sum of the following series
using switch case
• S = (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + ----+ (1 + 2 + 3 + ----- + n ) /
(1 * 2 * 3 * ----- * n)
• S = 1 + (x+2)/2! + (2x+3)/3! + (3x+4)/4! + ……… to n terms:
Algorithm:
Algorithm for Series 1
1. Input: n (number of terms).
2. Initialize: sum = 0.
3. For i from 1 to n:
Compute the numerator: Sum of integers from 1 to i.
Compute the denominator: Factorial of i.
Add (numerator/denominator) to sum.
4. Output: sum.
Algorithm for Series 2
1. Input: x (variable) and n (number of terms).
2. Initialize: sum = 1 (first term is 1).
3. For i from 2 to n:
Compute the numerator: i * x + (i + 1).
Compute the denominator: Factorial of i.
Add (numerator/denominator) to sum.
4. Output: sum.
4
Program code:
import java.util.Scanner;
class SeriesSum
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Choose the series to compute:");
System.out.println("1. S = (1+2)/(1*2) + (1+2+3)/(1*2*3) + ... +
(1+2+3+...+n)/(1*2*3*...*n)");
System.out.println("2. S = 1 + (x+2)/2! + (2x+3)/3! + (3x+4)/4! + ...
to n terms");
int choice = sc.nextInt();
switch (choice)
{
case 1:
{
System.out.println("Enter the value of n:");
int n = sc.nextInt();
double sum = 0;
for (int i = 1; i <= n; i++)
{
int numerator = 0, denominator = 1;
for (int j = 1; j <= i; j++)
{
numerator += j;
denominator *= j;
}
sum += (double) numerator / denominator;
}
System.out.println("Sum of the series: " + sum);
}
case 2:
{
5
System.out.println("Enter the value of x:");
int x = sc.nextInt();
System.out.println("Enter the value of n:");
int n = sc.nextInt();
double sum = 1; // Initial term is 1
for (int i = 2; i <= n; i++) {
int numerator = i * x + (i + 1);
int denominator = 1; // Factorial computation
for (int j = 1; j <= i; j++) {
denominator *= j;
}
sum += (double) numerator / denominator;
}
System.out.println("Sum of the series: " + sum);
}
default -> System.out.println("Invalid choice! Please choose 1
or 2.");
}
}
}
VARIABLE DATA TYPE PURPOSE
sc Scanner to take input from the
user.
choice Int stores the user's
choice
n Int representing the
number of terms
x Int to store the user-
provided value of
sum double accumulates the total
sum
i Int outer loop counter,
representing the
current term
j Int inner loop counter
numerator Int stores the computed
numerator
6
denominator Int computed factorial
value
Program no.: 2
Program Definition:
Write programs to display the following patterns:
1
21
321
4321
54321
Pattern (a)
12345
12341
12322
12333
14444
Pattern(b)
Algorithm:
PATTERN A:
1. Start
2. Input: n (number of rows).
3. For each row i from 1 to n (outer loop):
For each column j from i to 1 (inner loop):
Print j without moving to the next line.Move to the next line after the inner
loop.
7
4. End
PATTERN B:
1. Start.
2. Input: n (number of rows).
3. For each row i from 1 to n (outer loop):
For each column j from 1 to n (inner loop):
If j <= n - i, print j without moving to the next line.
Else, print i without moving to the next line.
Move to the next line after the inner loop.
4. End
Program code:
PATTERN A:
public class PatternA
{
public static void main()
{
int n = 5; // Number of rows
for (int i = 1; i <= n; i++)
{
for (int j = i; j >= 1; j--)
{
System.out.print(j);
}
System.out.println();
}
}
}
PATTERN B:
8
public class PatternB
{
public static void main()
{
int n = 5; // Number of rows
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (j <= n - i)
{
System.out.print(j);
}
else
{
System.out.print(i);
}
}
System.out.println();
}
}
}
VARIABLE DATA TYPE PURPOSE
n Int to store the number
of rows
i Int the outer loop
counter
j Int the inner loop
counter
9
Program no.: 3
Program Definition:
Generate the pattern:
*
***
*****
*******
*****
***
*
Algorithm:
1. Input: n (number of rows for the top half excluding the middle row).
2. Top Half:
For i from 1 to n:
Print (n - i) spaces.
Print (2 * i - 1) stars.
3. Middle Row: It is the last row of the top half.
4. Bottom Half:
For i from n - 1 to 1:
Print (n - i) spaces.
Print (2 * i - 1) stars.
5. Output: The diamond pattern.
Program code:
public class DiamondPattern
{
public static void main()
{
int n = 4; // Number of rows for the top half (excluding the middle
row)
10
// Generate the top half of the diamond (including the middle row)
for (int i = 1; i <= n; i++)
{
// Print spaces
for (int j = i; j < n; j++)
{
System.out.print(" ");
}
// Print stars
for (int j = 1; j <= (2 * i - 1); j++)
{
System.out.print("*");
}
System.out.println();
}
// Generate the bottom half of the diamond
for (int i = n - 1; i >= 1; i--)
{
// Print spaces
for (int j = n; j > i; j--)
{
System.out.print(" ");
}
// Print stars
for (int j = 1; j <= (2 * i - 1); j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
11
VARIABLE DATA TYPE PURPOSE
N Int stores the number of
rows for the top half
I Int loop counter that
iterates through the
rows for both the top
and bottom halves of
the diamond
J Int loop counter used to
print spaces and
stars
Program no.: 4
Program Definition:
A Dudeney number is a positive integer that is a perfect cube such that the
sum of its digits is equal to the cube root of the number. Write a program to
input a number and check and print whether it is a Dudeney number or not.
Example: Consider the number 512. Sum of digits = 5 + 1 + 2 = 8 Cube
root of 512 = 8 .As Sum of digits = Cube root of Number hence 512 is a
Dudeney number.
Algorithm:
1. Start
2. Input: Read the number n (a positive integer).
3. Calculate the cube root:
Compute cubeRoot = round(cbrt(n)) (round the cube root to the nearest
integer).
4. Check if the number is a perfect cube:
If cubeRoot * cubeRoot * cubeRoot != n, print "n is not a Dudeney number"
and Exit.
5. Calculate the sum of digits of the number:
Initialize sumOfDigits = 0 and temp = n.
While temp > 0:
Add the last digit of temp to sumOfDigits using sumOfDigits += temp % 10.
Remove the last digit from temp using temp /= 10.
12
6. Compare the sum of digits with the cube root:
If sumOfDigits == cubeRoot, print "n is a Dudeney number".
7. End.
Program code:
import java.util.Scanner;
class DudeneyNumber
{
public static void main()
{
Scanner sc = new Scanner(System.in);
// Input the number
System.out.println("Enter a positive integer:");
int number = sc.nextInt();
// Calculate the cube root of the number
int cubeRoot = (int) Math.round(Math.cbrt(number));
// Check if the number is a perfect cube
if (cubeRoot * cubeRoot * cubeRoot == number)
{
// Calculate the sum of the digits
int sumOfDigits = 0, temp = number;
while (temp > 0) {
sumOfDigits += temp % 10;
temp /= 10;
}
// Check if sum of digits equals the cube root
if (sumOfDigits == cubeRoot)
{
System.out.println(number + " is a Dudeney number.");
}
else
{
System.out.println(number + " is not a Dudeney number.");
13
}
}
else
{
System.out.println(number + " is not a Dudeney number.");
}
}
}
VARIABLE DATA TYPE PURPOSE
Number Int stores the positive
integer input
cubeRoot Int stores the cube root
of the number
sumOfDigits Int accumulates the sum
of the digits
Temp Int a temporary variable
to hold the value of
number
Program no.: 5
Program Definition:
Write a program to display all the numbers between m and n input from the
keyboard (where m0, n>0), check and print the numbers that are perfect
square. e.g. 25, 36, 49, are said to be perfect square numbers.
Algorithm:
1. Start
2. Input: Read two integers m and n such that m > 0, n > 0, and m < n.
3. Check Input Validity:
If m ≤ 0 or n ≤ 0 or m ≥ n, print an error message and exit.
4. Iterate through Numbers:
For each number i from m to n:
Compute sqrt = (int) Math.sqrt(i).
14
If sqrt * sqrt == i, print i as a perfect square.
5. End.
Program code:
import java.util.Scanner;
class PerfectSquares
{
public static void main()
{
Scanner sc = new Scanner(System.in);
// Input m and n
System.out.println("Enter the value of m (m > 0):");
int m = sc.nextInt();
System.out.println("Enter the value of n (n > 0 and m < n):");
int n = sc.nextInt();
// Validate input
if (m > 0 && n > 0 && m < n)
{
System.out.println("Perfect square numbers between " + m + "
and " + n + ":");
for (int i = m; i <= n; i++)
{
int sqrt = (int) Math.sqrt(i);
if (sqrt * sqrt == i)
{
System.out.println(i);
}
}
}
else
{
System.out.println("Invalid input! Ensure m > 0, n > 0, and m <
n.");
}
}
}
15
VARIABLE DATA TYPE PURPOSE
M Int stores the lower
bound of the range
N Int stores the upper
bound of the range
Sqrt Int stores the square
root of the current
number (i)
I Int The loop counter
Program no.: 6
Program Definition:
Write a Java program to accept an amount from the user and hence find in
each case:-
a) No of Rs. 100 notes and the remainder amount.
b) No of Rs. 50 notes and the remainder amount.
c) No of Rs. 20 notes and the remainder amount.
d) No of Rs. 10 notes and the remainder amount.
Eg. In Rs. 542; No of Rs. 100 = 5, Remainder = Rs. 42
No of Rs. 50 = 10, Remainder = Rs. 42
No of Rs. 20 = 27, Remainder = Rs. 2.
No of Rs. 10 = 54, Remainder = Rs. 2.
Algorithm:
1. Start
2. Input: Read the amount amount from the user.
3. Initialize Denominations: Set denominations as 100, 50, 20, and 10.
4. Calculate Notes:
For each denomination d in [100, 50, 20, 10]:
Compute numNotes = amount / d.
Compute remainder = amount % d.
Print numNotes and remainder.
Update amount = remainder.
5. End.
16
Program code:
import java.util.Scanner;
public class CalculateNotes
{
public static void main()
{
Scanner sc = new Scanner(System.in);
// Input the amount
System.out.println("Enter the amount:");
int amount = sc.nextInt();
// Denominations
int denominations = {100, 50, 20, 10};
// Calculate notes and remainders
for (int d : denominations) {
int numNotes = amount / d;
int remainder = amount % d;
System.out.println("Number of Rs. " + d + " notes: " +
numNotes + ", Remainder: Rs. " + remainder);
amount = remainder;
}
}
}
VARIABLE DATA TYPE PURPOSE
Amount int Stores the total
amount entered
Denominations int holds the
denominations of
notes
d int A loop variable
numNotes int Stores the number of
notes
Remainder int Stores the remaining
amount
17
18