Escolar Documentos
Profissional Documentos
Cultura Documentos
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
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
{
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.
}
}
}
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
Phone No.
_________
__________
______
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
--------4
2000
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
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
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
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 ****
}
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
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
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
Output:
Enter 10 numbers
4
5
6
3
6
7
4
8
4
3
Highest Number is 8 and its subscript is 7
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
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.
*
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
{
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
{
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
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
*
* * *
* * * *
* * * * *
import java.io.*;
public class pattern2
{
Output:
*
*
* * *
* * * *
* * * * *
System.out.print((char)p);
}
p=p+1;
m = m-1;
System.out.println();
}
}
}
Output:
1AAAAA
22BBBB
333CCC
4444DD
55555E
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:
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
System.out.println("Enter a number");
n=Integer.parseInt(br.readLine());
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
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
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
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.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
{
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
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
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
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
{
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: ");
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
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
2
2
2
2
2
2
1
3
3
3
3
1
2
4
4
2
1
3 2 1
3 2 1
1
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
}
}
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
}
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
i=s.indexOf(' ',sv);
}
}}
Output:
Enter a sentence
London bridge is falling down.
London
bridge
is
falling
down.
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
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
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
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
Output:
Enter a number
3562
The reverse of 3562 is 2653
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
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
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
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!
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
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;
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
Title Author
Cell
Stephen King
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
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
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
}
}
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
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
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)
{
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());
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
int choice=0;
int a=0;
int b=0;
int i=0;
int num=0;
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.
{
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:
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
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
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
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
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
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
Output:
Enter a name which comprises of first name, middle
name ,last name
Austin Capria Timmons
A.C.Timmons
{
System.out.print(" "+j+" ");
}
System.out.print("\n");
}
}
}
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
{
System.out.print(" * ");
} System.out.print("\n");
}
}
}
Output:
*
* *
* * *
* * * *
* * * * *
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
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
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
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
Output:
Enter a string to reverse
camera
Reverse of entered string is: aremac
Output:
Enter the string.
problem
melborp
Output 1:
Enter the string.
traffic
Not a palindrome.
Output 2:
Enter the string.
madam
Palindrome.
Output:
Enter the principal
50
Enter the rate %
2
Enter the time in years.
3
Interest: 3.0604000000000076
Output:
Enter the principal
120
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
Output:
Enter the principal
500
Enter the rate %
15
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
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
Kms travelled
Amount per Km
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
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
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
Output:
Enter the number of rows.
5
1
11
121
1331
14641
Output:
Enter the mass(in kg) and velocity(in m/s).
453
2
Kinetic Energy is:906.0
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
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!!!
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
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;
}
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
{
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
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
switch(choice)
{
case 1 :
System.out.println("Enter A Number");
n = Integer.parseInt(br.readLine());
natural_log = Math.log(n);
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;
}
}
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
import java.util.Scanner;
");
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:
Discount
Upto 2000
2001-5000
5001-10000
5%
25%
35%
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];
}
else System.out.println("INVALID MARKS");
}
while(marks[i]>100 || marks[i]<0);
}
}
average = (total_marks)/5;
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
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:
System.out.print(j);
for(k=1;k<=i;k++)
System.out.print(i);
System.out.print("\n");
}
}}
Ouput:
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:
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
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
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:
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