0% found this document useful (0 votes)
8 views

computer prj boards

The document is a computer project by Aditi M. Shendge, covering various programming concepts including control structures (if-else, switch-case), loops (for, while, do-while), and functions in Java. It contains multiple code examples demonstrating these concepts, such as calculating squares and cubes, determining grades based on marks, and checking divisibility. Additionally, it includes programs for calculating profit/loss, identifying special two-digit numbers, and generating Fibonacci series.

Uploaded by

aditimshendge01
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)
8 views

computer prj boards

The document is a computer project by Aditi M. Shendge, covering various programming concepts including control structures (if-else, switch-case), loops (for, while, do-while), and functions in Java. It contains multiple code examples demonstrating these concepts, such as calculating squares and cubes, determining grades based on marks, and checking divisibility. Additionally, it includes programs for calculating profit/loss, identifying special two-digit numbers, and generating Fibonacci series.

Uploaded by

aditimshendge01
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/ 131

COMPUTER

PROJECT

Name: Aditi.M.Shendge
Class:”X”
Section:”C”
INDEX
1) IF ELSE IF
2) SWITCH CASE
3) FOR LOOP
4) WHILE LOOP
5) DO-WHILE LOOP
6) NESTED LOOP
7) FUNCTION
8) OVERLOADED FUNCTIONS

IF ELSE IF:
1) A program to display square and cube of numbers
import java.util.*;
public class program
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int a,b,sq,cb;
System.out.println("Enter two numbers");
a=in.nextInt();
b=in.nextInt();
if(a!=b)
{
if(a<b)
{
sq=a*a;
cb=b*b*b;
}
else
{
sq=b*b;
cb=a*a*a;
}
System.out.println("The square of smaller number:"+sq);
System.out.println("The cube of greater number:"+cb);
}
else
System.out.println("Both the numbers are equal");
}
}

OUTPUT:
Enter two numbers
67
87
The square of smaller number:4489
The cube of greater number:658503

2)Marks Grade
0-34 Fail
35-59 C grade
60-79 B grade
80-94 A grade
95+ A+ grade
import java.util.Scanner;

public class marks


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

System.out.print("Enter marks: ");


int marks = in.nextInt();

if (marks < 35)


System.out.println("Fail");
else if (marks < 60)
System.out.println("C grade");
else if (marks < 80)
System.out.println("B grade");
else if (marks < 95)
System.out.println("A grade");
else
System.out.println("A+ grade");

System.out.println(" ");

}
}
OUTPUT:
Enter marks:
34
Fail
3) A program to find the greatest of 3 numbers

import java.util.Scanner;

public class seokjin


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
System.out.print("Enter third number: ");
int c = in.nextInt();

if (a == b && b == c) {
System.out.println("All the numbers are equal");
}
else {
int g = a;

if (b > g)
g = b;

if (c > g)
g = c;
System.out.println("Greatest number: " + g);
}
}
}
OUTPUT:
Enter first number: 89
Enter second number: 65
Enter third number: 72
Greatest number: 89
1.Find if the number is positive, negative, or zero

import java.util.*;
public class marks
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int num=sc.nextInt();
if(num>0)
System.out.println("Positive number");
else if(num<0)
System.out.println("Negative number");
else
System.out.println("zero");
}
}
OUTPUT:
Enter a number
34
Positive number

4)Accept a numbers and find out the given number is


divisible by 5 or not, if not then print the nearest
number from the given number which is divisible
by 5.
class divisible
{
public static void main(int n)
{
int r = n%5;
if(r == 0)
System.out.println ("The number "+n+" is
divisible by 5");
else
if(r>2)
System.out.println ("The nearest number is "+
(n-r+5));
else
System.out.println ("The nearest number is "+
(n-r));
}
}
OUTPUT:
The nearest number is 55
5)A program to find if the number is divisible by 5
and 3.
import java.util.Scanner;
public class prg
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int num = in.nextInt();
{
if (num % 3 == 0 && num % 5 == 0)
System.out.println("Divisible by 3 and 5");
else if (num % 3 == 0)
System.out.println("Divisible by 3 but not by 5");
else if (num % 5 == 0)
System.out.println("Divisible by 5 but not by 3");
else
System.out.println("Neither divisible by 3 nor by 5");
}
}
}
OUTPUT:
Enter number: 78
Divisible by 3 but not by 5

6)An electric shop has announced seasonal


discount on the purchase of certain items:

Category Discount Discount on


on desktop
laptop
Upto 25,000 0.0% 5.0%
25,001- 5.0% 7.0%
57,000
57,001- 7.0% 10.0%
1,00,000
Above 10.0% 15.0%
1,00,000

Write a menu driven program to input name,


address, amount of purchase, and type of purchase.
Print the net amount along with name and address.
import java.util.*;
public class q27
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int cat;
String name, add;
System.out.println(“enter your name”);
name=in.nextLine();
System.out.println(“enter the address”);
add=in.nextLine();

Double p1=null, net=null,dis=null;


System.out.println(“enter the price of the item”);
p1=in.nextDouble();
System.out.println(“enter the kind of item: L for
laptop, D for desktop”);
cat=in.nextInt();
switch (cat)
{

case 1:
if (p1&lt;=25000)
{
dis=0.0/100.0*p1;
net=p1-dis;

}
else if (p1&gt;25000 &amp;&amp; p1&lt;57001)
{
dis=5.0/100.0*p1;

net=p1-dis;
}
else if (p1&gt;57000 &amp;&amp; p1&lt;1000001)
{
dis=7.0/100.0*p1;
net=p1-dis;
}
else
{
dis=10.0/100.0*p1;
net=p1-dis;
}
break;
case 2:
if(p1&lt;=25000)
{
dis=5.0/100.0*p1;
net=p1-dis;
}

else if(p1&gt;25000 &amp;&amp; p1&lt;57001)


{
dis=7.0/100.0*p1;
net=p1-dis;
}
else if(p1&gt;57000 &amp;&amp; p1&lt;100001)
{
dis=10.0/100.0;
net=p1-dis;
}
else
{
dis=15.0/100.0*p1;
net=p1-dis;
}
break;
}

System.out.println(“name:”;+name);

System.out.println(“address:”+add);
System.out.println(“the net amount:”+net);

}
}
OUTPUT:
enter your name:
Aditi
enter the address:
villa zakrie
enter the price of the item:
25000
Enter the kind of item:1 for laptop,2 for desktop
2
name:Aditi
address:villa zakrie
the net amount:23750.0

7)Write a program to enter three sides of a triangle


and check whether the triangle is possible or not. if
possible, then display whether it is an equilateral,
an isosceles, or a scalene triangle, otherwise
display the message ‘triangle is not possible’
import java.util.Scanner;
public class Program
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first angle: ");
int a1 = in.nextInt();
System.out.print("Enter second angle: ");
int a2 = in.nextInt();
System.out.print("Enter third angle: ");
int a3 = in.nextInt();
int angleSum = a1 + a2 + a3;

if (angleSum == 180 && a1 > 0 && a2 > 0 && a3 > 0) {


if (a1 < 90 && a2 < 90 && a3 < 90) {
System.out.println("Acute-angled Triangle");
}
else if (a1 == 90 || a2 == 90 || a3 == 90) {
System.out.println("Right-angled Triangle");
}
else {
System.out.println("Obtuse-angled Triangle");
}
}
else {
System.out.println("Triangle not possible");
}
}
}
OUTPUT:
Enter first angle: 70
Enter second angle: 187
Enter third angle: 289
Triangle not possible
8)Write a program to enter three numbers and a
character .find and display sum of the numbers
and a character is ‘s’ and product of numbers if the
given character is ‘p’ .the program displays a
message “invalid character” if the user enters a
letter other than ‘s’ or ‘p’.
import java.util.*;
public class numbers
public static void main(String args)
{
Scanner in = new Scanner(System.in);
int a,b,c,sum=0,pr=1;
char ch;
System.out.println(“Enter three numbers”);
a=in.nextInt();
b=in.nextInt();
c=in.nextInt();
System.out.println(“Enter ‘s’ for sum and ‘p’ for
product of three numbers”);
ch=in.next().Char At(0);
if(ch==’s’)
{
sum=a+b+c;
System.out.println(“The sum of three
numbers:”+sum);
}
else if (ch==’p’)
{
pr=a*b*c;
System.out.println(“The product of three
numbers:”+pr);
}
else
System.out.println(“Entered and invalid
character!!!”);
}
}
OUTPUT:
Enter three numbers
3
4
5
Enter ‘s’ for sum and ‘p’ for product of three
numbers
s
The sum of three numbers:12

