Você está na página 1de 203

Write a program to generate a random number from 1-10:

import java.lang.*;
public class Random
{
public static void main(String args[])
{
double number = Math.random();
int finalNum = ((int) (number * 10))+1;
System.out.println(finalNum);
}
}

Output :
1.
2.
3.
4.
5.

10
1
3
6
7

Write a program to check if an inputted number is


automorphic or not:
import java.util.*;

import java.lang.*;
public class automorphic
{
public static void main (String args[])
{
System.out.println("Enter your number.");
Scanner sc = new Scanner(System.in);
int A = sc.nextInt();
int square = A*A;
String sq = String.valueOf(square);
int lengthOfSquare = sq.length();
int div = (int) Math.pow (10, lengthOfSquare);
boolean flag = false;
while(square!=0)
{
if(square==A)
{
flag = true;
break;
}
square = square%div;
div = div/10;

}
if(flag)

{
System.out.println("Automorphic");
}
else{
System.out.println("Not automorphic");
}
}
}

Output :
1. Enter your number.
15
Not automorphic
2. Enter your number.
25
Automorphic
3. Enter your number.
12
Not automorphic
4. Enter your number.
6
Automorphic
5. Enter your number.
5
Automorphic

Write a program to find the HCF and LCM of a number:


import java.util.*;
public class hcflcm
{
public int FindHCF(int a, int b)

{
int hcf = 0;
for (int i = 1; i<=a; i++)
{
if(a%i==0&&b%i==0)
{
hcf = i;
}
}
return hcf;
}
public static void main(String args[])
{
System.out.println("Enter the two numbers.");
Scanner sc = new Scanner(System.in);
int A = sc.nextInt();
int B = sc.nextInt();
int LCM;
int HCF;
int prod = A*B;
hcflcm obj = new hcflcm();
HCF = obj.FindHCF(A,B);
System.out.println("HCF = " +HCF);
LCM = prod/HCF;
System.out.println("LCM = " +LCM);

}
}

Output :
1. Enter the two
35
32
HCF = 1
LCM = 1120
2. Enter the two
23
46
HCF = 23
LCM = 46
3. Enter the two
34
12
HCF = 2
LCM = 204
4. Enter the two
3
7
HCF = 1
LCM = 21

numbers.

numbers.

numbers.

numbers.

Write a program to print the Fibonacci series of numbers


from 0 to n where n is inputted by the user:
import java.util.*;
public class Fibonacci
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of terms. This program will print


the Fibonacci series starting from zero and ending at the nth term of the series
where n is the inputted number of terms.");
int n = sc.nextInt();
int A = 0;
int B = 1;
int sum = 0;
System.out.println("The Fibonacci series for n terms where n = "+n+"
is:");
System.out.println(A);
System.out.println(B);
for(int i = 1; i<=n-2; i++)
{
sum = A+B;
A = B;
B = sum;
System.out.println(sum);

}
}
}

Output:
Enter the number of terms. This program will print the
Fibonacci series starting from zero and ending at the nth
term of the series where n is the inputted number of
terms.
10

The fibonacci series for n terms where n = 10 is:


0
1
1
2
3
5
8
13
21
34

Define a class mobike with the following description:


Instance variables/data members:
int bno to store the bikes number
int phno to store the phone number of the customer
String name to store the name of the customer
int days to store the number of days the bike is taken on rent
int charge to calculate and store the rental charge
Member methods:

void input() to input and store the details of the customer


void compute() to compute the rental charge
The rent is charged on the following basis:
First five days 500 per day
Next five days 400 per day
Rest of the days 200 per day
void display() to display the details in the following format
Bike No.
_______

Phone No.
_________

NameNo. of days Charge


______

__________

______

import java.util.*;
public class mobike
{
int days, charge;
String name, bno;
long phno;
public void input()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter the bike no.");
bno = sc.next();
System.out.println("Enter your phone no.");
phno = sc.nextLong();
System.out.println("Enter your name.");
name = sc.next();
System.out.println("Enter the no. of days rented.");
days = sc.nextInt();

}
public void compute()
{

if(days<=5)
charge = 500*days;
else if(days>5&&days<=10)
charge = (500*5)+(400*(days-5));
else
charge = (500*5)+(400*5)+(200*(days-10));
}
public 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"+phno+"\t"+name+"\t\t\t"+days+"\t\t\t"+charge+"\t");
}
public static void main(String args[])
{
mobike obj = new mobike();
obj.input();
obj.compute();
obj.display();

}
}

Output:
Enter the bike no.
KA05MB6093
Enter your phone no.
9448365432
Enter your name.
Romario
Enter the no. of days rented.
4
Bike no.

Phone no.

----------

---------

Name
--------

KA05MB6093 9448365432

No. of days Charge


-------Romario

--------4

2000

Write a program to accept the size of an array, accept the


elements of the array and print the array.
import java.util.*;
class AcceptArray
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n,i;
System.out.println("Enter the number of terms ");

n=sc.nextInt();
System.out.println("Enter the terms ");
int a[] = new int[n];
for(i=0;i<n;i++)
{
a[i] = sc.nextInt();
}
System.out.println("The array:");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}

Output:
Enter the number of terms
5
Enter the terms
3
5
5
7
1
The array:

3
5
5
7
1

Write a program to accept two arrays and to print the


sum of the corresponding elements of the arrays.
import java.util.*;
public class SumOfArrays
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a[] = new int[10];
int b[] = new int[10];
int c[] = new int[10];
int i;
System.out.println("Enter the first array.");
for(i=0;i<10;i++)
{

a[i] = sc.nextInt();
}
System.out.println("Enter the second array. ");
for(i=0;i<10;i++)
{
b[i] = sc.nextInt();
}
System.out.println("The sum of the corresponding elements of the array:");
for(i=0;i<10;i++)
{
c[i]= a[i] + b[i];
System.out.println(c[i]);
}
}
}

Output:
Enter the first array.
43
2
5
78
5
7
9

10
34
25
Enter the second array.
56
8
3
5
1
74
98
3
4
52
The sum of the corresponding elements of the array:
99
10
8
83
6
81

107
13
38
77

Write a program to find the sum of prime digits in a


number:
import java.io.*;
class PrimeDigits
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int n,n1,r,s=0;

System.out.println("Enter a number");
n=Integer.parseInt(br.readLine());
n1=n;
while(n!=0)
{
r=n%10;
if(r==1 || r==2 || r==3 || r==5 || r==7)
s=s+r;

n=n/10;
}
System.out.println("The sum of all prime digits in "+n1+" is "+s);
}}

Output:
Enter a number
328
The sum of all prime digits in 328 is 5

Write a program to accept a number and check whether it


is a unique number (a number with no repeating digits).
import java.io.*;
class UniqueNumber
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter any number : ");
int n=Integer.parseInt(br.readLine());
String s=Integer.toString(n); //converting the number into String form
int l=s.length();
int flag=0;
/* loop for checking whether there are repeated digits */
for(int i=0;i<l-1;i++)
{
for(int j=i+1;j<l;j++)
{

if(s.charAt(i)==s.charAt(j)) //if any digits match, then we know it is not a


Unique Number
{
flag=1;
break;
}
}
}
if(flag==0)
System.out.println("**** The Number is a Unique Number ****");
else
System.out.println("**** The Number is Not a Unique Number ****");
}
}

Output:
1. Enter any number : 5623
**** The Number is a Unique Number ****
2. Enter any number : 45985
**** The Number is Not a Unique Number ****
3. Enter any number : 2354
**** The Number is a Unique Number ****

Write a program to find the prime factors of a number:


import java.io.*;
class PrimeFactors
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int n;
System.out.print("Enter a Number : ");
n=Integer.parseInt(br.readLine());
System.out.print("The Prime Factors of "+n+" are : ");
int i=2;
while(n>1)
{
if(n%i == 0)
{
System.out.print(i+" ");
n=n/i;

}
else
i++;
}
}
}

Output:
1. Enter a Number : 2464
The Prime Factors of 2464 are : 2 2 2 2 2 7 11
2. Enter a Number : 346
The Prime Factors of 346 are : 2 173
3. Enter a Number : 29
The Prime Factors of 29 are : 29
4. Enter a Number : 12
The Prime Factors of 12 are : 2 2 3

Write a program to accept the sides of a triangle and print


whether it is equilateral, scalene or isosceles.
import java.util.*;
public class Triangle
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the 3 sides of the triangle.");
double a, b, c;
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();

if(a==b || b==c || c==a)


{
if(a==b && b==c && c==a)
System.out.println("Equilateral Triangle");
else
System.out.println("Isosceles Triangle");
}

else
System.out.println("Scalene Triangle");
}
}

Output:
1. Enter the 3 sides of the triangle.
5
5
5
Equilateral Triangle
2. Enter the 3 sides of the triangle.
4
7
4
Isosceles Triangle

Write a program to accept cost of 5 items and their


corresponding quantity in 2 separate arrays. Calculate
discount in another array based on the following
conditions. If cost<=100 discount is 10%, if cost is
greater than 100 and less than 200 then discount is 15%
else discount is 20%. Also calculate amount paid for each
item into another array. Print all the arrays with
appropriate headings with the final total amount.
import java.util.*;
class DiscCost
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
float cost[] = new float[5];
float qty[] = new float[5];
float dis[] = new float[5];
float tot[] = new float[5];
int i; float totamt;
totamt=0;
// To accept all the information
for(i=0;i<5;i++)
{

System.out.println("Enter cost and quantity of item " + (i+1));


cost[i] = sc.nextFloat();
qty[i] = sc.nextFloat();
}
System.out.println("Cost \t Quantity \t Discount \t Total");
for(i=0;i<5;i++)
{
if(cost[i] <= 100 )
dis[i] = 10 /100 * cost[i];
else if(cost[i] <=200 && cost[i]>=100)
dis[i] = 15 /100 * cost[i];
else
dis[i] = 20 /100 * cost[i];
tot[i] = cost[i] - dis[i];
totamt = totamt + tot[i];
System.out.println(cost[i]+"\t\t"+qty[i]+"\t\t\t"+dis[i]+"\t\t\t"+tot[i]);
}
System.out.println("Total amount : "+ totamt);
}
}

Output:
Enter cost and quantity of item 1
54
2
Enter cost and quantity of item 2

76
2
Enter cost and quantity of item 3
65
3
Enter cost and quantity of item 4
16
6
Enter cost and quantity of item 5
34
5
Cost

Quantity

Discount

Total

54.0

2.0

0.0

54.0

76.0

2.0

0.0

76.0

65.0

3.0

0.0

65.0

16.0

6.0

0.0

16.0

34.0

5.0

0.0

34.0

Total amount : 245.0

Write a program to accept ten numbers in an array and


