[go: up one dir, main page]

0% found this document useful (0 votes)
16 views62 pages

Document X

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)
16 views62 pages

Document X

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/ 62

Computer

Assignment
NAME: SUBHADIP SARKAR
CLASS:IX
UNIQUE ID:
SCHOOL CODE:WB422
YEAR:2024-2025
Acknowledgement
I would like to thank my computer
teacher as well as our Principal mam
who gave me pleasure to do
fantastic project which also enlarged
my knowledge and I was able to
know many things. I am really
thankful to them.

I would also like to thank my parents


who provided me with all the
resources also helped in finding the
accurate imformation and thus,
helping me to finalize my project
within the provided time frame.
Page:1
Index
Serial. No Topic Page.
No
1 Introduction 6
2 Converting from Celsius to 7-8
farenheit
3 Checking if a number is a 9-10
prime number or not
4 Swapping values of two 11-12
variables without using
third variable
5 Checking if a year is a leap 13-14
year or not
6 Assigning grades to student 15-16
based on the percentage of
marks scored(using if-else-if
statement)
7 Performing an arithmetic 17-19
operation on two number
based on the symbol of the
operated entered.
Page:2
8 Calculating the factorial of a 20-
number inputted by the user. 21
9 Displaying the fibonacci series 22-
up to the given number of terms. 23
10 Reversing the digits of the 24-
number. 25
11 Menu driven program to 26-
calculate and print the sum of 27
the following series:
i) S=1/2+2/3+3/4+4/5+5/6+
….n
ii) S=2/4+4/6+6/8+8/10+10/
12+….n

12 Calculating and printing the 28-


series 29
i) S=x/2+x/5+x/8+…..+x/20
Page:3

13 Displaying the following 32-34


patterns :
i) 54321
5432
543
54
5
54
543
5432
54321 35-36
ii) *
* *
* * *
* * * *
* * * * *

14 Displaying the Floyd’s 37-38


triangle
15 Program to check whether 39-41
the is an armstrong
number or not
16 Program to check whether 42-44
the number is a niven
number or not
17 Program to check whether 45-47
the number is palindrome
or not

Page:4
18 Inputting number of lines 48-49
and printing the following
structure:
A
AB
ABC
ABCD
19 Program to check whether 50-52
the number is a pronic
number or not
20 Program to check whether 53-55
the number is a Special
number or not
21 Program to find whether 56-58
the number is a spy
number or not
22 Conclusion 59
23 Bibliography 60
Page :5
Introduction
Java is an object-oriented language .It was
developed at Sun Microsystems in 1995.Java
evolved from programming language called
OAK.
Java is simple, case sensitive, object oriented
,platform
independent ,.secure ,robust ,multithreaded
and distributed language .Due to all these
features .Java is used worldwide for various
purposes .Its has many advantages and some
disadvantages.
We can work in java with the help of an IDE.
BlueJ is an IDE which is designed for
beginners. It was designed
By the BlueJ team at Deakin
University ,Melbourne, Australia ,and the
university of kent at Canterbury ,UK .It has a
built in editor ,debugger and viewer. it is a
GUI.
Page:6
Q.2) converting celsius in fahrenheit:
import java.util.*;
class celsiusToFarenheit
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter celsius
value=");
float celsius=sc.nextFloat();
float Fahrenheit=(celsius*9/5)+32;
System.out.println("the Fahrenheit
value is = "+ Fahrenheit);
}
}

Page:7
Output:

Enter celsius value=


55.0
the fahrenheit value=
131.0

Page:8
Q.3)checking if a number is prime
number or not:
import java.util.Scanner;
class check_for_prime_num
{
public static void main(String args[])
{
int n, count=0;
System.out.print("Enter any Number ");
Scanner r=new Scanner(System.in);
n=r.nextInt();
for(int i=1; i<=n; i++)
{
if(n%i==0)
{
count++;
}
}
if(count==2)
{
System.out.print("Prime Number");
}
else
{
System.out.print("Not Prime Number");
}
}
}
Page:9
Output:
Enter any number
6
Not Prime
Number
Enter any number
3
Prime Number

