0% found this document useful (0 votes)
29 views23 pages

Computer Project

The document is a computer project by Shlok Gupta, a 10th-grade student, focusing on Java programming. It includes a gratitude section, an index of 20 programming topics, and detailed implementations of various Java programs such as accepting user input, checking for odd/even numbers, calculating factorials, and generating Fibonacci series. Each program is accompanied by variable descriptions, sample outputs, and error handling.
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)
29 views23 pages

Computer Project

The document is a computer project by Shlok Gupta, a 10th-grade student, focusing on Java programming. It includes a gratitude section, an index of 20 programming topics, and detailed implementations of various Java programs such as accepting user input, checking for odd/even numbers, calculating factorials, and generating Fibonacci series. Each program is accompanied by variable descriptions, sample outputs, and error handling.
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

INTRODUCTION

Name - Shlok Gupta


Class - 10th B
Roll no.- 22
Subject – Computer
Applications
Subject Teacher - Divya
Ma’am
Internal examiner sign –
External examiner sign -

Acknowledgmen
t
I would like to express my sincere gratitude
to all those who helped me complete this
computer project successfully.

First and foremost, I am thankful to [Ma’am


Divya], my Computer Applications teacher,
for their constant support, guidance, and
encouragement throughout the completion of
this project.

I would also like to thank my school, [St


thomas School ], for providing me with the
opportunity and resources to carry out this
project work.
Finally, I am grateful to my parents and
friends for their continuous support,
motivation, and help in every possible way.

This project has enhanced my understanding


of Java programming, and I am truly thankful
for the experience.
Index
Page
Program No. Topic
(Approx.)

1 Accept Name and Age 1

2 Determine if Integer is Odd, Even, or Prime 2

3 Days in a Month with Leap Year Check 3

4 Check if 3-digit Number is Armstrong 4

5 Print Multiplication Table (1 to 10) 5

6 Find Sum of Digits of an Integer 6

7 Print Reverse of a Number 7

8 Count Even and Odd Digits in a Number 8

9 Calculate Factorial using a Loop 9

10 Check if a Number is Prime 10

11 Check if a Number is a Palindrome 11

12 Generate Fibonacci Series for n Terms 12

13 Calculate Power of a Number using a Loop 13

14 Check if a Number is a Perfect Number 14


Input 3x3 Matrix and Find Sum of Each
15 15
Row/Column

16 Count Number of Words in a Sentence 16

17 Check if a Given String is Palindrome 17

Use Constructor to Initialize/Display Student


18 18
Info

19 Calculate Square Root using [Link]() 19

20 Create User-Defined Method for Factorial 20

Topic- Write 20 -25 executed program on all the topics of syllabus.


Each program must contain problem,program, VDT and output.

1 : Write a program to accept the name and age of the user. Display a greeting with their name
and validate that age is a positive number.
Program 1 :

import [Link];

public class GreetingApp {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
greetUser(sc);
}

public static void greetUser(Scanner sc) {


[Link]("Enter your name: ");
String name = [Link]().trim();

if ([Link]()) {
[Link]("Invalid input! Name cannot be empty.");
return;
}

[Link]("Enter your age: ");


try {
int age = [Link]();
if (age <= 0 || age > 120) {
[Link]("Invalid age entered.");
return;
}
[Link]("Welcome, " + name + "! You are " + age + "
years old.");
} catch (Exception e) {
[Link]("Invalid input for age. Please enter a
number.");
}
}
}

Variab Data Description


le Type
sc Object for user
Scanner input
name Stores the name of
String the user
age int Stores the age of
the user

Sample Run 1:

Enter your name: Arjun


Enter your age: 15
Welcome, Arjun! You are 15 years old.
Sample Run 2:

Enter your name:


Invalid input! Name cannot be empty.

PROGRAM 2:

Write a program in Java to accept an integer from the user and determine if it is odd, even, or
prime. Handle invalid input and use efficient logic.

import [Link];

public class NumberClassifier {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter an integer: ");
if ([Link]()) {
int num = [Link]();
if (num == 0) {
[Link]("Zero is neither even nor odd.");
} else {
[Link](num + " is " + (num % 2 == 0 ? "Even" :
"Odd"));
if (isPrime(num)) {
[Link](num + " is also a Prime number.");
}
}
} else {
[Link]("Invalid input. Please enter an integer.");
}
}

public static boolean isPrime(int n) {


if (n <= 1) return false;
for (int i = 2; i <= [Link](n); i++) {
if (n % i == 0) return false;
}
return true;
}
}

