
/* 1. Write a Java Program to accept some decimal
numbers from the user and display the round off value of those
numbers */
public class Round_Number
{
void Round(int n,double num[])
{
double a[] = new double[10];
for(int i = 0;i<n;i++)
{
a[i] = num[i];
int x = (int)a[i];
if((a[i] - x) > 0.5)
{
int m = x + 1;
System.out.println(m);
}
else
System.out.println(x);
}
}
}
/* 2. Wrire a Java Program to accept 10 numbers form
the user and print them in a reverse order */
class Reverse_Number
{
public void Rec_Num()
{
int num[] = new int[10];
for(int i = 0; i<10; i++)
num[i] = i + 1;
for(int i = 9; i >=0 ; i--)
{
System.out.println(num[i]);
}
}
}
/* 3. Write a java Program to accept some numbers
from the user and display only the even numbers among them */
public class Even
{
public void Even_Number(int num,int n[])
{
int a[] = new int[20];
int ev[] = new int[20];
int j = 0;
for(int i = 0; i< num;i++)
{
a[i] = n[i];
if(a[i] % 2 == 0)
{
ev[j] = a[i];
j++;
}
}
for(int i = 0;i<j;i++)
System.out.println(ev[i]);
}
}
/* 4. Write a Java Program to accept some students
name, marks in subject-1, subject-2, subject-3 and display the total
and the average of three subjects */
public class Students_Report
{
public void Marks(int n,String name[], double
Sub1[], double Sub2[], double Sub3[])
{
double total[] = new double[10];
double avg[] = new double[10];
for(int i = 0;i<n;i++)
{
total[i] = Sub1[i] + Sub2[i] + Sub3[i] ;
avg[i] = total[i] / 3;
System.out.print("\n\n"+name[i]);
System.out.print("\t\t"+Sub1[i]);
System.out.print("\t\t"+Sub2[i]);
System.out.print("\t\t"+Sub3[i]);
System.out.print("\t\t"+total[i]);
System.out.print("\t\t"+avg[i]);
}
}
}
/* 5. Write a Java Program to accept some numbers in
first array and some numbers in second array , store and print the
sum of these respective numbers in the third array */
public class Sum
{
public void Sum_Array(int n,int a[], int b[])
{
int c[] = new int[10];
for(int i = 0 ;i<n;i++)
{
c[i] = a[i] + b[i];
System.out.println(c[i]);
}
}
}
/* 6. Write a Java Program to accept some students
Name and Marks in Subject-1 , Subject-2, and Subject-3 and print
them in a neat format */
class Marks
{
public void Marks(int n,String name[], double Sub1[],
double Sub2[], double Sub3[])
{
double total[] = new double[10];
double avg[] = new double[10];
for(int i = 0;i<n;i++)
{
total[i] = Sub1[i] + Sub2[i] + Sub3[i] ;
avg[i] = total[i] / 3;
System.out.println("Student Name :"+name[i]);
System.out.println("Subject - I
:"+Sub1[i]);
System.out.println("Subject - II
:"+Sub2[i]);
System.out.println("Subject - III
:"+Sub3[i]);
System.out.println("Total
:"+Sub3[i]);
System.out.println("Average
:"+Sub3[i]);
}
}
}
/* 7. Write a Java Program to accept some numbers in
first array and some in second array, store the sum in the third
array and print them */
public class Sum_Array
{
public void Sum_Array(int n,int a[], int b[])
{
int c[] = new int[10];
for(int i = 0 ;i<n;i++)
{
c[i] = a[i] + b[i];
System.out.println(c[i]);
}
}
}
/* 8. Write a Java Program to take an integer
argument n and print the letter 'a' as shown in the following
pattern
(For n = 5, the output should be
as shown below)
a
a a
a a a
a a a a
a a a a a
a a a a
a a a
a a
a
*/
class Pattern_a
{
public void Pat(int n)
{
int i = 0,j = 0,c,k;
c = n -1;
for(i = 1; i<=n;i++)
{
for(k = 1;k<=c;k++)
System.out.print(" ");
for(j = 1;j<=i;j++)
System.out.print(" a");
System.out.println();
c = c - 1;
}
c = c+ 2;
for(i = n- 1; i>=1; i--)
{
for(k = 1;k<=c;k++)
System.out.print(" ");
for(j = 1;j<=i;j++)
System.out.print(" a");
System.out.println();
c = c+ 1;
}
}
}
/* 9. Write a Java Program to develop a Simple
Calculator */
class Calculator
{
public void Calc(double op1,double op2,char ch)
{
double res = 0.0;
switch(ch)
{
case '+' : res = op1 + op2 ;
break;
case '-' : res = op1 - op2 ;
break;
case '*' : res = op1 * op2 ;
break;
case '/' : if(op2 == 0)
System.out.println("Stupid,Division by 0 is not
possible. Dont u know that!. Learn Mathematics and come");
res = op1 / op2 ;
break;
case '%' : if(op2 == 0)
System.out.println("Stupid,Modulus is not possible
when the second operand is 0 Dont u know that!. Learn Mathematics
and come");
res = op1 % op2;
break;
default :
System.out.println("Stupid, Enter the correct operator");
}
System.out.println("\n Result = "+res);
}
}
/* 10. Write a Java Program to input an integer
number and print them as shown in the followqing pattern
( For n = 5 the output
shoul be as shown below )
1
1 2
1
2 3
1
2 3 4
1 2
3 4 5
1
2 3 4
1
2 3
1 2
1
*/
class Pattern_No
{
public void Pat(int n)
{
int i = 0,j = 0,c,k;
c = n -1;
for(i = 1; i<=n;i++)
{
for(k = 1;k<=c;k++)
System.out.print(" ");
for(j = 1;j<=i;j++)
System.out.print(j);
for(j =i-1;j>=1;j--)
System.out.print(j);
System.out.println();
c = c - 1;
}
c = c+ 2;
for(i = n- 1; i>=1; i--)
{
for(k = 1;k<=c;k++)
System.out.print(" ");
for(j = 1;j<=i;j++)
System.out.print(j);
for(j = i-1;j>=1;j--)
System.out.print(j);
System.out.println();
c = c+ 1;
}
}
}
/* 11. Write a Java program to accept an integer
number and print the pairs of twin primes.
Twin primes : (3,5), (5,7), (11,13) etc
A pair of
primes is said to be twin primes if and only if the difference
between them is 2
*/
class Twin
{
void twinPrime(int lim)throws ArithmeticException
{
int n,a =1,b =0,flag,i = 0;
for(n= 2;n<=lim;n++)
{
flag =0;
for(i = 2;i<=n/2;i++)
if(n % i == 0)
{
flag = 1;
break;
}
if(flag == 0)
{
b = n;
if((b-a) == 2)
System.out.println("Twin Pair ("+a
+","+b+")");
a = b;
}
}
}
}
/* 12. Write a Java Program to accept an integer
number and print the alphabets as shown below
(For n = 4 the output
should be as shown below )
abcdcba
abc cba
ab ba
a a
ab ba
abc cba
abcdcba
*/
class Pattern_abcd
{
public void Pat(int n)
{
int i,j,c,k;
char ch;
c =1;
ch = 'a';
for(i = 1;i<=n;i++)
System.out.print(ch++);
--ch;
for(i=1;i<n;i++)
System.out.print(--ch);
System.out.println();
for(i = n - 1; i>= 1; i--)
{
ch = 'a';
for(j = 1;j<=i;j++)
System.out.print(ch++);
for(k=1;k<=c;k++)
System.out.print(" ");
for(j = 1; j<=i;j++)
System.out.print(--ch);
System.out.println();
c = c + 2;
}
c = c - 4;
for(i = 2; i<n;i++)
{
ch = 'a';
for(j = 1;j<=i;j++)
System.out.print(ch++);
for(k = 1;k<=c;k++)
System.out.print(" ");
for(j = 1;j<=i;j++)
System.out.print(--ch);
System.out.println();
c = c+ 2;
}
ch = 'a';
for(i = 1;i<=n;i++)
System.out.print(ch++);
--ch;
for(i = 1;i<n;i++)
System.out.print(--ch);
}
}
/* 13. Write a java program to accept the values of 3
sides and check whether the triangle is a right angled triangle or
not */
class Right_Angle
{
void accpet(double a, double b, double c)
{
if(a > b && a > c)
{
if((a * a) == (b * b) + (c * c))
System.out.println("It is a right angled
triangle ");
else
System.out.println("It is not aright
angled triangle ");
}
if(b > a && b > c )
{
if((b * b) == ( a * a) + (c * c))
System.out.println("It is a right angles
triangle");
else
System.out.println("It is not a right
angles triangle");
}
if(c > a && c > b )
{
if((c * c) == ( b * b) + (a * a))
System.out.println("It is a right angles
triangle");
else
System.out.println("It is not a right
angles triangle");
}
}
}
/* 14. Write a java program to accept two strings and
display the largest string */
class Largest_String
{
void Accept_String(String first, String second)
{
if(first.length() > second.length())
System.out.println("The Largest String is
:"+first);
else
System.out.println("The Largest String is
:"+second);
}
}
/* 15. Write a java program to accept the previous
and present electric meter reading and display the electric bill to
be paid for that month */
class electricity_Temp
{
void calculate(String name, int Prv_Unit, int
Present_Unit)
{
int units = Present_Unit - Prv_Unit;
double charge = 0;
if(units <= 100)
charge = units * 0.4;
if(units <=300)
charge = 100 * 0.40 + 200 + (units - 100) *
0.6;
if(units >300) charge = 100 * 0.40 + 200 *
0.60 + (units - 300) * 1;
double netcharge = charge + 250 ;
System.out.println();
System.out.println("\t
********************************************");
System.out.println("Name :
"+name);
System.out.println("Previous Units : "+Prv_Unit);
System.out.println("Present Units : "+Present_Unit);
System.out.println("Consumed Units :
"+units);
System.out.println("\n Net amount to be
paid :" +netcharge);
}
}
/* 16. Write a java program to accept the type of
cloth ( type M for Mill cloth and H for Handloom item ), amount of
cloth and print the amount to be paid after discount */
class Discount
{
void Accept_Purchase_Amount(double amt, char
type)
{
double d,s;
switch(type)
{
case 'M' : if(amt <= 1000)
{
d = (2.0 / 100 ) * amt;
s = amt - d;
System.out.println("Amount to be payed ="+s);
}
else
if(amt > 1000 && amt<= 5000)
{
d = (20.0 / 100 ) * amt;
s = amt - d;
System.out.println("Amount to be payed ="+s);
}
else
if(amt > 5001 && amt<= 10000)
{
d = (40.0 / 100 ) * amt;
s = amt - d;
System.out.println("Amount to be payed ="+s);
}
else
if(amt > 10000)
{
d = (40.0 / 100 ) * amt;
s = amt - d;
System.out.println("Amount to be payed ="+s);
}
break;
case 'H' : if(amt <= 1000)
{
d = (5.0 / 100 ) * amt;
s = amt - d;
System.out.println("Amount to be payed ="+s);
}
else
if(amt > 1000 && amt<= 5000)
{
d = (25.0 / 100 ) * amt;
s = amt - d;
System.out.println("Amount to be payed ="+s);
}
else
if(amt > 5001 && amt<= 10000)
{
d = (50.0 / 100 ) * amt;
s = amt - d;
System.out.println("Amount to be payed ="+s);
}
else
if(amt > 10000)
{
d = (60.0 / 100 ) * amt;
s = amt - d;
System.out.println("Amount to be payed ="+s);
}
break;
default : System.out.println("No discount
available ");
}
}
}
/* 17. Write a java program to accept a string from
the keyboard and print the no of spaces in it */
class Spaces
{
void accept_display(String str)
{
int len;
int count = 0;
len = str.length();
for(int i = 0; i<len; i++)
{
if(str.charAt(i) == ' ')
count++;
}
System.out.println("The no of spaces in the
string is :"+count);
}
}
/* 18. Write a program to accept a line of text and
print the no of times each of the vowels appears in the text */
class Frequency
{
void accept_print(String str)
{
int a =0,e = 0, i = 0, o = 0, u = 0;
int len = str.length();
for(int j = 0; j< len ;j++)
{
switch(str.charAt(j))
{
case 'a':
case 'A':
a++;
break;
case 'e':
case 'E':
e++;
break;
case 'i':
case 'I':
i++;
break;
case 'o':
case 'O':
o++;
break;
case 'u':
case 'U':
u++;
break;
}
}
System.out.println("The Number of time the
vowel a occurs is :"+a);
System.out.println("The Number of time the
vowel a occurs is :"+e);
System.out.println("The Number of time the
vowel a occurs is :"+i);
System.out.println("The Number of time the
vowel a occurs is :"+o);
System.out.println("The Number of time the
vowel a occurs is :"+u);
}
}
/* 19. Write a java program to accept two integer
variables x , y and evaluate the expression (4x + y ) if x > 0 else
evaluate (4x - y) and print the result */
class Expression
{
void accept(int x, int y)
{
int result;
if(x > 0)
result = 4 * x + y;
else
result = 4 * x - y;
System.out.println("The Result of the
expression is :"+result);
}
}
/* 20. Write a java program to accept the salary,
calculate and dispaly the income tax for that perticular salary */
class Income
{
void accept(double gross)
{
double t;
if(gross <= 100000)
{
t = (0.0 /100) * gross ;
System.out.println("TAX = "+t);
}
else
if(gross > 100001 && gross <= 500000)
{
t = 1000 + (10.0 /100) * (gross -
100000);
System.out.println("TAX = "+t);
}
else
if(gross > 500001 && gross <= 800000)
{
t = 5000 + (20.0 /100) * (gross -
500000);
System.out.println("TAX = "+t);
}
else
if(gross > 800000 )
{
t = 10000 + (30.0 /100) * (gross -
800000);
System.out.println("TAX = "+t);
}
}
}
/* 21. Write a Java program to accept an integer
number and print the pairs of twin primes.
Twin primes : (3,5), (5,7), (11,13) etc
A pair of
primes is said to be twin primes if and only if the difference
between them is 2
*/
class Twin
{
void twinPrime(int lim)throws ArithmeticException
{
int n,a =1,b =0,flag,i = 0;
for(n= 2;n<=lim;n++)
{
flag =0;
for(i = 2;i<=n/2;i++)
if(n % i == 0)
{
flag = 1;
break;
}
if(flag == 0)
{
b = n;
if((b-a) == 2)
System.out.println("Twin Pair ("+a
+","+b+")");
a = b;
}
}
}
}
/* 22. Write a java program to accept the values of 3
sides and check whether the triangle is a right angled triangle or
not */
class Right_Angle
{
void accpet(double a, double b, double c)
{
if(a > b && a > c)
{
if((a * a) == (b * b) + (c * c))
System.out.println("It is a right angled
triangle ");
else
System.out.println("It is not aright
angled triangle ");
}
if(b > a && b > c )
{
if((b * b) == ( a * a) + (c * c))
System.out.println("It is a right angles
triangle");
else
System.out.println("It is not a right
angles triangle");
}
if(c > a && c > b )
{
if((c * c) == ( b * b) + (a * a))
System.out.println("It is a right angles
triangle");
else
System.out.println("It is not a right
angles triangle");
}
}
}
 |