9)A program to find profit or loss.

import java.util.Scanner;
public class prg

{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter cost price of the article: ");
double cp = in.nextDouble();
System.out.print("Enter selling price of the article: ");
double sp = in.nextDouble();
double pl = sp - cp;
double percent = Math.abs(pl) / cp * 100;
if (pl > 0) {
System.out.println("Profit = " + pl);
System.out.println("Profit % = " + percent);
}
else if (pl < 0) {
System.out.println("Loss = " + Math.abs(pl));
System.out.println("Loss % = " + percent);
}
else {
System.out.println("Neither profit nor loss");
}
}
}
OUTPUT:
Enter cost price of the article: 768
Enter selling price of the article: 900
Profit = 132.0
Profit % = 17.1875

10) A special two-digit number is such that when


the sum of its digits is added to the product of its
digits, the result is equal to the original two-digit
number.
Example: Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of digits = 5 * 9 = 45
Sum of the sum of digits and product of
digits = 14 + 45 = 59
Write a program to accept a two-digit number. Add
the sum of its digits to the product of its digits. If
the value is equal to the number input, then
display the message "Special 2 - digit number"
otherwise, display the message "Not a special two-
digit number".

import java.util.Scanner;

public class SpecialNumber


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter a 2 digit number: ");


int orgNum = in.nextInt();

int num = orgNum;


int count = 0, digitSum = 0, digitProduct = 1;

while (num != 0) {
int digit = num % 10;
num /= 10;
digitSum += digit;
digitProduct *= digit;
count++;
}

if (count != 2)
System.out.println("Invalid input, please
enter a 2-digit number");
else if ((digitSum + digitProduct) == orgNum)
System.out.println("Special 2-digit
number");
else
System.out.println("Not a special 2-digit
number");

}
}
OUTPUT:
Enter a 2 digit number:59
Special 2-digit number
Enter a 2 digit number:85
Not a special 2-digit number

SWITCH CASE
1) Using a switch statement, write a menu driven
program to:
(a) Generate and display the first 10 terms of the
Fibonacci series
0, 1, 1, 2, 3, 5
The first two Fibonacci numbers are 0 and 1, and each
subsequent number is the sum of the previous two.
(b) Find the sum of the digits of an integer that is input.
Sample Input: 15390
Sample Output: Sum of the digits = 18
For an incorrect choice, an appropriate error message
should be displayed.
import java.util.Scanner;

public class FibonacciNDigitSum


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Fibonacci Series");
System.out.println("2. Sum of digits");
System.out.print("Enter your choice: ");
int ch = in.nextInt();

switch (ch) {
case 1:
int a = 0, b = 1;
System.out.print(a + " " + b);
for (int i = 3; i <= 10; i++) {
int term = a + b;
System.out.print(" " + term);
a = b;
b = term;
}
break;

case 2:
System.out.print("Enter number: ");
int num = in.nextInt();
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
System.out.println("Sum of Digits " + " = " +
sum);
break;

default:
System.out.println("Incorrect choice");
break;
}
}
}
OUTPUT:
1. Fibonacci Series
2. Sum of digits
Enter your choice:1
0 1 1 2 3 5 8 13 21 34

2) Write a menu driven program to perform the


following tasks by using Switch case statement:
(a) To print the series:
0, 3, 8, 15, 24, ............ to n terms. (value of 'n' is to be
an input by the user)
(b) To find the sum of the series:
S = (1/2) + (3/4) + (5/6) + (7/8) + ........... + (19/20)

import java.util.Scanner;

public class SeriesMenu


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 to print series");
System.out.println("0, 3, 8, 15, 24,....to n terms");
System.out.println();
System.out.println("Type 2 to find sum of series");
System.out.println("(1/2) + (3/4) + (5/6) + (7/8)
+....+ (19/20)");
System.out.println();
System.out.print("Enter your choice: ");
int choice = in.nextInt();

switch (choice) {
case 1:
System.out.print("Enter n: ");
int n = in.nextInt();
for (int i = 1; i <= n; i++)
System.out.print(((i * i) - 1) + " ");
System.out.println();
break;

case 2:
double sum = 0;
for (int i = 1; i <= 19; i = i + 2)
sum += i / (double)(i + 1);
System.out.println("Sum = " + sum);
break;

default:
System.out.println("Incorrect Choice");
break;
}
}
}
OUTPUT:
Type 1 to print series
0, 3, 8, 15, 24,....to n terms
Type 2 to find sum of series
("(1/2) + (3/4) + (5/6) + (7/8) +....+ (19/20)"
Enter your choice:1
Enter n:5
0,3,8,15,24,35

3) Write a menu driven program to accept a number


from the user and check whether it is a Prime number
or an Automorphic number.
(a) Prime number: (A number is said to be prime, if
it is only divisible by 1 and itself)
Example: 3,5,7,11
(b) Automorphic number: (Automorphic number is
the number which is contained in the last digit(s) of
its square.)
Example: 25 is an Automorphic number as its
square is 625 and 25 is present as the last two
digits.

import java.util.Scanner;

public class PrimeAutomorphic


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Prime number");
System.out.println("2. Automorphic number");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
System.out.print("Enter number: ");
int num = in.nextInt();

switch (choice) {
case 1:
int c = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
c++;
}
}
if (c == 2)
System.out.println(num + " is Prime");
else
System.out.println(num + " is not
Prime");
break;

case 2:
int numCopy = num;
int sq = num * num;
int d = 0;
while(num > 0)
{
d++;
num /= 10;
}
int ld = (int)(sq % Math.pow(10, d));

if (ld == numCopy)
System.out.println(numCopy + " is
automorphic");
else
System.out.println(numCopy + " is not
automorphic");
break;

default:
System.out.println("Incorrect Choice");
break;
}
}
}
OUTPUT:
1. Prime number
2.Automorphic number
Enter your choice:1
Enter number:11
11 is a prime number
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: 11, 101, 151 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 PalinOrPerfect


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Palindrome number");
System.out.println("2. Perfect number");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
System.out.print("Enter number: ");
int num = in.nextInt();

switch (choice) {
case 1:
int copyNum = num;
int revNum = 0;

while(copyNum != 0) {
int digit = copyNum % 10;
copyNum /= 10;
revNum = revNum * 10 + digit;
}

if (revNum == num)
System.out.println(num + " is
palindrome");
else
System.out.println(num + " is not
palindrome");
break;

case 2:
int sum = 0;

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


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

if (num == sum)
System.out.println(num + " is a perfect
number");
else
System.out.println(num + " is not a
perfect number");
break;

default:
System.out.println("Incorrect Choice");
break;
}
}
}

OUTPUT:
1. Palindrome number
2. Perfect number
Enter your choice: 1
Enter number: 151
151 is palindrome

5) Write a menu driven program to input two


positive numbers m and n (where m>n) and
perform the following tasks:
(a) Find the sum of two numbers without using '+'
operator.
(b) Find the product of two numbers without using
'*' operator.
(c) Find the quotient and remainder of two
numbers without using '/' and '%' operator.

[Hint: The last value obtained after each


subtraction is the remainder and the number of
iterations results in quotient.]
Sample Input: m=5, n=2
5 - 2 =3
3 - 2 = 1, thus Quotient = 2 and Remainder = 1

import java.util.Scanner;

public class NumberOperations


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Sum without '+'
operator");
System.out.println("2. Product without '*'
operator");
System.out.println("3. Quotient and
Remainder without '/' & '%' operators");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
System.out.print("Enter m: ");
int m = in.nextInt();
System.out.print("Enter n: ");
int n = in.nextInt();

if (m > n) {
switch (choice) {
case 1:
while (n > 0) {
m++;
n--;
}
System.out.println("Sum = " + m);
break;

case 2:
int p = 0;
while (n > 0) {
p += m;
n--;
}
System.out.println("Product = " + p);
break;

case 3:
int q = 0;
while (m >= n) {
m = m - n;
q++;
}
System.out.println("Quotient = " + q);
System.out.println("Remainder = " +
m);
break;

default:
System.out.println("Incorrect Choice");
break;
}
}
else {
System.out.println("Invalid Inputs");
}
}
}
OUTPUT:
Options 1. Sum without 't' operator 2. Product
without '*' operator 3. Quotient and Remainder
without '/' & '%' operators Enter your choice: 1
Enter m: 5
Enter n: 2
Sum = 7

6) Using a switch case statement, write a menu