Page:10
Q.4) Swapping values of two variable
with using third variable
Import java.util.Scanner;
Class Swap
{
Public static void main(String[] args) {
Int a,b;
System.out.print(“Enter any two Numbers
“);
Scanner ref=new Scanner(System.in);
A=ref.nextInt();
B=ref.nextInt();
System.out.println(“Before Swapping
“+a+” “+b);
A=a+b;
B=a-b;
A=a-b;
System.out.println(“After Swapping
“+a+” “+b);
}
}
Page:11
Output:
Enter any two
numbers 56
64
Before swapping 56
64
After swapping 64
56
Page:12
Q.5)Checking if a year is leap year or
not:
import java.util.Scanner;
class LeapYear
{
public static void main(String[] args) {
System.out.print("Enter your year:");
Scanner sc=new Scanner(System.in);
int year=sc.nextInt();
if(year%400==0||year%4==0)
{
System.out.println("This year is leap
year.");
}
else
{
System.out.println("This year is not a
leap year.");
}
}
}
Page:13
Output:
Enter your year:2020
This year is leap year

Enter your year:2019


This year is not a leap year
Page:14
Q.6)Assigning grades to student based on
the percentage of marks scored(using if-else-
if statement):
import java.util.Scanner;

public class GradeAssignment {


public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);
System.out.print("Enter the percentage of
marks scored: ");
double percentage =
scanner.nextDouble();
char grade;
if (percentage >= 90) {
grade = 'A';
} else if (percentage >= 80) {
grade = 'B';
} else if (percentage >= 70) {
grade = 'C';
} else if (percentage >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade: " + grade);
}
}

Page:15
Output:
Enter the percentage of marks
scored:99
Grade A

Page : 16
Q.7)Performing an arithmetic operation on
two numbers based on the symbol of the
operator entered:
import java.util.Scanner;

public class ArithmeticOperations {


public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);

System.out.print("Enter the first


number: ");
double num1 = scanner.nextDouble();

System.out.print("Enter the operator (+,


-, *, /): ");
char operator =
scanner.next().charAt(0);

System.out.print("Enter the second


number: ");
double num2 = scanner.nextDouble();

double result = 0;

switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("Error:
Division by zero!");
return;
}
break;
default:
System.out.println("Error: Invalid
operator!");
return;
}

System.out.println("Result: " + result);


}
}
Page:18
Output:
Enter the first number:7
Enter the operator (+ , - , * , /):/
Enter the second number:14
Result:0.5

Page:19
Q.8)Calculating the factorial of a number
inputted by the user:
import java.util.Scanner;

public class FactorialRecursive {


public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
scanner.close();

long factorial =
calculateFactorialRecursive(num);
System.out.println("Factorial of " + num
+ " is " + factorial);
}

public static long


calculateFactorialRecursive(int n) {
if (n == 0) {
return 1;
} else {
return n *
calculateFactorialRecursive(n - 1);
}
}
}

Page:20
Output:
Enter a number:5
Factorial of 5 is 120

Page:21
Q.9)Displaying the Fibonacci series up to a
given number of terms:
import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);
System.out.print("Enter the number of
terms: ");
int numTerms = scanner.nextInt();
scanner.close();
int t1 = 0, t2 = 1;
System.out.print("Fibonacci Series: ");
for (int i = 1; i <= numTerms; ++i) {
System.out.print(t1 + " + ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
}
}
}

Page:22
Output:
Enter the number of terms:8
Fibonacci series:0+1+1+2+3+5+8+13+

Page:23
Q.10)Reversing the digits of a number:
public class ReverseNumber {
public static int reverse(int num) {
int reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
return reversed;
}

public static void main(String[] args) {


int original = 12345;
int reversed = reverse(original);
System.out.println("Original: " +
original);
System.out.println("Reversed: " +
reversed);
}
}

Page:24
Output:
Original:12345
Reversed:54321

Page:25
Q.11)Menu-driven program to calculate and
print the sum of the following series:
i)S=1/2+2/3+3/44/55/6+…n
import java.util.Scanner;

public class SeriesSum {


public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);

System.out.println("Enter the number of


terms (n): ");
int n = scanner.nextInt();

double sum = 0.0;

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


sum += (double) i / (i + 1);
}

System.out.println("The sum of the


series is: " + sum);
}

Page:26
Output:
Enter the number in terms(n):1
The sum of the series is:0.5

Page:27
ii)S=2/4+4/6+6/8+8/10+10/12+….n
public class SeriesSum {

public static double harmonicNumber(int


n) {
double sum = 0.0;
for (int i = 1; i <= n; i++) {
sum += 1.0 / i;
}
return sum;
}

public static double calculateSum(int n) {


int m = n / 2; // m is half of n
return m + 1 - harmonicNumber(m + 1);
}

public static void main(String[] args) {


int n = 10; // Example value for n
double result = calculateSum(n);
System.out.println("The sum S for n = "
+ n + " is: " + result);
}
}

