Vous êtes sur la page 1sur 40

Index

Topic Page No.


Program 1: Define a class called mobike to store the 3 TO 4
bikes number, to store the phone number of the
customer, to store his/her name, to store the number of
days for which the bike is taken on rent, and to calculate
and store the rental charges.
Program 2: Design a class to overload a function 5 TO 7
area(), to find the areas of a scalene triangle, trapezium,
and rhombus.
Program 3: Write a program to display twin primes, 8 TO 9
which lie between given ranges.
Program 4: Write a program to enter a sentence from the 10 TO 11
keyboard and count the number of times a particular
word occurs in it. Display the frequency of the searched
word.
Program 5: Write a program to read a positive integer 12
from the console and find out whether it is automorphic
or not.
Program 6: Accept two numbers from the user, and 13 TO 15
calculate their LCM, GCD, or remainder after division.
Program 7: Accept a number from the user, and find 16 TO 18
whether it is a prime number, an Armstrong number, or a
perfect number.
Program 8: Write a program to store 10 country names 19 TO 20
in an array, and arrange these in alphabetical order by
sorting. Print the sorted list.
Program 9: Define a class named BookFair to store the 21 TO 22
name of the book, to store the price of the book, and to
print the price after discount.
Program 10: Write a menu driven program to print the 23 TO 24
Floyds triangle and another ICSE pattern.
Program 11: Write a program to accept a word and 25 TO 26
check and print, whether the word is palindrome or only
special word.

1
Program 12: Design a class to overload a function 27 TO 29
SumSeries() to display two series.
Program 13: Write a program to input 10 numbers into 30 TO 32
an integer array and shift all the prime numbers to the
beginning of the array and composite numbers to the end
of the array without changing their order.
Program 14: Write a program to input a sentence and 33 TO 34
print the word having highest number of vowels.
Program 15: Write a program to initialize an array of 5 35 TO 36
names and initialize another array with their respective
birth year. Search for a name input by the user in the list.
If found, display Search Successful and print the name
along with the birth year, otherwise display Search
unsuccessful.
Program 16: Write a program to input any two numbers 36
and check whether they form an Amicable pair or not.
Program 17: Write a program to print the first 20 37
numbers of the Pell series.
Program 18: Write a program to input a sentence and 38
print only the initials. All initials must be in uppercase
followed by a dot(.).
Program 19: Write a program to print all the sunny 39
numbers in a given range.
Program 20: Write a program that encodes a word into 40
Piglatin.

2
PROGRAM 1:
import java.util.Scanner;
public class mobike {
intbno, phno, days, charge;
String name;
public void input() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter bike no.: ");
if(sc.hasNextInt()) {
bno = sc.nextInt();
}
else
System.out.println("Non integered value entered");
System.out.print("Enter phone no.: ");
if(sc.hasNextInt()) {
phno = sc.nextInt();
}
else
System.out.println("Non integered value entered");
sc.nextLine();
System.out.print("Enter name: ");
name = sc.nextLine();
System.out.print("Enter days: ");
if(sc.hasNextInt()) {
days = sc.nextInt();
}

3
else
System.out.println("Non integered value entered");
compute();
}
public void compute() {
if(days <= 5){
charge = 500 * days;
}else if(days <= 10){
charge = 500 * 5 + 400 * (days - 5);
}else
charge = 500 * 5 + 400 * (days - 5) + 200 * (days - 10);
}
void display() {
System.out.println("Bike no.\t Phone no.\t Name\t No. of days\t
Charge\t");
System.out.println("--------\t ---------\t ----\t -----------\t ------\t");
System.out.println(bno + "\t\t" + phno + "\t" + name + "\t\t" + days +
"\t\t" + charge);
}
public static void main(String[]args){
Scanner in = new Scanner(System.in);
mobikembk = new mobike();
mbk.input();
mbk.display();
}
}

