0% found this document useful (0 votes)
11 views35 pages

Java Programs for Number Operations

The document outlines a series of programming tasks in Java, each with specific requirements and sample inputs/outputs. It includes programs for identifying numbers, performing calculations, and handling user input for various scenarios such as checking for palindromes, calculating fines, and determining attendance percentages. Each task is accompanied by code snippets and variable descriptions to aid understanding.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views35 pages

Java Programs for Number Operations

The document outlines a series of programming tasks in Java, each with specific requirements and sample inputs/outputs. It includes programs for identifying numbers, performing calculations, and handling user input for various scenarios such as checking for palindromes, calculating fines, and determining attendance percentages. Each task is accompanied by code snippets and variable descriptions to aid understanding.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

S.

No Program Page No

1 Identification of Two Digit Nest Number 2

2 Identification of Middle Number 4

3 Swapping Two Numbers 6

4 Identification of Buzz, Even, Odd, Positive, Negative Numbers 8

5 Division Using Ternary Operator 11

6 Identification of Divisibility by 5 and/or 10 13

7 Identification of Palindrome Number 14

8 Calculation of Fine for Library Books 17

9 Calculation of Student’s Attendance Percentage 19

10 Identification of Positive-Even or Negative-Odd Numbers 21

11 Conversion of Days to Years, Months, Days 23

12 Income Tax Calculation for an Indian Citizen Below 60 Years 25

13 Calculation of Courier Charges for Two Booking Types 28

14 Identification of Perfect Square Numbers 31

15 Identification of Leap, Century and Century Leap Years 34

1
Question # 1: Write a program to input a two-digit number and check if it is a
Nest number or not. A number is said to be a ‘Nest Number’ if the number contains at least
one digit which is zero.
Sample input: 20
Sample output: It is a Nest Number

Program # 1:

import [Link];
class abc
{
public static void main()
{
Scanner in = new Scanner([Link]);
[Link]("Enter a two-digit number: ");
int number = [Link]();
int tens = number / 10;
int units = number % 10;
if (tens == 0 || units == 0)
{
[Link](number + " is a Nest Number.");
}
else
{
[Link](number + " is not a Nest Number.");
}
}
}

Variable Description # 1:

2
Data Type Variable Name Description
int Used to store an integer value of the two-digit
number
number to be inputted.
int Used to store an integer value of the number in
tens
the ten’s digit
int Used to store an integer value of the number in
units
the unit’s digit

Output # 1:

Question # 2: Write a program that accepts three integers as parameters and find the middle
value of the three.

3
Sample input: 5, 1, 8
Sample output: 5

Program # 2:

import [Link];
class bcd
{
public static void main ()
{
Scanner in = new Scanner([Link]);
[Link]("Enter the first number: ");
int a = [Link]();
[Link]("Enter the second number: ");
int b = [Link]();
[Link]("Enter the third number: ");
int c = [Link]();
if(a<b && a<c)
{
if (b<c)
[Link](b+ " is the middle number.");
else
[Link](c+ " is the middle number.");
}
if (b < a && b< c)
{
if (a<c)
[Link](a+ " is the middle number.");
else
[Link](c+ " is the middle number.");
}
if (c < b && c < a)

4
{
if (a<b)
[Link](a+ " is the middle number.");
else
[Link](b+ " is the middle number.");
}
}
}

Variable Description # 2:

Data Type Variable Name Description


int Used to store an integer value of the first number
a
to be inputted.
int Used to store an integer value of the second
b
number to be inputted.
int Used to store an integer value of the third number
c
to be inputted.

Output # 2:

Question # 3: Write a program to input two numbers. Display the numbers after swapping
them by without using a third variable.

5
Sample Input x=40, y= 50 ;
Sample Output: x=50, y= 40 ;

Program # 3:

import [Link];
class cde
{
public static void main()
{
Scanner in = new Scanner([Link]);
[Link]("Enter the first number:");
int a = [Link]();
[Link]("Enter the second number:");
int b = [Link]();
a = a + b;
b = a-b;
a = a-b;
[Link]("After swapping:");
[Link]("First number:" +a+"\nSecond number:" +b);
}
}

Variable Description # 3:

6
Data Type Variable Name Description
int Used to store an integer value of the first number
a
to be inputted.
int Used to store an integer value of the second
b
number to be inputted.