driven program
convert a given temperature from Fahrenheit to
Celsius and vice versa. For an incorrect choice, an
appropriate error message should be displayed.

Hint: C = 5/9(F - 32) and F = 1.8 *C+32

import java.util.”;
public class Temperature
{
public static void main(String args[])
{
int ch;
double c,f,c1 ,f1;
Scanner in new Scanner (System.in);
System.out.println(“Enter 1 to find the temp.from
Fahrenheit to Celsius ”);
System.out.println (“ Enter 2 to find the temp from
Celsius to Fahrenheit”) ;
System.out.println( “Enter your choice 1 or 2 ”);
ch= in.nextint();
switch(ch)
{
case 1:
System.out.printin("Enter temperature in
Fahrenheit:”);
f= in.nextDouble();
cl=(double)5 /9*(f-32);
System.out.printin(“The temperature in Celsius is
“+c1);
break;
case 2:
System.out.println(“Enter temperature in
Celsius:”);
c= in.nextDouble();
fi=(double)1.8*c+32;
System.out.println(“The temperature in Fahrenheit
is “+f1);
break;
default:
System.out.printin(“ Wrong Choice !!");
}
}
}

OUTPUT:
Enter 1 to find the temp.from Fahrenheit to Celsius
Enter 2 to find the temp from Celsius to Fahrenheit

Enter your choice 1 or 2


1
Enter temperature in Fahrenheit
108
The temperature in Celsius is 42.7777555556

7) Write a menu driven program to display all


prime and non-prime numbers from 1 to 100.
Enter 1: to display all prime numbers
Enter 2: to display all non-prime numbers
Hint: A number is said to be prime if it is only
divisible by 1 and the number itself.

import java.util.Scanner;

public class Prime


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter 1: to display all
prime numbers");
System.out.println("Enter 2: to display all non-
prime numbers");
System.out.print("Enter your choice: ");
int choice = in.nextInt();

switch (choice) {
case 1:
for (int i = 2; i <= 100; i++) {
boolean isPrime = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime)
System.out.println(i);
}
break;

case 2:
System.out.println(1);
for (int i = 2; i <= 100; i++) {
boolean isPrime = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (!isPrime)
System.out.println(i);
}
break;

default:
System.out.println("Incorrect Choice");
break;
}
}
}
OUTPUT:
Enter 1: to display all prime numbers
Enter 2: to display all non-prime numbers
Enter your choice: 1
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
8) You want to buy a car from “sales and
purchase” where you can get a car in its
depreciated
price. Depreciation value of a car is calculated on
its
showroom price and the number of years it has
been
used. The depreciation value is calculated as per
the
tariff given below:

No of years Rate of depreciation


1. 10%
2. 20%
3. 30%
4. 40%
Above 4 60%

Write a menu driven program to input showroom


price and number of years used. Calculate the
depreciated value of the car and display the
original price, depreciated value and the amount
after depreciation.
import java.util.*;
public class q26
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int year;
Double p1=null,dep=null,p2=null;
System.out.println(“enter the showroom price of
the car:”);
p1=in.nextDouble();
System.out.println(“enter the choice:1 for one
year, 2 for two years, 3 for three years, 4 for four
years
and 5 for more than 4 years”);
year=in.nextInt();

switch (year)
{

case 1:
dep=10.0/100.0*p1;
p2=p1-dep;
break;
case 2:
dep=20.0/100.0*p1;
p2=p1-dep;
break;
case 3:
dep=30.0/100.0*p1;
p2=p1-dep;
break;
case 4:
dep=30.0/100.0*p1;
p2=p1-dep;
break;
case 5:
dep=60.0/100.0*p1;
p2=p1-dep;

break;
}
System.out.println(“the original price of the car is
:”+p1);
System.out.println(“the depreciation value is
:”+dep);
System.out.println(“the price of the car after
deprecition is :”);
}
}
OUTPUT:
enter the showroom price of the car:
10000

enter the choice:1 for one year, 2 for two years, 3


for three years, 4 for four years and 5 for more
than 4 years:
3
the original price of the car is:10000.0
the depreciation value is:30000.0
the price of the car after depreciation is :70000.0
9)An electric shop has announced seasonal discount
on the purchase of certain items:

Category Discount Discount on


on desktop
laptop
Upto 25,000 0.0% 5.0%
25,001- 5.0% 7.0%
57,000
57,001- 7.0% 10.0%
1,00,000
Above 10.0% 15.0%
1,00,000

Write a menu driven program to input name,


address, amount of purchase, and type of purchase.
Print the net amount along with name and address.
import java.util.*;
public class q27
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int cat;

String name, add;


System.out.println(“enter your name”);
name=in.nextLine();
System.out.println(“enter the address”);
add=in.nextLine();

Double p1=null, net=null,dis=null;


System.out.println(“enter the price of the item”);
p1=in.nextDouble();
System.out.println(“enter the kind of item: L for
laptop, D for desktop”);
cat=in.nextInt();
switch (cat)
{

case 1:
if (p1&lt;=25000)
{
dis=0.0/100.0*p1;
net=p1-dis;

}
else if (p1&gt;25000 &amp;&amp; p1&lt;57001)
{
dis=5.0/100.0*p1;

net=p1-dis;
}
else if (p1&gt;57000 &amp;&amp; p1&lt;1000001)
{
dis=7.0/100.0*p1;
net=p1-dis;
}
else
{
dis=10.0/100.0*p1;
net=p1-dis;
}
break;
case 2:
if(p1&lt;=25000)
{
dis=5.0/100.0*p1;
net=p1-dis;
}

else if(p1&gt;25000 &amp;&amp; p1&lt;57001)


{
dis=7.0/100.0*p1;
net=p1-dis;
}
else if(p1&gt;57000 &amp;&amp; p1&lt;100001)
{
dis=10.0/100.0;
net=p1-dis;
}
else
{
dis=15.0/100.0*p1;
net=p1-dis;
}
break;
}

System.out.println(“name:”;+name);

System.out.println(“address:”+add);
System.out.println(“the net amount:”+net);

}
}
OUTPUT:
enter your name:
Aditi
enter the address:
villa marine
enter the price of the item:
25000
Enter the kind of item:1 for laptop,2 for desktop
2
name: Aditi
address:villa marine
the net amount:23750.0
10) A promoter cum developer announces a
special
bonanza for early booking of the flats for their
customers as per the tariff given below

Category Discount on
price

Discount on
development
charge
Ground floor 10% 8%
First floor 20% 10%
Second floor 5% 5%
Third floor 7.5% 10%
Write a menu driven program to input price and the
category:0,1,2,3. Calculate and display the total
discount price of the flat after getting discount.
import java.util.*;
public class q25
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int cat;
Double
d1=null,d2=null,p1=null,p2=null,fp=null,fd=null;
System.out.println(“enter the price”;);
p1=in.nextDouble();
System.out.println(“enter the category: 0 for
ground floor, 1 for first floor, 2 for second floor and 3
for third floor”);
cat=in.nextInt();
switch(cat)

{
case 0:
d1=10.0/100.0*p1;
p2=p1-d1;
d2=8.0/100.0*p2;
fp=p2-d2;
break;

case 1:
d1=20.0/100.0*p1;
p2=p1-d1;
d2=10.0/100.0*p2;
fp=p2-d2;
break;
case 2:
d1=5.0/100.0*p1;

p2=p1-d1;
d2=5.0/100.0*p2;
fp=p2-d2;
break;

case 3:
d1=7.5/100.0*p1;
p2=p1-d1;
d2=10.0/100.0*p2;
fp=p2-d2;
break;
}
fd=d1+d2;
System.out.println(“the total discount is:”+fd);
System.out.println(“the price after discount is:”);
}
}
OUTPUT:
enter the price
100000
enter the category: 0 for ground floor,1 for first
floor,2 for second floor, 3 for third floor
0
The total discount is:17200.0
The price after discount is:82800.0