4
PROGRAM 2:
import java.util.*;
class AREA
{
double Area(double a, double b, double c)
{
double s=(a+b+c)/2;
double area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}
double Area(inta,intb,int height)
{
double area=(height*(a+b))/2;
return area;
}
double Area(double diagonal1, double diagonal2)
{
double area=(diagonal1*diagonal2)/2;
return area;
}
public static void main(String [] args)
{
Scanner in = new Scanner (System.in);
System.out.println("To find the area of a scalene triangle-(1)");
System.out.println("To find the area of a trapezium-(2)");

5
System.out.println("To find the area of a rhombus-(3)");
System.out.print("Enter your choice:");
intch=in.nextInt();
AREA obj1 = new AREA();
AREA obj2 = new AREA();
AREA obj3 = new AREA();
if (ch==1)
{
System.out.print("Enter the length of first side:");
double a=in.nextDouble();
System.out.print("Enter the length of second side:");
double b=in.nextDouble();
System.out.print("Enter the length of third side:");
double c=in.nextDouble();
double d=obj1.Area(a,b,c);
System.out.println(d);
}
else if(ch==2)
{
System.out.print("Enter first side of trapezium:");
int a=in.nextInt();
System.out.print("Enter second side of trapezium:");
int b=in.nextInt();
System.out.print("Enter the height of trapezium:");
int height=in.nextInt();

6
double d=obj2.Area(a,b,height);
System.out.println(d);
}
else if(ch==3)
{
System.out.print("Enter first diagonal:");
double diagonal1=in.nextDouble();
System.out.print("Enter second diagonal:");
double diagonal2=in.nextDouble();
double d=obj3.Area(diagonal1,diagonal2);
System.out.println(d);
}
else
System.out.print("Invalid choice.");
}
}

7
PROGRAM 3:
import java.io.*;
class Twin_Prime {
booleanisPrime(int n) //funton for checking prime
{
int count=0;
for(inti=1; i<=n; i++)
{
if(n%i == 0)
count++;
}
if(count == 2)
return true;
else
return false;
}

public static void main(String args[]) throws IOException


{
Twin_Primeob = new Twin_Prime();
BufferedReaderbr=new BufferedReader(new
InputStreamReader(System.in));

System.out.print("Enter the lower range : ");


int p = Integer.parseInt(br.readLine());

8
System.out.print("Enter the upper range : ");
int q = Integer.parseInt(br.readLine());

if(p>q)
System.out.println("Invalid Range !");
else
{
System.out.println("nThe Twin Prime Numbers within the given
range are : ");
for(inti=p; i<=(q-2); i++)
{
if(ob.isPrime(i) == true &&ob.isPrime(i+2) == true)
{
System.out.print("("+i+","+(i+2)+") ");
}
}
}
}
}

9
PROGRAM 4:
import java.util.Scanner;
public class wordcount {
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String Str1 = sc.nextLine().toLowerCase();
System.out.print("Enter word to be searched: ");
String wd = sc.next().toLowerCase();
int lnStr1 = Str1.length();
intlnwd = wd.length();
char ch1, ch2;
intctr = 0;
String S = Str1.substring(0,lnwd);
if(S.equalsIgnoreCase(wd))
ctr = ctr + 1;
for(inti = 0; i< lnStr1 - 1; i++){
ch1 = Str1.charAt(i);
ch2 = Str1.charAt(i + 2);
if(ch1 == ' '&& ch2 != ' '){
S = Str1.substring(i + 1, i + 1 + lnwd);
if(S.equalsIgnoreCase(wd))
ctr = ctr + 1;
}
}

10
System.out.println("Searched word occurs " + ctr + "times");
}
}

11
PROGRAM 5:
import java.util.*;
class Automorphic
{
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter an integer:");
int a=in.nextInt();
intsqu=a*a;
String str1=String.valueOf(a);
intlen=str1.length();
int m=(int)Math.pow(10,len);
int rem=squ%m;
if(rem==a)
System.out.print("It is automorphic.");
else
System.out.print("It is not automorphic.");
}
}