Page:28
Output:
The sum S for n = 10 is:
3.5500000000000003

Page:29
Q.12)Calculate and print the series:
i)S=x/2+x/5+x/8+…..+x/20
public class SeriesSum {
public static void main(String[] args) {
double x = 10; // Example value for x
double sum = calculateSum(x);
System.out.println("The sum S is: " +
sum);
}

public static double calculateSum(double


x) {
double sum = 0;
int[] denominators = {2, 5, 8, 11, 14,
17, 20};

for (int denominator : denominators) {


sum += x / denominator;
}

return sum;
}
}

Page:30
Output:
The sum S is: 10.961611917494269

Page:31
Q.13)Display the following patterns:
i) 54321
4321
321
21
1
21
321
4321
54321

Page:32
public class NumberPattern {
public static void main(String[] args)
{
int n = 5; // You can change this
value to generate a larger or smaller
pattern

// First half of the pattern


(descending)
for (int i = n; i >= 1; i--) {
for (int j = i; j >= 1; j--) {
System.out.print(j);
}
System.out.println();
}

// Second half of the pattern


(ascending)
for (int i = 2; i <= n; i++) {
for (int j = i; j >= 1; j--) {
System.out.print(j);
}
System.out.println();
}
}
}
Page:33
Output:
54321
4321
321
21
1
21
321
4321
54321
Page:34
ii) *
**
***
****
*****
public class RightAlignedTriangle {
public static void main(String[] args)
{
int height =5;
for (int i = 1; i <= height; i++) {
for (int j = i; j < height; j++) {
System.out.print(" ");
}
// Print stars
for (int k = 1; k <= i; k++) {
System.out.print("*");
}
// Move to the next line
System.out.println();
}
}
}

Page:35
Output
*
**
***
****
*****

Page:36
Q.14)Displaying the Floyd’s triangle:
public class FloydsTriangle {
public static void main(String[] args)
{
int rows = 5; // Number of rows in
Floyd's Triangle
int number = 1; // Starting number

for (int i = 1; i <= rows; i++) { //


Loop for each row
for (int j = 1; j <= i; j++) { //
Loop for each column in the row
System.out.print(number + "
"); // Print the current number
number++; // Increment the
number
}
System.out.println(); // Move to
the next line after each row
}
}
}

Page:37
Output:
1
23
456
7 8 9 10

Page:38
Q.15)Program to check whether the is an
armstrong number or not:
import java.util.Scanner;