Variabl Data Description


e. Type
sc Used to take input from the
Scanner user
num int Stores the number entered by
the user
i int Loop variable used to check for
primality
n int Argument to the isPrime()
method

🔹 Output:

Sample Run 1:

Enter an integer: 17
17 is Odd
17 is also a Prime number.

Sample Run 2:

Enter an integer: 10
10 is Even

Sample Run 3:

Enter an integer: 0
Zero is neither even nor odd.

Program 3:
Write a program to input a month number and a year. Display the number of days in that month
considering leap year

Program:

import [Link];
public class MonthDays {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter month number (1-12): ");
int month = [Link]();
[Link]("Enter year: ");
int year = [Link]();
int days = 0;

switch(month) {
case 1: case 3: case 5: case 7:
case 8: case 10: case 12:
days = 31; break;
case 4: case 6: case 9: case 11:
days = 30; break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
days = 29;
else
days = 28;
break;
default:
[Link]("Invalid month!");
return;
}
[Link]("This month has " + days + " days.");
}
}

VDT:
Variab Data Description
le Type
sc Scanner Used to take user input
month int Stores the month number
(1–12)
year int Stores the year entered by
user
days int Stores the number of days in
month

Sample Output:
Enter month number (1-12): 2
Enter year: 2024
This month has 29 days.

Program 4:
Write a program to accept a 3-digit number and check if it is an Armstrong number.

Java Program:
import [Link];

public class ArmstrongNumber {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a 3-digit number: ");
int num = [Link]();
int original = num, sum = 0;

while (num > 0) {


int digit = num % 10;
sum += digit * digit * digit;
num /= 10;
}
if (sum == original)
[Link](original + " is an Armstrong number.");
else
[Link](original + " is not an Armstrong number.");
}
}

VDT:
Variabl Data Description
e Type
sc Scanner Used to read input from
user
num int Stores the 3-digit number
original int Holds a copy of the original
number
sum int Accumulates sum of cubes of
digits
digit int Holds the current digit being
processed

Sample Output:
Enter a 3-digit number: 153
153 is an Armstrong number.

Program 5:
Write a Java program to accept a number and print its multiplication table from 1 to 10.

Java Program:
import [Link];

public class MultiplicationTable {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();

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


[Link](num + " x " + i + " = " + (num * i));
}
}
}
VDT:
Variab Data Description
le Type
sc Scanner Used to take user input
num int The number for which table is
printed
i int Loop counter from 1
to 10

Sample Output:
Enter a number: 5
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50

Program 6:
Write a Java program to find the sum of digits of an integer input by the user.

import [Link];

public class DigitSum {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int sum = 0, original = num;

while (num > 0) {


sum += num % 10;
num /= 10;
}

[Link]("Sum of digits of " + original + " is: " + sum);


}
}

VDT:
Variabl Data Description
e Type
sc Scanner Used to take user input
num int Stores the input
number
sum int Stores the sum of digits
original int Copy of the original number
entered

Sample Output:
Enter a number: 456
Sum of digits of 456 is: 15

Program 7:
Write a Java program to accept a number and print its reverse.

import [Link];

public class ReverseNumber {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int reversed = 0, original = num;

while (num > 0) {


reversed = reversed * 10 + (num % 10);
num /= 10;
}

[Link]("Reverse of " + original + " is: " + reversed);


}
}