print the highest number and its subscript.
import java.util.*;
class FindMax
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a[] = new int[10];
int i,hn,hs;
System.out.println("Enter 10 numbers");
for(i=0;i<10;i++)
a[i]=sc.nextInt();
hn=a[0];hs=0;
for(i=1;i<10;i++)
{ if(a[i] > hn)
{ hn=a[i];
hs=i;
}}
System.out.println("Highest Number is "+hn+ " and its subscript is "+hs);
}}

Output:
Enter 10 numbers

4
5
6
3
6
7
4
8
4
3
Highest Number is 8 and its subscript is 7

Write a program to print the corresponding temperature


(in C) after searching for a given city, assuming that the
searching can be stopped by typing stop.
import java.io.*;
class Temperature
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String ct;
String cty[] = {"Delhi","Mumbai","Calcutta","Bangalore","Chennai"};
double tmp[] = {40.3,45.3,42.3,30.5,48.3};
int nu,i;
boolean f = false;
while(true)
{
// To accept the city you are looking for
System.out.println("Enter the city whose temperature you want to know:");
ct = br.readLine();
if(ct.equals("stop"))
break;
// search for the number in the array
for(i=0;i<5;i++)
{
if( ct.equals(cty[i]))
{

System.out.println("The temperature is "+ tmp[i]);


f=true;
}}
if( f== false )
System.out.println("No such city stored.");
}
}}

Output:
Enter the city whose temperature you want to know:
Gurgaon
No such city stored.
Enter the city whose temperature you want to know:
Bangalore
The temperature is 30.5
Enter the city whose temperature you want to know:
Delhi
The temperature is 40.3
Enter the city whose temperature you want to know:
Mumbai
The temperature is 45.3
Enter the city whose temperature you want to know:
Stop

Write a program to perform the basic operations of a


calculator.

import java.util.*;
import java.io.*;
public class Sw
{
public static void main(String args[]) throws IOException
{
double A, B, res = 0;
char op;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first no.");
A = sc.nextDouble();
System.out.println("Enter the operator.");
op = (char) System.in.read();
System.out.println("Enter the 2nd no.");
B = sc.nextDouble();
switch(op)
{
case '+' :
{
res = A+B;
break;
}
case '-' :
{
res = A-B;
break;

}
case '*' :
{
res = A*B;
break;
}
case '/' :
{
res = A/B;
break;
}
default :
{
System.out.println("Invalid operator");
break;
}
}
System.out.println ("Answer = "+res);
}
}

Output 1:
Enter the first no.
34
Enter the operator.
*

Enter the 2nd no.


2
Answer = 68.0

Output 2:
Enter the first no.
6
Enter the operator.
Enter the 2nd no.
9
Answer = -3.0

Output 3:
Enter the first no.
12
Enter the operator.
/
Enter the 2nd no.
7
Answer = 1.7142857142857142

Write a program to accept a word and check if it is an


anagram or not.
import java.util.*;
public class anagram

{
public static void main (String args[])
{
String word1, word2;
int fin1 = 0;
char temp1, temp2;
int fin2 = 0;
int l1, l2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the 1st word.");
word1 = sc.next();
System.out.println("Enter the 2nd word.");
word2 = sc.next();
l1 = word1.length();
l2 = word2.length();
for(int i = 0; i<l1; i++)
{
temp1 = word1.charAt(i);
fin1 = fin1 + (int)(temp1);
}
for(int j = 0; j<l2; j++)
{
temp2 = word2.charAt(j);
fin2 = fin2 + (int)(temp2);
}
if(fin1==fin2)

System.out.println("ANAGRAM");
else
System.out.println("NOT AN ANAGRAM");

}
}

Output 1:
Enter the 1st word.
bare
Enter the 2nd word.
bear
ANAGRAM
Output 2:
Enter the 1st word.
happy
Enter the 2nd word.
sad
NOT AN ANAGRAM
Write a program to accept a decimal number and print its
binary code.
import java.util.*;
public class BINARYNOS

{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the decimal no.");
int A = sc.nextInt();
String S = " ";
int rem, q = 0;
while(q!=1)
{
rem = A%2;
q = A/2;
S = rem+S;
A = q;
}
S = 1 + S;
System.out.println("The binary code is: "+S);
}
}

Output 1:
Enter the decimal no.
5

The binary code is: 101


Output 2:
Enter the decimal no.
23
The binary code is: 10111
Output 3:
Enter the decimal no.
15
The binary code is: 1111

Write a menu-driven program to find the area of a circle,


square or rectangle, based on the users choice, using
function overloading.
import java.util.*;
class AreaFunctions

{
double area(double a)
{
double cir = (22/7)*a*a;
return cir;
}
double area(double b,double c)
{
double rect = b*c;
return (rect);
}
int area(int d)
{
int squ = d*d;
return squ;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
double radius, length, breadth;
int side;
double result = 0;
AreaFunctions obj = new AreaFunctions();
System.out.println("Enter 1 to find the area of a circle, 2 to find the area of a
rectangle and 3 to find the area of a square");
int choice = sc.nextInt();
switch(choice)

{
case 1:
System.out.println("Enter the radius of the circle.");
radius = sc.nextDouble();
result = obj.area(radius);
break;
case 2:
System.out.println("Enter the length and breadth of the rectangle.");
length = sc.nextDouble();
breadth = sc.nextDouble();
result = obj.area(length, breadth);
break;
case 3:
System.out.println("Enter the side of the square.");
side = sc.nextInt();
result = (double) obj.area(side);
break;
default:
System.out.println("Invalid choice.");
}
System.out.println("Result = "+result+ " square units");
}
}

Output 1:
Enter 1 to find the area of a circle, 2 to find the area of a
rectangle and 3 to find the area of a square

2
Enter the length and breadth of the rectangle.
5
3
Result = 15.0 square units
Output 2:
Enter 1 to find the area of a circle, 2 to find the area of a
rectangle and 3 to find the area of a square
1
Enter the radius of the circle.
3
Result = 28.28571429 square units

Write a program to accept a word from the keyboard and


print it in the following manner:
Inputted word : CLASS
Pattern :
CLASS
CLAS

CLA
CL
C
import java.util.*;
public class pattern1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word.");
StringBuffer A = new StringBuffer(sc.next());
int l = A.length();
char c;
System.out.println("The pattern:");
for(int i = l; i>0; i--)
{
for (int j = 0; j<i; j++)
{
c = A.charAt(j);
System.out.print(c);

}
System.out.println();
}
}

Output:
Enter a word.
COMPUTER
The pattern:
COMPUTER
COMPUTE
COMPUT
COMPU
COMP
COM
CO
C

Write a program to print the following pattern:


*

*
* * *
* * * *
* * * * *
import java.io.*;
public class pattern2
{

public static void main(String args[])


{
int m = 5;
for (int i = 1; i <= 5; i++)
{
for(int j = 1; j<=m; j++)
{
System.out.print(" ");
}
for(int k = 1; k<=i; k++)
{
System.out.print("* ");
}
--m;
System.out.println();
}
}
}

Output:
*

*
* * *
* * * *
* * * * *

Write a program to print the following pattern:


1AAAAA
22BBBB
333CCC
4444DD
55555E
public class Oth
{

public static void main(String args[])


{
int m = 5;
int p = 65;
for(int i = 1; i <=5; i++)
{
for(int j = 1; j <=i; j++)
{
System.out.print(+i);
}
for(int k = 5; k>=i; k--)
{

System.out.print((char)p);
}
p=p+1;
m = m-1;
System.out.println();
}
}
}

Output:
1AAAAA
22BBBB
333CCC

4444DD
55555E

Write a program to accept a number and print whether it


is prime or composite.
import java.io.*;
public class Prime
{
public static void main (String[] args) throws IOException{
int A;
int counter = 0;
BufferedReader br = new BufferedReader (new
InputStreamReader(System.in));
System.out.println("Enter the number");

A = Integer.parseInt(br.readLine());
for(int i = 2; i < A ; i++)
{
if(A%i == 0)
{
counter = 0;
break;
}
counter = 1;
}
if(counter > 0)
{
System.out.println("Prime Number");
}
else
{
System.out.println("Composite number");
}
}
}

Output 1:
Enter the number
56
Composite number
Output 2:

Enter the number


487
Prime Number
Output 3:
Enter the number
23
Prime Number
Output 4:
Enter the number
68
Composite number

Write a program to accept 5 numbers in an array and sort


them using the bubble sort technique:
import java.util.*;
class bubsort
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a[] = new int[5];
int i,k,t,sn,ss;
System.out.println("Enter 5 numbers :");

for(i=0;i<5;i++)
{
a[i]= sc.nextInt();
}
for(i=0;i<=3;i++)
{
for(k=0;k<=3-i;k++)
{
if(a[k] > a[k+1])
{
t=a[k];
a[k]=a[k+1];
a[k+1]=t;
}}}
System.out.println("The sorted array is :");
for(i=0;i<5;i++)
{
System.out.println(a[i]);
}
}}

Output:
57647
2354
7856
23456

1346
The sorted array is :
1346
2354
7856
23456
57647

Write a program to accept a number and find its factors:


import java.io.*;
class factors
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n,i;

System.out.println("Enter a number");
n=Integer.parseInt(br.readLine());

System.out.print("Factors of "+n+" = ");


for(i=1;i<=n;i++)
{
if(n%i==0)
System.out.print(i+" ");
}
}}

Output 1:
Enter a number
56
Factors of 56 = 1 2 4 7 8 14 28 56
Output 2:
Enter a number
243
Factors of 243 = 1 3 9 27 81 243

Write a program to accept a number and check if it is a


perfect number or not. A perfect number is a number

which is equal to the sum of its factors excluding itself.


Eg. 6 = 1 + 2 + 3
import java.io.*;
class PerfectNum
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int n,i,s=0;

System.out.println("Enter a number");
n=Integer.parseInt(br.readLine());
for(i=1;i<n;i++)
{
if(n%i==0)
s=s+i;
}
if(s==n)
System.out.print(n+" is a perfect number");
else
System.out.print(n+" is not a perfect number");
}
}

Output 1:

Enter a number
28
28 is a perfect number
Output 2:
Enter a number
36
36 is not a perfect number
Output 3:
Enter a number
496
496 is a perfect number
Output 4:
Enter a number
301
301 is not a perfect number

Write a program to print all prime numbers till n where


the value of n is inputted by the user.

import java.io.*;
class primeTillN
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int j,n,i,c=0;
System.out.println("Enter n.");
n = Integer.parseInt(br.readLine());
System.out.println("The prime numbers till " +n + " are:");
for(i=1;i<=n;i++)
{ c=0;
for(j=1;j<=n;j++)
{
if(i%j==0)
c=c+1;
}
if(c==2)
System.out.println(+i);
}
}}

Output:
Enter n.
26
The prime numbers till 26 are:

2
3
5
7
11
13
17
19
23

Write a program to accept a sentence and print the


length of each word.
import java.io.*;

class StringLength
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a sentence :");
String s=br.readLine();
char ch;
s=s+" ";
int array[]=new int[50];
int lenword=0;
int lenstr=s.length();
int j=0;

System.out.println("The string entered by you is: " +s);


for(int i=0; i<lenstr; i++)
{
ch=s.charAt(i);
if(ch!=' ')
{
lenword++;

System.out.print(ch);
}

if(ch==' ')

{
array[j]=lenword;
j++;
System.out.println(" The length of this word is " + lenword );
lenword=0;
}
}
int m=0;
}
}

Output:
Enter a sentence :
Today is a Sunday
The string entered by you is: Today is a Sunday
Today The length of this word is 5
is The length of this word is 2
a The length of this word is 1
Sunday The length of this word is 6

Write a program to accept a word and print all the vowels


in the word.
import java.util.*;
class Vowel

{
void vowel(String str)
{
int a,b;
char c;
a = str.length();
System.out.println("vowels :");
for(b=0;b<a;b++)
{
c = str.charAt(b);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||
c=='U')
{
System.out.println(c);
}
}
}
public static void main(String args[])
{
String d;
Vowel obj = new Vowel();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word :");
d = sc.nextLine();
obj.vowel(d);
}
}

Output 1:
balance
vowels :
a
a
e
Output 2:
Enter a word :
computer
vowels :
o
u
e

Write a program to accept an array and print the smallest


element and its subscript.
import java.io.*;
class prog1
{
public static void main(String args[])throws IOException

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


int a[] = new int[10];
int i, sn,ss;
System.out.println("Enter 10 numbers");
for(i=0;i<10;i++)
{
System.out.println("Enter the numbers. ");
a[i]=Integer.parseInt(br.readLine());
}
sn=a[0];ss=0;
for(i=1;i<10;i++)
{ if(a[i] < sn)
{ sn=a[i];
ss=i;
}}
System.out.println("Smallest Number is "+sn+ " and its subscript is "+ss);
}}

Output:
Enter the numbers.
3
Enter the numbers.
57
Enter the numbers.

235
Enter the numbers.
65
Enter the numbers.
45
Enter the numbers.
865
Enter the numbers.
35364
Enter the numbers.
86547
Enter the numbers.
23545
Enter the numbers.
77947
Smallest Number is 3 and its subscript is 0

Write a program to accept an array and to perform a


linear search in the array where the search term is
inputted by the user.
import java.io.*;
class linser
{
public static void main(String args[])throws IOException
{

BufferedReader br = new BufferedReader(new


InputStreamReader(System.in));
int a[] = new int[10];
int nu,i;
boolean f = false;
System.out.println("Enter 10 numbers :");
System.out.println("Enter the first number.");
a[0]= Integer.parseInt(br.readLine());
for(i=1;i<10;i++)
{
System.out.println("Enter the next number.");
a[i]= Integer.parseInt(br.readLine());
}
System.out.println("Enter the number you are looking for :");
nu = Integer.parseInt(br.readLine());
for(i=0;i<10;i++)
{
if( nu == a[i])
{
System.out.println("Number found in position "+ i);
f=true;
}}
if( f== false )
System.out.println("No such number");
}}

Output:

Enter 10 numbers :
Enter the first number.
10
Enter the next number.
17
Enter the next number.
42
Enter the next number.
75
Enter the next number.
25
Enter the next number.
86
Enter the next number.
46
Enter the next number.
92
Enter the next number.
47
Enter the next number.
29

Enter the number you are looking for :


75
Number found in position 3

Write a program to accept an array and sort it using


selection sort.
import java.io.*;
public class SelectionSort
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int a[] = new int[5];

int i,k,t,sn,ss;
// To accept the number you are looking for
System.out.println("Enter 5 numbers :");
for(i=0;i<5;i++)
{
System.out.println("Enter the next number.");
a[i]= Integer.parseInt(br.readLine());
}
//To arrange number in ascending order
for(i=0;i<=3;i++)
{
sn=a[i];
ss=i;
for(k=i+1;k<=4;k++)
{
if(a[k] < sn)
{
sn=a[k];
ss=k;
}
}
t=a[i];a[i]=a[ss];a[ss]=t;
}
//To print the array
System.out.println("The sorted array is :");
for(i=0;i<5;i++)

{
System.out.println(a[i]);
}
}}

Output:
Enter 5 numbers :
Enter the next number.
76
Enter the next number.
45
Enter the next number.
976
Enter the next number.
42
Enter the next number.
75
The sorted array is :
42
45
75
76

976

Write a program to accept 2 arrays A and B, and find


sum1 and sum2 where sum1 is the sum of elements of
odd subscript in array A + sum of elements of even
subscript in array B and sum2 is the sum of elements of
even subscript in array A + sum of elements of odd
subscript in array B.
import java.io.*;
public class OddEvenSubScript
{
public static void main(String args[])throws IOException

{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int a[] = new int[6];
int b[] = new int[6];
int i,sum1,sum2;
sum1=sum2=0;
System.out.println("First array: ");
// to accept 6 numbers for the first array
for(i=0;i<6;i++)
{
System.out.println("Enter a number ");
a[i] = Integer.parseInt(br.readLine());
}
System.out.println("Second array: ");

// to accept 6 numbers for the second array


for(i=0;i<6;i++)
{
System.out.println("Enter a number ");
b[i] = Integer.parseInt(br.readLine());
}
for(i=0;i<6;i+=2)
{
sum1 += a[i] + b[i+1];
sum2 += b[i] + a[i+1];
}

System.out.println(" sum1 = "+ sum1);


System.out.println(" sum2 = "+ sum2);
}}

Output:
First array:
Enter a number
5
Enter a number
7
Enter a number
3
Enter a number
9
Enter a number
12
Enter a number
7
Second array:
Enter a number
4
Enter a number

8
Enter a number
9
Enter a number
9
Enter a number
5
Enter a number
3
sum1 = 40
sum2 = 41

Write a program to accept a number and print its


factorial.
import java.util.*;
public class factorial
{
public static void main(String args[])
{
int n, i;
long fact = 1;
System.out.println("Enter an integer to calculate its factorial");
Scanner in = new Scanner(System.in);

n = in.nextInt();
{
for ( i = 1 ; i <= n ; i++ )
fact = fact*i;
System.out.println("Factorial of "+n+" is = "+fact);
}
}
}
Output 1:
Enter an integer to calculate its factorial
4
Factorial of 4 is = 24
Output 2:
Enter an integer to calculate its factorial
6
Factorial of 6 is = 720

Write a program to generate the following pattern:


1
1
1
1
1
1
1
1

2
2
2
2
2
2

1
3
3
3
3
1

2
4
4
2

1
3 2 1
3 2 1
1

public class Pattern5


{
public static void main(String args[])
{
int a,b,c,d,p,k;
k = 4;
for(a=1;a<=4;a++)
{
System.out.print(" ");
for(b=1;b<=a;b++)
System.out.print(" "+b+" ");

for(b=a-1;b>=1;b--)
{
System.out.print(" "+b+" ");
}
System.out.println();
k = k-1;
}
for(a=4;a>=1;a--)
{
System.out.print(" ");
for(b=1;b<=a;b++)
System.out.print(" "+b+" ");
for(b=a-1;b>=1;b--)
{
System.out.print(" "+b+" ");
}
System.out.println();
k = k+1;
}
}
}

Output:
1
1
1
1
1
1
1
1

2
2
2
2
2
2

1
3
3
3
3
1

2
4
4
2

1
3 2 1
3 2 1
1

Write a program to accept a binary number and print the corresponding


decimal number.
import java.lang.*;
import java.io.*;
public class BinaryToDecimal {
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter a Binary number");
String str=br.readLine();
long num=Long.parseLong(str);
long rem;
while(num>0){
rem=num%10;
num=num/10;
if(rem!=0&&rem!=1){
System.out.println("This is not a binary number,Please enter again");

}
}
int i=Integer.parseInt(str,2);
System.out.println("Decimal:"+i);
}
}

Output 1:
Enter a Binary number
1100011
Decimal:99
Output 2:
Enter a Binary number
10
Decimal:2
Output 3:
Enter a Binary number
111111
Decimal:63

Write a program to accept a word from the


keyboard and print its Pig Latin form.
import java.io.*;
public class Piglatin
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String s;char ch;
int i;
System.out.println("Enter a word");
s = br.readLine();
s = s.toUpperCase();
for(i=0;i<s.length();i++)
{
ch=s.charAt(i);
if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
break;

}
System.out.println(s.substring(i)+s.substring(0,i)+"AY");
}}

Output 1:
Enter a word
computer
OMPUTERCAY
Output 2:
Enter a word
London
ONDONLAY

Write a program to accept a sentence and print each


word on a separate line.
import java.io.*;
public class Sentence
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s,w,s1;
int sv,i;
System.out.println("Enter a sentence");
s = br.readLine();
s=s+" ";
sv=0;
i=s.indexOf(' ');
while(i!=-1)
{
w = s.substring(sv,i);
System.out.println(w);
sv=i+1;

i=s.indexOf(' ',sv);
}
}}

Output:
Enter a sentence
London bridge is falling down.
London
bridge
is
falling
down.

Write a program to accept a sentence and reverse it by


reversing each word of the sentence but keeping the
order of words the same.
import java.io.*;
public class ReverseSentence
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String s,w,s1;
int sv,i,l,k;
char ch;
s1="";
System.out.println("Enter a String");
s = br.readLine();
s=s+" ";
sv=0;
i=s.indexOf(' ');
while(i!=-1)
{
w = s.substring(sv,i);
l=w.length();

for(k=l-1;k>=0;k--)
{ ch=w.charAt(k);
s1=s1+ch;
}
s1=s1+" ";
sv=i+1;
i=s.indexOf(' ',sv);
}
System.out.println(s1);
}}

Output:
Enter a String
the quick brown fox jumps over the lazy dog
eht kciuq nworb xof spmuj revo eht yzal god

Write a program to accept a String and search for a word


in the String and print how many times it appears in the
String.
import java.io.*;
class SentenceSearch
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String s,w,s1,wd;
int sv,i,c;
System.out.println("Enter a String");
s = br.readLine();
System.out.println("Enter the word to be searched.");
w = br.readLine();
s=s+" ";
sv=0;c=0;
i=s.indexOf(' ');
while(i!=-1)
{
wd = s.substring(sv,i);
if(wd.equalsIgnoreCase(w))

c=c+1;
sv=i+1;
i=s.indexOf(' ',sv);
}
System.out.println("Number of times " + w +" occurs is "+c);
}
}

Output:
Enter a String
Jack and Jill went up the hill to fetch a pail of water. Jack
came down and broke his crown and Jill came tumbling
after.
Enter the word to be searched.
Jack
Number of times Jack occurs is 2

Write a program to accept a number and print whether it


is an Armstrong number or not.
import java.io.*;
public class Armstrong
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int n,n1,r,s=0;

System.out.println("Enter a number");
n=Integer.parseInt(br.readLine());
n1=n;
while(n!=0)
{
r=n%10;
s=s+r*r*r;
n=n/10;
}
if(n1==s)
System.out.println("It is an Armstrong number");
else
System.out.println("It is not an Armstrong number");
}

Output 1:
Enter a number
153
It is an Armstrong number
Output 2:
Enter a number
345
It is not an Armstrong number
Output 3:
Enter a number
371
It is an Armstrong number

Write a program to accept a number and print the reverse


of the number digit by digit.
import java.io.*;
public class Reverse1
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int n,n1,r,s=0;

System.out.println("Enter a number");
n=Integer.parseInt(br.readLine());
n1=n;
while(n!=0)
{
r=n%10;
System.out.print(r);
n=n/10;
}
}}

Output:
Enter a number
4587
7854

Write a program to accept a number and print its reverse


by storing the value of the reverse in a variable and
printing it.
import java.io.*;
public class Reverse2
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int n,n1,r,s=0;
System.out.println("Enter a number");
n=Integer.parseInt(br.readLine());
n1=n;
while(n!=0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
System.out.print("The reverse of "+n1+" is "+s);
}}

Output:
Enter a number
3562
The reverse of 3562 is 2653

Write a program to accept a number and print the sum of


its digits.
import java.io.*;
public class SumOfDigits
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int n,n1,r,s=0;

System.out.println("Enter a number");
n=Integer.parseInt(br.readLine());
n1=n;
while(n!=0)
{
r=n%10;
s=s+r;
n=n/10;
}
System.out.println("The sum of all digits in "+n1+" is "+s);
}}

Output:
Enter a number
654

The sum of all digits in 654 is 15

Write a program to accept the name, address,


monthly salary, subject specialisation and phone
number of a teacher. If the teachers annual salary
is more than 175000, a tax of 5% is charged on the
annual salary.
import java.io.*;
public class teacherdata
{
public static void main(String args[])throws IOException
{
BufferedReader keyin=new BufferedReader (new
InputStreamReader(System.in));
String instr;
String name;
String add;
double monthsalary;
double asalary;
double tax;
String subspec;
long phone;
System.out.println("Enter the name of the teacher : ");
instr=keyin.readLine();
name=instr;
System.out.println("Enter the address of the teacher : ");
instr=keyin.readLine();

add=instr;
System.out.println("Enter the monthly salary : ");
instr=keyin.readLine();
monthsalary=Double.parseDouble (instr);
System.out.println("Enter the subject specialization of the teacher : ");
instr=keyin.readLine();
subspec=instr;
System.out.println("Enter the teacher's phone number : ");
instr=keyin.readLine();
phone=Long.parseLong (instr);
asalary=monthsalary*12;
if(asalary>175000)
{
tax=asalary*5/100;
}
else
{
tax=0;
}
System.out.println("Name = "+name);
System.out.println("Address = "+add);
System.out.println("Phone Number = "+phone);
System.out.println("Subject Specialisiation = "+subspec);
System.out.println("Monthly Salary = "+monthsalary);
System.out.println("Tax = "+tax);
}

Output:
McKenzie
Enter the address of the teacher :
#2, Victoria Layout, Bangalore - 560004
Enter the monthly salary :
16000
Enter the subject specialization of the teacher :
Geography
Enter the teacher's phone number :
9538645691
Name = McKenzie
Address = #2, Victoria Layout, Bangalore - 560004
Phone Number = 9538645691
Subject Specialisiation = Geography
Monthly Salary = 16000.0
Tax = 9600.0

Write a program to accept the names of five cities and


arrange them in alphabetical order.
import java.io.*;
public class alphabetical
{
public static void main(String args[])throws IOException
{
BufferedReader keyin=new BufferedReader (new
InputStreamReader(System.in));
String instr;
String name[]=new String[5];
int i=0;
int j=0;
String temp;
for (i=0;i<5;i++)
{
System.out.println("Enter the name of a city : ");
instr=keyin.readLine();
name[i]=instr;
}
for (i=0;i<5;i++)
{
for (j=i+1;j<5;j++)
{
if(name[i].compareTo(name[j])>0)
{

temp=name[j];
name[j]=name[i];
name[i]=temp;
}
}
}
System.out.println("THE NAMES OF THE CITIES IN ALPHABETICAL
ORDER ARE : ");
for (i=0;i<5;i++)
{
System.out.println(name[i]);
}
}
}

Output:
Enter the name of a city :
Paris
Enter the name of a city :
Tokyo
Enter the name of a city :
New York
Enter the name of a city :
Berlin
Enter the name of a city :

Canberra
THE NAMES OF THE CITIES IN ALPHABETICAL ORDER
ARE :
Berlin
Canberra
New York
Paris
Tokyo

Write a program to accept a sentence, reverse the case of


the letters and display the new sentence.
import java.io.*;
public class ReverseCase
{
public static void main(String args[])throws IOException
{
BufferedReader keyin=new BufferedReader (new InputStreamReader(System.in));
String instr;
String s;
String s1;
char c;
int l;
int i=0;
System.out.println("Enter the sentence : ");
instr=keyin.readLine();
s=instr;
l=s.length();
s1="";
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(Character.isUpperCase(c))
{
s1=s1+Character.toLowerCase(c);
}

else if(Character.isLowerCase(c))
{
s1=s1+Character.toUpperCase(c);
}
else
{
s1=s1+s.charAt(i);
}
}
s=s1;
System.out.println("THE SENTENCE IN REVERSE CASE IS : ");
System.out.println(s);
}
}

Output:
Enter the sentence :
Happy Birthday to YOU!
THE SENTENCE IN REVERSE CASE IS :
hAPPY bIRTHDAY TO you!

Write a program to find the sum of the series: x/2 + x/5 +


x/8. x/20 where x is inputted by the user.

import java.io.*;
public class sumofseries
{
public static void main(String args[])throws IOException
{
BufferedReader keyin=new BufferedReader (new InputStreamReader(System.in));
String instr;
int x=0;
int i=0;
double sum=0;
System.out.println("Enter the value of x : ");
instr=keyin.readLine();
x=Integer.parseInt (instr);
for(i=2;i<=20;i=i+3)
{
sum=sum+(double)x/i;
}
System.out.println("The sum of the series = "+sum);
}
}

Output:
Enter the value of x :
5
The sum of the series = 5.480805958747134

Write a program to accept the accession number, title


and author of the book borrowed, number of days late
that the book was returned to the library and the fine

charged for the late return where


No. of days late.

fine = 40 *

import java.io.*;
public class bookfine
{
public static void main(String[] args) throws IOException
{
BufferedReader keyin=new BufferedReader (new InputStreamReader(System.in));
int acc=0;
String instr;
String title;
String author;
int fine=0;
System.out.println("Enter the accession number ");
instr=keyin.readLine();
acc=Integer.parseInt(instr);
System.out.println("Enter the number of days ");
instr=keyin.readLine();
days=Integer.parseInt(instr);
System.out.println("Enter the title of the book ");
instr=keyin.readLine();
title=instr;
System.out.println("Enter the author of the book ");
instr=keyin.readLine();
author =instr;
fine=50*days;

System.out.println("\nFine charged = "+fine);


System.out.println("Accession No. "+'\t'+" Title "+'\t'+" Author");
System.out.println(acc +"\t\t"+ title +"\t"+ author);
}
}

Output:
Enter the accession number
34143345
Enter the number of days
2
Enter the title of the book
Cell
Enter the author of the book
Stephen King

Fine charged = 100


Accession No.
34143345

Title Author
Cell

Stephen King

Write a program to store ten cities and their STD codes.


Allow the user to search for the STD code of a city or the
city name of a STD code.

import java.util.*;
public class citycode
{
public void acceptCode(int code[], int count, String cities[], int j)
{
Scanner sc2 = new Scanner(System.in);
System.out.println("Enter the STD code.");
int searchCode = sc2.nextInt();
for(j=0;j<10;j++)
{
if(code[j]==searchCode)
{
count++;
System.out.println("The city with that STD code is : ");
System.out.println(cities[j]);
}
}
if(count==0)
{
System.out.println("The given code has no corresponding city ");
}
}
public void acceptCity(int codes2[], int count, String cities2[], int k)
{
Scanner sc3 = new Scanner (System.in);
System.out.println("Enter the name of the city you are searching for : ");

String searchCity=sc3.next();

//to check the compare the name with the other 10 names
for(k=0;k<10;k++)
{
if(searchCity.equalsIgnoreCase(cities2[k]))
{
count++;
System.out.println("The name of the entered city and its STD code
are : ");
System.out.println(cities2[k]+" - "+codes2[k]);
}
}
if(count==0)
{
System.out.println("The name of the entered city is not in the list ");
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int code[]=new int[10];//to store the STD code of the cities
String city[]=new String[10];//to store the name of the 10 cities
int i=0;//counter
int c=0;//counter
String cityname;//to store the name of the city to be found
String instr;

for(i=0;i<10;i++)
{
System.out.println("Enter the name of the city in postion "+(i+1)+":");
instr=sc.next();
city[i]=instr;
}
//to store the STD codes of 10 cities
for(i=0;i<10;i++)
{
System.out.println("Enter the STD code of "+(city[i])+":");
instr=sc.next();
code[i]=Integer.parseInt(instr);
}
System.out.println("Enter 1 to find the STD code of a city and 2 to find the city for
an inputted STD code.");
int choice = sc.nextInt();
citycode obj = new citycode();
switch(choice)
{
case 1:
obj.acceptCity(code, c, city, i);
break;
case 2:
obj.acceptCode(code, c, city, i );

break;

default:
System.out.println("Invalid choice");
}
}
}

Output:
Enter the name of the city in postion 1:
Agra
Enter the name of the city in postion 2:
Bangalore
Enter the name of the city in postion 3:
Mumbai
Enter the name of the city in postion 4:
Chennai
Enter the name of the city in postion 5:
Kolkata
Enter the name of the city in postion 6:
Delhi
Enter the name of the city in postion 7:
Mysore
Enter the name of the city in postion 8:
Pune

Enter the name of the city in postion 9:


Bhopal
Enter the name of the city in postion 10:
Srinagar
Enter the STD code of Agra:
562
Enter the STD code of Bangalore:
80
Enter the STD code of Mumbai:
22
Enter the STD code of Chennai:
44
Enter the STD code of Kolkata:
33
Enter the STD code of Delhi:
11
Enter the STD code of Mysore:
821
Enter the STD code of Pune:
20
Enter the STD code of Bhopal:

755
Enter the STD code of Srinagar:
194
Enter 1 to find the STD code of a city and 2 to find the
city for an inputted STD code.
2
Enter the STD code.
44
The city with that STD code is :
Chennai

Write a java program to convert hours to days.


import java.util.*;
public class convert
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int hours;
System.out.print("Enter the hours to convert:");

hours =input.nextInt();
int d=hours/24;
int m=hours%24;
System.out.println(d+"days"+" "+m+"hours");
}
}

Output 1:
Enter the hours to convert:45
1days 21hours
Output 2:
Enter the hours to convert:567
23days 15hours

Write a program to perform the basic trigonometric


operations sin, cos and tan on an inputted angle.
import java.lang.*;
import java.util.*;
public class trig
{
public static void main (String args[])
{
double inp,rad, res = 0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter 1 to find sin, 2 for cos and 3 for tan");


int choice = sc.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter the angle in degrees.");
inp = sc.nextDouble();
rad = Math.toRadians(inp);
res = Math.sin(rad);
break;
case 2:
System.out.println("Enter the angle in degrees.");
inp = sc.nextDouble();
rad = Math.toRadians(inp);
res = Math.cos(rad);
break;
case 3:
System.out.println("Enter the angle in degrees.");
inp = sc.nextDouble();
rad = Math.toRadians(inp);
res = Math.tan(rad);
break;
default:
System.out.println("Invalid choice.");
}
System.out.println("The result is " +res);

}
}

Output 1:
Enter 1 to find sin, 2 for cos and 3 for tan
1
Enter the angle in degrees.
0
The result is 0.0
Output 2:
Enter 1 to find sin, 2 for cos and 3 for tan
3
Enter the angle in degrees.
25
The result is 0.4663076581549986

Write a program to convert Celsius to Fahrenheit and vice


versa.
import java.io.*;
public class celfar
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader (new
InputStreamReader(System.in));
String instr;
double f=0.0;
double c=0.0;

int choice;
System.out.println("Enter 1 for fahrenheit to celsius or 2 for celsius to
fahrenheit : ");
instr=br.readLine();
choice=Integer.parseInt(instr);
switch (choice)
{
case 1:
System.out.println("Enter the temerature in fahrenheit : ");
instr=br.readLine();
f=Double.parseDouble(instr);
f=f-32;
c=0.55*f;
System.out.println("The temperature in celsius is "+c);
break;
case 2:
System.out.println("Enter the temerature in celsius : ");
instr=br.readLine();
c=Double.parseDouble(instr);
f=(1.8*c)+32;
System.out.println("The temperature in fahrenheit is "+f);
break;
default:
System.out.println("Invalid input");
}
}
}

Output 1:
Enter 1 for fahrenheit to celsius or 2 for celsius to fahrenheit :
2
Enter the temerature in celsius :
34
The temperature in fahrenheit is 93.2
Output 2:
Enter 1 for fahrenheit to celsius or 2 for celsius to fahrenheit :
1
Enter the temerature in fahrenheit :
86
The temperature in celsius is 29.700000000000003

Write a menu-driven program to find the area of a cube,


sphere or a cuboid depending on the users choice.
import java.io.*;
public class volume
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));

double s=0;
double vol;
double r=0;

double h=0;
double l=0;
double b=0;
int choice;
System.out.println("Enter 1 for volume of a cube , 2 for volume of a sphere and 3
for the volume of a cuboid : ");
choice=Integer.parseInt(br.readLine());
if(choice==1)
{
System.out.println("Enter the side of the cube : ");
s=Integer.parseInt(br.readLine());
vol=s*s*s;
System.out.println("The volume of the cube = "+vol);
}
else if(choice==2)
{

System.out.println("Enter the radius of the sphere : ");

r=Integer.parseInt(br.readLine());
vol=4/3*3.14*r*r*r;
System.out.println("The volume of the sphere = "+vol);
}
else if(choice==3)
{
System.out.println("Enter the length of the cuboid : ");
l=Integer.parseInt(br.readLine());

System.out.println("Enter the breadth of the cuboid : ");

b=Integer.parseInt(br.readLine());
System.out.println("Enter the height of the cuboid : ");
h=Integer.parseInt(br.readLine());
vol=l*b*h;
System.out.println("The volume of the cuboid = "+vol);
}
else
System.out.println("Invalid input");
}
}

Output 1:
Enter 1 for volume of a cube , 2 for volume of a sphere
and 3 for the volume of a cuboid :
3
Enter the length of the cuboid :
5
Enter the breadth of the cuboid :
4
Enter the height of the cuboid :
2
The volume of the cuboid = 40.0

Output 2:
Enter 1 for volume of a cube , 2 for volume of a sphere
and 3 for the volume of a cuboid :
2
Enter the radius of the sphere :
1
The volume of the sphere = 3.14

Write a program to check if a number is a buzz number or


not and to find the GCD of two numbers based on the
users choice.
import java.io.*;
public class buzzgcd
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));

int choice=0;
int a=0;
int b=0;
int i=0;
int num=0;

System.out.println("Enter 1 to check for BUZZ number or 2 to find the GCD of the


numbers : ");
choice=Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
System.out.println("Enter the number : ");

num=Integer.parseInt(br.readLine());
if(num%7==0||num%10==7)
System.out.println(num+" is a BUZZ number . ");
else
System.out.println(num+" is a not BUZZ number .");
break;
case 2:
System.out.println("Enter the first number : ");

a=Integer.parseInt(br.readLine());
System.out.println("Enter the second number : ");

b=Integer.parseInt(br.readLine());
for(i=(a<b?a:b);i>=1;i--)
{
if(a%i==0&&b%i==0)
break;
}
System.out.println("The GCD of "+a+" and "+b+" is : ");

System.out.println(i);
break;
default:
System.out.println("Invalid choice.");
}
}
}

Output 1:
Enter 1 to check for BUZZ number or 2 to find the GCD of
the numbers :
1
Enter the number :
14
14 is a BUZZ number .
Output 2:
Enter 1 to check for BUZZ number or 2 to find the GCD of
the numbers :
2
Enter the first number :
85
Enter the second number :
34
The GCD of 85 and 34 is :

17
Output 3:
Enter 1 to check for BUZZ number or 2 to find the GCD of
the numbers :
5
Invalid choice.

An electronics shop has announced the following seasonal


discounts on the purchase of certain items:
Purchase Amount Discount on
Discount on
in rupees
Laptops
Desktop PC
0-25000
0%
5%
25001-57000
5%
7.5%
57001-100000
7.5%
10%
More than 100000 10%
15%
Write a program based on the above criteria to input
name, address, amount of purchase and the type of
purchase (Laptop or desktop) by a customer. Compute
and print the net amount along with the other details of
the customer.
import java.io.*;
public class lapdesk

{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader (new
InputStreamReader(System.in));
String name;
String add;
double amt;
double namt=0;//to store the discounted amount
char s;//to store the users choice
System.out.println("Enter the name of the customer : ");
name=br.readLine();
System.out.println("Enter the address of the customer : ");
add=br.readLine();
System.out.println("Enter amount of purchase : ");
amt=Double.parseDouble (br.readLine());
System.out.println("Enter L for laptops and D for desktops
: ");
s=(char)br.read();
s=Character.toUpperCase(s);

if(s=='L')
{
if(amt>0&&amt<=25000)
namt=amt;
else if(amt>25000&&amt<=57000)
namt=amt*95/100;

else if(amt>57000&&amt<=100000)
namt=amt*92.5/100;
else
namt=amt*90/100;
}
else if (s=='D')
{
if(amt>0&&amt<=25000)
namt=amt*95/100;
else if(amt>25000&&amt<=57000)
namt=amt*92.5/100;
else if(amt>57000&&amt<=100000)
namt=amt*90/100;
else
namt=amt*85/100;
}
else
{
System.out.println("Invalid input");
}
System.out.println("The name of the customer is "+name);
System.out.println("The customer's address is "+add);
System.out.println("Net amount to be paid = "+namt);
}
}

Output:

Enter the name of the customer :


Hannah
Enter the address of the customer :
#23, Perhandores Layout, Pune.
Enter amount of purchase :
64000
Enter L for laptops and D for desktops :
L
The name of the customer is Hannah
The customer's address is #23, Perhandores Layout, Pune.
Net amount to be paid = 59200.0

Write a program to accept a word and print the frequency


of each character in the word.
import java.io.*;
public class characterfrequency
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
String s;
int arr[]=new int[26];
int i=0;
int l=0;
System.out.println("Enter the string : ");

s=br.readLine();
s=s.toUpperCase();
for(i=0;i<26;i++)
{
arr[i]=0;
}
l=s.length();
for(i=0;i<l;i++)
{
if(s.charAt(i)>=65&&s.charAt(i)<=90)
{
arr[s.charAt(i)-65]++;
}
}
System.out.println("Characters\tFrequency");
for(i=0;i<26;i++)
{
System.out.println((char)(i+65)+"\t"+arr[i]);
}
}
}

Output:
Enter the string :
alphabetical
Characters Frequency

A 3
B 1
C 1
D 0
E 1
F 0
G 0
H 1
I 1
J 0
K 0
L 2
M 0
N 0
O 0
P 1
Q 0
R 0
S 0
T 1
U 0

V 0
W 0
X 0
Y 0
Z 0

Write a program to find the sum of all even digits in a


number.
import java.io.*;
class evenDigits
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int n,n1,r,s=0;
System.out.println("Enter a number");
n=Integer.parseInt(br.readLine());
n1=n;
while(n!=0)
{
r=n%10;

if(r%2==0)
s=s+r;
n=n/10;
}
System.out.println("The sum of all even digits in "+n1+" is "+s);
}
}

Output:
Enter a number
53456765
The sum of all even digits in 53456765 is 16

Write a program to find the sum of all odd digits in a


number.
import java.io.*;
class oddDigits
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int n,n1,r,s=0;
System.out.println("Enter a number");
n=Integer.parseInt(br.readLine());
n1=n;
while(n!=0)
{

r=n%10;
if(r%2!=0)
s=s+r;
n=n/10;
}
System.out.println("The sum of all odd digits in "+n1+" is "+s);
}
}

Output:
Enter a number
6853
The sum of all odd digits in 6853 is 8

Write a program to print the following pattern:

class SpecialPattern

a
aa
aaa
aaaa
aaaaa
aaaa
aaa
aa
a

{
public static void main(String args[])
{
int i,j,k,m;

m=5;
for(i=1;i<=5;i++)
{

for(j=1;j<=m;j++)
System.out.print(" ");
for(k=1;k<=i;k++)
System.out.print("a ");
System.out.println();
m=m-1;
}
m=2;
for(i=4;i>=1;i--)
{
for(j=1;j<=m;j++)
System.out.print(" ");
for(k=1;k<=i;k++)
System.out.print("a ");
System.out.println();
m=m+1;
}
}
}

Output:

a
aa
aaa
aaaa
aaaaa
aaaa
aaa
aa
a

Write a program to generate the following pattern:


1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
For n terms where n is inputted by the user.
import java.util.*;
public class pattern7
{
public static void main(String args[])
{
System.out.println("Enter n.");

Scanner sc = new Scanner(System.in);


int num = sc.nextInt();
for(int i=1;i<=num;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(" "+i+" ");
}
System.out.print("\n");
}
}
} // closing the class

Output:
Enter n.
9
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8

9 9 9 9 9 9 9 9 9

Write a program to accept a string and encode it by


replacing each letter with another letter at a fixed
distance d ahead of the letter where the letters wrap
around, that is, A follows Z.
import java.io.*;
class st1
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String s,s1;
int d,av;
s1="";
System.out.println("Enter a string");
s=br.readLine();

System.out.println("Enter value for d");


d=Integer.parseInt(br.readLine());

for(int i=0;i<s.length();i++)
{
av=s.charAt(i);
if(av>=65 && av<=90)
{ av=av+d;
if(av>90)
{
av=(av-90)+64;
}
}
else if(av>=97 && av<=122)
{ av=av+d;
if(av>122)
{
av=(av-122)+96;
}
}
s1=s1+(char)av;
}
System.out.println("Encoded string is "+s1);
}}

Output:
Enter a string

parrot
Enter value for d
3
Encoded string is sduurw

Write a program to accept the name of a person and print


the first and middle names as initials and the last name
as it is.
Example: Input- Ronald Billius Weasly Output
R.B.Weasly
import java.io.*;
class initials
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String s;
int i1,i2;
System.out.println("Enter a name which comprises of first name,
middle name ,last name ");
s = br.readLine();
i1 = s.indexOf(' ');

i2= s.indexOf(' ',i1+1); // i2=s.lastIndexOf(' ');


System.out.println(s.charAt(0)+"."+ s.charAt(i1+1)+"."+s.substring(i2+1));
}}

Output:
Enter a name which comprises of first name, middle
name ,last name
Austin Capria Timmons
A.C.Timmons

Write a program to print the following pattern:


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
public class pattern9
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)

{
System.out.print(" "+j+" ");
}
System.out.print("\n");
}
}
}

Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Write a program to print the following pattern:


*
* *
* * *
* * * *
* * * * *
public class pattern3
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)

{
System.out.print(" * ");
} System.out.print("\n");
}
}
}
Output:
*
* *
* * *
* * * *
* * * * *

An ISBN code is legal if it is a ten-digit number and the


sum of the 1st digit*1, 2nd digit*2, 3rd digit*310th
digit*10, is divisible by 11. Write a program to accept an
ISBN code and print whether it is legal or not.
import java.io.*;
public class isbn
{
public static void main(String args[])throws IOException
{
String a;
double s=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the ISBN code");
a=br.readLine();
if(a.length()!=10)
{
System.out.println("illegal ISBN");
System.exit(0);
}

else
for(int i=0;i<10;i++)
s=s+Integer.valueOf(a.charAt(i)+"")*(i+1);
System.out.println("SUM="+(int)s);
if(s%11==0)
System.out.println("legal ISBN");
else
System.out.println("illegal ISBN");
}
}

Output 1:
enter the ISBN code
1539684732
SUM=265
illegal ISBN
Output 2:
enter the ISBN code
1401601499
SUM=253
legal ISBN

Write a program to accept a word and print it in the


following manner:
Input-BOOK
OutputB
BO
BOO
BOOK
import java.util.*;
class pattern10
{
public static void main(String args[])
{
System.out.println("Enter a word.");
Scanner sc = new Scanner(System.in);
String s= sc.next();
String s1;
int l,i,sp,l1;

l=s.length();
s1="";
System.out.println("Pattern:");
for(i=1;i<=l;i++)
{
System.out.println(s.substring(0,i));
}
}
}

Output:
Enter a word.
CALL
Pattern:
C
CA
CAL
CALL

Write a program to accept a word and print it in the


following manner:
Input: care
Output:

e
er
era
erac
import java.util.*;
class pattern10
{
public static void main(String args[])
{
System.out.println("Enter a word.");
Scanner sc = new Scanner(System.in);
String s= sc.next();
String s1;
int l,i,sp,l1;
l=s.length();
s1="";

System.out.println("Pattern:");

for(i=l-1;i>=0;i--)
{
s1=s1+s.charAt(i);
System.out.println(s1);
}

}
}

Output:
Enter a word.
receipt
Pattern:
t
tp
tpi
tpie
tpiec
tpiece
tpiecer

Write a program to accept a word and print it diagonally.


import java.util.*;
class pattern10
{
public static void main(String args[])
{
System.out.println("Enter a word.");
Scanner sc = new Scanner(System.in);
String s= sc.next();
String s1;
int l,i,sp,l1;
l=s.length();
s1="";

System.out.println("Pattern:");
sp=0;
for(i=0;i<l;i++)
{
for(l1=0;l1<sp;l1++)
System.out.print(" ");

System.out.println(s.charAt(i));
sp=sp+1;
}
}
}

Output:
Enter a word.
NAME
Pattern:
N
A
M
E

Write a program to accept a String and reverse it without


using a library class.
import java.util.*;
class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);
System.out.println("Reverse of entered string is: "+reverse);
}
}

Output:
Enter a string to reverse
camera
Reverse of entered string is: aremac

Write a program to accept a String and reverse it using a


library class.
import java.util.*;
class InvertString
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string.");
StringBuffer a = new StringBuffer(sc.next());
System.out.println(a.reverse());
}
}

Output:
Enter the string.
problem
melborp

Write a program to accept a String and check if it is a


palindrome or not.
import java.util.*;
class InvertString
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string.");
StringBuffer a = new StringBuffer(sc.next());
StringBuffer b = new StringBuffer("");
b = a.reverse();
if(a.equals(b))
System.out.println("Palindrome.");
else
System.out.println("Not a palindrome.");
}
}

Output 1:
Enter the string.
traffic
Not a palindrome.
Output 2:
Enter the string.

madam
Palindrome.

Write a program to accept the values of principal, rate


and time and calculate the compound interest computed
annually.
import java.io.*;
import java.lang.*;
class Interest
{
public static void main(String args[]) throws IOException
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(ir);

System.out.println("Enter the principal");


double P = Double.parseDouble(buf.readLine());
System.out.println("Enter the rate % ");
double r = Double.parseDouble(buf.readLine());
System.out.println("Enter the time in years.");
double t = Double.parseDouble(buf.readLine());
double inter = P*(Math.pow((1 + r/100),t)-1);
System.out.println("Interest: "+inter);
}
}

Output:
Enter the principal

50
Enter the rate %
2
Enter the time in years.
3
Interest: 3.0604000000000076

Write a program to accept the principal, rate and time


and print the outstanding amount at the end of the
inputted number of years when the interest is compound
interest.
import java.io.*;
import java.lang.*;
class Interest
{
public static void main(String args[]) throws IOException
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(ir);
System.out.println("Enter the principal");
double P = Double.parseDouble(buf.readLine());
System.out.println("Enter the rate % ");
double r = Double.parseDouble(buf.readLine());
System.out.println("Enter the time in years.");
double t = Double.parseDouble(buf.readLine());
double amt = P*Math.pow((1 + r/100),t);
System.out.println("Amount:"+amt);
}
}

Output:
Enter the principal
120

Enter the rate %


15
Enter the time in years.
3
Amount:182.50499999999997

Write a program to calculate simple interest on a sum of money.

import java.io.*;
class Interest3
{
public static void main(String args[]) throws IOException
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(ir);
System.out.println("Enter the principal");
double P = Double.parseDouble(buf.readLine());
System.out.println("Enter the rate % ");
double r = Double.parseDouble(buf.readLine());
System.out.println("Enter the time in years.");
double t = Double.parseDouble(buf.readLine());
double inter =(P*t*r)/100;
System.out.println("Interest:"+inter);
}
}

Output:
Enter the principal
436
Enter the rate %
2
Enter the time in years.
6
Interest:52.32

Write a program to calculate the outstanding amount due


at the end of an inputted number of years for an inputted

sum of money at an inputted rate when the interest is


simple interest.
import java.io.*;
import java.lang.*;
class Interest4
{
public static void main(String args[]) throws IOException
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(ir);
System.out.println("Enter the principal");
double P = Double.parseDouble(buf.readLine());
System.out.println("Enter the rate % ");
double r = Double.parseDouble(buf.readLine());
System.out.println("Enter the time in years.");
double t = Double.parseDouble(buf.readLine());
double amt = P+((P*t*r)/100);
System.out.println("Amount:"+amt);
}
}

Output:
Enter the principal
500
Enter the rate %
15

Enter the time in years.


3
Amount:725.0

Write a program to accept a number from the keyboard


and print the integer and decimal part separately.
import java.util.*;

class Splitting
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
float num = sc.nextFloat();
int ipart = (int) num;
float rpart = num - ipart;
System.out.println("Integer part=" + ipart);
System.out.print("Decimal part=" + rpart);
}
}

Output:
34.325
Integer part=34
Decimal part=0.32500076

Write a program to compute the railway fare depending


on the criteria as given below :
Age (in years)

Distance (in Kms)

Fare (in Rupees)

Below 10

Below 10

Between 10 and
60

Above 60

Between 10 and
50
Above 50
Below 10
Between 10 and
50
Above 50
Below 10
Between 10 and
50
Above 50

20
50
10
40
80
4
15
35