12
PROGRAM 6:
import java.util.*;
class LCD_GCD_remainder
{
public static void main(String[]args)
{
Scanner in = new Scanner (System.in);
System.out.print("Enter a number:");
int a= in.nextInt();
System.out.print("Enter another number:");
int b= in.nextInt();
System.out.println("Find LCM...(1)");
System.out.println("Find GCD...(2)");
System.out.println("Find remainder...(3)");
System.out.print("Enter your choice:");
intch=in.nextInt();
switch (ch)
{
case 1:
{
int lcm=(a>b)?a:b;
while(true)
{
if(lcm%a==0 &&lcm%b==0)
{
System.out.print("The LCM is:"+lcm);

13
break;
}
++lcm;
}
}
break;
case 2:
{
int dividend=0,divisor=0,remainder=0;
if(a>b)
{
dividend=a;
divisor=b;
}
else
{
dividend=b;
divisor=a;
}
do
{
remainder=dividend%divisor;
if(remainder!=0)
{
dividend=divisor;
divisor=remainder;

14
}
else
break;
}while(true);
System.out.print("GCD of two numbers is:"+divisor);
}
break;
case 3:
{
intdividend,divisor,remainder;
if(a>b)
{
dividend=a;
divisor=b;
}
else
{
dividend=b;
divisor=a;
}
remainder=dividend%divisor;
System.out.print("Remainder is:"+remainder);
}
}
}
}

15
PROGRAM 7:
import java.util.*;
class prime_armstrong_perfect
{
public static void main(String []args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number:");
int a=in.nextInt();
System.out.println("To determine prime number...(1)");
System.out.println("To determine armstrong number...(2)");
System.out.println("To determine perfect number...(3)");
System.out.print("Enter your choice:");
intch=in.nextInt();
switch (ch)
{
case 1:
{
int k=0;
for(inti=2;i<a;i++)
{
if(a%i==0)
k=1;

16
if(k==1)
System.out.print("It is not a prime number");
else
System.out.print("It is a prime number.");
}
break;
case 2:
{
int sum=0;
int temp=a;
while(a!=0)
{
int r=a%10;
sum+=(r*r*r);
a=a/10;
}
if(temp==sum)
System.out.print("It is an armstrong number.");
else
System.out.print("It is not an armstrong number.");
}
break;
case 3:
{
inti=1,sum=0, newnum=a;

17
while(i<a)
{
if(a%i==0)
{
sum=sum+i;
}
i++;
}
if(sum==a)
System.out.print("It is perfect.");
else
System.out.print("It is not perfect.");
}
}
}
}

18
PROGRAM 8:
class CountryArray{
public static void main(String[]args){
String Country[] = {"INDIA", "BANGLADESH", "CHINA",
"IRAN", "USA", "LATVIA", "QATAR", "YUGOSLAVIA",
"GERMANY", "AUSTRALIA"};
String temp;
inti, j;
int ln = Country.length;
System.out.print("INPUT: ");
for(i = 0;i <ln;i++)
System.out.print(Country[i] + "\t");
for(i = 0; i<= ln - 1;i++){
for(j = i+1;j<ln;j++){
if((Country[i].compareTo(Country[j]))>0){
temp = Country[i];
Country[i] = Country[j];
Country[j] = temp;
}
}
}
System.out.println();
System.out.println("OUTPUT: ");
for(i = 0;i<ln;i++)
System.out.print(Country[i] + "\t");
}
19
}

20
PROGRAM 9:
import java.io.*;
import java.util.Scanner;
public class BookFair {
double price, discount, sp;
String name;
public void input(){
Scanner sc = new Scanner(System.in);
System.out.print("Enter book price: ");
if(sc.hasNextInt()){
price = sc.nextInt();
}else
System.out.println("Non integer value entered");
sc.nextLine();
System.out.print("Enter your name: ");
name = sc.nextLine();
compute();
}
public void compute(){
if(price<=1000){
discount = (2/100)*price;
}else if((1000<price) && (price<=3000)){
discount = (10/100)*price;
}else
discount = 15/100*price;
}

21
void display(){
System.out.print("Your name is " + name);
System.out.print("Marked price of book is " + price);
System.out.print("Selling price of book is " + sp);
sp = price - discount;
}
public static void main(String[]args)throws IOException{
BufferedReaderstdin = new BufferedReader(new
InputStreamReader(System.in));
BookFair bf = new BookFair();
bf.input();
bf.display();
}
}

22
PROGRAM 10:
import java.util.*;
class Pattern
{
public static void main(String[]args)
{
Scanner in = new Scanner (System.in);
System.out.println("To print pattern 1...(1)");
System.out.println("To print pattern 2...(2)");
System.out.print("Enter your choice:");
int a=in.nextInt();
switch(a)
{
case 1:
{
int k=1;
for(inti=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(k+" ");
k++;
}
System.out.println();
}

23
}
break;
case 2:
{
String s="ICSE";
for(inti=0;i<4;i++)
{
for(int j=0;j<=i;j++)
{
System.out.print(s.charAt(j));
}
System.out.println();
}
}
break;
}
}
}

24
PROGRAM 11:
import java.util.Scanner;
class Palindrome {
public static void main(String args[]) {
String org, rev = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
org = in.nextLine();
int length = org.length();
for ( inti = length - 1; i>= 0; i-- )
rev = rev + org.charAt(i);
if (org.equals(rev))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}

25
PROGRAM 12:
import java.util.Scanner;//package
public class Program_12__series{//class name
double S = 0.0;//variable
int f = 1;//variable
void SumSeries(int n, double x){//function
for(double i=1;i<=n;i++){//loop
double p = x/i;
if(i%2!=0)//condition
S+=p;//adding p
else
S-=p;//adding p
}
System.out.println("The answer is " + S);
}
void SumSeries(){//function
for(int j=1;j<=20;j++){//loop
f*=j;//multiplying j
S+=f;//adding f
}
System.out.println("The answer is " + S);
}
public static void main(String[]args){//main function
Scanner sc = new Scanner(System.in);//scanner class
System.out.println("1. S = (x/1) - (x/2) + (x/3) - (x/4)...(x/n)");
System.out.println("2. S = 1 + (1*2) + (1*2*3) + ... +
(1*2*3*...*20)");
26
System.out.println();//leaving a line
System.out.print("Enter your choice: ");
if(sc.hasNextInt()){//checking variable type
int opt = sc.nextInt();//input
Program_12__series qw = new Program_12__series();//object
switch (opt){//switch case
case 1:
System.out.print("Enter the value of x: ");
if(sc.hasNextDouble()){//checking variable type
double q = sc.nextDouble();//input
System.out.print("Enter the value of n: ");
if(sc.hasNextInt()){//checking variable type
int y = sc.nextInt();//input
if(y<1)//condition
System.out.println("Enter positive value");
else{
qw.SumSeries(y,q);//calling function
}
}
else
System.out.println("Enter integral value");
}
else
System.out.println("Wrong input");
break;
case 2:

27
qw.SumSeries();//calling function
break;
default:
System.out.println("Only 2 choices are provided");
}
}
else
System.out.println("Enter integral value");
}
}

28
PROGRAM 13:
import java.util.Scanner;//package
public class Program_13__Prime_Composite_Array {//class
public static booleanisPrime(int n){//function
for(inti=2;i<=(n+1)/2;i++)//loop
{
if(n%i==0)//condition
return false;
}
return true;
}
public static void main(String args[]){//main function
inti=0,left=0,right=0;//variables
intarr[] = new int[10];//variables
int composite[] = new int[10];//variables
Scanner sc = new Scanner(System.in);//scanner class
for(i=0; i<10; i++)//loop
{
System.out.print("Enter number: ");
if(sc.hasNextInt()){//checking variable type
int n = sc.nextInt();//input
if(n<2){//condition
System.out.println("wrong input");
i--;//decrement
continue;
}

29
else{
if(isPrime(n))//condition
{
arr[left] = n;
left++;//increment
}
else
{
composite[right]= n;
right++;//increment
}
}
}
else{
System.out.println("Enter an integer");
i--;//decrement
sc.next();//clearing the buffer
}
}
for (i=left,right=0;i<10;i++,right++)//loop
{
arr[i]=composite[right];
}
System.out.println("Arranged array: ");
for(i=0; i<10;i++)//loop
System.out.print(arr[i] + ",");

30
}
}

31
PROGRAM 14:
import java.util.*;
class vowels
{
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a sentence:");
String sen = in.nextLine();
String arr[]=sen.split(" ");
int count[]=new int[arr.length];
for(inti=0;i<arr.length;i++)
{
for (int j=0;j<arr[i].length();j++)
{
char ch=arr[i].charAt(j);
if((ch=='a')||(ch=='e')||(ch=='i')||(ch=='o')||(ch=='u'))
count[i]++;
}
}
int max=count[0];
int j=0;
for(inti=0;i<count.length;i++)
{
if(max<count[i])

32
{
max=count[i];
j=i;
}
}
System.out.println(arr[j]);
}
}

33
PROGRAM 15:
import java.util.Scanner;
class Search {
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
String Name[]={"Rucha","deepika", "aishwarya", "alia",
"twinkle"};
long Phone[]={ 907962186,985553234, 999560964 ,
975737446, 999807935, };
System.out.print("Enter the name whose phone no. is to be searched:
");
int f = 0, i;
String SearchName = sc.next().toUpperCase();
for(i=0;i<5;i++) {
if(SearchName.equals(Name[i].toUpperCase())==true) {
f = 1;
break;
}
}
if(f==1) {
System.out.println("Search Successful");
System.out.println("Name: " + SearchName);
System.out.println("Phone No.: " + Phone[i]);
}
else {
System.out.println("Search Unsuccessful");
System.out.println("Name not enlisted");

34
}
}
}

35
PROGRAM 16:
import java.util.Scanner;
public class Amicable {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Input the first number: ");
int num1 = in.nextInt();
System.out.print("Input the second number: ");
int num2 = in.nextInt();
int sum_num1 = 0, sum_num2 = 0;
for (inti = 1; i<= num1; i++) {
if (num1 % i == 0)
sum_num1 += i;
}
for (inti = 1; i<= num2; i++) {
if (num2 % i == 0)
sum_num2 += i;
}
if (sum_num1 == sum_num2)
System.out.println("These numbers are amicable.");
else
System.out.println("These numbers are not amicable.");
System.out.println("\n");
}
}