VDT: Variable description


Sample Output:
Enter a number: 1234
Reverse of 1234 is: 4321

Program 8:
Write a Java program to count the number of even and odd digits in a given number.

import [Link];

public class EvenOddDigitCount {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int even = 0, odd = 0, digit;

int temp = num;


while (temp > 0) {
digit = temp % 10;
if (digit % 2 == 0)
even++;
else
odd++;
temp /= 10;
}

[Link]("Even digits: " + even);


[Link]("Odd digits: " + odd);
}
}

VDT:
Variab Data Description
le Type
sc Scanner Used to read
input
num int Input number
even int Counter for even
digits
odd int Counter for odd
digits
digit int Stores current digit
temp int Copy of the input
number

Sample Output:
Enter a number: 12345
Even digits: 2
Odd digits: 3

Program 9:
Write a program to calculate the factorial of a number using a loop.

import [Link];

public class Factorial {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
long fact = 1;

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


fact *= i;
}

[Link]("Factorial of " + num + " is " + fact);


}
}

VDT:
Variab Data Description
le Type
sc Scanner Used to take input
from user
num int Input number
fact long Stores factorial
result
i int Loop counter from 1 to
num

Sample Output:
Enter a number: 5
Factorial of 5 is 120

Program 10:
Write a Java program to check whether a number is prime or not.

import [Link];

public class PrimeCheck {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");

int num = [Link]();


boolean isPrime = true;

if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}

if (isPrime)
[Link](num + " is a prime number.");
else
[Link](num + " is not a prime number.");
}
}

VDT:
Variab Data Description
le Type
sc Scanner Used to take input
num int Input number
isPrime boolean Flag to track if number
is prime
i int Loop counter

Sample Output:
Enter a number: 17
17 is a prime number.

Program 11:
Write a Java program to check whether a number is a palindrome.

import [Link];

public class PalindromeCheck {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int reversed = 0, original = num;

while (num > 0) {


reversed = reversed * 10 + (num % 10);
num /= 10;
}

if (reversed == original)
[Link](original + " is a palindrome.");
else
[Link](original + " is not a palindrome.");
}
}
VDT:
Variabl Data Description
e Type
sc Scanner Used to take user input
num int Input number
reversed int Stores the reversed
number
original int Stores copy of original
number

Sample Output:
Enter a number: 121
121 is a palindrome.

Program 12:
Write a Java program to generate the Fibonacci series for n terms.

import [Link];

public class FibonacciSeries {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of terms: ");
int n = [Link]();
int a = 0, b = 1, c;

[Link]("Fibonacci series: " + a + " " + b + " ");


for (int i = 3; i <= n; i++) {
c = a + b;
[Link](c + " ");
a = b;
b = c;
}
}
}

VDT:
Variab Data Description
le Type
sc Scanner Used to take user input
n int Number of terms in the
series
a int First term of Fibonacci
b int Second term of Fibonacci
c int Next term in series
i int Loop counter

Sample Output:
Enter number of terms: 7
Fibonacci series: 0 1 1 2 3 5 8

Program 13:
Write a Java program to calculate the power of a number using a loop.

import [Link];

public class PowerCalculation {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter base: ");
int base = [Link]();
[Link]("Enter exponent: ");
int exp = [Link]();
long result = 1;

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


result *= base;
}

[Link](base + "^" + exp + " = " + result);


}
}

VDT:
Variab Data Description
le Type
sc Scanner Used for input
base int Base value
exp int Exponent
result long Stores result of base raised
to exp
i int Loop counter

Sample Output:
Enter base: 2
Enter exponent: 5
2^5 = 32
Program 14:
Write a Java program to check whether a number is a perfect number.
A perfect number is a number whose sum of proper divisors equals the number itself.

import [Link];

public class PerfectNumber {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int sum = 0;

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


if (num % i == 0)
sum += i;
}

