Comp-Project-Java
Comp-Project-Java
No Program Page No
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 java.util.Scanner;
class abc
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a two-digit number: ");
int number = in.nextInt();
int tens = number / 10;
int units = number % 10;
if (tens == 0 || units == 0)
{
System.out.println(number + " is a Nest Number.");
}
else
{
System.out.println(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 java.util.Scanner;
class bcd
{
public static void main ()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the first number: ");
int a = in.nextInt();
System.out.println("Enter the second number: ");
int b = in.nextInt();
System.out.println("Enter the third number: ");
int c = in.nextInt();
if(a<b && a<c)
{
if (b<c)
System.out.println(b+ " is the middle number.");
else
System.out.println(c+ " is the middle number.");
}
if (b < a && b< c)
{
if (a<c)
System.out.println(a+ " is the middle number.");
else
System.out.println(c+ " is the middle number.");
}
if (c < b && c < a)
4
{
if (a<b)
System.out.println(a+ " is the middle number.");
else
System.out.println(b+ " is the middle number.");
}
}
}
Variable Description # 2:
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 java.util.Scanner;
class cde
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the first number:");
int a = in.nextInt();
System.out.println("Enter the second number:");
int b = in.nextInt();
a = a + b;
b = a-b;
a = a-b;
System.out.println("After swapping:");
System.out.println("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 java.util.Scanner;
class def
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("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");
System.out.print("Enter your choice: ");
int a = in.nextInt();
System.out.print("Enter number: ");
int n = in.nextInt();
switch (a)
{
case 1:
if(n % 7 == 0 || n % 10 == 7)
System.out.println( n+ " is a buzz number. ");
else
System.out.println( n+ " is not a buzz number. ");
break;
case 2:
if(n%2==0)
System.out.println( n+ " is an even number. ");
else
System.out.println( n+ " is an odd number. ");
break;
8
case 3:
if(n>=0)
System.out.println( n+ " is an positive number. ");
else
System.out.println( n+ " is an negative number. ");
break;
default:
System.out.println("Wrong Choice!!");
}
}
}
Variable Description # 4:
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 java.util.Scanner;
class efg
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the first number: ");
double a = in.nextDouble();
System.out.println("Enter the second number: ");
double b = in.nextDouble();
double c;
c=(b!=0)? (a/b) : (1);
if(b!=0)
System.out.println("The result of the division is: " +c);
else
System.out.println("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 java.util.Scanner;
class fgh
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter an integer: ");
int n = in.nextInt();
if(n % 5 == 0 && n % 7 == 0)
System.out.println(n+ " is divisible by 5 and 7.");
else if(n % 5 == 0 || n % 7 == 0)
{
if(n % 5 == 0 && !(n % 7 == 0))
System.out.println(n+ " is divisible by 5 but not by 7.");
else if(!(n % 5 == 0) && n % 7 == 0)
System.out.println(n+ " is divisible by 7 but not by 5.");
}
else
System.out.println(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 java.util.Scanner;
class ghi
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a two-digit number: ");
int n = in.nextInt();
if(n % 11 == 0)
System.out.println(n+ " is a palindrome number. ");
else
System.out.println(n+ " is not a palindrome number. ");
}
}
Variable Description # 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 java.util.Scanner;
class hij
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of days the book was returned late: ");
int n = in.nextInt();
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
{
System.out.println("The caution money is seized if a book is kept beyond 15
days. ");
}
System.out.println("The late fine applicable is ₹" +x);
}
}
17
Variable Description # 8:
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 java.util.Scanner;
class ijk
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of classes held: ");
int H = in.nextInt();
System.out.println("Enter the number of classes attended: ");
int A = in.nextInt();
double p = 0;
p=100*A/H;
if(p>75)
System.out.println("The student will be allowed to sit in exam ");
else
System.out.println("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 java.util.Scanner;
class jkl
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a number: ");
int n = in.nextInt();
if (n > 0 && n % 2 == 0)
{
System.out.println("The number is positive and even. The three succeeding
even numbers are: "+(n + 2)+", "+(n + 4)+", "+(n + 6)+".");
}
else
System.out.println("number is neither positive even or negative odd. ");
}
}
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 java.util.Scanner;
class klm
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of days: ");
int n = in.nextInt();
int y; int z; int m; int d;
y = Math.round(n/365);
System.out.println("The number of years is equal to: " +y);
z = n - (y*365);
m = Math.round(z/30);
System.out.println("The number of months is equal to: " +m);
d = z - (m*30);
System.out.println("The number of days is equal to: " +d);
}
}
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 java.util.Scanner;
class lmn
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter you Name: ");
String name = in.nextLine();
System.out.println("Enter you Age: ");
int age = in.nextInt();
if(age<60)
{
System.out.println("Enter you Income: ");
int TI = in.nextInt(); double tax;
if(TI<=250000)
{
tax=0;
25
System.out.println("Mr/Mrs."+name+", Tax Bracket: Below 250000 ; Your
Income Tax is: ₹" +tax);
}
else if(TI > 250000 && TI <= 500000)
{
tax=((TI - 160000) * 0.1);
System.out.println("Mr/Mrs."+name+", Tax Bracket: 250000-500000 ; Your
Income Tax is: ₹" +tax);
}
else if(TI > 500000 && TI <= 1000000)
{
tax=((TI - 500000) * 0.2) + 34000;
System.out.println("Mr/Mrs."+name+", Tax Bracket: 500000-1000000 ;
Your Income Tax is: ₹" +tax);
}
else
{
tax=((TI - 1000000) * 0.3)+94000 ;
System.out.println("Mr/Mrs."+name+", Tax Bracket: Above 1000000 ;
Your Income Tax is: ₹" +tax);
}
}
else
System.out.println("Mr/Mrs."+name+",since your age is above 60, you fall in
the wrong Income Tax Category");
}
}
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 java.util.Scanner;
class mno
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the weight of the parcel: ");
int w = in.nextInt();
System.out.println("Enter the type of booking: \n 'O' for Ordinary type
and 'E' for Express Type.");
char b = in.next().charAt(0);
if(w <= 100)
{
if( b == 'O')
System.out.println("Charges for the Ordinary Type of Booking is Rs.80.");
else
System.out.println("Charges for the Express Type of Booking is Rs.100.");
}
else if(w >= 101 && w <=500)
{
if(b == 'O')
28
System.out.println("Charges for the Ordinary Type of Booking is Rs.150.");
else
System.out.println("Charges for the Express Type of Booking is Rs.200.");
}
else if(w >=501 && w <=1000)
{
if(b == 'O')
System.out.println("Charges for the Ordinary Type of Booking is Rs.210.");
else
System.out.println("Charges for the Express Type of Booking is Rs.250.");
}
else
{
if(b == 'O')
System.out.println("Charges for the Ordinary Type of Booking is Rs.250.");
else
System.out.println("Charges for the Ordinary Type of Booking is Rs.300.");
}
}
}
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 java.util.Scanner;
class nop
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a positive number: ");
int n1 = in.nextInt();
System.out.println("Enter another positive number: ");
int n2 = in.nextInt(); int a; int b;
if(n1<0 ||n2<0)
{
System.out.println("Square root of a negative number can't be determined. ");
}
else if(n1 != n2)
{
a = (int)Math.sqrt(n1);
if(a*a == n1)
{
System.out.println(n1+ " is a Perfect Square number. ");
}
else
{
System.out.println(n1+ " is not a Perfect Square number. ");
}
31
b = (int)Math.sqrt(n2);
if(b*b == n2)
{
System.out.println(n2+ " is a Perfect Square number. ");
}
else
{
System.out.println(n2+ " is not a Perfect Square number. ");
}
}
else
{
System.out.println("Number should be unequal ");
}
}
}
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 java.util.Scanner;
class opq
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a year: ");
int year = in.nextInt();
if(year % 100 == 0 && year % 400 == 0)
System.out.println("The year is a century leap year.");
else if(year % 100 == 0 && year % 400 != 0 && year % 4 == 0)
System.out.println("The year is a century year but also a leap year.");
else if(year % 4 == 0)
System.out.println("This year is a leap year.");
else
System.out.println("This is not a leap year, a century year, and a century leap
year.");
}
}
34
Data Type Variable Name Description
int Used to store an integer value of the year to be
year
inputted.
Output # 15:
35