Escolar Documentos
Profissional Documentos
Cultura Documentos
DWIVEDI
CLASS : IX
SEC : A
SUB : COMPUTER FILE
TOPIC : JAVA PROGRAMS
PREFACE
In today’s time we need to perform various lengthy calculations and various
other tasks. Doing all this manually would consume a lot of time. In order to
perform the tasks quickly and with more accuracy computer programs are
developed.
The file consists of 30 Java programs made for verifying a range of numbers viz;
Armstrong number, Palindrome number, Special number, Buzz number, for shops
to calculate discounts, gifts to be given on purchase of items of various amounts,
for displaying various types of series using different programming logic etc.
INDEX
S.no. Topic Pg.no Remark
1. Front Page 1
2. Preface 2
3. Index 3
4. Introduction 4
5 Java programs 5 to
6. Acknowledgement
INTRODUCTION
Java is an Object Oriented Programming language i.e. , it is a style of programming
based on objects , classes and inheritance . An object is a software bundle of data
and related operations. A class is a template of objects that consists of or that
defines objects of a certain kind.
It was developed primarily by James Gosling at Sun Micro Systems (which has
since been acquired by Oracle Corporation). In 1991, the Sun Microsystems
developed a programming language to solve the problems of consumer
electronics. Its most advantageous feature is that it is platform independent
language. It has the capability to be transported from one computer to another .
It was first launched on 23 May 1995. Since then it has been used to write
programs to solve complex real-life problems such as verification of numbers ,
varying discounts on various prices , etc. As of 2016, Java is most popular
programming language in use , particularly for client-server web applications,
with a reported 9 million developers.
JAVA PROGRAMS
Program 1): Write a program to input 10 numbers and separately display the sum
of even and odd numbers
import java.util.*;
public class sum_even_odd
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i,n,esum = 0,osum = 0;
for(i = 1 ; i<=10 ; i++)
{
System.out.println("Enter a number");
n = sc.nextInt();
if (n%2 == 0)
esum =esum+n ;
else
osum = osum + n;
}
System.out.println("Sum of even numbers =" + esum);
System.out.println("Sum of odd numbers" + osum);
}
}
Program 2): 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 original number and the reversed number .
import java.util.*;
public class absolute_difference
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();int i,d,sum=0;
for (i = n ; i>0 ; i = i/10)
{
d =i%10 ;
sum = sum*10 + d ;
}
int ab_diff = Math.abs(sum - n);
System.out.println("Reversed number = "+sum);
System.out.println("Absolute difference = "+ab_diff);
}
}
Program 3): Write a program to calculate the GCD or the Greatest Common
Divisor of two numbers .
import java.util.*;
public class gcd
{
public static void main (String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter two numbers ");
int m =sc.nextInt();
int n =sc.nextInt();
int p = m * n;int i,hcf=0;
for(i=1;i<p;i++)
{
if ((m%i == 0)&&(n % i == 0))
hcf = i ;
}
System.out.println("Greatest Common Divisor : "+ hcf);
}
}
Program 4): Write a program to enter ten numbers and display the smallest and
greatest of them.
import java.util.*;
public class greatest_smallest
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i,n,max = 0;
System.out.println("Enter a number ");
n= sc.nextInt();int min = n;
for (i=1;i<=9;i++)
{
System.out.println("Enter a number ");
n= sc.nextInt();
if (n>max)
max=n;
else if (n<min)
min = n;
}
System.out.println("Maximum number: " +max);
System.out.println("Minimum number: " +min);
}
}
Program 5): Write a program to input a number and to display the first 7
multiples of that number.
( Example : 7*1=7 )
import java.util.*;
public class first_7_multiples
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();int i,a=0;
for(i=1;i<=7;i++)
{
a = n*1;
System.out.println(n+"*"+i+"="+a);
}
}
}
Program 6): Write a program to check the number of digits in a number and
display whether it contains odd number or even number of digits .
import java.util.*;
public class number_even_odd
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter a number");
int n = sc.nextInt(); int sum = 0; int i,d;
for (i=n;i>0;i=i/10)
{
d=i%10;
sum =sum +1 ;
}
System.out.println("The number of digits : "+ sum);
if (sum%2==0)
System.out.println("It contains even number of digits");
else
System.out.println("It contains odd number of digits");
}
}
Program 7): Write a program to check for an Armstrong number.
import java.util.*;
public class Armstrong_number
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();int i,d,sum = 0;
for(i = n;i > 0;i=i/10)
{
d= i%10;
sum = sum + (d*d*d);
}
if(sum == n)
System.out.println("It is Armstrong number");
else
System.out.println("It is not an Armstrong number ");
}
}
Program 8): Write a program to check for a neon number .
(A number is said to be a neon number if the sum of the digits of the number’s
square is same as the original number . )
import java.util.*;
public class neon_number
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
int nc = n*n;int d,i,sum = 0;
for(i=n ; i>0 ; i=i/10)
{
d= i%10;
sum = sum + d ;
}
if (sum == n)
System.out.println("It is a neon number");
else
System.out.println("It is not a neon number");
}
}
Program 9): Write a program to check whether a number is a prime number or
not .
import java.util.*;
public class prime_number
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
int i,c = 0;
for(i =1 ; i<=n ; i =i+1)
{
if (n%i == 0)
c =c +1 ;
}
if (c==2)
System.out.println("It is a prime number");
else
System.out.println("It is not a prime number");
}
}
Program 10 ):Write a program to input a number check whether it is a special
number or not .
( A number is a special number if the sum of the factorial of the digits of the
number is same as the original number . )
import java.util.*;
public class special_number
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a numnber ");
int n = sc.nextInt();int i,d,f = 1,j,s = 0;
for (i = n ; i>0 ; i = i/10)
{
d = i%10 ;
f= 1;
for (j =1 ; j<=d ;j=j+1)
{
f= f* j ;
}
s = s + f;
}
if (s == n)
System.out.println("It is a special number ");
else
System.out.println("It is not a special number");
}
}
Program 11): Write a program to enter a number and check whether it is a niven
number of not .
import java.util.*;
public class niven_number
{
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();int i , d , sum = 0;
for(i=n;i>0;i=i/10)
{
d= i% 10;
sum=sum+d;
}
if (n%sum == 0)
System.out.println("It is a niven number");
else
System.out.println("It is not a niven number");
}
}
Program 12): Write a program to input a number and check whether it is a
perfect number or not .
import java.util.*;
public class perfect_number
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();int i,sum = 0;
for(i = 1 ; i <n ; i++)
{
if (n%i == 0)
sum = sum + i;
}
if (sum == n)
System.out.println("It is a perfect number");
else
System.out.println("It is not a perfect number");
}
}
Program 13): Write a program to input a number of three digits and display the
greatest of three numbers
import java.util.*;
public class largest_3_digit_number
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the three numbers of three digits each");
int n1 = sc.nextInt();
int n2 = sc.nextInt();
int n3 = sc.nextInt();
if (n1>n2)
{
if (n1>n3)
System.out.println("The largest number: "+n1);
else
System.out.println("The largest number: "+n3);
}
else
{
if (n2>n3)
System.out.println("The largest three digit number: "+n2);
else
System.out.println("The largest three digit number: "+n3);
}
}
}
Program 14):Write a program to input a number the check whether it is a
Palindrome number or not .
import java.util.*;
public class palindrome_number
{
public static void main (String args [])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();int i,d,sum = 0;
for(i= n;i>0;i=i/10)
{
d = i%10;
sum = sum*10+d;
}
if (sum==n)
System.out.println("The number is Palindrome");
else
System.out.println("The number is Palindrome");
}
}
Program 15): Write a program to check whether a number is a buzz number or not .
import java.util.*;
public class buzz_number
{
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();int a = n%7 ; int b = n%10 ;
if (a==0)
System.out.println("It is a buzz number");
else if (b==7)
System.out.println("It is a buzz number");
else
System.out.println("It is not a buzz number");
}
}
Program 16): Write a program to accept a number and check whether it is a “Spy
Number” or not .
(A number is a spy number if the sum of its digits equals the product of its digits)
import java.util.*;
public class spy_number
{
public static void main (String args [])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number : ");
int n = sc.nextInt();
int i,p=1 ;
int d,sum = 0 ;
for (i=n;i>0;i= i /10)
{
d= i%10;
sum= sum + d ;
p = p*d ;
}
if (sum == p)
System.out.println("It is a spy number");
else
System.out.println("It is not a spy number");
}
}
Program 17):Write a program to display the first 10 terms of the Fibonacci series
( Example : 0,1,1,2,3,5……. )
import java.util.*;
public class fibonacci_series
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a =0 ; int b = 1 ;int i;
for (i=1 ; i<=5;i++)
{
System.out.print(a + " , ");
System.out.print(b+ " , ");
a=a+b;
b=b+a;
}
}
}
Program 18): Write a program to display the sum of the series :
1 3 5 7 19
+ + + + ………… +
2 4 6 8 20
import java.util.*;
public class fraction_series
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
double i,n,d,sum = 0.0;
for(i=1.0;i<=19.0;i++)
{
d = i+1.0;
sum = sum + i/d;
}
System.out.println("Sum of the series = "+ sum);
}
}
Program 19): Write a program to input a number and display its factorial
import java.util.*;
public class factorial_number
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
int i,a =1;
for (i=1 ; i<=n;i++)
{
a = a*i;
}
System.out.println("Factorial of a number: " + a);
}
}
Program 20): Using the switch-case statement find the area , perimeter and
diagonal of a rectangle as per the user’s choice :
The formulae :
import java.util.*;
public class rectangle
{
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1 to calculate area");
System.out.println("Enter 2 to calculate perimeter");
System.out.println("Enter 3 to calculate diagnol");
int ch = sc.nextInt();
System.out.println("Enter length and breadth");
double l = sc.nextDouble();
double b = sc.nextDouble();
switch (ch)
{
case 1:
double a = l*b ;
System.out.println("Area = "+ a);
break ;
case 2:
double p = (l+b)*2;
System.out.println(" Perimeter = "+ p);
break ;
case 3:
double d = Math.sqrt(l*l + b*b);
System.out.println(" Diagnol = " + d);
break ;
default :
System.out.println("Wrong choice !! ");
break ;
}
}
}
Program 21): Using a switch statement, write a menu driven program to:
(a) Generate and display the first 10 terms of the Fibonacci series
(b) Find the sum of the digits of an integer entered by the user.
import java.util.*;
public class case_for
{
public static void main (String args[])
{
Scanner sc =new Scanner(System.in);
System.out.println("Enter 1 to display fibonacci series ");
System.out.println("Enter 2 to find sum of all digits of the number the user
shall enter ");
int ch = sc.nextInt();int a,b,d,i=1 ;int sum = 0;
switch (ch)
{
case 1:
a=0;b=1;
for (i =1 ; i<=5; i=i+1)
{
System.out.print(a +", ");
System.out.print(b+", ");
a=a+b;
b=b+a;
}
break ;
case 2:
System.out.println("Enter a number");
int n =sc.nextInt();
for (i=n;i>0;i=i/10)
{
d=i%10;
sum=sum+d;
}
System.out.println(" Sum of the digits : "+sum);
break ;
default :
System.out.println(" wrong choice !! ");
break ;
}
}
}
Program 22): Write menu – driven program to check whether
import java.util.*;
public class case_numbers
{
public static void main (String args [])
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter 1 to check for Prime number and 2 to check for
Armstrong number");
int ch =sc.nextInt();int n ,d,i,sum = 0;
switch (ch)
{
case 1:
System.out.println("Enter a number");
n = sc.nextInt();
int a;
for (i = 1 ; i<=n ; i++)
{
a = n%i;
if (a == 0)
sum= sum+1;
}
if (sum == 2)
System.out.println("It is Prime Number");
else
System.out.println("It is not a prime number");
break ;
case 2 :
System.out.println("Enter a number");
n=sc.nextInt();
for (i = n ; i>0 ; i = i/10)
{
d = i%10;
sum = (d*d*d) + sum ;
}
if (sum == n )
System.out.println("It is Armstrong number");
else
System.out.println("It is not an Amstrong number ");
break;
default :
System.out.println("Wrong choice !! ");
}
}
}
Program 23): a library charges fine for books returned late .Following are the
fines :
import java.util.*;
public class library
{
public static void main (String args [])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of days the book is returned late :");
int n = sc.nextInt();double f=0,f1=0,f2=0,f3=0;
if (n<=5)
f = 0.40*5.0;
else if ((n>6)&&(n<=10))
{
f1 = 5.0 * 0.40;
f2 = n-5.0 * 0.65;
f= f1+f2;
}
else if (n>10)
{
f1 = 5.0 * 0.40;
f2 = 5.0 * 0.65;
f3 = n-10.0 * 0.80 ;
f = f1 + f2 + f3 ;
}
System.out.println("The number of days book returnrd late : "+n);
System.out.println("The fine to be paid = "+f);
}
}
Program 24): Write a program to calculate the tax on water consumed according
to the following conditions :
import java.util.*;
public class water_tax
{
public static void main(String args[])
{
Scanner sc =new Scanner(System.in);
System.out.println("Enter water consumed in gallons ");
double wc =sc.nextDouble(); double tx = 0 ;
if (wc <=60)
tx = 0.0;
else if ((wc >= 60)&&(wc <=120))
tx = 300.0;
else if ((wc>120)&& (wc<= 220))
tx = 500.0 ;
else if ((wc >220)&&(wc<= 350))
tx = 800.0 ;
else if (wc > 350)
tx = 1200.0 ;
System.out.println("Water consumed (in gallons) =" + wc);
System.out.println("Amount to be paid = " + tx);
}
}
Program 25): ‘Kumar electronics ‘ has announced the following seasonal
discounts on purchase of certain items
import java.util.*;
public class kumar_electronics
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter amount of purchase = ");
double a = sc.nextInt();double dl = 0.0,dd = 0.0;
if(a<=25000.0)
{
dl = 0.0;
dd = a*5.0/100.0;
}
if ((a>25000.0)&&(a<=50000.0))
{
dl = a*5.0/100.0;
dd= a*7.5/100.0;
}
if ((a>50000.0)&&(a<=100000.0))
{
dl = a*7.5/100.0;
dd = a*10.0/100.0;
}
if (a>100000)
{
dl = a*10.0/100.0;
dd = a*15.0/100.0;
}
System.out.println("Discount on Laptop = " + dl);
System.out.println("Discount on Desktop = "+ dd);
System.out.println("Amount of Laptop = "+ (a-dl));
System.out.println("Amount of Desktop = "+(a-dd));
}
}
Program 26):A pre-paid taxi charges from the passengers as per the given tariff is
given below :
Distance Rate
Upto 5 km 100 Rs.
For the next 10 km 10 Rs/km
For the next 10 km 8 Rs/km
More than 25 km 5 Rs/km
Write a program to to input the distance covered and calculate the amount to be
paid by the passenger.
import java.util.*;
public class taxi_charges
{
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter taxi number ");
int txno = sc.nextInt();
System.out.println("Enter the distance travelled");
int d = sc.nextInt();
int a = 0;
if(d<=5)
a = 100;
else if ((d>5)&&(d<=15))
a= d*10;
else if ((d>15)&&(d<=25))
a = d*8;
else if (d>25)
a = d* 5;
System.out.println("Taxi number = "+ txno);
System.out.println("Distance travelled " + d);
System.out.println("Amouint to be paid = " + a);
}
}
Program 27): A computerized ticket counter of an underground metro rail station
charges for each ride at the following rate:
Age Amount
18 or above Rs. 5
5 or above but below 18 Rs. 3
Accompanying kids below 5 NIL
Write a java application to take number of peoples of various ages as inputs . At
the end of the journey the program states the number of persons of various age
groups and total amount received as fares .
import java.util.*;
public class metro
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of passengers");
int np = sc.nextInt();int i,age,a=0,ab18 = 0,bt = 0,be5 = 0;
for(i = 1;i <= np ; i ++)
{
System.out.println("Enter age : ");
age = sc.nextInt();
if(age <= 18)
{
a=a+5;
ab18 = ab18 + 1;
}
if ((age >= 5)&&(age < 18))
{
a= a+5;
bt = bt + 1;
}
if (age<5)
be5 = be5 + 1;
}
System.out.println("Persons of age 18 years or above : " + ab18);
System.out.println("Persons of age 5 years or above but below 18 years : " +
bt);
System.out.println("Children below 5 yrs of age : "+be5);
System.out.println("Total amount collected : "+ a);
}
}
Program 28):Develop a java class to computerize the billing operation of a
telephone company :
import java.util.*;
public class call_rates
{
public static void main (String args [])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter customer's name : ");
String name = sc.nextLine();
System.out.println("Enter number of calls");
int nc = sc.nextInt();
double ac = 0;
if (nc <= 50)
ac=0;
else if (nc <= 150)
ac = 0.50 * (nc-50);
else if (nc <= 300)
ac = 0.50 * 100 + 0.80 * (nc - 150);
else if (nc > 300)
ac = 0.50 *100 + 0.80 *(nc-150) + 1.20 * (nc - 300);
double tc = ac*115/100 + 120;
System.out.println("BILL :");
System.out.println("Name : " +name);
System.out.println("Amount = "+ tc + " Rupees");
}
}
Program 29 ): SBI Home finance revised its rate of interest for Public Deposits as
follows:
(a)Amount due for the sum invested under cumulative option scheme , by using
formula A=P.(1+0.01r)^t
(b)Interest for each year , under the annual interest scheme using the formula :
I = .01 * P * R
import java.util.*;
public class SBI
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter type of interest");
System.out.println("1 for Cumulative Interest");
System.out.println("2 for Annual Interest Scheme");
int ch = sc.nextInt();double p,a,r,i,t,ti = 0;
switch (ch)
{
case 1 :
System.out.println("Enter number of years");
int n = sc.nextInt();
if ((n > 2))
{
System.out.println("Enter Principal ");
p = sc.nextDouble();r =11.5;
a = p*Math.pow((1+(0.01*r)),2.0);
System.out.println("Amount= " + a);
}
else
System.out.println("Cumulative Interest is not for 1 or 2 years ");
break ;
case 2:
System.out.println("Enter Principal and time ");
p = sc.nextDouble();t =sc.nextDouble() ;
if(t==1)
{
r=10;
for (i=1;i<=t;i++)
{
i = p*r*1/100 ;
ti = ti + i;
}
System.out.println("Wrong choice");
}
}
}
}
Program 30): A survey is conducted by a welfare association among the members
of a locality to count the number of persons in favor and against the government
policies . If the person favors the policy , he enters, 1 and if he is against , he
enters 2 and if he has no opinion then he enters 0 ,WAP in java to count the
number of persons I each category assuming the total number of persons to be 10
.
import java.util.*;
public class government_policy
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int ch,i,y=0 , n=0 , nop = 0;
System.out.println("Enter: 1 if u are in favour of it ");
System.out.println(" 2 if you are against it ");
System.out.println(" 0 if you have no opinion ");
System.out.println("Enter according to the above mentioned criteria when
asked below");
for (i = 1;i<=10;i++)
{
System.out.println("Enter your choice");
ch = sc.nextInt();
if (ch == 1)
y = y+1 ;
else if (ch==2)
n = n+1;
else if (ch == 0)
nop = nop +1;
else
System.out.println("Wrong choice");
}
System.out.println(" The number of persons in favour category: " + y);
System.out.println("The number of persons in not favour category: "+n);
System.out.println("The number of persons in can't say category: "+nop);
}
}
ACKNOWLEDGEMENT
I would like express my special thank of gratitude to my respected teacher Mr.
Asif Ismail , sir who not only taught us, how to write programs in Java
programming language but also (for our summer break) gave us a golden
opportunity to learn, practice and understand more about this programming
language by giving 30 java programs to write as homework (for the more we
practice, the more we learn).
Secondly I would also like to thank my parents and friends who gave me
emotional support thus helping me to complete my project within the limited
time frame.
I would also like to thank the authors of the following books, which helped me in
completing my project: