Академический Документы
Профессиональный Документы
Культура Документы
Of
Computer Applications
AMAN SINGH
RAILWAY TICKET
import java.util.*;
class railway_ticket
String coach; //declaring a variable to store type of coach customer wants to travel
name=sc.nextLine();
coach=sc.nextLine();
mobno=sc.nextLong();
amt=sc.nextInt();
void update()//for updating the bill of customer as per his choice of coach
if(coach.equals("First_AC"))
{
totalamt=amt+700;
else if(coach.equalsIgnoreCase("Second_AC"))
totalamt=amt+500;
else if(coach.equalsIgnoreCase("Third_AC"))
totalamt=amt+250;
else if(coach.equalsIgnoreCase("Sleeper"))
totalamt=amt;
r1.accept();
r1.update();
r1.display();
}
OUTPUT:
1. enter your name:
AMAN SINGH
First_AC
5436685248
457
OUTPUT:
coach of passenger:First_AC
Total Amount:1157
Sunil Nigam
Second_AC
9876788455
342
OUTPUT:
Total Amount:842
Vishal Singh
Sleeper
9879347541
148
OUTPUT:
coach of passenger:Sleeper
Total Amount:148
Find largest and smallest no. In Array
import java.util.*;
class array1
{
public static void main()
{
int i,j,max,min,x=0;
int s[]=new int[20];
Scanner sc=new Scanner(System.in);
for(i=0;i<20;i++)
{
System.out.println("enter array elements");
s[i]=sc.nextInt();
}
max=s[2];
min=s[3];
for(j=0;j<20;j++)
{
if(s[j]>max)
{
max=s[j];
}
else if(s[j]<min)
{
min=s[j];
}
for(int a=0;a<20;a++)
{
x=x+s[a];
}
}
System.out.println("Maximum number ="+max);
System.out.println("Minimum number ="+min);
System.out.println("Sum of array elements ="+x);
}
}
OUTPUT:
enter array elements
4
enter array elements
5
enter array elements
3
enter array elements
2
enter array elements
1
enter array elements
6
enter array elements
4
enter array elements
8
enter array elements
34
enter array elements
7
enter array elements
3
enter array elements
2
enter array elements
5
enter array elements
3
enter array elements
5
enter array elements
3
enter array elements5
5
enter array elements
2
enter array elements
45
enter array elements
2
Maximum number =45
Minimum number =1
Sum of array elements =2980
STORING STUDENTS MARKS AND NAMES
import java.util.Scanner;
public class Student
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students: "); //asking for no. of student
int n = sc.nextInt();
String[] name = new String[n]; //declaring a array named name
int[] totalmarks = new int[n]; //declaring array named total marks
for (int i = 0; i < n; i++) //taking input from user
{
System.out.println("Student " + (i + 1));
System.out.println("Enter name: ");
name[i] = sc.nextLine();
System.out.print("Enter marks: ");
totalmarks[i] = sc.nextInt();
}
int sum = 0; //storing 0 in sum
for (int i = 0; i < n; i++)
{
sum = sum + totalmarks[i]; //finding sumof marks
}
double average = (double) sum / n; //finding average
System.out.println("Average is " + average); //printing average
for (int i = 0; i < n; i++)
{
double deviation = totalmarks[i] - average; //finding deviation
System.out.println("Deviation of " + name[i] + " is " + deviation);
}
}
}
OUTPUT:
Enter number of students: 4
Student 1:
Enter name:
John
Enter marks:
123
Student 2:
Enter name:
Jimmy
Enter marks:
321
Student 3:
Enter name:
Ramakant
Enter marks:
213
Student 4:
Enter name:
Raju
Enter marks:
421
Average is= 269.5
Deviation of John= -146.5
Deviation of Jimmy= 51.5
Deviation of Ramakant= -56.5
Deviation of raju= 151.5
FIRST LETTER OF STRING TO LOWER CASE
import java.util.*;
class cyber1
{
public static void main()
{
String st;
Scanner sc=new Scanner(System.in);
System.out.println("Enter your string:"); //asking for input from user
st=sc.nextLine(); //taking input from user
String w=" "; //storing null to string w
int l=st.length(); //storing length of string st in l
for(int i=0;i<l;i++)
{
char ch=st.charAt(i); //storing characer got after running loop
if(i==0||st.charAt(i-1)==' ') //checking wheather character is at first place
{
w=w+Character.toUpperCase(ch); //converting character to upper case
}
else
{
w=w+ch;
}
}
System.out.println("OUTPUT:"+w); //printing the output
}
}
OUTPUT:
1. Enter your string:
we are in cyber world
OUTPUT: We Are In Cyber World
2.Enter your string:
he is riding a bike
OUTPUT: He Is Riding A Bike
3.Enter your string:
we are going to the market
OUTPUT: We Are Going To The Market
ELECTRIC BILL
import java.util.*;
class E_Bill
{
String n; //to store name of the customer
int units; //to store number of units consumed
double bill; //to store the amount to be paid
void accept() //to give input to the system
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the name of customer");
n=sc.nextLine();
System.out.println("enter the units consumed by the customer");
units=sc.nextInt();
}
void calculate() //to calculate electric bill of customer
{
if(units<=100)
{
bill=units*2.00;
}
else if(units>100 && units<=300)
{
bill=units*3;
}
else if(units>300)
{
bill=units*5+units*2.5/100;
}
}
void display() //to display details of the customer
{
System.out.println("Name of customer: "+n);
System.out.println("Number ofunits consumed by customer: "+units);
System.out.println("Bill amount: "+bill);
}
public static void main()
{
E_Bill b1=new E_Bill(); //declaring a object named b1
b1.accept();
b1.calculate();
b1.display();
}
}
OUTPUT:
1.enter the name of customer
aman
enter the units consumed by the customer
23
Name of customer: aman
Number ofunits consumed by customer: 23
Bill amount: 46.0
2. enter the name of customer
arjun
enter the units consumed by the customer
456
3.Name of customer: arjun
Number ofunits consumed by customer: 456
Bill amount: 2291.4 enter the name of customer
arjun
enter the units consumed by the customer
456
Name of customer: arjun
Number ofunits consumed by customer: 456
Bill amount: 2291.4
PATTERN
import java.util.*;
class patter2
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter your choice:"); //taking user’s choice
int ch=sc.nextInt();
switch(ch)
{
case 1:
String a="ABCDE"; //declaring a variable a as string
int l=a.length(); //storing length of a in variable l
for(int i=l;i>=1;i--)
{
for(int j=0;j<i;j++)
{
char x=a.charAt(j); //storing character got after running a loop
System.out.print(x); //printing a pattern
}
System.out.println(); //used for shifting the cursor to next line
}
break;
case 2:
char y;
String b="BLUE"; //declaring a variable a as string
int l1=b.length(); //storing length of a in variable l
for(int p=0;p<l1;p++)
{
for(int q=0;q<=p;q++)
{
y=b.charAt(q); //storing character got after running a loop
System.out.print(y); //printing a pattern
}
System.out.println(); //used for shifting the cursor to next line
}
break;
default:
System.out.println("INVALID CHOICE");
}
}
}
OUTPUT:
1. Enter your choice:1
ABCDE
ABCD
ABC
AB
A
2.Enter your choice:2
B
BL
BLU
BLUE
SPY NUMBER
import java.util.*;
class spy
{
public static void main()
{
int a,b=0,s=0,x=1; // declaring variables
Scanner sc=new Scanner(System.in);
System.out.println("enter your number"); //taking input from user
int n=sc.nextInt();
int m=n; //storing value of n in m
while(m!=0) //running a loop to check spy number
{
a=m%10;
s=s+a;
x=x*a;
m=m/10;
}
if(s==x)
{
System.out.println("spy no"); //printing result
}
else
{
System.out.println("not spy no"); //printing result
}
}
}
OUTPUT:
1. enter your number
1124
spy no
2. enter your number
1111
not spy no
3.enter your number
4567
not spy no
PRONIC NO.
import java.util.*;
class pronic
{
public static void main()
{
int a,x,y;
Scanner sc=new Scanner(System.in);
System.out.println("Enter your number:"); //taking input from user
int n=sc.nextInt();
for(int i=1;i<=n;i++) //running a loop for checking pronic no.
{
int res=i*(i+1);
if(res==n) //checking weather res is equal to number
{
System.out.println("pronic number"); //printing result
break;
}
else if((res!=n) && (i==n))
{
System.out.println("not a pronic number");
}
}
}
}
OUTPUT:
1.Enter your number:
123
not a pronic number
2.Enter your number:
435
not a pronic number
3.Enter your number:
12
pronic number
4.Enter your number:
20
pronic number
PATTERN
import java.util.Scanner;
class loop1
{
public static void main()
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n: ");
int n = scanner.nextInt();
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
if (j % n == 1)
{
System.out.print("* ");
}
else
{
System.out.print("# ");
}
}
System.out.println();
}
}
}
OUTPUT:
1. Enter n: 5
*
*#
*##
*###
*####
2.Enter n: 3
*
*#
*##
3. Enter n: 7
*
*#
*##
*###
*####
*#####
*######
BINARY SEARCH
import java.util.Scanner;
class BinarySearch
{
public static void main()
{
int[] array = {5, 7, 9, 11, 15, 20, 30, 45, 89, 97};
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number you want to search for: ");
int target = scanner.nextInt(); //taking input from user
int left = 0;
int right = array.length - 1;
int result = -1;
while (left <= right) //checking left is less than rqual to right
{
int middle = (left + right) / 2; //finding middle element
if (array[middle] == target) //finding found element
{
result = middle; //storing meddle element in result
break;
}
else if (array[middle] > target) //checking weather meddle element is greater
than found element
{
right = middle - 1;
}
else
{
left = middle + 1;
}
}
if (result != -1)
{
System.out.println(target + " found at index " + result); //printing result
}
else
{
System.out.println("Search element not found");
}}}
OUTPUT:
1. Enter the number you want to search for: 11
11 found at index 3
2.Enter the number you want to search for: 20
20 found at index 5
3.Enter the number you want to search for: 97
97 found at index 9
SHASHA TRAVELS PROGRAM
import java.util.Scanner;
public class Travels
{
public static void main()
{
String name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter name: "); //taking input for name
name = sc.nextLine();
System.out.println("Enter sno: "); //taking input for seat no.
int sno = sc.nextInt();
System.out.println("Enter ticket charges: "); //taking input for ticket charges
int charges = sc.nextInt();
int discountPercentage = 0;
if (charges > 70000) //checking charges for given condition
{
discountPercentage = 18;
} else if (charges >= 55001 && charges <= 70000)
{
discountPercentage = 16;
}
else if (charges >= 35001 && charges <= 55000)
{
discountPercentage = 12;
}
else if (charges >= 25001 && charges <= 35000)
{
discountPercentage = 10;
}
else if (charges < 25001)
{
discountPercentage = 2;
}
double discount = discountPercentage * charges / 100.0; //finding discount
double netAmount = charges - discount; //finding net amount
System.out.println(" Name \t Sl. No. \t Ticket Charges \t Discount \t Net
Amount"); //printing result
System.out.println(name + "\t" +sno+ "\t" + charges + "\t" + discount + "\t" +
netAmount);
}
}
OUTPUT:
1. Enter name:
rohit sharma
Enter sno:
2
Enter ticket charges:
345
Name Sl. No. Ticket Charges Discount Net Amount
rohit 2 345 6.9 338.1
2. Enter name:
raj
Enter sno:
32
Enter ticket charges:
125
Name Sl. No. Ticket Charges Discount Net Amount
raj 32 125 2.5 122.5
STUDENT RESULT
import java.util.Scanner;
class aarr
{
public static void main()
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = scanner.nextInt();
int[] rollNumbers = new int[n];
String[] names = new String[n];
int[] subject1Marks = new int[n];
int[] subject2Marks = new int[n];
int[] subject3Marks = new int[n];
for (int i = 0; i < n; i++)
{
System.out.println("Student " + (i + 1));
System.out.print("Enter roll number: ");
rollNumbers[i] = scanner.nextInt();
System.out.print("Enter name: ");
scanner.nextLine(); // To consume the new line produced on hitting
// enter after entering roll number
names[i] = scanner.nextLine();
System.out.print("Enter marks in subject 1: ");//entering marks of subject1
subject1Marks[i] = scanner.nextInt();
System.out.print("Enter marks in subject 2: ");//entering marks of subject2
subject2Marks[i] = scanner.nextInt();
System.out.print("Enter marks in subject 3: ");//entering marks of subject3
subject3Marks[i] = scanner.nextInt();
}
for (int i = 0; i < n; i++)
{
System.out.println("Roll number = " + rollNumbers[i] + ", Name = " + names[i]);
int averageMarks = (subject1Marks[i] + subject2Marks[i] + subject3Marks[i]) / 3;
//finding average marks
if (averageMarks >= 85 && averageMarks <= 100) //printing result as per the
given condition
{
System.out.println("EXCELLENT");
}
else if (averageMarks >= 75 && averageMarks <= 84)
{
System.out.println("DISTINCTION");
}
else if (averageMarks >= 60 && averageMarks <= 74)
{
System.out.println("FIRST CLASS");
}
else if (averageMarks >= 40 && averageMarks <= 59)
{
System.out.println("PASS");
}
else if (averageMarks < 40)
{
System.out.println("POOR");
}
}
}
}
OUTPUT:
Enter number of students: 3
Student 1
Enter roll number: 1
Enter name: aman
Enter marks in subject 1: 65
Enter marks in subject 2: 67
Enter marks in subject 3: 59
Student 2
Enter roll number: 2
Enter name: ankit
Enter marks in subject 1: 40
Enter marks in subject 2: 32
Enter marks in subject 3: 10
Student 3
Enter roll number: 3
Enter name: vishal
Enter marks in subject 1: 30
Enter marks in subject 2: 31
Enter marks in subject 3: 20
Roll number = 1, Name = aman
FIRST CLASS
Roll number = 2, Name = ankit
SECOND CLASS
Roll number = 3, Name = vishal
THIRD CLASS
FUNCTION OVERLOAD
import java.util.*;
class Overloading
{
void num_calc(int num, char ch)
{
if (ch == 's') //checking if ch is equal to s
{
double square = Math.pow(num, 2); //finding square of num
System.out.println("Square is " + square); //printing the square
}
else
{
double cube = Math.pow(num, 3); //finding cube of num
System.out.println("Cube is " + cube); //printing cube
}
}
void num_calc(int a, int b, char cha)
{
if (cha == 'p') //checking if cha is equal to p
{
int product = a * b; //finding product of a and b
System.out.println("Product is " + product); //printing the product
}
else
{
int sum = a + b; //finding sum of a and b
System.out.println("Sum is " + sum); //printing the sum
}
}
void num_calc(String s1, String s2)
{
if (s1.equals(s2)) //checking if s1 is equal to s2
{
System.out.println("Strings are same"); //printing result
}
else
{
System.out.println("Strings are different"); //printing the result
}
}
public static void main()
{
String e,f;
Scanner sc=new Scanner(System.in);
System.out.println("enter value of num and ch"); //taking input from user
int x=sc.nextInt();
char y='s';
System.out.println("enter value of a,b and cha"); //taking input from user
int p=sc.nextInt();
int q=sc.nextInt();
char r='p';
System.out.println("enter value of s1 and s2"); //taking input from user
e=sc.nextLine();
f=sc.nextLine();
Overloading o1=new Overloading(); //making an object named o1
o1.num_calc(x,y);
o1.num_calc(p,q,r);
o1.num_calc(e,f);
}
}
OUTPUT:
1.enter value of num
54
enter value of a,b
1
32
enter value of s1 and s2
Aman
man
Square is 2916.0
Product is 32
Strings are different
2. enter value of num
2
enter value of a,b
2
1
enter value of s1 and s2
Raj
Raj
Square is 4.0
Product is 2
Strings are same
PARKING LOT PROGRAM
import java.util.*;
class ParkingLot
{
int Vno; //declaring variable Vno to store vehicle number
int hours; //declaring variable hours to store number of hours
double bill; //declaring variable bill to store BILL of parking lot
void input() //to take input from user
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter your Vehicle Number"); //input of vehicle number
Vno=sc.nextInt();
System.out.println("Enter Number of hours"); //input number of hours
hours=sc.nextInt();
}
void calculate() //to calculate the BILL
{
if(hours==1)
{
bill=hours*3;
}
else
{
bill=hours*3+1.5*hours;
}
}
void display() //printing the BILL of parking lot
{
System.out.println("vehicle no:"+Vno);
System.out.println("number of hours:"+hours);
System.out.println("Total bill:"+bill);
}
public static void main()
{
ParkingLot p1=new ParkingLot();
p1.input();
p1.calculate();
p1.display();
}
}
OUTPUT:
1. Enter your Vehicle Number
2453
Enter Number of hours
3
vehicle no:2453
number of hours:3
Total bill:13.5
import java.util.*;
class discount
{
public static void main()
{
String name;
String purchase;
double dis,price;
Scanner sc=new Scanner(System.in);
System.out.println("enter L for Laptop and D for Desktop ");
purchase=sc.nextLine(); //taking input from user
System.out.println("enter customer's name"); //taking name as input
name=sc.nextLine();
System.out.println("enter customer's address"); //taking address as input
int address=sc.nextInt();
System.out.println("enter amount "); //taking amount as input
int amount=sc.nextInt();
switch(purchase)
{
case "L": //calculating bill for laptop
if(amount>0 && amount<=25000)
{
price=amount;
System.out.println("amount to be paid"+price);
}
else if(amount>25001 && amount<=57000)
{
dis=amount*5/100;
price=amount-dis;
System.out.println("amount to be paid"+price);
}
else if(amount>57001 && amount<=100000)
{
dis=amount*7.5/100;
price=amount-dis;
System.out.println("amount to be paid"+price);
}
else
{
dis=amount*10/100;
price=amount-dis;
System.out.println("amount to be paid"+price);
}
break;
case "D": //calculating bill for desktop
if(amount>0 && amount<=25000)
{
dis=amount*5/100;
price=amount-dis;
System.out.println("amount to be paid"+price);
}
else if(amount>25001 && amount<=57000)
{
dis=amount*7.6/100;
price=amount-dis;
System.out.println("amount to be paid"+price);
}
else if(amount>57001 && amount<=100000)
{
dis=amount*10/100;
price=amount-dis;
System.out.println("amount to be paid"+price);
}
else
{
dis=amount*15/100;
price=amount-dis;
System.out.println("amount to be paid"+price);
}
}
}
}
OUTPUT:
1. enter L for Laptop and D for Desktop
L
enter customer's name
aman
enter customer's address
43654
enter amount
123632
amount to be paid111269.0
import java.util.*;
class number
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter your number"); //taking input from user
int n=sc.nextInt();
int m=n,a,b=0,c=1,s; //declaring the variable
while(m!=0) //initializing while loop to check special no.
{
a=m%10;
b=b+a;
c=c*a;
m=m/10;
}
s=b+c;
if(s==n) //checking weather it is special number or not
{
System.out.println("Special 2-digit number"); //printing the result special number
}
else
{
System.out.println("Not a Special 2-digit number"); //printing result for not a
special number
}
}
}
OUTPUT:
1. Enter your number
59
Special 2-digit number
2.Enter your number
78
Not a Special 2-digit number
3. Enter your number
45
Not a Special 2-digit number
FRUIT JUICE
import java.util.Scanner;
class FruitJuice
{
int product_code;
String flavour;
String pack_type;
int pack_size;
int product_price;
FruitJuice() //default constructor to put default values to all variables
{
product_code = 0;
flavour = "";
pack_type = "";
pack_size = 0;
product_price = 0;
}
void input() //to take input from user
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter product code: ");
product_code = scanner.nextInt();
System.out.print("Enter flavour: ");
flavour = scanner.next();
System.out.print("Enter pack type: ");
pack_type = scanner.next();
System.out.print("Enter pack size: ");
pack_size = scanner.nextInt();
System.out.print("Enter product price: ");
product_price = scanner.nextInt();
}
void discount() //to calculate descount
{
product_price = (int) (0.9 * product_price);
}
void display() //to desplay all details of juice pack
{
System.out.println("Product Code: " + product_code);
System.out.println("Flavour: " + flavour);
System.out.println("Pack Type: " + pack_type);
System.out.println("Pack Size: " + pack_size);
System.out.println("Product Price: " + product_price);
}
public static void main()
{
FruitJuice f1=new FruitJuice();
f1.input();
f1.discount();
f1.display();
}
}
OUTPUT:
import java.util.*;
class movieMagic
{
int year;
String title;
float rating;
movieMagic() //default constructor to initialize variables
{
year=0;
title="";
rating=0;
}
void accept() //to take input fron user
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the title of movie");
title=sc.nextLine();
System.out.println("Enter year of movie");
year=sc.nextInt();
System.out.println("Enter rating of movie between 0.0 to 5.0");
rating=sc.nextFloat();
}
void display() //to print results as per given conditions
{
System.out.println("title of movie:"+title);
if(rating>=0.0 && rating<=2.0)
{
System.out.println("Flop");
}
else if(rating>=2.1 && rating<=3.4)
{
System.out.println("Semi-hit");
}
else if(rating>=3.5 && rating<=4.5)
{
System.out.println("Hit");
}
else
{
System.out.println("Super Hit");
}
}
public static void main() //to call all the functions
{
movieMagic M1=new movieMagic(); //creating an object named M1
M1.accept();
M1.display();
}
}
OUTPUT:
1. Enter the title of movie
border
Enter year of movie
1986
Enter rating of movie between 0.0 to 5.0
4.5
title of movie:border
Hit
2. Enter the title of movie
tiger zinda hai
Enter year of movie
2018
Enter rating of movie between 0.0 to 5.0
5.0
title of movie:tiger zinda hai
Super Hit
BUS TICKET PROGRAM
import java.util.Scanner;
public class Travels
{
public static void main()
{
String name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter name: ");
name = sc.nextLine();
System.out.println("Enter sno: ");
int sno = sc.nextInt();
System.out.println("Enter ticket charges: ");
int charges = sc.nextInt();
int discountPercentage = 0;
if (charges > 70000)
{
discountPercentage = 18;
} else if (charges >= 55001 && charges <= 70000)
{
discountPercentage = 16;
}
else if (charges >= 35001 && charges <= 55000)
{
discountPercentage = 12;
}
else if (charges >= 25001 && charges <= 35000)
{
discountPercentage = 10;
}
else if (charges < 25001)
{
discountPercentage = 2;
}
double discount = discountPercentage * charges / 100.0;
double netAmount = charges - discount;
System.out.println(" Name \t Sl. No. \t Ticket Charges \t Discount \t Net
Amount");
System.out.println(name + "\t" +sno+ "\t" + charges + "\t" + discount + "\t" +
netAmount);
}
}
OUTPUT:
1. Enter name:
rohit sharma
Enter sno:
2
Enter ticket charges:
345
Name Sl. No. Ticket Charges Discount Net Amount
rohit 2 345 6.9 338.1
2. Enter name:
raj
Enter sno:
32
Enter ticket charges:
125
Name Sl. No. Ticket Charges Discount Net Amount
raj 32 125 2.5 122.5
FILE EXTENSION
import java.util.Scanner;
public class FileName
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter file name and path: "); //taking input fron user
String input = sc.nextLine();
int indexOfLastBackslash = input.lastIndexOf('\\'); //checking last
occurence of '//'
int indexOfDot = input.lastIndexOf('.'); //c hecking last index of '.'
String outputPath = input.substring(0, indexOfLastBackslash + 1);
String fileName = input.substring(indexOfLastBackslash + 1, indexOfDot);
String extension = input.substring(indexOfDot + 1);
System.out.println("Path: " + outputPath); //printing file path
System.out.println("File Name: " + fileName); //printing file name
System.out.println("Extension: " + extension); //printing extension
}
}
OUTPUT:
Enter file name and path: user\admin\pictures\flower.jpg
Path: user\admin\pictures\
File Name: flower
TO FIND FACTOR AND FACTORIAL OF A NUMBER
import java.util.*;
class factor
{
public static void main()
{
int f;
Scanner sc=new Scanner(System.in);
System.out.println("Enter your number");
int n=sc.nextInt();
System.out.println("enter your choice :");//enter choice for factorial or factor of
digit
System.out.println("enter 1 to find factor");
System.out.println("enter 2 to find factorial");
int ch=sc.nextInt();
switch(ch)
{
case 1:
for(int i=1;i<n;i++) //finding factor
{
if(n%i==0)
{
System.out.println("factors are ="+i);
}
}
break;
case 2:
f=1;
for(int b=1;b<=n;b++) //finding factorial
{
f=f*b;
}
System.out.println("factorial is ="+f);
break;
default:
System.out.println("Invalid Choice");
}
}
}
OUTPUT:
Enter your number
12
enter your choice :
enter 1 to find factor
enter 2 to find factorial
1
factors are =1
factors are =2
factors are =3
factors are =4
factors are =6
import java.util.Scanner;
class Wonders
{
public static void main()
{
String[] sevenWonders = { "CHICHEN ITZA", "CHRIST THE RDEEEMER",
"TAJMAHAL", "GREAT WALL OF CHINA",
"MACHU PICCHU", "PETRA", "COLOSSEUM" };
String[] locations = { "MEXICO", "BRAZIL", "INDIA", "CHINA", "PERU", "JORDAN",
"ITALY" };
Scanner sc = new Scanner(System.in);
System.out.print("Enter country: ");
String country = sc.next();
// Search country in the array using linear search
int index = -1;
for (int i = 0; i < locations.length; i++)
{
if (locations[i].equals(country))
{
index = i;
}
}
if (index != -1)
{
System.out.println(locations[index] + " - " + sevenWonders[index]);
}
else
{
System.out.println("Sorry Not Found!");
}
}
}
OUTPUT:
1.Enter country: INDIA
INDIA - TAJMAHAL
import java.util.*;
class amount
{
public static void main()
{
double A,B;
double x;
Scanner sc=new Scanner(System.in);
System.out.println("ENTER YOUR CHOICE"); //taking user's choice
System.out.println("Enter 1 for Term Deposit");
System.out.println("Enter 2 for Recurring Deposit");
int ch=sc.nextInt();
System.out.println("Enter princilal"); //taking input of principal from user
double P=sc.nextDouble();
System.out.println("Enter rate of intrest"); //taking input of rate from user
int r=sc.nextInt();
System.out.println("Enter time period in months"); //taking input of time from
user
int n=sc.nextInt();
switch(ch)
{
case 1:
x=Math.pow((1+r/100),n); //finding maturity amount for term deposit
A=P*x;
System.out.println("maturity amount is"+A);
break;
case 2:
B=(P*n)+P*(n*(n+1)/2)*r/100*1/12; //finding maturity amount for recurring
deposit
System.out.println("maturity amount is"+B);
break;
default:
System.out.println("INVALID CHOICE");
}
}
}
OUTPUT:
1. ENTER YOUR CHOICE
Enter 1 for Term Deposit
Enter 2 for Recurring Deposit
1
Enter princilal
45635
Enter rate of intrest
5
Enter time period in months
25
maturity amount is 5335.0
2. ENTER YOUR CHOICE
Enter 1 for Term Deposit
Enter 2 for Recurring Deposit
2
Enter princilal
723
Enter rate of intrest
2
Enter time period in months
3
maturity amount is2176.23
PRINT FREQUENCY OF EACH LETTER IN A STRING
import java.util.Scanner;
class String_Freq
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a String: ");
String input = sc.nextLine();
int frequency[] = new int[26];
for (int i = 0; i < input.length(); i++)
{
char ch = input.charAt(i);
if (Character.isUpperCase(ch))
{
frequency[ch - 65]++;
}
}
System.out.println("Characters Frequency=");
for (int i = 0; i < 26; i++)
{
if (frequency[i] != 0) {
System.out.println((char) (i + 65) + "\t" + frequency[i]);
}
}
}
}
OUTPUT:
Enter a String: COMPUTER HARDWARE
Characters Frequency
A 2
C 1
D 1
E 2
H 1
M 1
O 1
P 1
R 3
T 1
U 1
W 1
BINARY SEARCH
import java.util.Scanner;
class BinarySearch
{
public static void main()
{
int[] array = {5, 7, 9, 11, 15, 20, 30, 45, 89, 97};
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number you want to search for: ");
int target = scanner.nextInt();
int left = 0;
int right = array.length - 1;
int result = -1;
while (left <= right)
{
int middle = (left + right) / 2;
if (array[middle] == target)
{
result = middle;
break;
}
else if (array[middle] > target)
{
right = middle - 1;
}
else
{
left = middle + 1;
}
}
if (result != -1)
{
System.out.println(target + " found at index " + result);
}
else
{
System.out.println("Search element not found");
}}}
OUTPUT:
1. Enter the number you want to search for: 11
11 found at index 3
2.Enter the number you want to search for: 20
20 found at index 5
3.Enter the number you want to search for: 97
97 found at index 9
STOREING ELEMENTS OF TWO ARRAY IN THIRD ARRAY
import java.util.Scanner;
class ArrayStore
{
public static void main()
{
Scanner scanner = new Scanner(System.in);
int[] p = new int[6]; //array for storing 6 elements
int[] q = new int[4]; //array for storing 4 elements
int[] r = new int[10]; //array for storing 10 elements
System.out.print("Enter elements of array P: "); //taking elements of array p as
input
for (int i = 0; i < 6; i++)
{
p[i] = scanner.nextInt();
}
System.out.print("Enter elements of array Q: "); //taking elements of array p as
input
for (int i = 0; i < 4; i++)
{
q[i] = scanner.nextInt();
}
for (int i = 0; i < 6; i++)
{
r[i] = p[i];
}
for (int i = 0; i < 4; i++)
{
r[i + 6] = q[i];
}
System.out.print("Elements of array R: "); //printing elements of arra r after
storing elements of array p and q in it
for (int i = 0; i < 10; i++)
{
System.out.print(r[i] + " ");
}
}
}
OUTPUT:
1.Enter elements of array P: 4
7
6
7
4
3
Enter elements of array Q: 6
3
2
4
Elements of array R: 4 7 6 7 4 3 6 3 2 4