FOR LOOP
1. S = 1 + 1 + 2 + 3 + 5 +……………. to n terms.
import java.util.*;
public class series6
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int a=1,b=1,c=0,s=0;
System.out.println("enter the number of
terms");
int n=in.nextInt();
for(int i=1;i<=n;i++)
{
c=a+b;
s=s+c;
System.out.print(s);
a=b;
b=c;
}}}
OUTPUT:
enter number of terms
3
1+1+2
2. S = 2 – 4 + 6 – 8 +………………… to n terms.
import java.util.*;
public class series7
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int s=0,i,a,b=0;
System.out.println("enter the number of
terms");
int n=in.nextInt();
for (i=1,a=2;i<=n;i++,a=a+2)
{
if(i%2==0)
{
b=b-a;
s=s+b;
b=0;
}
else
{
b=b+a;
s=s+b;
b=0;
}
} System.out.print(s);
}}
OUTPUT:
enter the number of terms
4
2–4+6–8
3. Write a program to display the Mathematical
Table of 5 for 10 iterations in the given format:
Sample Output: Table of 5
5*1 = 5
5*2 =10
--------
--------
5*10 = 50
public class Table
{

public static void main(String[] args)


{

int num = 5;
for(int i = 1; i <= 10; ++i)
{
System.out.println(num+" * "+i+" =
"+num*i);
}
}
}

OUTPUT:
5*1 = 5
5*2 =10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50

4.Write a program to accept any 5 numbers and


display only those numbers which are prime.
Hint: A number is said to be prime if it is only
divisible by 1 and the number itself.

import java.util.Scanner;

public class PrimeCheck


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter 5 numbers");
for (int i = 1; i <= 5; i++) {
int n = in.nextInt();
boolean isPrime = true;
for (int j = 2; j <= n / 2; j++) {
if (n % j == 0) {
isPrime = false;
break;
}
}
if (isPrime)
System.out.println(n + " is a Prime
Number");
}
}
}
OUTPUT:
Enter 5 numbers
2
4
5
5 is a prime number
6
8

5 WAP to display the series. 1, 4, 9, 16,

public class Series


{
public static void main(String args[]) {
for (int i = 1; i <= 10; i++) {
System.out.print(i * i + " ");
}
}
}
OUTPUT:
1,4,9,16,25,36,49,64,81,100

6. WAP to display the series 1, 2, 4, 7, 11,

public class Series


{
public static void main(String args[]) {
for (int i = 0; i < 10; i++) {
int term = 1 + ((i * (i + 1)) / 2);
System.out.print(term + " ");
}
}
}
OUTPUT:
1,2,4,7,11,16,22,29,37,46
7. WAP to display the series : 3, 6, 9, 12,

public class Series


{
public static void main(String args[]) {
for (int i = 3; i <= 30; i = i + 3) {
System.out.print(i + " ");
}
}
}
OUTPUT:
3,6,9,12,15,18,21,24,27,30

8. WAP to display the series 4, 8, 16, 32

public class Series


{
public static void main(String args[]) {
for (int i = 2; i <= 11; i++) {
System.out.print((int)(Math.pow(2, i)) + " ");
}
}
}
OUTPUT:
4,8,16,32,64,128, 256 ,512,1024,2084

9. 1.5, 3.0, 4.5, 6.0,


public class Series
{
public static void main(String args[]) {
float term = 1.5f;
for (int i = 1; i <= 10; i++) {
System.out.print(term + " ");
term += 1.5f;
}
}
}
OUTPUT:
1.5, 3.0, 4.5, 6.0,7.5,9.0,10.5,12.0,13.5 15.0

10. WAP to display the series 1, 9, 25, 49,

public class Series


{
public static void main(String args[]) {
for (int i = 1; i <= 19; i = i + 2) {
System.out.print((i * i) + " ");
}
}
}
OUTPUT:
1 9 25 49 81 121 169 225 289 361

NESTED FOR LOOP:


1.1,12,123,1234,.…………
public class series4
{
public static void main(String args[])
{
for(int i=1;i<=10;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.print(",");
} }}
OUTPUT:
1,12,123,1234,12345,123456,1234567,12345678,
123456789,12345678910
2. 1, 11, 111, 1111.…………
public class series4
{
public static void main(String args[])
{
for(int i=1;i<=10;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(“1”);
}
System.out.print(",");
} }}

OUTPUT:
1,11,111,1111,11111,111111,1111111,11111111,
111111111,1111111111,

3.
*
* #
* # *
* # * #
* # * # *
public class Pattern
{
public static void main(String args[]) {
for (int i = 1; i <=5; i++) {
for (int j = 1; j <= i; j++) {
if (j % 2 == 0)
System.out.print("# ");
else
System.out.print("* ");
}
System.out.println();
}
}
}
OUTPUT:
*
* #
* # *
* # * #
* # * # *
4.1 + (1+2) + (1+2+3) + .......... + (1+2+3+ ...... + n)

import java.util.Scanner;

public class SeriesSum


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();
long sum = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
sum += j;
}
}
System.out.println("Sum=" + sum);
}
}
OUTPUT:
Enter n: 12
Sum=364

5. WAP to print the pattern


54321
5432
543
54
5

public class Pattern


{
public static void main(String args[]) {
for (int i = 1; i <=5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
OUTPUT:
54321
5432
543
54
5

6. Write program to find the sum of the given serie:


1 + (1*2) + (1*2*3) + .......... + (1*2*3* ...... * n)

import java.util.Scanner;

public class SeriesSum


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();
long sum = 0;
for (int i = 1; i <= n; i++) {
int p = 1;
for (int j = 1; j <= i; j++) {
p *= j;
}
sum += p;
}
System.out.println("Sum=" + sum);
}
}
OUTPUT:
Enter n=7
sum=5913.

7. WAP to find the sum of the given series


(1/2) + (1/3) + (1/5) + (1/7) + (1/11) + .......... + (1/29)

public class SeriesSum


{
public static void main(String args[]) {
double sum = 0.0;
for (int i = 2; i < 30; i++) {
boolean isPrime = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime)
sum += 1.0 / i;
}
System.out.println("Sum=" + sum);
}
}
OUTPUT:
Sum=1.5334387718720317
8. Write the program in Java to display the following
pattern:
1
21
321
4321
54321

public class Pattern