import java.io.*;
public class Railway
{
public static void main(String h[])throws IOException
{
BufferedReader buf = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Please enter your age in years:");
String a=buf.readLine();
int age=Integer.parseInt(a);
System.out.println("Please enter the distance(in km) to be
travelled:");
String d=buf.readLine();
int dis=Integer.parseInt(d);

if (age<=10)
{
if(dis<=10)
System.out.print("Fare = Rs 5.00 ");
else if (dis<=50)
System.out.print("Fare = Rs 20.00 ");
else if (dis>50)
System.out.print("Fare = Rs 50.00 ");
}
if(age>10&&age<=60)
{
if(dis<=10)
System.out.print ("Fare = Rs 10.00 ");
else if (dis<=50)
System.out.print ("Fare = Rs 40.00 ");
else if (dis>50)
System.out.print("Fare = Rs 80.00 ");
}
else
{if(dis<=10)
System.out.print("Fare = Rs 4.00");
else

if (dis<=50)

System.out.print("Fare = Rs 15.00");

else

if (dis>50)

System.out.print("Fare = Rs 35.00");
}
}
}

Output 1:
Please enter your age in years:
36
Please enter the distance(in km) to be travelled:
245
Fare = Rs 80.00
Output 2:
Please enter your age in years:
63
Please enter the distance(in km) to be travelled:
25
Fare = Rs 15.00

Write a program to compute the amount that a customer


pays for the taxi that he hires based on the following
conditions:

Kms travelled

Amount per Km

Upto less or equal to 1 Km


More than 1 to 6 km
More than 6 to 12
More than 12 to 18
More than 18

25
10
15
20
25

import java.util.*;
public class Taximeter
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the taxi no.");
int taxino = sc.nextInt();
System.out.println("Enter your name.");
String name = sc.next();
System.out.println("Enter the distance(in km).");
int Km = sc.nextInt();
double amt = 0;
if (Km<=1)
amt = 25 * Km;
else if (Km > 1 && Km <= 6)
amt = 10 * Km;
else if (Km > 6 && Km <+ 12)
amt = 15 * Km;
else if (Km > 12 && Km <= 18)
amt = 20 * Km;

else
amt = 25 * Km;
System.out.println("Taxi No.\t Name\t Kilometers travelled \t Bill Amount");
System.out.println(taxino + "\t\t\t" + name+ "\t\t\t" + Km + "\t\t\t\t\t\t" +
amt);
}
}

Output:
Enter the taxi no.
324
Enter your name.
Mahesh
Enter the distance(in km).
25
Taxi No.
324

Name
Mahesh

Kilometers travelled
25

Bill Amount
625.0

Write a program to find the discriminant of a quadratic


equation and its roots, by accepting the values of a, b

and c to satisfy the equation :


import java.util.*;
public class Discriminant {
public static void main(String args[])
{
int a,b,c;
double R1,R2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the values of a, b and c");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
{
double d =Math.sqrt(b*b-4*a*c);
System.out.println("Discriminant = " +d);
if((d>0)||(d==0))
{
R1=(-b+d)/2*a;
R2=(-b-d)/2*a;
System.out.println("Roots are: "+R1+" and "+R2);
}

else
System.out.println("Roots are imaginary & unequal");

}
}
}

Output 1:
Enter the values of a, b and c
1
3
2
Discriminant = 1.0
Roots are: -1.0 and -2.0
Output 2:
Enter the values of a, b and c
2
5
7
Discriminant = NaN
Roots are imaginary & unequal

Write a program to accept a number and print its


multiplication table.
import java.util.*;
public class MultiplicationTable
{
public static void main(String args[])
{
int n, c;
System.out.println("Enter an integer to print its multiplication table");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("Multiplication table of "+n+" is :-");
for ( c = 1 ; c <= 10 ; c++ )
System.out.println(n+"*"+c+" = "+(n*c));
}
}

Output:
Enter an integer to print its multiplication table
213
Multiplication table of 213 is :213*1 = 213
213*2 = 426
213*3 = 639

213*4 = 852
213*5 = 1065
213*6 = 1278
213*7 = 1491
213*8 = 1704
213*9 = 1917
213*10 = 2130

Write a program to generate the Pascals triangle for an


inputted number of rows.
import java.util.*;
public class Pascals_Triangle
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i,j,k;
int m[]=new int[20];
System.out.println("Enter the number of rows.");
k=sc.nextInt();
m[0]=1;
for(i=0;i<k;i++)
{
for(j=0;j<=i;j++)
System.out.print(m[j]+" ");
System.out.println();
for(j=1+i;j>0;j--)
m[j]=m[j]+m[j-1];
}
}
}

Output:
Enter the number of rows.

5
1
11
121
1331
14641

Write a program to calculate the kinetic energy possessed


by a body.
import java.util.*;
public class kineticEnergy {
public static void main(String args[])
{
double m,v,E;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the mass(in kg) and velocity(in m/s).");
m=sc.nextInt();
v=sc.nextInt();
{
E=1.0/2.0*m*v*v;
}
System.out.println("Kinetic Energy is:"+E);
}
}

Output:
Enter the mass(in kg) and velocity(in m/s).
453
2
Kinetic Energy is:906.0

Write a program to accept a number and print the


corresponding day of the week.
import java.util.*;
public class days
{
public static void main (String args[])
{

System.out.println("Enter the no. from 1-7");


Scanner sc=new Scanner(System.in);
int m=sc.nextInt();

switch(m)
{
case 1:
System.out.println("Sunday");
break;

case 2:
System.out.println("Monday");
break;

case 3:
System.out.println("Tuesday");
break;

case 4:
System.out.println("Wednesday");
break;

case 5:
System.out.println("Thursday");
break;

case 6:
System.out.println("Friday");
break;

case 7:
System.out.println("Saturday");
break;

default :
System.err.println("Invalid Input");
}
}
}

Output 1:
Enter the no. from 1-7
3
Tuesday

Output 2:
Enter the no. from 1-7
7
Saturday

Write a program to accept a String and remove all letters


that repeat themselves such that there is only one
occurrence of a letter.
import java.io.*;
class RemoveDuplicate
{
void remove(String s)
{
int l=s.length();
int c;
String org=s, s1="";
for(int i=0;i<(l-1);i++)
{
s1=s.substring(0,i+1);
c=0;
for(int j=i+1; j<l;j++)
{
if(s.charAt(i)==s.charAt(j))
{
c++;
continue;
}
else
s1=s1+s.charAt(j);
}
s=s1;

s1="";
if(c>0)
l-=c;
}
System.out.println("Original String: "+org);
System.out.println("String after removing duplicates: "+s);
}
public static void main(String args[])throws IOException
{
String str;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
RemoveDuplicate obj=new RemoveDuplicate();
System.out.println("Enter the Sentence");
str=br.readLine();
obj.remove(str);
}
}

Output:
Enter the Sentence
Happy Birthday!!!
Original String: Happy Birthday!!!

Write a program to accept the radius of two concentric


circles and find the inner and outer area and
circumference and the area of the ring.

import java.util.*;
public class Ring {
public static void main(String args[])
{
int r1,r2;
System.out.println("Enter the values of inner & outer radius");
Scanner sc=new Scanner(System.in);
r1=sc.nextInt();
r2=sc.nextInt();
double a1,a2,a,c1,c2;
a1=3.14*r1*r1;
a2=3.14*r2*r2;
a=a2-a1;
c1=2*3.14*r1;
c2 = 2 * 3.14 * r2;
System.out.println("The inner area="+a1);
System.out.println("The outer area="+a2);
System.out.println("The area of the ring="+a);
System.out.println("The inner circumfrence="+c1);
System.out.println("The outer circumfrence="+c2);
}
}

Output:
Enter the values of inner & outer radius
2

6
The inner area=12.56
The outer area=113.03999999999999
The area of the ring=100.47999999999999
The inner circumfrence=12.56
The outer circumfrence=37.68

Write a program in java to display the sum of the


following series :
1+2/1*2 + 1+2+3/1*2*3 + 1+2+3...n/1*2*3.....n