public class ArmstrongNumberChecker {

public static void main(String[] args)


{
Scanner scanner = new
Scanner(System.in);
System.out.print("Enter a number:
");
int number = scanner.nextInt();
if (isArmstrong(number)) {
System.out.println(number + " is
an Armstrong number.");
} else {
System.out.println(number + " is
not an Armstrong number.");
}
scanner.close();
}

public static boolean isArmstrong(int


number) {
int originalNumber = number;
int sum = 0;
int digits =
String.valueOf(number).length();
while (number > 0) {
int digit = number % 10;
sum += Math.pow(digit, digits);
number /= 10;
}
return sum == originalNumber;
}
}

Page:40
Output:

Enter a number: 156


156 is not an Armstrong number.
Enter a number: 153
153 is an Armstrong number.

Page:41
Q.16)Program to check whether the number
is a niven number or not:
import java.util.Scanner;

public class NivenNumberChecker {

public static void main(String[] args)


{
Scanner scanner = new
Scanner(System.in);
// Input from user
System.out.print("Enter a number:
");
int number = scanner.nextInt();
// Check if the number is a Niven
number
if (isNivenNumber(number)) {
System.out.println(number + " is
a Niven number.");
} else {
System.out.println(number + " is
not a Niven number.");
}
scanner.close();
}

// Method to check if a number is a


Niven number
public static boolean
isNivenNumber(int number) {
int sumOfDigits =
sumDigits(number);
return number % sumOfDigits ==
0;
}

// Method to calculate the sum of


digits
public static int sumDigits(int
number) {
int sum = 0;
while (number > 0) {
sum += number % 10; // Add the
last digit
number /= 10; // Remove the
last digit
}
return sum;
}
}

Page:43
Q.17)Program to check whether the number
is palindrome or not:
import java.util.Scanner;

public class PalindromeChecker {

public static void main(String[] args)


{
Scanner scanner = new
Scanner(System.in);
System.out.print("Enter a number:
");
int number = scanner.nextInt();
if (isPalindrome(number)) {
System.out.println(number + " is
a palindrome.");
} else {
System.out.println(number + " is
not a palindrome.");
}
scanner.close();
}

public static boolean isPalindrome(int


number) {
int originalNumber = number;
int reversedNumber = 0;
while (number > 0) {
int digit = number % 10; // Get
the last digit
reversedNumber =
reversedNumber * 10 + digit; // Build
the reversed number
number /= 10; // Remove the last
digit
}

return originalNumber ==
reversedNumber; // Check if original and
reversed are the same
}
}

Page:46
Q.18)Inputting number of lines and printing
the following structure: A
AB
ABC
ABCD
public class LetterPattern {
public static void main(String[] args)
{

int n = 4;
// Generate the pattern
for (int i = 1; i <= n; i++) {
for (char j = 'A'; j < 'A' + i; j++) {
System.out.print(j);
}
System.out.println(); // Move to
the next line
}
}
}

Page:48
Output:
A
AB
ABC
ABCD

Page:49
Q.19)Program to check whether the number
is a pronic number or not:
public class PronicNumberChecker {

/**
* Checks if a number is a pronic
number.
*
* @param num the number to check
* @return true if the number is
pronic, false otherwise
*/
public static boolean isPronic(int
num) {
int i = 0;
while (true) {
i++;
int pronic = i * (i + 1);
if (pronic == num) {
return true;
} else if (pronic > num) {
return false;
}
}
}

public static void main(String[] args)


{
int num = 20; // example number
if (isPronic(num)) {
System.out.println(num + " is a
pronic number.");
} else {
System.out.println(num + " is
not a pronic number.");
}
}
}

Page:51
Output:
20 is a pronic number.
Page:52
Q.20)Program to check whether the number
is a Special number or not
public class SpecialNumber {
public static boolean isSpecial(int
num) {
int sum = 0;
int temp = num;
while (temp != 0) {
int digit = temp % 10;
sum += factorial(digit);
temp /= 10;
}
return sum == num;
}

public static int factorial(int num) {


int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}

public static void main(String[] args)


{
/*Example
int num = 145;
if (isSpecial(num)) {
System.out.println(num + " is a
special number.");
} else {
System.out.println(num + " is
not a special number.");
}
}
}

Page:54
Q.21)Program to find whether the number is
a spy number or not:
import java.util.Scanner;

public class SpyNumberChecker {


public static void main(String[] args)
{
Scanner scanner = new
Scanner(System.in);
System.out.print("Enter a number:
");
int number = scanner.nextInt();
if (isSpyNumber(number)) {
System.out.println(number + " is
a spy number.");
} else {
System.out.println(number + " is
not a spy number.");
}
scanner.close();
}

public static boolean isSpyNumber(int


number) {
int sum = 0;
int product = 1;

// Process each digit of the number


while (number > 0) {
int digit = number % 10; // Get
the last digit
sum += digit; // Add to
sum
product *= digit; // Multiply
to product
number /= 10; // Remove
the last digit
}

// Check if sum is equal to product


return sum == product;
}
}

Page:57
Output:
Enter a number: 112
112 is not a spy number.
Enter a number: 123
123 is a spy number.

Page:58

Conclusion
Java is one of the most popular and in-demand
programming languages to learn and it was one of
the first languages to standardize high-level
threading utilities.

A Java program may include classes, objects,


variables, data types, operators, comments,
conditional constructs and loops. A Java program
can also import an in-built Java package to perform
some tasks. We sometimes need nested loops in
some Java programs. Sometimes there can be an
error in the program. Java tries to rectify it by
compile and Runtime error checking.

Java is one of the easiest programming languages


to learn. Many computer programming
departments are using Java as an introductory
language for beginners for producing more
programmers in our country.

Page:59

Bibliography
Websites
www.javapoint.com
www.geeksforgeeks.c
om
Books
Computer Applications
with Blue) for Class
9(ICSE)

ICSE Total Computer


Applications for Class
9
Page:60
Output:
Enter a number: 3
3 is a Niven number.

Enter a number:66
66 is not a niven number.

Page:44
Output:
Enter a number: 99
99 is a palindrome

Enter a number: 24
24 is not a palindrome.

Page:47

You might also like