if (sum == num)
[Link](num + " is a perfect number.");
else
[Link](num + " is not a perfect number.");
}
}

VDT:
Variab Data Description
le type
sc Scanner Used for user
input
num int Number to be
checked
sum int Sum of divisors
i int Loop counter

Sample Output:
Enter a number: 28
28 is a perfect number.

Program 15:
Input a 3x3 matrix and find sum of each row and column.

import [Link];
public class MatrixSum {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int[][] mat = new int[3][3];
[Link]("Enter 3x3 matrix:");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
mat[i][j] = [Link]();
for (int i = 0; i < 3; i++) {
int rowSum = 0;
for (int j = 0; j < 3; j++)
rowSum += mat[i][j];
[Link]("Sum of row " + i + ": " + rowSum);
}
for (int j = 0; j < 3; j++) {
int colSum = 0;
for (int i = 0; i < 3; i++)
colSum += mat[i][j];
[Link]("Sum of column " + j + ": " + colSum);
}
}
}

Variable Description Table:

Variab Data Description


le Type
mat int[][] 2D array to store
matrix
rowSum int Sum of each row
colSum int Sum of each
column
i, j int Loop counters

Sample Output:

Enter 3x3 matrix:


1 2 3
4 5 6
7 8 9
Sum of row 0: 6
Sum of row 1: 15
Sum of row 2: 24
Sum of column 0: 12
Sum of column 1: 15
Sum of column 2: 18

Program 16:
Count the number of words in a sentence.
import [Link];
public class WordCounter {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence: ");
String sentence = [Link]().trim();
if ([Link]()) {
[Link]("Words: 0");
} else {
String[] words = [Link]("\\s+");
[Link]("Words: " + [Link]);
}
}
}

Variable Description Table:

Variabl Data Description


e Type
sentence String Input sentence
words String[] Array of words split from
sentence

Sample Output:

Enter a sentence: Hello and welcome to Java.


Words: 5

Program 17:

Check weather a given string is palindrome.

Code:

import [Link];
public class PalindromeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a word: ");
String str = [Link]();
String rev = "";
for (int i = [Link]() - 1; i >= 0; i--) {
rev += [Link](i);
}
if ([Link](rev)) {
[Link]("Palindrome");
} else {
[Link]("Not a Palindrome");
}
}
}
Variable Description Table:

Variab Data Description


le Type
str String Input string
rev String Reversed
string
i int Loop counter

Sample Output:

Enter a word: madam


Palindrome

Program 18:

Use a constructor to initialize and display student information.

class Student {
String name;
int age;

Student(String n, int a) {
name = n;
age = a;
}

void display() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}

public static void main(String[] args) {


Student s = new Student("Alice", 15);
[Link]();
}
}

Variable Description Table:

Variab Data Description


le Type
name String Student name
age int Student age
s Student Object of Student
class
Sample Output:

Name: Alice
Age: 15

Program 19:

Calculate the square root of a given number using the [Link]() method.

import [Link];

public class SquareRoot {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
double num = [Link]();

// Using library method [Link]()


double sqrtValue = [Link](num);

[Link]("Square root of " + num + " is: " + sqrtValue);


}
}

Variable Description Table:

Data
Variable Description
Type

Number input by the


num double
user

Square root of the


sqrtValue double
number

Sample Output:

Enter a number: 49
Square root of 49.0 is: 7.0
Program 20:

Create a user-defined method factorial that takes an integer and returns its factorial.

import [Link];

public class FactorialMethod {

// User-defined method to calculate factorial


static int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
[Link]("Enter a positive integer: ");
int num = [Link]();

int result = factorial(num); // Calling the method


[Link]("Factorial of " + num + " is: " + result);
}
}

Variable Description Table:

Variab Data Description


le Type
n int Input number for
factorial
fact int Stores factorial
calculation
num int Number input by user
result int Result returned by
method

Sample Output:

Enter a positive integer: 5


Factorial of 5 is: 120

You might also like