import java.io.*;
class seriessum
{
public static void main(String args[])throws IOException
{
BufferedReader keyin=new BufferedReader (new
InputStreamReader(System.in));
String instr;
int n=0;
double s=0;
double a=1;
double d=1;
int i=0;
System.out.println("Enter the last number of the series :
");
instr=keyin.readLine();
n=Integer.parseInt(instr);
for(i=2;i<=n;i++)
{
a=a+1;
d=d*i;
s=s+a/d;
}

System.out.println("The sum of the series = "+s);


}
}

Output 1:
Enter the last number of the series :
54
The sum of the series = 1.7182818284590455
Output 2:
Enter the last number of the series :
3
The sum of the series = 1.5

Write a program to accept ten numbers and print a count


of the number of zeroes, positive and negative numbers.
import java.util.Scanner;
class PosOrNeg

{
public static void main(String args[])
{
int num;
int Poscount;
int Negcount;
int Zerocount;
Zerocount=0;
Poscount=0;
Negcount=0;
Scanner keyin=new Scanner(System.in);
for(int mycount=1;mycount<=10;mycount++)
{
System.out.println("enter number "+mycount);
num=keyin.nextInt();

if(num>0)
{
Poscount++;
}
else if (num<0)
{
Negcount++;
}
else
{

Zerocount++;
}

}
System.out.println("number of positive numbers="+Poscount);
System.out.println("number of negative numbers="+Negcount);
System.out.println("number of zeroes="+Zerocount);
}
}

Output:
enter number 1
-34
enter number 2
324
enter number 3
634
enter number 4
123
enter number 5
3546
enter number 6
-234

enter number 7
64
enter number 8
0
enter number 9
0
enter number 10
235
number of positive numbers=6
number of negative numbers=2
number of zeroes=2

Write a program to accept a year and print whether it is a


leap year or not.
import java.util.Scanner;
public class leapyear {
public static void main(String args[]){
Scanner keyin=new Scanner(System.in);

int y,s=0;
System.out.print("Enter a year number : ");
y=keyin.nextInt();
s=y%100;
if(s==0)
{
if(y%400==0)
System.out.println("The year is a leap year");
else
System.out.println("The year is not a leap year");
}
else
{
if(y%4==0)
System.out.println("The year is a leap year");
else
System.out.println("The year is not a leap year");
}
}
}

Output 1:
Enter a year number : 2012
The year is a leap year
Output 2:
Enter a year number : 1938

The year is not a leap year

Write a menu-driven program to accept a number and


find its natural logarithm or its absolute value based on
the users choice.
import java.io.*;
public class Number_Evaluation
{
public static void main(String[] args)throws IOException
{

BufferedReader br = new BufferedReader(new


InputStreamReader(System.in));
double natural_log , abs_val , sqr_root , rand ;
int choice , n;
System.out.println(

" ***WELCOME*** " );

System.out.println("Enter 1 : For Natural Logarithm");


System.out.println("Enter 2 : For Absolute Value");
System.out.println("Enter Your chice");
choice = Integer.parseInt(br.readLine());

switch(choice)
{
case 1 :

System.out.println("Enter A Number");
n = Integer.parseInt(br.readLine());
natural_log = Math.log(n);

System.out.println("The Natural Logarithm of the


number "+n+" = "+natural_log);
break;
case 2 :

System.out.println("Enter A Number");
n = Integer.parseInt(br.readLine());
abs_val = Math.abs(n);
System.out.println("The Absolute of the number

"+n+" = "+abs_val);
break;

default : System.out.println("Wrong choice");


}

}
}

Output 1:
***WELCOME***
Enter 1 : For Natural Logarithm
Enter 2 : For Absolute Value
Enter Your chice
1
Enter A Number
54
The Natural Logarithm of the number 54 =
3.9889840465642745
Output 2:
***WELCOME***
Enter 1 : For Natural Logarithm
Enter 2 : For Absolute Value
Enter Your chice
2
Enter A Number
-4332
The Absolute of the number -4332 = 4332.0

Write a program to print the following pattern:

import java.util.Scanner;

public class ChrisTreePat {


public static void main(String args[])
{
int i,j,k;
System.out.println("

");

for(i=1;i<=7;i=i+2)
{
for(j=8;j>=i;j=j-2)
{
System.out.print(" ");//This statement is used to move over to the
next line an alternative to \n
}
System.out.print("*");
for(k=1;k<=i;k++)
{
System.out.print(" ");//This statement is used to move over to the
next line an alternative to \n
}
System.out.print("*");
System.out.print("\n");
}
System.out.println("***********");
}
}

Output:

Write a program to accept the purchase amount of a


customer in a clothing store and print the discounted
price based on the following criteria.
Purchase Amount

Discount

Upto 2000
2001-5000
5001-10000

5%
25%
35%

More than 10000

50%

import java.io.*;
public class Showroom_Discount
{
public static void main(String[] args)throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Please Enter the purchase amount");
double amt = Double.parseDouble(br.readLine());
double discount = 0;
double bill_amt =0;

if(amt<=2000)
{
discount = 0.05*amt;
bill_amt = amt - discount;
}
if(amt>=2001 && amt<=5000)
{
discount = 0.25*amt;
bill_amt = amt - discount;
}
if(amt>=5001 && amt<=10000)
{
discount = 0.35*amt;
bill_amt = amt - discount;

}
if(amt>=10001 )
{
discount = 0.5*amt;
bill_amt = amt - discount;
}
System.out.println("Discount = "+discount);
System.out.println("The final aount to be paid : Rs."+bill_amt);
}
}

Output:
Please Enter the purchase amount
2000
Discount = 100.0
The final aount to be paid : Rs.1900.0
Write a program to accept a number and shuffle the digits
in such a way that the even numbers come first followed
by the odd numbers.
import java.io.*;
public class Shuffle
{
public static void main(String args[])throws IOException
{
int num;
String even;

String odd;
int rem;
String zero;
String instr;
BufferedReader keyin=new BufferedReader (new
InputStreamReader(System.in));
System.out.println("Enter the number");
instr=keyin.readLine();
num=Integer.parseInt(instr);
even="";
odd="";
zero="";
while(num!=0)
{
rem=num%10;
if(rem%2==0 && rem!=0)
{
String e=Integer.toString(rem);
even=even.concat(e);
}
else if(rem%2==1)
{
String o=Integer.toString(rem);
odd =odd.concat(o);
}
else if(rem==0)
{

String z=Integer.toString(rem);
zero=zero.concat(z);
}
num=num/10;
}
String res1=even.concat(zero);
String res2=res1.concat(odd);
System.out.println("After reshuffling the number is="+res2);
}
}

Output:
Enter the number
45754732
After reshuffling the number is=24437575
Write a program to find the sum of the series: 2-4+6-820
import java.util.*;
class Xseries
{
public static void main (String args[])
{
double sum=0.0;
double x;
Scanner keyin= new Scanner (System.in);
int c=1;

System.out.println("Sum of 2-4+6-8+...-20=");
for(int i=2;i<=20;i=i+2)
{
if (c%2!=0)
sum =sum+i;
else
sum=sum-i;
c++;
}
System.out.println(sum);
}}

Output:
Sum of 2-4+6-8+...-20=
-10.0
Write a program to accept the marks of 5 students in any
subject. Print the average marks, the student who scored
the most marks and the maximum marks.
import java.io.*;
public class Student_Marks
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String name[] = new String[5];
int marks[] = new int[5];

int max = marks[0];


double average;
int total_marks=0;
String max_name = " ";

for(int i=0; i<5; i++)


{
System.out.println("Enter the Name of the student : ");
name[i] = br.readLine();
System.out.println("Enter the Marks of the Student : ");
marks[i] = Integer.parseInt(br.readLine());
if(marks[i]<=100 && marks[i]>=0)
{
total_marks += marks[i];
}
else
{
System.out.println("INVALID MARKS");
do
{

System.out.println("Enter the Marks of the


Student : ");
marks[i] = Integer.parseInt(br.readLine());
if(marks[i]<=100 && marks[i]>=0)
{
total_marks += marks[i];

}
else System.out.println("INVALID MARKS");
}
while(marks[i]>100 || marks[i]<0);
}
}
average = (total_marks)/5;

for(int j=0; j<5; j++)


{
if(marks[j]>max)
{
max=marks[j];
max_name = name[j];
}
}
System.out.println("Average Marks in the subject : "+average);
System.out.println("Name of the Student with the Highest Marks:
"+max_name);
System.out.println("Maximum Marks : "+max);
}
}

Output:
Enter the Name of the student :
A
Enter the Marks of the Student :

12
Enter the Name of the student :
B
Enter the Marks of the Student :
64
Enter the Name of the student :
C
Enter the Marks of the Student :
100
Enter the Name of the student :
D
Enter the Marks of the Student :
93
Enter the Name of the student :
E
Enter the Marks of the Student :
53
Average Marks in the subject : 64.0
Name of the Student with the Highest Marks: C
Maximum Marks : 100

Write a program to print the following pattern:

import java.io.*;
public class diapatt
{
public static void main(String [] args)
{
int n=10;
for(int i=1;i<=n;i++)

{
for(int j=1;j<=(n-i);j++)
{
System.out.print("
for(int h=1;h<=i;h++)
{
System.out.print("*"+" ");
}
System.out.println();
}

for(int k=n-1;k>=1;k--)
{
for(int f=1;f<=(n-k);f++)
{
System.out.print(" ");
}
for(int l=1;l<=k;l++)
{
System.out.print("*"+" ");
}
System.out.println();
}
}
}

Output:

Write a program to print the following pattern:

public class fivepatt


{
public static void main(String args[])
{
int i,j,k;
int a=5;
for(i=a;i>=1;i--)
{
for(j=a;j>i;j--)

System.out.print(j);
for(k=1;k<=i;k++)
System.out.print(i);
System.out.print("\n");
}
}}

Ouput:

Write a program to generate the following pattern:

public class pattern1


{
public static void main(String args[])
{
int i,j,k,c=0,a=7;
for(i=5;i>=1;i--)
{
for(j=1;j<=i-1;j++)
System.out.print (j);

if(c==0)
System.out.print(5);
else
{
for(k=7;k>=a;k--)
System.out.print(" ");
}
for(j=i-1;j>=1;j--)
System.out.print(j);
System.out.println(" ");
c++;
a=a-2;
}
}
}

Output:

Write a program to accept the heights of 6 athletes and


sort them under category A and category B where all
those under 5 ft are in category A while those who are 5
ft or taller are in category B.
import java.io.*;
public class jumpcatgry
{
public static void main(String [] args)throws IOException
{
BufferedReader keyin=new BufferedReader(new
InputStreamReader(System.in));
int a=0;
int b=0;
int h=0;
int roll=0;
String instr;
while(roll<=5)
{

System.out.println("please enter the height");


instr=keyin.readLine();
h=Integer.parseInt(instr);
if(h<5)
{
a=a+1;
roll=roll+1;
}
else
{
b=b+1;
roll=roll+1;
}
}
System.out.println("catogory A : "+a);
System.out.println("catogory B : "+b);
}
}

Ouput:
please enter the height
7
please enter the height
2
please enter the height
8
please enter the height

5
please enter the height
6
please enter the height
4
catogory A :2
catogory B :4

Write a program to accept an array of 5 numbers and sort


it using the selection sort technique.
import java.io.*;
public class Selection_Sort
{
public static void main(String[] args)throws IOException
{
int num[] = new int[5];
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
for(int i=0; i<5; i++)
{
System.out.println("Enter Number "+(i+1)+":");
num[i] =Integer.parseInt(br.readLine());
}
int min , pos;
pos = 0;
for(int i=0; i<5; i++)
{

min=num[i];
for(int j=i+1; j<5; j++)
{
if(min>num[j])
{
min=num[j];
pos=j;
}
}
int temp;
temp = num[i];
num[i] = min;
num[pos] = temp;
}
System.out.println("The sorted values in ascending order are : ");
for(int i=0; i<5; i++)
{
System.out.println(num[i]);
}
}
}

Ouput:
Enter Number 1:
3
Enter Number 2:

4
Enter Number 3:
1
Enter Number 4:
8
Enter Number 5:
7
The sorted values in ascending order are :
1
3
4
7
8

Write a program to print the following pattern:

class pattern
{
static void pattern(int n)
{
int j,i,k=0;
int num=n;
for(j=65+num;j>=65;j--)
{
for(i=65;i<j;i++)
{
System.out.print((char)i);
}
for(i=1;i<k;i++)
{
System.out.print(" ");

}
for(i=j-1;i>64;i--)
{
if(i!=65+num-1)
{
System.out.print((char)i);
}
}
k=k+2;
if(j!=66)
{
System.out.print("\n");
}
}
k=num-1;
for(j=67;j<=65+num;j++)
{
for(i=65;i<j;i++)
{
System.out.print((char)i);
}
for(i=k;i>0;i--)
{
System.out.print(" ");
}
for(i=j-1;i>64;i--)

if(i!=65+num-1)
{
System.out.print((char)i);
}

}
k=k-2;
System.out.print("\n");
}
}
public static void main(String args[])
{
pattern(4);

}
}

Output:

Write a program to accept the name, address and water


consumption in gallons and prepare the users water bill
based on the following criteria:
Consumption (in gallons)

Charge (in dollars)

Upto 45
46-75
76-125
126-200
201-350
More than 350

Free
15%
25%
50%
70%
75%

import java.util.*;
class bills
{
public static void main(String args[])
{
int wat;
String n,add;
double charge;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name ");
n = sc.nextLine();
System.out.println("Enter your address ");
add = sc.nextLine();
System.out.println("Enter your consumption in gallons ");
wat = sc.nextInt();
if(wat<=45)

{
charge = 0*wat;
}
else if(wat>45&&wat<=75)
{
charge = 0.15*wat;
}
else if(wat>75&&wat<=125)
{
charge = 0.25*wat;
}
else if(wat>125&&wat<=200)
{
charge = 0.5*wat;
}
else if(wat>200&&wat<=350)
{
charge = 0.7*wat;
}
else
{
charge = 0.75*wat;
}
System.out.println("Name ="+n);
System.out.println("Address ="+add);
System.out.println("Charge = $"+charge);

}
}

Output:
Enter your name
Pillai
Enter your address
#206, Vegetable Market Lane, Pine Street, Wisconsin.
Enter your consumption in gallons
70
Name =Pillai
Address =#206, Vegetable Market Lane, Pine Street,
Wisconsin.
Charge =$10.5

Você também pode gostar