{
public static void main(String args[]) {
for (int i = 1; i <= 5; i++) {
for (int j = i; j >= 1; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
OUTPUT:
1
21
321
4321
54321

9. Write the program in Java to display the following


pattern:

12345
1234
123
12
1

public class Pattern


{
public static void main(String args[]) {
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
OUTPUT:
12345
1234
123
12
1
10. Write the program in Java to display the following
pattern:
13579
1357
135
13
1

public class Pattern


{
public static void main(String args[]) {
for (int i = 9; i >= 1; i -= 2) {
for (int j = 1; j <= i; j += 2) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
OUTPUT:
13579
1357
135
13
1

DO WHILE LOOP
1. 0, 1, 2, 3, 6.……………..
public class series1
{
public static void main(String args[])
{
int a=0,b=1,c=2,d=0,n=4;
System.out.print(a+ “ ”);
System.out.print(b+ “ ”);
System.out.print(c+ “ ” );
do
{
d=a+b+c;
System.out.print(d+ “ ”;);
a=b;
b=c;
c=d;
n=n+1;
}while(n<=10);
}
}
OUTPUT:
0,1,2,3,6,

2.1, -3, 5, -7, 9.………..


public class series2
{

public static void main(String args[])


{
int a=1,b=-3,c=0;
double n=0.0;
System.out.print(a+ “ ”);
System.out.print(b+ “”;);
do
{
c=-b+2;
System.out.print(c+ “ ”;);
a=b;
b=c;
c=-b-2;
System.out.print(c+ “ ”);
a=b;
b=c;
n=n+1.0;
}
while(n<=7.5);
}
}
OUTPUT:
1, -3, 5, -7, 9

3.0, 3, 8, 15………………..
public class series3
{

public static void main(String args[])


{
int a=0,b=3,c=0,x=5,n=3;
System.out.print(a+&quot;,&quot;);
System.out.print(b+&quot;,&quot;);
do
{
c=b+x;
System.out.print(c+&quot;,&quot;);
a=b;
b=c;
x=x+2;
n=n+1;
}while(n&lt;=10);
}
}

4.Write a program in java to diplay the first ten 10


numbers of the Fibonacci series :0,1,1,2,3,…
Class Fibonacci
{
public static void main(String args[])
{
int a,b,c,n;a=0;b=1,c=0;n=3;
System.out,println(“the Fibonacci series is”)
System.out.println(a);
System.out.println(b);
do
{
c=a+b;
System.out.println(c);
a=b;
b=c;
n=n+1;
}
while(n<=10);
}
}
OUTPUT:
the Fibonacci series is
0,1,1,2,3,5,8,13,21,34

3.WAP to check whether a number is a palindrome


number or not
Sample input:707
Sample output:707

import java.io.*;
public class palindrome
{
public static void main(String args[])throws
IOException
{
Scanner in=new Scanner (System. in);
int dn,p,r=0;
System.out. printin(”“Enter a number”);
n=Integer.parselnt(in. readlane()
p=n;
do
{
d=n%10;
r=r*10+d;
n=n/10;
}
while(n!=0);
if(r==p)
System.out.printin(“Palindrome Number”);
else
System.out.printin(“Not a Palindrome Number”);
}

4.How to find the sum of all the entered positive


numbers
import java.util.Scanner;

public class Main


{
public static void main(String[] args)
{
// Take input from the user
// create an object of Scanner class
Scanner sc = new Scanner(System.in);

int sum = 0;
int num = 0;

// do...while loop continues


// until entered number is positive
do {
// add only positive numbers
sum += num;
System.out.println("Enter a number");
num = sc.nextInt();
}
while(num >= 0);

System.out.println("The sum of entered positive


numbers is " + sum);
sc.close();
}
}
OUTPUT:
Enter a number: 4
Enter a number: 6
Enter a number: 2
Enter a number: 8
Enter a number: 5
Enter a number: 1
Enter a number: 3
Enter a number: -9
The sum of entered positive numbers is 29
5.Print number from 10 to 1 and you use below
code:
public class Loop
{
public static void main(String[] args) {

int i=10;
do
{
System.out.print(" "+i);
i--;
}while(i>0)
}
}
OUTPUT:
10 9 8 7 6 5 4 3 2 1

6.WAP to show a multiplication table


public class Main
{
public static void main(String []args)
{
//Take input from the user
//Create instance of the Scanner Class
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number: ");
int n=sc.nextInt(); //Declare and initialize the
number
int i=1;
System.out.println("The multiplication table of
"+n+" is: ");
//Infinite Loop Example
do
{
System.out.println(n+" * "+i+" = "+ (n*i));
i++;
}
while(i<=10);
}
}
OUTPUT:
Enter the number: 4
The multiplication table of 4 is:
4*1=4
4*2=8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
7.How to use a do-while loop to perform a certain
task infinite times.
public class Main
{
public static void main(String []args)
{
int i=1;
System.out.println("Example of Infinite do while
loop: ");
//Infinite Loop Example
do
{
System.out.println(i+" Hello World!");
i++;
}
while(true);
}
}
OUTPUT:

Example of Infinite do-while loop:


1 Hello World!
2 Hello World!
3 Hello World!
4 Hello World!
5 Hello World!
6 Hello World!
7 Hello World!
ctrl+c

8.Print integer values from 1 to 10.


public class EG
{
public static void main(String[] args) {
int i=1;
do{
System.out.println(i);
i++;
}while(i<=10);
}
}
OUTPUT:
1
2
3
4
5
6
7
8
9
10

WHILE LOOP
1.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.
Sample Input: 45, 20
Sample Output: GCD=5
import java.util.Scanner;

public class GCD


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
System.out.println("GCD=" + a);
}
}
OUTPUT:
Enter first number:45
Enter second number:20
GCD 5
2.In order to reach the top of a pole, a monkey in his
first attempt reaches to a height of 5 feet and in
the subsequent jumps, he slips down by 2% of the
height attained in the previous jump. The process
repeats and finally the monkey reaches the top of
the pole. Write a program to input height of the
pole. Calculate and display the number of attempts
the monkey makes to reach the top of the pole.

import java.util.Scanner;

public class MonkeyPole


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter height of the pole: ");
double poleHt = in.nextDouble();
double jumpHt = 5.0;
int numAttempts = 1;
while (jumpHt < poleHt) {
jumpHt += 5.0;
jumpHt -= 2 * jumpHt / 100.0;
numAttempts++;
}
System.out.println("Number of Attempts = " +
numAttempts);
}
}
OUTPUT:
Enter height of the pole:12.5
Number of Attempts 3
3.A special two-digit number is such that when the
sum of its digits is added to the product of its
digits, the result is equal to the original two-digit
number.
Example: Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of digits = 5 * 9 = 45
Sum of the sum of digits and product of
digits = 14 + 45 = 59
Write a program to accept a two-digit number. Add
the sum of its digits to the product of its digits. If the
value is equal to the number input, then display the
message "Special 2 - digit number" otherwise,
display the message "Not a special two-digit
number".

import java.util.Scanner;

public class SpecialNumber


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


System.out.print("Enter a 2 digit number: ");
int orgNum = in.nextInt();

int num = orgNum;


int count = 0, digitSum = 0, digitProduct = 1;

while (num != 0) {
int digit = num % 10;
num /= 10;
digitSum += digit;
digitProduct *= digit;
count++;
}

if (count != 2)
System.out.println("Invalid input, please
enter a 2-digit number");
else if ((digitSum + digitProduct) == orgNum)
System.out.println("Special 2-digit number");
else
System.out.println("Not a special 2-digit
number");

}
}
OUTPUT: Enter a 2 digit number:59
Special 2-digit number
Enter a 2 digit number:85
Not a special 2-digit number

4.Write a program to accept a number and check


whether it is a 'Spy Number' or not. (A number is
spy if the sum of its digits equals the product of its
digits.)
Example: Sample Input: 1124
Sum of the digits = 1 + 1 + 2 + 4 = 8
Product of the digits = 1*1*2*4 = 8

import java.util.Scanner;

public class SpyNumber


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter Number: ");


int num = in.nextInt();

int digit, sum = 0;


int orgNum = num;
int prod = 1;

while (num > 0) {


digit = num % 10;

sum += digit;
prod *= digit;
num /= 10;
}

if (sum == prod)
System.out.println(orgNum + " is Spy
Number");
else
System.out.println(orgNum + " is not Spy
Number");

}
}
OUTPUT:
Enter the number:1124
1124 is a spy number

5.Write a program to input a number. Check and


display whether it is a Niven number or not. (A
number is said to be Niven which is divisible by the
sum of its digits).
Example: Sample Input 126
Sum of its digits = 1 + 2 + 6 = 9 and 126 is divisible
by 9.

import java.util.Scanner;

public class NivenNumber


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

int digitSum = 0;

while (num != 0) {
int digit = num % 10;
num /= 10;
digitSum += digit;
}

/*
* digitSum != 0 check prevents
* division by zero error for the
* case when users gives the number
* 0 as input
*/
if (digitSum != 0 && orgNum % digitSum == 0)
System.out.println(orgNum + " is a Niven
number");
else
System.out.println(orgNum + " is not a Niven
number");
}
}
OUTPUT:
Enter a number: 126
126 is a niven number

6. Write a program to input a number and count the


number of digits. The program further checks
whether the number contains odd number of digits
or even number of digits.
Sample Input: 749
Sample Output: Number of digits=3
The number contains odd number of digits.
import java.util.Scanner;
public class DigitCount
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int n = in.nextInt();
int dc = 0;
while (n != 0) {
dc++;
n /= 10;
}
System.out.println("Number of digits = " + dc);
if (dc % 2 == 0)
System.out.println("The number contains
even number of digits");
else
System.out.println("The number contains odd
number of digits");
}
}
OUTPUT:
Enter a number:749
Number of digits:3
The number contains odd number of digits

7. 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 DigitReverse


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


System.out.print("Enter Number: ");
int orgNum = in.nextInt();

int copyNum = orgNum;


int revNum = 0;

while(copyNum != 0) {
int digit = copyNum % 10;
copyNum /= 10;
revNum = revNum * 10 + digit;
}
int diff = revNum - orgNum;
System.out.println("Reversed Number = " +
revNum);
System.out.println("Absolute Difference = " +
Math.abs(diff));
}
}
OUTPUT:
Enter number: 149
Reversed number: 941
Absolute number: 297
8. Write a program to input a number. Find the sum
of digits and the number of digits. Display the
output.
Sample Input: 7359
Sum of digits = 24
Number of digits = 4

import java.util.Scanner;

public class DigitSum


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
int sum = 0, count = 0;
while (n != 0) {
int d = n % 10;
sum += d;
count++;
n /= 10;
}
System.out.println("Sum of digits = " + sum);
System.out.println("Number of digits = " +
count);
}
}
OUTPUT:
Enter a number: 7359
Sum of digits: 24
Number of digits: 4

9.Write a program to accept any two numbers and


print the 'GCD' of them. The GCD (Greatest
Common Divisor) of two integers is calculated by
continued division method. Divide the larger
number by the smaller, the remainder then divides
the previous divisor. The process is repeated till
the remainder is zero. The divisor then results the
GCD.
Sample Input: 25 35
Sample Output: GCD of the numbers 25 and 35 is 5.
import java.util.Scanner;
public class GCD
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int x = in.nextInt();
System.out.print("Enter second number: ");
int y = in.nextInt();
while (y != 0) {
int t = y;
y = x % y;
x = t;
}
System.out.println("GCD = " + x);
}
}
OUTPUT:
Enter first number: 25
Enter first number: 35
GCD = 5
10. Write a program to input a number. Display
the product of the successors of even digits of the
number entered by user.
Input: 2745
Output: 15
[Hint: The even digits are: 2 and 4
The product of successor of even digits is: 3*5= 15]

import java.util.Scanner;

public class EvenSuccessor


{
public void computeProduct() {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
int num = in.nextInt();
int orgNum = num;
int prod = 1;

while (num != 0) {
int digit = num % 10;
num /= 10;

if (digit % 2 == 0)
prod = prod * (digit + 1);
}

if (prod == 1)
System.out.println("No even digits in " +
orgNum);
else
System.out.println("Product of even digits
successors is "
+ prod);
}
}
OUTPUT:
Enter the number: 2745
Product of even number sucessors is 15
FUNCTIONS
1.Write a program in Java using a method
Discount( ), to calculate a single discount or a
successive discount. Use overload methods
Discount(int), Discount(int,int) and
Discount(int,int,int) to calculate single discount
and successive discount respectively. Calculate
and display the amount to be paid by the
customer after getting discounts on the printed
price of an article.
Sample Input:
Printed price: ₹12000
Successive discounts = 10%, 8%
= ₹(12000 - 1200)
= ₹(10800 - 864)
Amount to be paid = ₹9936
import java.util.Scanner;

public class successiveDiscount


{
public void discount(int price) {
System.out.println("Amount after single discount
= " + discount(price, 10));
System.out.println("Amount after successive
discount = " + discount(price, 10, 8));
}

public double discount(int price, int d) {


double priceAfterDisc = price - price * d / 100.0;
return priceAfterDisc;
}

public double discount(int price, int d1, int d2) {


double priceAfterDisc1 = price - price * d1 /
100.0;
double priceAfterDisc2 = priceAfterDisc1 -
priceAfterDisc1 * d2 / 100.0;
return priceAfterDisc2;
}

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter price: ");


int price = in.nextInt();

SuccessiveDiscount obj = new


SuccessiveDiscount();

obj.discount(price);
}
}
OUTPUT:

Enter the price:12000


Amount after single discount = 10800.0
Amount after successive discount = 9936.0
2.Write a program to input a number. Use a function
int Armstrong(int n) to accept the number. The
function returns 1, if the number is Armstrong,
otherwise zero(0).

Sample Output: 153 ⇒ 13 + 53 + 33 = 153


Sample Input: 153

It is an Armstrong Number.
import java.util.Scanner;

public class ArmstrongNumber


{
public int armstrong(int n) {

int num = n, cubeSum = 0;

while (num > 0) {


int digit = num % 10;
cubeSum = cubeSum + (digit * digit * digit);
num /= 10;
}

if (cubeSum == n)
return 1;
else
return 0;
}

public static void main(String args[]) {

Scanner in = new Scanner(System.in);


System.out.print("Enter Number: ");
int num = in.nextInt();

ArmstrongNumber obj = new


ArmstrongNumber();
int r = obj.armstrong(num);
if (r == 1)
System.out.println(num + " is an Armstrong
number");
else
System.out.println(num + " is not an
Armstrong number");
}
}
OUTPUT:
Enter Number :153
153 is an Armstrong number

3.Write a program to input a number and check and


print whether it is a 'Pronic' number or not. Use a
function int Pronic(int n) to accept a number. The
function returns 1, if the number is 'Pronic',
otherwise returns zero (0).
(Hint: Pronic number is the number which is the
product of two consecutive integers)
Examples:
12 = 3 * 4
20 = 4 * 5
42 = 6 * 7
import java.util.Scanner;

public class PronicNumber


{
public int pronic(int n) {

int isPronic = 0;

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


if (i * (i + 1) == n) {
isPronic = 1;
break;
}
}

return isPronic;
}

public static void main(String args[]) {

Scanner in = new Scanner(System.in);


System.out.print("Enter the number to check: ");
int num = in.nextInt();

PronicNumber obj = new PronicNumber();


int r = obj.pronic(num);

if (r == 1)
System.out.println(num + " is a pronic
number");
else
System.out.println(num + " is not a pronic
number");

}
}
OUTPUT:
Enter the number to check :12
12 is a pronic number

4. Write a program to enter a two digit number and


find out its first factor excluding 1 (one). The
program then find the second factor (when the
number is divide by the first factor) and finally
displays both the factors.
Hint: Use a non-return type function as void
fact(int n) to accept the number.
Sample Input: 21
The first factor of 21 is 3
Sample Output: 3, 7
Sample Input: 30
The first factor of 30 is 2
Sample Output: 2, 15
import java.util.Scanner;

public class Factors


{
public void fact(int n) {

if (n < 10 || n > 99) {


System.out.println("ERROR!!! Not a 2-digit
number");
return;
}

int i;
for (i = 2; i <= n; i++) {
if (n % i == 0)
break;
}

int sf = n / i;

System.out.println(i + ", " + sf);


}

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter number: ");


int num = in.nextInt();

Factors obj = new Factors();


obj.fact(num);
}
}
OUTPUT:
Enter number: 21
3,7

5. Write a program using a function called area() to


compute area of the following:
(a) Area of circle = (22/7) * r * r
(b) Area of square= side * side
(c) Area of rectangle = length * breadth
Display the menu to display the area as per the user's
choice.

import java.util.Scanner;

public class MenuArea


{
public void area() {

Scanner in = new Scanner(System.in);

System.out.println("Enter a to calculate area of


circle");
System.out.println("Enter b to calculate area of
square");
System.out.println("Enter c to calculate area of
rectangle");
System.out.print("Enter your choice: ");
char choice = in.next().charAt(0);

switch(choice) {
case 'a':
System.out.print("Enter radius of circle: ");
double r = in.nextDouble();
double ca = (22 / 7.0) * r * r;
System.out.println("Area of circle = " + ca);
break;

case 'b':
System.out.print("Enter side of square: ");
double side = in.nextDouble();
double sa = side * side;
System.out.println("Area of square = " + sa);
break;

case 'c':
System.out.print("Enter length of rectangle:
");
double l = in.nextDouble();
System.out.print("Enter breadth of
rectangle: ");
double b = in.nextDouble();
double ra = l * b;
System.out.println("Area of rectangle = " +
ra);
break;

default:
System.out.println("Wrong choice!");
}
}
}
OUTPUT:
Enter a to calculate area of circle
Enter b to calculate area of square
Enter c to calculate area of rectangle
Enter your choice :a
Enter radius of circle:9
Area of circle = 254.71428571
6. Write a program using method name
Glcm(int,int) to find the Lowest Common Multiple
(LCM) of two numbers by GCD (Greatest Common
Divisor) of the numbers. GCD of two integers is
calculated by continued division method. Divide the
larger number by the smaller, the remainder then
divides the previous divisor. The process is repeated
till the remainder is zero. The divisor then results in
the GCD.
LCM = product of two numbers / GCD

import java.util.Scanner;

public class Glcm


{

public void Glcm(int a, int b) {


int x = a, y = b;
while (y != 0) {
int t = y;
y = x % y;
x = t;
}
int lcm = (a * b) / x;
System.out.println("LCM = " + lcm);
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int x = in.nextInt();
System.out.print("Enter second number: ");
int y = in.nextInt();

Glcm obj = new Glcm();


obj.Glcm(x, y);
}
}
OUTPUT:
Enter the first number: 6
Enter the second number: 10
LCM = 30
7. Write a program in Java to accept the name of an
employee and his/her annual income. Pass the
name and the annual income to a function
Tax(String name, int income) which displays the
name of the employee and the income tax as per
the given tariff:
Annual Income Income Tax
Up to ₹2,50,000 No tax

Up to ₹2,50,000 to 10% of the income


₹2,50,001 to ₹5,00,000 exceeding ₹2,50,000
₹5,00,001 to ₹10,00,000 ₹30,000 + 20% of the
amount exceeding
₹5,00,000
₹10,00,001 and above ₹50,000 + 30% of the
amount exceeding
₹10,00,000
import java.util.Scanner;

public class EmployeeTax


{
public void tax(String name, int income) {

double tax;
if (income <= 250000)
tax = 0;
else if (income <= 500000)
tax = (income - 250000) * 0.1;
else if (income <= 1000000)
tax = 30000 + ((income - 500000) * 0.2);
else
tax = 50000 + ((income - 1000000) * 0.3);

System.out.println("Name: " + name);


System.out.println("Income Tax: " + tax);
}

public static void main(String args[]) {


Scanner in = new Scanner(System.in);
System.out.print("Enter name: ");
String n = in.nextLine();
System.out.print("Enter annual income: ");
int i = in.nextInt();

EmployeeTax obj = new EmployeeTax();


obj.tax(n, i);
}
}
OUTPUT:
Aditi M Shendge
A
M
S
Write a menu driven program using a method
Number() to perform the following tasks:

8. Accept a number from the user and display it in its


Binary Equivalents.
For example:
Sample Input: (21)10
Sample Output: (10101)2
Accept a binary number from the user and display it
in its Decimal Equivalents.
For example:
Sample Input: (11101)2
Sample Output: (29)10
import java.util.Scanner;
public class Binary
{
long decimal2Binary(int n) {
String str = "";
int t = n;
while (t > 0) {
int d = t % 2;
str = d + str;
t /= 2;
}
long b = Long.parseLong(str);
return b;
}
long binary2Decimal(long n) {
long decimal = 0;
int base = 1;
long t = n;
while (t > 0) {
int d = (int)(t % 10);
decimal += d * base;
base *= 2;
t /= 10;
}

return decimal;
}
void number() {
Scanner in = new Scanner(System.in);
System.out.println("Enter 1 to display number
in Binary Equivalent");
System.out.println("Enter 2 to display number
in Decimal Equivalent");
System.out.print("Enter your choice: ");
int c = in.nextInt();

switch (c) {
case 1:
System.out.print("Enter a decimal number:
");
int num = in.nextInt();
long binNum = decimal2Binary(num);
System.out.println("Binary Equivalent");
System.out.println(binNum);
break;

case 2:
System.out.print("Enter a binary number: ");
long ipNum = in.nextLong();
long decNum = binary2Decimal(ipNum);
System.out.println("Decimal Equivalent");
System.out.println(decNum);
break;

default:
System.out.println("Incorrect Choice");
break;
}
}
}
OUTPUT:
Enter 1 to display number in Binary Equivalent
Enter 2 to display number in Decimal Equivalent
Enter your choice: 1
Enter a decimal number: 21
Binary Equivalent 10101
9. Write a class using a function primeCheck(int num)
to check whether a given number is Prime or not.
Function should return a value 1 if number is prime
otherwise it return 0 if not.
public class ques
{
public static int primeCheck(int num)
{
int flag = 1;
for(int i = 2;i<num;i++)
{
if(num%i = = 0)
{
flag = 0;
break;
}
}
return(flag);
}
}
OUTPUT:
prime check (4)
returned
int =0

10. Write a program to input a positive natural number


N and output all combination of consecutive natural
numbers which added up to give N
Example: If the N is 15 then output should be
1 2 3 4 5 = 15
4 5 6 = 15
7 8 = 15
Define a class consecutive that has the following
functions.
private static void printSet(int i, int j) -> which print
one line series of numbers starting from i to j, as
mentioned in the above format.
public static void main(int N) -> which will print all
number of possibilities upto N, as mentioned in the
above example
public class q
{
private static void printSet(int i, int j)
{
int s = 0;
for(int k = i;k<j;k++)
{
s+= k;
System.out.print (k+" ");
}
System.out.println (" = "+s);
}
public static void main(int N)
{
int i,j,s;
for(i = 1;i<N;i++)
{
s = 0; j = i;
while(s<N)
{
s=s+j; j++;
}
if(s == N)
{
printSet(i,j);
}
}
}
}
OUTPUT:
2 +3 = 5

OVERLOADED FUNCTIONS
1.Design a class to overload a function compare() as
follows:
(i) void compare( int, int) : to compare two integers
and print the greater of the two
(ii) void compare( char, char) : to compare the
numeric value of two characters and printing the
higher of the two
(iii) void compare( String, String) : to compare the
length of two strings and print the longer of the two.
import java.util.*;
public class display
{
void compare( int n1, int n2)
{
int max=0;
if(n1>n2)
max=n1;
else if(n2>n1)
max=n2;
System.out.println("the greater number is:
"+max);
}
void compare( char c1, char c2)
{
char greater=' ';
if((int)c1>(int)c2)
greater=c1;
else if((int)c2>(int)c1)
greater=c2;
System.out.println("the character with greater
numerical value is: "+greater);
}
void compare( String s1, String s2)
{
String longer=" ";
int len1=s1.length();
int len2=s2.length();
if(len1>len2)
longer=s1;
else if(len2>len1)
longer=s2;
System.out.println("the longer string: "+longer);
}
}
OUTPUT:
the greater number is 2

the character with greater numerical value is b


the greater number is aditi

2. Design a class to overload a function volume() as


follows:
(i) double volume(double r) : with radius as an
argument, returns the volume of the sphere.
(ii) double volume(double h, double r) : with height
and radius as the arguments, returns the volume of
the cylinder.
(iii) double volume(double l, double b, double h) :
with length, breadth and height as the arguments,
returns the volume of the cuboid.
import java.util.*;
public class math
{
void volume(double r)
{
double v1;
v1=(4/3)*(22/7)*r*r*r;
System.out.println("the volume is: "+v1);
}
void volume(double h, double r)
{
double v2;
v2=(22/7)*r*r*h;
System.out.println("the volume is: "+v2);
}
void volume(double l, double b,double h)
{
double v3;
v3=l*b*h;
System.out.println("the volume is: "+v3);
}
}
OUTPUT:
the volume is:30.0
the volume is:60.0
the volume is:375.0
3. Write a class with the name Area using function
overloading that computes the area of a
parallelogram, a rhombus and a trapezium.
Formula:

Area of a parallelogram (pg) = base * ht

Area of a rhombus (rh) = (1/2) * d1 * d2


(where, d1 and d2 are the diagonals)

Area of a trapezium (tr) = (1/2) * ( a + b) * h


(where a and b are the parallel sides, h is the
perpendicular distance between the parallel sides)

import java.util.Scanner;

public class Area


{
public double area(double base, double height) {
double a = base * height;
return a;
}

public double area(double c, double d1, double


d2) {
double a = c * d1 * d2;
return a;
}
public double area(double c, double a, double b,
double h) {
double x = c * (a + b) * h;
return x;
}

public static void main(String args[]) {


Scanner in = new Scanner(System.in);
Area obj = new Area();

System.out.print("Enter base of parallelogram:


");
double base = in.nextDouble();
System.out.print("Enter height of parallelogram:
");
double ht = in.nextDouble();
System.out.println("Area of parallelogram = " +
obj.area(base, ht));

System.out.print("Enter first diagonal of


rhombus: ");
double d1 = in.nextDouble();
System.out.print("Enter second diagonal of
rhombus: ");
double d2 = in.nextDouble();
System.out.println("Area of rhombus = " +
obj.area(0.5, d1, d2));
System.out.print("Enter first parallel side of
trapezium: ");
double a = in.nextDouble();
System.out.print("Enter second parallel side of
trapezium: ");
double b = in.nextDouble();
System.out.print("Enter height of trapezium: ");
double h = in.nextDouble();
System.out.println("Area of trapezium = " +
obj.area(0.5, a, b, h));
}
}
OUTPUT:
Enter base of parallelogram: 24
Enter height of parallelogram: 17
Area of parallelogram = 408.0
Enter first diagonal of rhombus: 40
Enter second diagonal of rhombus: 25
Area of rhombus = 500.0
Enter first parallel side of trapezium: 4
Enter second parallel side of trapezium: 6
Enter height of trapezium: 3
Area of trapezium = 15.0
4.Write a class with the name Perimeter using
function overloading that computes the perimeter of
a square, a rectangle and a circle.

Formula:
Perimeter of a square = 4 * s
Perimeter of a rectangle = 2 * (l + b)
Perimeter of a circle = 2 * (22/7) * r

import java.util.Scanner;
public class Perimeter
{
public double perimeter(double s) {
double p = 4 * s;
return p;
}
public double perimeter(double l, double b) {
double p = 2 * (l + b);
return p;
}
public double perimeter(int c, double pi, double r)
{
double p = c * pi * r;
return p;
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
Perimeter obj = new Perimeter();
System.out.print("Enter side of square: ");
double side = in.nextDouble();
System.out.println("Perimeter of square = " +
obj.perimeter(side));
System.out.print("Enter length of rectangle: ");
double l = in.nextDouble();
System.out.print("Enter breadth of rectangle: ");
double b = in.nextDouble();
System.out.println("Perimeter of rectangle = "
+ obj.perimeter(l, b));
System.out.print("Enter radius of circle: ");
double r = in.nextDouble();
System.out.println("Perimeter of circle = " +
obj.perimeter(2, 3.14159, r));
}
}
OUTPUT:
Enter side of square 3
Perimeter of square =12.0
Enter length of rectangle: 10
Enter breadth of rectangle: 7
Enter radius of circle: 34.0
Perimeter of rectangle =37.699079999
5.Design a class overloading a function calculate() as
follows:

void calculate(int m, char ch) with one integer


argument and one character argument. It checks
whether the integer argument is divisible by 7 or not,
if ch is 's', otherwise, it checks whether the last digit
of the integer argument is 7 or not.
void calculate(int a, int b, char ch) with two integer
arguments and one character argument. It displays
the greater of integer arguments if ch is 'g'
otherwise, it displays the smaller of integer
arguments.
import java.util.Scanner;

public class Calculate


{
public void calculate(int m, char ch) {
if (ch == 's') {
if (m % 7 == 0)
System.out.println("It is divisible by 7");
else
System.out.println("It is not divisible by
7");
}
else {
if (m % 10 == 7)
System.out.println("Last digit is 7");
else
System.out.println("Last digit is not 7");
}
}

public void calculate(int a, int b, char ch) {


if (ch == 'g')
System.out.println(a > b ? a : b);
else
System.out.println(a < b ? a : b);
}

public static void main(String args[]) {


Scanner in = new Scanner(System.in);
KboatCalculate obj = new KboatCalculate();

System.out.print("Enter a number: ");


int n1 = in.nextInt();
obj.calculate(n1, 's');
obj.calculate(n1, 't');

System.out.print("Enter first number: ");


n1 = in.nextInt();
System.out.print("Enter second number: ");
int n2 = in.nextInt();
obj.calculate(n1, n2, 'g');
obj.calculate(n1, n2, 'k');

}
}
OUTPUT:
Enter a number:77
it is divisible by :7
Last digit is :7
Enter first number:52
Enter second number: 75
75
52

6.Design a class to overload a function compare( ) as


follows:
void compare(int, int) — to compare two integers
values and print the greater of the two integers.
void compare(char, char) — to compare the numeric
value of two characters and print with the higher
numeric value.
void compare(String, String) — to compare the
length of the two strings and print the longer of the
two.
import java.util.Scanner;
public class Compare
{
public void compare(int a, int b)
{
if (a > b) {
System.out.println(a);
}
else {
System.out.println(b);
}
}

public void compare(char a, char b) {


int x = (int)a;
int y = (int)b;
if (x > y) {
System.out.println(a);
}
else {
System.out.println(b);
}
}

public void compare(String a, String b) {


int l1 = a.length();
int l2 = b.length();
if (l1 > l2) {
System.out.println(a);
}
else {
System.out.println(b);
}

public static void main(String args[]) {


Scanner in = new Scanner(System.in);
Compare obj = new Compare();

System.out.print("Enter first integer: ");


int n1 = in.nextInt();
System.out.print("Enter second integer: ");
int n2 = in.nextInt();
obj.compare(n1, n2);

System.out.print("Enter first character: ");


char c1 = in.next().charAt(0);
System.out.print("Enter second character: ");
char c2 = in.next().charAt(0);
in.nextLine();
obj.compare(c1, c2);

System.out.print("Enter first string: ");


String s1 = in.nextLine();
System.out.print("Enter second string: ");
String s2 = in.nextLine();
obj.compare(s1, s2);
}
}
OUTPUT:
Enter first integer: 78
Enter second integer: 23
Enter first character: K
Enter second character: C
Enter first string: Aditi
Enter second string: Aditi is my name
Aditi
7.Design a class to overload a function polygon() as
follows:

void polygon(int n, char ch) — with one integer and


one character type argument to draw a filled square
of side n using the character stored in ch.
void polygon(int x, int y) — with two integer
arguments that draws a filled rectangle of length x
and breadth y, using the symbol '@'.
void polygon() — with no argument that draws a
filled triangle shown below:
Example:
Input value of n=2, ch = 'O'
Output:
OO
OO
Input value of x = 2, y = 5
Output:
@@@@@
@@@@@
Output:
*
**
***
public class Polygon
{
public void polygon(int n, char ch) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(ch);
}
System.out.println();
}
}

public void polygon(int x, int y) {


for (int i = 1; i <= x; i++) {
for (int j = 1; j <= y; j++) {
System.out.print('@');
}
System.out.println();
}
}

public void polygon() {


for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
System.out.print('*');
}
System.out.println();
}
}

public static void main(String args[]) {


Polygon obj = new Polygon();
obj.polygon(2, 'o');
System.out.println();
obj.polygon(2, 5);
System.out.println();
obj.polygon();
}
}
OUTPUT:

@@@@@ @@@@@
***

8.Design a class to overload a function series( ) as


follows:

double series(double n) with one double argument


and returns the sum of the series.
sum = (1/1) + (1/2) + (1/3) + .......... + (1/n)
double series(double a, double n) with two double
arguments and returns the sum of the series.
sum = (1/a2) + (4/a5) + (7/a8) + (10/a11) + ..........
to n terms
public class Series
{
double series(double n) {
double sum = 0;
for (int i = 1; i <= n; i++) {
double term = 1.0 / i;
sum += term;
}
return sum;
}

double series(double a, double n) {


double sum = 0;
int x = 1;
for (int i = 1; i <= n; i++) {
int e = x + 1;
double term = x / Math.pow(a, e);
sum += term;
x += 3;
}
return sum;
}

public static void main(String args[]) {


Series obj = new Series();
System.out.println("First series sum = " +
obj.series(5));
System.out.println("Second series sum = " +
obj.series(3, 8));
}
OUTPUT:
First series sum = 2.283333333333333
Second series sum = 0.1286982248418103

}
9.Design a class to overload the function display(.....)
as follows:

void display(int num) — checks and prints whether


the number is a perfect square or not.
void display(String str, char ch) — checks and prints
if the word str contains the letter ch or not.
void display(String str) — checks and prints the
number of special characters present in the word str.
Write a suitable main( ) function.

public class Display


{
public void display(int num) {

double sroot = Math.sqrt(num);


double diff = sroot - Math.floor(sroot);

if (diff == 0)
System.out.println(num + " is a perfect
square");
else
System.out.println(num + " is not a perfect
square");

public void display(String str, char ch) {

int idx = str.indexOf(ch);

if (idx == -1)
System.out.println(ch + " not found");
else
System.out.println(ch + " found");

public void display(String str) {

int count = 0;
int len = str.length();

for (int i = 0; i < len; i++) {


char ch = str.charAt(i);
if (!Character.isLetterOrDigit(ch) &&
!Character.isWhitespace(ch)) {
count++;
}
}

System.out.println("Number of special
characters = " + count);
}

public static void main(String args[]) {


Display obj = new Display();
obj.display(15);
obj.display(" ADITI", 't');
obj.display(" aditi");
}
}
OUTPUT:
18 is not a perfect square
t found
Number of special characters = 6

10. WAP with functions that will perform


multiplication but which will have a different number
of arguments assigned to them.

class Adder
{
static int multiply(int a,int b)
{
return a*b;
}
static int multiply(int a,int b,int c)
{
return a*b*c;}
}
class TestOverloading1
{
public static void main(String[] args)
{
System.out.println(Adder.multiply(110,110));
System.out.println(Adder.multiply(110,110,110));
}
}
OUTPUT:
multiply(3,5,7)
105
BIBLIOGRAPHY
www.knowlegeboat.com
www.javapoint.com
ICSE APC COMPUTER APPLICATIONS.
ACKNOWLEDGEMENT
First and foremost, I would like to thank God
almighty, without whose help doing these projects
would have been an impossible task. Secondly I
would like to thank our principal, Sr. Malar Joseph
who encourages us to learn and explore the
creativity within us. Thirdly, I would like to thank
miss Savita who gave me suitable guidelines to
complete this project in the right manner. Last but
not the least I would like to thank my family and
friends whose ample support and guidance helped
me to complete this project in a creative manner.
THANK
YOU

You might also like