[go: up one dir, main page]

0% found this document useful (0 votes)
30 views8 pages

Computer Java 2

The document contains a self-profile of a student named Delisha Dutta, who is in IX Science and studying Computer Applications. It includes four programming assignments in Java, covering series generation, reversing numbers, calculating GCD and LCM, and checking for palindrome and perfect numbers. Each program is accompanied by its algorithm, sample input/output, and source code.

Uploaded by

duttadelisha09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views8 pages

Computer Java 2

The document contains a self-profile of a student named Delisha Dutta, who is in IX Science and studying Computer Applications. It includes four programming assignments in Java, covering series generation, reversing numbers, calculating GCD and LCM, and checking for palindrome and perfect numbers. Each program is accompanied by its algorithm, sample input/output, and source code.

Uploaded by

duttadelisha09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

SELF PROFILE

NAME: DELISHA DUTTA


CLASS: IX SCIENCE
ROLL NO: 09
SUBJECT: COMPUTER APPLICATIONS

1
INDEX
SLOT TITLE PAGE
NO NO

1. PROGRAM NO. 1

2. PROGRAM NO. 2

3. PROGRAM NO. 3

4. PROGRAM NO. 4

2
Program No: 1
Write a Program in java to obtain the first eight numbers of the following series using switch cases.

i) 1,11,111,1111………………
ii) 4, 16, 36, 64,
iii) 0, 3, 8, 15,
iv) 24, 99, 224, 399
v) 2, 5, 10, 17,

import java.util.Scanner;

public class SeriesGenerator {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Choose a series (1-5):");
int choice = sc.nextInt();

switch(choice) {
case 1:
// Series i: 1, 11, 111, 1111...
for(int i = 1, num = 1; i <= 8; i++, num = num * 10 + 1) {
System.out.print(num + " ");
}
break;
case 2:
// Series ii: 4, 16, 36, 64...
for(int i = 2; i <= 8; i++) {
System.out.print((int)Math.pow(i, 2) * 4 + " ");
}
break;
case 3:
// Series iii: 0, 3, 8, 15...
for(int i = 1; i <= 8; i++) {
System.out.print((i * i - 1) + " ");
}
break;
case 4:
// Series iv: 24, 99, 224, 399...
int[] series4 = {24, 99, 224, 399, 624, 899, 1224, 1599};
for(int num : series4) {
System.out.print(num + " ");
}
break;
case 5:
// Series v: 2, 5, 10, 17...
for(int i = 1; i <= 8; i++) {

3
System.out.print(i * i + 1 + " ");
}
break;
default:
System.out.println("Invalid choice!");
}
}
}

ALGORITHM:

1. Start the program.


2. Take input from the user to choose a series (1-5).
3. Use a switch-case to handle different series generation:
Case 1: Generate the series 1, 11, 111, 1111... up to 8 terms.
Case 2: Generate the series 4, 16, 36, 64... up to 8 terms.
Case 3: Generate the series 0, 3, 8, 15... up to 8 terms.
Case 4: Output the predefined series 24, 99, 224, 399... up to 8 terms.
Case 5: Generate the series 2, 5, 10, 17... up to 8 terms.
4. Display the generated series.
5. If the input is invalid, print an error message.
6. End the program.

Program No: 2
Write a program to input a number and display the new number after reversing the digits of the original
number. The program also displays the absolute difference between the original number and the
reversed number.

Sample Input: 194


Sample Output: 491
Absolute Difference= 297

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 original = num;


int reversed = 0;

// Reverse the number


while(num != 0)

4
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}

int absoluteDifference = Math.abs(original - reversed);

System.out.println("Reversed Number: " + reversed);


System.out.println("Absolute Difference: " + absoluteDifference);
}
}

ALGORITHM:

1. Start the program.


2. Take input from the user (an integer number).
3. Initialize reversed = 0.
4. Use a loop to reverse the number:
Extract the last digit of the number by calculating num % 10.
Multiply reversed by 10 and add the extracted digit.
Remove the last digit of the number by dividing num by 10.
5. After the loop ends, store the reversed number.
6. Calculate the absolute difference between the original number and the reversed number.
7. Display the reversed number and the absolute difference.
8. End the program.

Program No:3
The Greatest Common Divisor (GCD) of two integers is calculated by the continued division method.
Divide the larger number by the smaller; the remainder then divides the previous divisor. The process
repeats unless the remainder reaches to zero. The last divisor results in GCD. Write a program to accept
two numbers and find LCM and GCD of two numbers.

Sample Input: 25,35


Sample Output: GCD=5
LCM=Multiplication of two numbers/GCD
Sample (25*35)/5=175

import java.util.Scanner;

public class GCD_LCM {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter two numbers: ");
int num1 = sc.nextInt();
int num2 = sc.nextInt();

5
int gcd = findGCD(num1, num2);
int lcm = (num1 * num2) / gcd;

System.out.println("GCD: " + gcd);


System.out.println("LCM: " + lcm);
}

// Helper method to calculate GCD


public static int findGCD(int a, int b) {
while(b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}

ALGORITHM:

1. Start the program.


2. Take two integer inputs from the user.
3. Use the Euclidean algorithm to find the GCD:
While the second number is not zero, replace the first number with the second number, and the
second number with the remainder of the division between the first number and the second number.
4. The first number after the loop ends is the GCD.
5. Calculate the LCM using the formula:
LCM = (num1 * num2) / GCD
6. Display the GCD and LCM.
7. End the program.

Program No: 4
Write a menu driven class to accept a number from the user and check whether it is a Palindrome or a
Perfect number.
(a) Palindrome number: (A number is a Palindrome which when read in reverse order is same as in the
right order) Example: 121, 101, 565 etc.
(b) Perfect number: (A number is called perfect if it is equal to the sum of its factors other than the
number itself.)

Example: 6 = 1 + 2 + 3

import java.util.Scanner;

public class PalindromePerfectNumber {


public static void main(String[] args) {

6
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();

System.out.println("Choose an option:");
System.out.println("1. Check if Palindrome");
System.out.println("2. Check if Perfect Number");
int choice = sc.nextInt();

switch(choice) {
case 1:
if(isPalindrome(num)) {
System.out.println(num + " is a Palindrome number.");
} else {
System.out.println(num + " is not a Palindrome number.");
}
break;
case 2:
if(isPerfect(num)) {
System.out.println(num + " is a Perfect number.");
} else {
System.out.println(num + " is not a Perfect number.");
}
break;
default:
System.out.println("Invalid choice!");
}
}

// Helper method to check if a number is a palindrome


public static boolean isPalindrome(int num) {
int original = num, reversed = 0;
while(num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
return original == reversed;
}

// Helper method to check if a number is a perfect number


public static boolean isPerfect(int num) {
int sum = 0;
for(int i = 1; i <= num / 2; i++) {
if(num % i == 0) {
sum += i;
}
}

7
return sum == num;
}
}

ALGORITHM:
1. Start the program.
2. Take an integer input from the user.
3. Display a menu with two options:
Check if the number is a Palindrome.
Check if the number is a Perfect number.
4. Based on the user’s choice:
If Palindrome:
Initialize reversed = 0.
Reverse the number by:
Extracting the last digit of the number.
Adding it to reversed after multiplying reversed by 10.
Dividing the original number by 10.
If the reversed number equals the original, the number is a palindrome.
If Perfect number:
Initialize sum = 0.
For each number from 1 to half of the input number, check if it divides the number without a
remainder.
Add all divisors to sum.
If sum equals the input number, the number is perfect.
5. Display the result based on the chosen check.
6. End the program.

You might also like