Output # 3:

Question # 4: Write a menu driven program in Java to accept a number and perform the
following operations depending on the user's choice:
Entered number is a Buzz number or not.

7
Entered number is even or odd.
Entered number is positive or negative.
Note: A Buzz number is a number which is either divisible by 7 or has 7 in its unit's place.

Program # 4:

import [Link];
class def
{
public static void main()
{
Scanner in = new Scanner([Link]);
[Link]("Enter: \n 1 Check if the number is a Buzz number \n 2 Check
if the number is even or odd \n 3 Check if the number is positive or negative");
[Link]("Enter your choice: ");
int a = [Link]();
[Link]("Enter number: ");
int n = [Link]();
switch (a)
{
case 1:
if(n % 7 == 0 || n % 10 == 7)
[Link]( n+ " is a buzz number. ");
else
[Link]( n+ " is not a buzz number. ");
break;
case 2:
if(n%2==0)
[Link]( n+ " is an even number. ");
else
[Link]( n+ " is an odd number. ");
break;

8
case 3:
if(n>=0)
[Link]( n+ " is an positive number. ");
else
[Link]( n+ " is an negative number. ");
break;
default:
[Link]("Wrong Choice!!");
}
}
}

Variable Description # 4:

Data Type Variable Name Description


int Used to store an integer value of the choice
a
number to be inputted.
int Used to store an integer value of the number to be
n
inputted.

Output # 4:

9
Question # 5: Input two numbers a, b. Divide a by b, and store in c, provided b is not equal
to 0. If b =0, then store 1 in c. Use ternary operator.

10
Program # 5:

import [Link];
class efg
{
public static void main()
{
Scanner in = new Scanner([Link]);
[Link]("Enter the first number: ");
double a = [Link]();
[Link]("Enter the second number: ");
double b = [Link]();
double c;
c=(b!=0)? (a/b) : (1);
if(b!=0)
[Link]("The result of the division is: " +c);
else
[Link]("Since the second number is 0, the result of the division is
equated to 1.");
}
}

Variable Description # 5:

11
Data Type Variable Name Description
double Used to store a double value of the first number to
a
be inputted.
double Used to store a double value of the second
b
number to be inputted.
double Used to store a double value of the result of the
c
division.

Output # 5:

Question # 6: Write a program that prompts the user to enter an integer and determines
whether it is divisible by 5 and 10, it is divisible by 5 or 10, and whether it is divisible by 5
or 10, but not both. Display the result accordingly.

12
Program # 6:

import [Link];
class fgh
{
public static void main()
{
Scanner in = new Scanner([Link]);
[Link]("Enter an integer: ");
int n = [Link]();
if(n % 5 == 0 && n % 7 == 0)
[Link](n+ " is divisible by 5 and 7.");
else if(n % 5 == 0 || n % 7 == 0)
{
if(n % 5 == 0 && !(n % 7 == 0))
[Link](n+ " is divisible by 5 but not by 7.");
else if(!(n % 5 == 0) && n % 7 == 0)
[Link](n+ " is divisible by 7 but not by 5.");
}
else
[Link](n+ " is neither divisble by 5 nor by 7.");
}
}

Variable Description # 6:

13
Data Type Variable Name Description
int Used to store an integer value of the integer to be
n
inputted.

Output # 6:

14
Question # 7: Write a program that checks whether a given 2-digit number is a palindrome
number or not.
Sample input: 55
Sample output: The given number is palindrome.
Sample input: 46
Sample output: The given number is not palindrome.

Program # 7:

import [Link];
class ghi
{
public static void main()
{
Scanner in = new Scanner([Link]);
[Link]("Enter a two-digit number: ");
int n = [Link]();
if(n % 11 == 0)
[Link](n+ " is a palindrome number. ");
else
[Link](n+ " is not a palindrome number. ");
}
}

Variable Description # 7:

Data Type Variable Name Description


int Used to store an integer value of the two-digit
n
number to be inputted.
Output # 7:

15
Question # 8: In a library, a fine is charged on the number of days a book is returned late. By
taking the input of days late, print the late fine applicable.

16
Number of days late(d) Fine(₹)
Up to 5 10
Next 5 days 5 for each day
Next 5 days 7 for each day
Seize the caution money if a book is kept beyond 15 days.[No fine calculation required.]

Program # 8:

import [Link];
class hij
{
public static void main()
{
Scanner in = new Scanner([Link]);
[Link]("Enter the number of days the book was returned late: ");
int n = [Link]();
int x = 0;
if(n<=5)
x=10;
else if(n<=10)
x=10+(n-5)*5;
else if(n<=15)
x=10+25+(n-10)*7;
else
{
[Link]("The caution money is seized if a book is kept beyond 15
days. ");
}
[Link]("The late fine applicable is ₹" +x);
}
}

17
Variable Description # 8:

Data Type Variable Name Description


int Used to store an integer value of the number of
n
days the book was returned late to be inputted.
int Used to store an integer value of the late fine
x
applicable.

Output # 8:

Question # 9: A student will not be allowed to sit in exam if his/her attendance is less than
75%. Take following input from user:
• Number of classes held.

18
• Number of classes attended.
Print the percentage of class attended and if the student is allowed to sit in exam or not.

Program # 9:

import [Link];
class ijk
{
public static void main()
{
Scanner in = new Scanner([Link]);
[Link]("Enter the number of classes held: ");
int H = [Link]();
[Link]("Enter the number of classes attended: ");
int A = [Link]();
double p = 0;
p=100*A/H;
if(p>75)
[Link]("The student will be allowed to sit in exam ");
else
[Link]("The student will not be allowed to sit in exam ");
}
}

Variable Description # 9:

19
Data Type Variable Name Description
int Used to store an integer value of the number of
H
classes held to be inputted.
int Used to store an integer value of the number of
A
classes attended to be inputted.
double Used to store a double value of the attendance
p
percentage.

Output # 9:

Question # 10: Write a program to enter a number. If the number is positive even number
displays three succeeding even numbers. If the number is negative odd, display three
succeeding odd numbers, otherwise display number is neither positive even or negative odd.

20
Sample input: -21
Output: -23, -25, -2 7

Program # 10:

import [Link];
class jkl
{
public static void main()
{
Scanner in = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();

if (n > 0 && n % 2 == 0)
{
[Link]("The number is positive and even. The three succeeding
even numbers are: "+(n + 2)+", "+(n + 4)+", "+(n + 6)+".");
}

else if (n < 0 && n % 2 != 0)


{
[Link]("The number is negative and odd. The three succeeding
odd numbers are: "+(n - 2)+", "+(n - 4)+", "+(n - 6)+".");
}

else
[Link]("number is neither positive even or negative odd. ");
}
}

Variable Description # 10:

21
Data Type Variable Name Description
int Used to store an integer value of the number to be
n
inputted.

Output # 10:

Question # 11: Write program to accept the number of days and display it after converting
into number of years, number of months and number of days.

Program # 11:

22
import [Link];
class klm
{
public static void main()
{
Scanner in = new Scanner([Link]);
[Link]("Enter the number of days: ");
int n = [Link]();
int y; int z; int m; int d;
y = [Link](n/365);
[Link]("The number of years is equal to: " +y);
z = n - (y*365);
m = [Link](z/30);
[Link]("The number of months is equal to: " +m);
d = z - (m*30);
[Link]("The number of days is equal to: " +d);
}
}

Variable Description # 11:

23
Data Type Variable Name Description
int Used to store an integer value of the number of
n
days to be inputted.
int Used to store an integer value of the years it is
x
equal to.
int Used to store an integer value of the months is
y
equal to.
int Used to store an integer value of the days is equal
z
to.

Output # 11:

Question # 12: Given below is a hypothetical table showing rate of income tax for an India
citizen, who is below or up to 60 years.
Taxable income (TI) in Rs. Tax Amount

24
Up to Rs. 2,50,000 Nil
More than Rs. 2,50,000 and less than or (TI - 1,60,000) * 10%
equal to Rs. 5,00,000
More than Rs. 5,00,000 and less than or (TI - 5,00,000) * 20% + 34,000
equal to Rs. 10,00,000
More than Rs. 10,00,000 (TI - 10,00,000) * 30% + 94,000

Write a program to input the name, age, and taxable income of a person. If the age is more
than 60 years, then display the message "Wrong Category". If the age is less than or equal to
60 years then compute and display the income tax payable along with the name of taxpayer,
as per the table given above.

Program # 12:

import [Link];
class lmn
{
public static void main()
{
Scanner in = new Scanner([Link]);
[Link]("Enter you Name: ");
String name = [Link]();
[Link]("Enter you Age: ");
int age = [Link]();

if(age<60)
{
[Link]("Enter you Income: ");
int TI = [Link](); double tax;
if(TI<=250000)
{
tax=0;

25
[Link]("Mr/Mrs."+name+", Tax Bracket: Below 250000 ; Your
Income Tax is: ₹" +tax);
}
else if(TI > 250000 && TI <= 500000)
{
tax=((TI - 160000) * 0.1);
[Link]("Mr/Mrs."+name+", Tax Bracket: 250000-500000 ; Your
Income Tax is: ₹" +tax);
}
else if(TI > 500000 && TI <= 1000000)
{
tax=((TI - 500000) * 0.2) + 34000;
[Link]("Mr/Mrs."+name+", Tax Bracket: 500000-1000000 ;
Your Income Tax is: ₹" +tax);
}
else
{
tax=((TI - 1000000) * 0.3)+94000 ;
[Link]("Mr/Mrs."+name+", Tax Bracket: Above 1000000 ;
Your Income Tax is: ₹" +tax);
}
}
else
[Link]("Mr/Mrs."+name+",since your age is above 60, you fall in
the wrong Income Tax Category");
}
}

Variable Description # 12:

26
Data Type Variable Name Description
String Used to store a string value of the name to be
name
inputted.
int Used to store an integer value of the age number
age
to be inputted.
int Used to store an integer value of the taxable
TI
income to be inputted.

Output # 12:

Question # 13: A courier company charges differently for “ORDINARY” Booking and “EXPRESS”
Booking based on the weight of the parcel as per the tariff given up:

27
Weight Ordinary Booking Express booking
up to 100 gm Rs.80 Rs.100
101 gm to 500 gm Rs.150 Rs.200
501 gm to 1000 gm Rs.210 Rs.250
more than 1000 gm Rs.250 Rs.300

Write a program to input weight of a parcel and type of booking (‘O’ for ordinary and ’E ‘for
Express). Calculate and print the charges accordingly.

Program # 13:

import [Link];
class mno
{
public static void main()
{
Scanner in = new Scanner([Link]);
[Link]("Enter the weight of the parcel: ");
int w = [Link]();
[Link]("Enter the type of booking: \n 'O' for Ordinary type
and 'E' for Express Type.");
char b = [Link]().charAt(0);
if(w <= 100)
{
if( b == 'O')
[Link]("Charges for the Ordinary Type of Booking is Rs.80.");
else
[Link]("Charges for the Express Type of Booking is Rs.100.");
}
else if(w >= 101 && w <=500)
{
if(b == 'O')

28
[Link]("Charges for the Ordinary Type of Booking is Rs.150.");
else
[Link]("Charges for the Express Type of Booking is Rs.200.");
}
else if(w >=501 && w <=1000)
{
if(b == 'O')
[Link]("Charges for the Ordinary Type of Booking is Rs.210.");
else
[Link]("Charges for the Express Type of Booking is Rs.250.");
}
else
{
if(b == 'O')
[Link]("Charges for the Ordinary Type of Booking is Rs.250.");
else
[Link]("Charges for the Ordinary Type of Booking is Rs.300.");
}
}
}

Variable Description # 13:

Data Type Variable Name Description


int Used to store an integer value of the weight to be
w
inputted.
char Used to store a character value of the type of
b
booking to be inputted.

Output # 13:

29
Question # 14: Write a program to input two unequal positive numbers and check whether
They are perfect square numbers or not. If the user enters a negative number, then the
program displays the message 'Square root of a negative number can't be determined'.
Sample Input: 81, 100

30
Sample Output: They are perfect square numbers.
Sample Input: 225, 99
Sample Output: 225 is a perfect square number.
99 is not a perfect square number.

Program # 14:

import [Link];
class nop
{
public static void main()
{
Scanner in = new Scanner([Link]);
[Link]("Enter a positive number: ");
int n1 = [Link]();
[Link]("Enter another positive number: ");
int n2 = [Link](); int a; int b;
if(n1<0 ||n2<0)
{
[Link]("Square root of a negative number can't be determined. ");
}
else if(n1 != n2)
{
a = (int)[Link](n1);
if(a*a == n1)
{
[Link](n1+ " is a Perfect Square number. ");
}
else
{
[Link](n1+ " is not a Perfect Square number. ");
}

31
b = (int)[Link](n2);
if(b*b == n2)
{
[Link](n2+ " is a Perfect Square number. ");
}
else
{
[Link](n2+ " is not a Perfect Square number. ");
}
}
else
{
[Link]("Number should be unequal ");
}
}
}

Variable Description # 14:

Data Type Variable Name Description


int Used to store an integer value of the positive
N1
number to be inputted.
int Used to store an integer value of another positive
N2
number to be inputted.
int Used to store an integer value of the square root
a
of the first positive.
int Used to store an integer value of the square root
b
of the second positive.

Output # 14:

32
Question # 15: Write a program to input year and check whether it is:
(a) a Leap year

33
(b) a Century Leap year
(c) a Century year but not Leap year Century years are NOT leap years UNLESS they
can be evenly divided by 400.
(For example, 1700, 1800, and 1900 were not leap years, but 1600 and 2000, which are
divisible by 400, were.) Sample Input: 2000 Sample Output: It is a Century Leap Year

Program # 15:

import [Link];
class opq
{
public static void main()
{
Scanner in = new Scanner([Link]);
[Link]("Enter a year: ");
int year = [Link]();
if(year % 100 == 0 && year % 400 == 0)
[Link]("The year is a century leap year.");
else if(year % 100 == 0 && year % 400 != 0 && year % 4 == 0)
[Link]("The year is a century year but also a leap year.");
else if(year % 4 == 0)
[Link]("This year is a leap year.");
else
[Link]("This is not a leap year, a century year, and a century leap
year.");
}
}

Variable Description # 15:

34
Data Type Variable Name Description
int Used to store an integer value of the year to be
year
inputted.

Output # 15:

35

Common questions

Powered by AI

The ternary operator allows for concise conditional evaluations in programming. For division with a default in the case of potential division by zero, it checks if the denominator is not zero before performing the division; if zero, it defaults the result to one. This prevents runtime errors without explicit branch statements .

Swapping two numbers without using a third variable can be achieved using arithmetic operations. First, set the first number as the sum of both numbers. Then subtract the second number from this sum to adjust the second number to the original first number. Finally, subtract the adjusted second number from the sum to revert the first number to the original second number .

The fine calculation involves inputting the number of days a book is returned late and using a predetermined fee rate per late day, calculating the total fine accordingly. This total depends on the number of days overdue multiplied by the daily late fee rate, though specific rates were not outlined .

To determine the middle value, you can compare the integers using a series of if-else statements. First, check which number is less than the other two and compare the remaining two numbers to determine the middle one. For example, if a < b and a < c, then compare b with c to choose the middle value, repeating similarly for other conditions .

Check if each number is a perfect square by verifying if its square root squared equals the number itself. If any input is negative, an error message indicates that negative numbers cannot form perfect squares, which addresses input constraints .

To evaluate a number's divisibility, first check if it is divisible by both 5 and 10, meaning it divides evenly by 10. If not, check for divisibility by either 5 or 10 exclusively using logical conditions, by confirming divisibility by one but not the other .

For citizens under 60, tax is calculated based on income brackets: no tax for income up to ₹250,000; 10% on the income exceeding ₹250,000 up to ₹500,000; 20% on the excess income over ₹500,000 up to ₹1,000,000, and 30% on the income exceeding ₹1,000,000. This calculation reflects progressively increased rates per bracket .

A two-digit number is a palindrome if reversing its digits results in the same number. This can be checked by ensuring it is divisible by 11, as this property indicates identical digits in two-digit numbers .

A number is considered a 'Buzz Number' if it is divisible by 7 or if it has 7 in its unit place. The logic checks whether the number mod 7 equals zero or mod 10 equals seven, covering both conditions for classifying a number as a Buzz Number .

A two-digit number is identified as a 'Nest Number' if it contains at least one zero digit. The algorithm involves dividing the number by 10 to obtain the tens digit and using modulo 10 to obtain the units digit. If either digit is zero, then it qualifies as a Nest Number .

You might also like