36
PROGRAM 17:
import java.util.*;
class pell_series
{
public static void main(String []args)
{
int a=1,b=2,c=0;
System.out.print(a+" ");
System.out.print(b+" ");

for(inti=1;i<=18;i++)
{
c=a+(b*2);
a=b;
b=c;
System.out.print(c+" ");
}
}
}

37
PROGRAM 18:
import java.io.*;
class Initials {
public static void main()throws IOException {
BufferedReaderbr=new BufferedReader(new
InputStreamReader(System.in));
String s;
char x;
int l;
System.out.print("Enter any sentence: ");
s=br.readLine();
s=" "+s;
s=s.toUpperCase();
l=s.length();
System.out.print("Output = ");
for(inti=0;i<l;i++)
{
x=s.charAt(i);
if(x==' ')
System.out.print(s.charAt(i+1)+".");
}
}
}

38
PROGRAM 19:
import java.util.*;
class pell_series
{
public static void main(String []args)
{
int a=1,b=2,c=0;
System.out.print(a+" ");
System.out.print(b+" ");

for(inti=1;i<=18;i++)
{
c=a+(b*2);
a=b;
b=c;
System.out.print(c+" ");
}
}
}

39
PROGRAM 20:
import java.io.*;
public class Piglatin {
public static void main(String[] args) throws IOException {
BufferedReaderstdin = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter word: ");
String findFirstVowel = stdin.readLine();
char v = Character.toLowerCase(findFirstVowel.charAt(0));
if (v == 'a' || v == 'e' || v == 'i' || v == 'o' || v == 'u')
{
String convertToPigLatin = findFirstVowel + "ay";
System.out.println(convertToPigLatin);
}
else
{
String first = findFirstVowel.substring(0,1);
String slice =
findFirstVowel.substring(1,findFirstVowel.length());
System.out.println(slice + first + "ay");
}
}
}

40

Vous aimerez peut-être aussi