0% found this document useful (0 votes)
256 views

Print Customer Details: Import Import Public Class Public Static Void New

The document contains multiple code snippets in Java that demonstrate different programming concepts like input/output, conditional statements, and data types. Specifically, the code snippets show: 1) Taking user input and printing output to display customer, number range, road sign, electricity bill, and mobile budget details. 2) Converting Celsius to Fahrenheit and displaying character codes. 3) Calculating season, leap year, and highest placement department based on conditional logic.

Uploaded by

Nikyl
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
256 views

Print Customer Details: Import Import Public Class Public Static Void New

The document contains multiple code snippets in Java that demonstrate different programming concepts like input/output, conditional statements, and data types. Specifically, the code snippets show: 1) Taking user input and printing output to display customer, number range, road sign, electricity bill, and mobile budget details. 2) Converting Celsius to Fahrenheit and displaying character codes. 3) Calculating season, leap year, and highest placement department based on conditional logic.

Uploaded by

Nikyl
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Print Customer Details

import java.util.*;
2 import java.io.*;
3 public class Customer{
4 public static void main(String args[]){
5 Scanner sc=new Scanner(System.in);
6 System.out.println("Enter your name:");
7 String name=sc.nextLine();
8 System.out.println("Enter age:");
9 int age=sc.nextInt();
10 System.out.println("Enter gender:");
11 String gender=sc.next();
12 sc.nextLine();
13 System.out.println("Hailing from:");
14 String from=sc.nextLine();
15 System.out.println("Welcome," +name+"!");
16 System.out.println("Age:"+age);
17 System.out.println("Gender:"+gender);
18 System.out.println("City:"+from);
19 }
20 }

Finding Range Of The Number


import java.util.Scanner;
2 public class Main
3{
4 public static void main(String[] args)
5 {
6 Scanner sc=new Scanner(System.in);
7 System.out.println("Enter the number");
8 int value=sc.nextInt();
9 if(value>0 && value<=100)
10 {
11 System.out.println("Range of the number "+value+" is 0 to 100");
12 }
13 else if(value>100 && value<=200)
14 {
15 System.out.println("Range of the number "+value+" is 100 to 200");
16 }
17 else if(value>200 && value<=500)
18 {
19 System.out.println("Range of the number "+value+" is 200 to 500");
20 }
21 else{
22 System.out.println("Entered number "+value+" is not in the expected range");
23 }
24 }
25
26 }

Road Signaling
import java.util.*;
2
3 public class Main{
4
5 public static void main(String[] args){
6
7 Scanner sc=new Scanner(System.in);
8 System.out.println("Enter the color");
9 String color=sc.nextLine();
10 if(color.equals("green")){
11 System.out.println("go");
12 }
13 else if(color.equals("red")){
14 System.out.println("stop");
15 }
16 else if(color.equals("yellow")){
17 System.out.println("proceed with caution");
18 }
19 else{
20 System.out.println("prepare to go");
21 }
22 }
23 }

Electricity bill calculation


import java.util.Scanner;
2
3 public class Main {
4
5 public static void main(String[] args) {
6
7 Scanner sc = new Scanner(System.in);
8 System.out.println("Enter the units consumed");
9 int units = sc.nextInt();
10 if (units <= 20) {
11 System.out.println("No charge");
12 } else if (units > 20 && units < 100) {
13 System.out.println("You have to pay Rs." + (units * 3.50));
14
15 } else if (units >= 100) {
16 System.out.println("You have to pay Rs." + (units * 5.0));
17 }
18
19 }
20 }

Checking budget of mobile


import java.util.Scanner;
2
3 public class Main{
4
5 public static void main(String[] args){
6
7 Scanner sc=new Scanner(System.in);
8 System.out.println("Enter the cost of the mobile");
9 int price=sc.nextInt();
10 if(price<=13000)
11 {
12 System.out.println("Mobile chosen is within the budget");
13
14 }
15 else{
16 System.out.println("Mobile chosen is beyond the budget");
17 }
18
19 }
20
21 }

Finding even number


1 import java.util.Scanner;
2 public class Main {
3 public static void main(String args[]){
4
5 Scanner sc = new Scanner(System.in);
6 System.out.println("Enter the number" );
7 int num = sc.nextInt();
8
9
10 if (num % 2 == 0){
11 System.out.println(num+" is an even number");
12 }
13 else {
14 System.out.println(num +
15 " is not an even number");
16 }
17 }
18 }

Registration Details
import java.util.Scanner;
2
3 public class RegistrationDetails{
4
5 public static void main(String[] args){
6
7 Scanner sc=new Scanner(System.in);
8
9 System.out.println("Enter your name:");
10 String name=sc.nextLine();
11 System.out.println("Enter your age:");
12 int age=sc.nextInt();
13 System.out.println("Enter your phoneno:");
14 String pno=sc.nextLine();
15 System.out.println("Enter your qualification:");
16 String qualification=sc.nextLine();
17 System.out.println("Enter your email id[Please provide valid id, after registering your registration id will be
mailed]:");
18 String email=sc.nextLine();
19 System.out.println("Enter your noofexperience[if any]:");
20 String noe=sc.nextLine();
21 System.out.println("Dear "+name+", Thanks for registering in our portal, registration id will be mailed to
"+noe+" within 2 working days ");
22 }
23
24 }
25

Celcius to Farenheit Conversion


import java.util.Scanner;
2
3
4 public class CelsiusConversion
5{
6 public static void main (String[] args) {
7 Scanner sc=new Scanner(System.in);
8 double cel=sc.nextDouble();
9 double fah=((9*cel)/5)+32;
10 System.out.println(fah);
11 }
12 }

Display Characters
import java.util.Scanner;
2
3 public class AsciValue{
4
5 public static void main(String args[]){
6
7 Scanner in=new Scanner(System.in);
8
9 System.out.println("Enter the digits: ");
10
11 int a=in.nextInt();
12
13 int b=in.nextInt();
14
15 int c=in.nextInt();
16
17 int d=in.nextInt();
18
19
20
21 char q=(char)a;
22
23 char w=(char)b;
24
25 char e=(char)c;
26
27 char r=(char)d;
28
29 System.out.println();
30
31 System.out.print(a);
32
33 System.out.println("-"+q);
34
35 System.out.print(b);
36
37 System.out.println("-"+w);
38
39 System.out.print(c);
40
41 System.out.println("-"+e);
42
43 System.out.print(d);
44
45 System.out.println("-"+r);
46
47 }
48
49 }
Bill Generation
1 import java.util.Scanner;
2 public class SnacksDetails
3{
4 public static void main (String[] args) {
5 Scanner sc=new Scanner(System.in);
6 System.out.println("Enter the no of pizzas bought:");
7 int piz=sc.nextInt();
8 System.out.println("Enter the no of puffs bought:");
9 int puf=sc.nextInt();
10 System.out.println("Enter the no of cool drinks bought:");
11 int cdk=sc.nextInt();
12
13 System.out.println("Bill Details");
14 System.out.println("No of pizzas:"+piz);
15 System.out.println("No of puffs:"+puf);
16 System.out.println("No of cooldrinks:"+cdk);
17 System.out.println("Total price="+(piz*100+puf*20+cdk*10));
18 System.out.println("ENJOY THE SHOW!!!");
19 }
20 }

Find Season
import java.util.Scanner;
2 public class Season
3 {
4 public static void main (String[] args) {
5 Scanner sc = new Scanner(System.in);
6 System.out.println("Enter the month:");
7 int mon=sc.nextInt();
8 if(mon>12||mon<1)
9 {
10 System.out.println("Invalid month");
11 }
12 else if(mon>=3&&mon<=5)
13 {
14 System.out.println("Season:Spring");
15 }
16 else if(mon>=6&&mon<=8)
17 {
18 System.out.println("Season:Summer");
19 }
20 else if(mon>=9&&mon<=11)
21 {
22 System.out.println("Season:Autumn");
23 }
24 else if(mon==12||mon==1||mon==2)
25 {
26 System.out.println("Season:Winter");
27 }
28 }
29 }

Check for Leap Year


1 import java.util.Scanner;
2 public class LeapYear
3{
4 public static void main (String[] args) {
5 Scanner sc=new Scanner(System.in);
6 System.out.println("Enter the Year");
7
8 int yr=sc.nextInt();
9
10 if(yr>999 && yr<10000)
11 {
12 if(yr%4==0)
13 {
14 if(yr%100==0)
15 {
16 if(yr%400==0)
17 {
18 System.out.println("Leap Year");
19 }
20 else
21 {
22 System.out.println("Not a Leap Year");
23 }
24 }
25 else
26 {
27 System.out.println("Leap Year");
28 }
29 }
30 else
31 {
32 System.out.println("Not a Leap Year");
33 }
34 }
35 else
36 {
37 System.out.println("Invalid Year");
38 }
39 }
40 }

Highest Placement
import java.util.Scanner;
2 public class Placement
3{
4 public static void main (String[] args) {
5 Scanner sc=new Scanner(System.in);
6 System.out.println("Enter the no of students placed in CSE:");
7 int CSE= sc.nextInt();
8 System.out.println("Enter the no of students placed in ECE:");
9 int ECE=sc.nextInt();
10 System.out.println("Enter the no of students placed in MECH:");
11 int MECH=sc.nextInt();
12
13 if(CSE<0||ECE<0||MECH<0)
14 {
15 System.out.println("Input is Invalid");
16 }
17
18 else if(CSE==ECE && ECE==MECH)
19 {
20 System.out.println("None of the department has got the highest placement");
21 }
22
23 else if(CSE==ECE && MECH<CSE)
24 {
25 System.out.println("Highest placement");
26 System.out.println("CSE");
27 System.out.println("ECE");
28 }
29
30 else if(CSE==MECH && ECE<MECH)
31 {
32 System.out.println("Highest placement");
33 System.out.println("CSE");
34 System.out.println("MECH");
35 }
36
37 else if(ECE==MECH && CSE<MECH)
38 {
39 System.out.println("Highest placement");
40 System.out.println("ECE");
41 System.out.println("MECH");
42 }
43
44 else if(CSE>ECE&&CSE>MECH)
45 {
46 System.out.println("Highest placement");
47 System.out.println("CSE");
48 }
49
50 else if(ECE>CSE && ECE>MECH)
51 {
52 System.out.println("Highest placement");
53 System.out.println("ECE");
54 }
55
56 else if(MECH>CSE&&MECH>ECE)
57 {
58 System.out.println("Highest placement");
59 System.out.println("MECH");
60 }
61 }
62 }
IncrementCalculation
import java.util.*;
2
3 class IncrementCalculation{
4
5 public static void main (String[] args) {
6
7
8
9 Scanner sc = new Scanner (System.in);
10
11 System.out.println("Enter the salary");
12
13 int salary = sc.nextInt();
14
15 System.out.println("Enter the Performance appraisal rating");
16
17 float rating = sc.nextFloat();
18
19 if(salary<1||rating<1.0||rating>5.0){
20
21 System.out.println("Invalid Input");
22
23 System.exit(0);
24
25 }
26
27 else if(rating>=1&&rating<=3){
28
29 salary=salary+(int)(0.1*salary);
30
31 System.out.println(salary);
32
33 }
34
35 else if(rating>3&&rating<=4){
36
37 salary=salary+(int)(0.25*salary);
38
39 System.out.println(salary);
40
41 }
42
43 else /*if(rating>4&&rating<=5)*/{
44
45 salary=salary+(int)(0.3*salary);
46
47 System.out.println(salary);
48
49 }
50
51 }
52
53 }

Display String
import java.util.Scanner;
3
4 public class Main {
5
6 public static void main(String[] args){
7 Scanner sc=new Scanner(System.in);
8 System.out.println("Enter the String");
9 String st=sc.nextLine();
10 System.out.println("Enter the number");
11 int nb=sc.nextInt();
12 if(nb<1){
13 System.out.println(nb+ "is not a valid input");
14 }
15 else{
16 for(int i=0;i<=nb;i++){
17 System.out.println(st);
18 }
19 }
20 }
21
22 }
import java.util.Scanner;
3
4 public class Main {
5
6 public static void main(String[] args){
7 Scanner sc=new Scanner(System.in);
8 System.out.println("Enter the String");
9 String st=sc.nextLine();
10 System.out.println("Enter the number");
11 int nb=sc.nextInt();
12 if(nb<1){
13 System.out.println(nb+ "is not a valid input");
14 }
15 else{
16 for(int i=0;i<=nb;i++){
17 System.out.println(st);
18 }
19 }
20 }
21
22 }

Reverse the number


import java.util.Scanner;
2 public class Main
3{
4 //method for reverse a number
5 public static void reverseNumber(int number)
6{
7 if (number < 10)
8{
9 //prints the same number if the number is less than 10
10 System.out.println(number);
11 return;
12 }
13 else
14 {
15 System.out.print(number % 10);
16 reverseNumber(number/10);
17 }
18 }
19 public static void main(String args[])
20 {
21 System.out.print("Enter the number that you want to reverse: ");
22 Scanner sc = new Scanner(System.in);
23 int num = sc.nextInt();
24 System.out.print("The reverse of the given number is: ");
25 //method calling
26 reverseNumber(num);
27 }
28 }
29

Prime Factors
1 import java.util.*;
2 class Main {
3 public static void main(String[] args)
4 {
5 Scanner sc=new Scanner(System.in);
6 Scanner scan = new Scanner(System.in);
7 System.out.print("Input a number: ");
8 int n = scan.nextInt();
9 if (n>0)
10 {
11 while (n%2==0)
12 {
13 System.out.print(2 + " ");
14 n /= 2;
15 }
16
17 for (int i = 3; i <= Math.sqrt(n); i+= 2)
18 {
19 while (n%i == 0)
20 {
21 System.out.print(i + " ");
22 n /= i;
23 }
24 }
25 if (n > 2)
26 System.out.print(n);
27 }
28 }
29 }
30

Count of Prime numbers


import java.util.Scanner;
2 public class Main
3{
4 public static void main(String[] args)
5 {
6
7 Scanner sc=new Scanner(System.in);
8
9 System.out.print("Enter starting range");
10 int start = sc.nextInt();
11 System.out.print("\nEnter ending range");
12 int end = sc.nextInt();
13
14 int count;
15
16 for(int i = start ; i <= end ; i++)
17 {
18
19 count = 0;
20 for(int j = 1 ; j <= i ; j++)
21 {
22 if(i % j == 0)
23 count = count+1;
24 }
25
26 System.out.println(i);
27 }
28
29
30 }
31 }
32
Palindrome numbers
import java.util.*;
2 class Main
3{
4
5 static int isPalindrome(int n)
6 {
7
8
9 int rev = 0;
10 for (int i = n; i > 0; i /= 10)
11 rev = rev * 10 + i % 10;
12
13
14 return(n == rev) ? 1 : 0;
15 }
16
17
18 static void countPal(int min, int max)
19 {
20 for (int i = min; i <= max; i++)
21 if (isPalindrome(i)==1)
22 System.out.print(i + " ");
23 }
24
25
26 public static void main(String args[])
27 {
28 System.out.println("Enter the starting range");
29 Scanner sc=new Scanner(System.in);
30 int start=sc.nextInt();
31 System.out.println("Enter the ending range");
32 int end=sc.nextInt();
33 countPal(start, end);
34 }
35 }
36

Display Month/Day
import java.util.Scanner;
2
3 public class Main {
4
5 public static void main(String[] args){
6 Scanner sc=new Scanner(System.in);
7 int n=0;
8 for(;n!=3;){
9 System.out.println("1.Display the Months");
10 System.out.println("2.Display the Days");
11 System.out.println("3.Exit");
12 n=sc.nextInt();
13 switch(n)
14 {
15 case 1:{
16 System.out.println("1.January");
17 System.out.println("2.February");
18 System.out.println("3.March");
19 System.out.println("4.April");
20 System.out.println("5.May");
21 System.out.println("6.June");
22 System.out.println("7.July");
23 System.out.println("8.August");
24 System.out.println("9.September");
25 System.out.println("10.October");
26 System.out.println("11.November");
27 System.out.println("12.December");
28 break;
29 }
30 case 2:{
31 System.out.println("1.Monday");
32 System.out.println("2.Tuesday");
33 System.out.println("3.Wednesday");
34 System.out.println("4.Thursday");
35 System.out.println("5.Friday");
36 System.out.println("6.Saturday");
37 System.out.println("7.Sunday");
38 break;
39 }
40 case 3: System.exit(0);
41
42 }
43
44 }
45 }
46 }
47

Power of a Number
import java.util.*;
2 public class Power
3 {
4 public static void main(String args[]){
5 Scanner scan = new Scanner (System.in);
6 long n, p;
7 System.out.println("Enter the number");
8 n = scan.nextLong();
9 System.out.println("Enter the digit");
10 p = scan.nextLong();
11 if(n < 0 || p < 0) {
12 System.out.println("Invalid input.");
13 return;
14 }
15 if(n == 0 && p == 0) {
16 System.out.println("Invalid input");
17 return;
18 }
19 if (n > 0 && p == 0) {
20 System.out.println("1");
21 return;
22 }
23 if(n == 0 && p > 0) {
24 System.out.println("0");
25 return;
26 }
27 else
28 System.out.println(power(n, p));
29 }
30 private static long power(long n, long p) {
31 long result = n;
32 for(int i=1; i<p; i++)
33 result *= n;
34 return result;
35 }
36 }
37

Factors of a Number
1 import java.util.*;
2 import java.lang.*;
3 public class FindFactor
4{
5 public static void main (String[] args) {
6 Scanner sc = new Scanner(System.in);
7 int num=Math.abs(sc.nextInt());
8 if(num==0)
9 {
10 System.out.println("No Factors");
11 }
12 else
13 {System.out.print("1");
14 for(int i=2;i<=num;i++)
15 {
16 if(num%i==0)
17 {
18 System.out.print(", "+i);
19 }
20 }
21 }
22 }
23 }

Lucky Number
import java.util.Scanner;
2 public class LuckyNum
3{
4 public static void main (String[] args) {
5 Scanner sc= new Scanner(System.in);
6 System.out.println("Enter the car no:");
7 int num=sc.nextInt();
8 if(num<1000 || num>9999)
9 {
10 System.out.println(num+" is not a valid car number");
11 }
12 else
13 {int sum=0;
14 while(num>0)
15 {
16 sum+=num%10;
17 num/=10;
18 }
19 if(sum%3==0 || sum%5==0 || sum%7==0)
20 {
21 System.out.println("Lucky Number");
22 }
23 else
24 {
25 System.out.println("Sorry its not my lucky number");
26 }
27 }
28 }
29 }

Number Palindrome
import java.util.Scanner;
2
3 public class Palindrome {
4
5 public static void main(String[] args) {
6
7 int lastDigit, sum = 0, a;
8 Scanner sc = new Scanner(System.in);
9 int num = sc.nextInt();
10 if (num < 0) {
11 System.out.println("Invalid Input");
12 } else {
13 a = num;
14
15
16 while (a > 0) {
17
18 lastDigit = a % 10;
19 sum = (sum * 10) + lastDigit;
20 a = a / 10;
21
22 }
23
24
25 if (sum == num)
26 System.out.println("Palindrome");
27 else
28 System.out.println("Not a Palindrome");
29
30 }
31
32 }
33 }
34

Find Number for Given Factorial


1 import java.util.Scanner;
2 public class FindNumber{
3 public static void main(String args[]){
4 Scanner in=new Scanner(System.in);
5 int count=0;
6 int n=in.nextInt();
7 if(n>0){
8 for(int i=1;i<=n;i++){
9 if(n%i==0){
10 n=n/i;
11 count=count+1;
12 }
13 else{
14 break;
15 }}
16 if(n==1){
17 System.out.print(count);
18 }else{
19 System.out.print("Sorry.The given number is not a perfect factorial");
20 }
21 }else if(n<=0)
22 {
23 System.out.print("Invalid Input");
24 }
25 }
26 }
27

List of prime numbers


import java.util.*;
2 public class PrimeNumbers
3{
4 public static void main (String[] args) {
5 Scanner sc=new Scanner(System.in);
6 int a=sc.nextInt();
7 int b=sc.nextInt();
8 int flag;
9 if(a<=0 || b<=0 || a>=b)
10
11 System.out.println("Provide valid input");
12 else
13 {
14 Inner:
15 while(a<=b)
16 {
17 if(a==2)
18 System.out.print(a+" ");
19 else if(a==1)
20 {
21 a++;
22 continue;
23 }
24 else
25 {
26 flag=0;
27 outer:
28 for(int i=2;i<=a/2;i++)
29 {
30 if(a%i==0)
31 {
32 flag=1;
33 break outer;
34 }
35 }
36 if(flag==0)
37 System.out.print(a+" ");
38 }
39 a++;
40 }
41 }
42 }
43 }

Student Details - Constructor


1 public class Student
2{
3 private int studentId;
4 private String studentName, studentAddress, collegeName;
5
6 public Student(int studentId, String studentName, String studentAddress)
7 {
8 this.studentAddress=studentAddress;
9 this.studentName=studentName;
10 this.studentId=studentId;
11 this.collegeName="NIT";
12 }
13
14 public Student(int studentId, String studentName, String studentAddress, String collegeName)
15 {
16 this(studentId, studentName, studentAddress);//invoking a constructor
17 this.collegeName=collegeName;
18 }
19
20 public int getStudentId()
21 {
22 return this.studentId;
23 }
24
25 public String getStudentName()
26 {
27 return this.studentName;
28 }
29
30 public String getStudentAddress()
31 {
32 return this.studentAddress;
33 }
34
35 public String getCollegeName()
36 {
37 return this.collegeName;
38 }
39 }

Book Detail
TestBook.java

import java.util.Scanner;
2 public class TestBook
3{
4 public static void main (String[] args) {
5 Scanner sc=new Scanner(System.in);
6
7 System.out.println("Enter the Book name:");
8 String bookname=sc.nextLine();
9
10 System.out.println("Enter the price:");
11 int price=sc.nextInt();
12 sc.nextLine();
13
14 System.out.println("Enter the Author name:");
15 String authorname=sc.nextLine();
16
17 Book obj=new Book();
18 obj.setBookName(bookname);
19 obj.setBookPrice(price);
20 obj.setAuthorName(authorname);
21 System.out.println("Book Details");
22 System.out.println("Book Name :"+obj.getBookName());
23 System.out.println("Book Price :"+obj.getBookPrice());
24 System.out.println("Author Name :"+obj.getAuthorName());
25 }
26 }
Book.java
1 public class Book
2{
3 private String bookName;
4 private int bookPrice;
5 private String authorName;
6
7 public void setBookName(String bookName)
8 {
9 this.bookName=bookName;
10 }
11
12 public String getBookName()
13 {
14 return this.bookName;
15 }
16
17 public void setBookPrice(int bookPrice)
18 {
19 this.bookPrice=bookPrice;
20 }
21
22 public int getBookPrice()
23 {
24 return this.bookPrice;
25 }
26
27 public void setAuthorName(String authorName)
28 {
29 this.authorName=authorName;
30 }
31
32 public String getAuthorName()
33 {
34 return this.authorName;
35 }
36 }

Student Details - Constructor


public class Student
2{
3 private int studentId;
4 private String studentName, studentAddress, collegeName;
5
6 public Student(int studentId, String studentName, String studentAddress)
7 {
8 this.studentAddress=studentAddress;
9 this.studentName=studentName;
10 this.studentId=studentId;
11 this.collegeName="NIT";
12 }
13
14 public Student(int studentId, String studentName, String studentAddress, String collegeName)
15 {
16 this(studentId, studentName, studentAddress);//invoking a constructor
17 this.collegeName=collegeName;
18 }
19
20 public int getStudentId()
21 {
22 return this.studentId;
23 }
24
25 public String getStudentName()
26 {
27 return this.studentName;
28 }
29
30 public String getStudentAddress()
31 {
32 return this.studentAddress;
33 }
34
35 public String getCollegeName()
36 {
37 return this.collegeName;
38 }
39 }
40
StudentMain.java
1 import java.util.Scanner;
2 public class StudentMain
3 { public static void main (String[] args)
4 {
5
6 Scanner sc=new Scanner(System.in);
7 System.out.println("Enter Student's Id:");
8 int studentId=sc.nextInt();
9 sc.nextLine();
10
11 System.out.println("Enter Student's Name:");
12 String studentName=sc.nextLine();
13
14 System.out.println("Enter Student's address:");
15 String studentAddress=sc.nextLine();
16
17 while(true)
18 {
19 System.out.println("Whether the student is from NIT(Yes/No):");
20 String answer=sc.nextLine();
21
22 if(answer.equalsIgnoreCase("yes"))
23 {
24 Student obj = new Student(studentId, studentName, studentAddress);
25 System.out.println("Student id:"+obj.getStudentId());
26 System.out.println("Student name:"+obj.getStudentName());
27 System.out.println("Address:"+obj.getStudentAddress());
28 System.out.println("College name:"+obj.getCollegeName());
29 break;
30 }
31
32 else if(answer.equalsIgnoreCase("no"))
33 {
34 System.out.println("Enter the college name:");
35 String collegename=sc.nextLine();
36
37 Student obj = new Student(studentId, studentName, studentAddress, collegename);
38
39 System.out.println("Student id:"+obj.getStudentId());
40 System.out.println("Student name:"+obj.getStudentName());
41 System.out.println("Address:"+obj.getStudentAddress());
42 System.out.println("College name:"+obj.getCollegeName());
43 break;
44 }
45
46 else
47 {
48 System.out.println("Wrong Input");
49 }
50 }
51 }
52 }
53

Volume calculator- Over Loading


import java.util.Scanner;
2
3 public class TestMain
4{
5
6
7 public static void main(String[] args)
8 {
9 System.out.println("Enter the choice : ");
10 System.out.println("1.Find the volume for cylinder");
11 System.out.println("2.Find the volume for cuboid");
12 Scanner sc = new Scanner(System.in);
13 VolumeCalculator vc=new VolumeCalculator();
14 int choice = sc.nextInt();
15
16 switch(choice)
17 {
18 case 1:
19 System.out.println("Enter the radius : ");
20 double radius = sc.nextDouble();
21 System.out.println("Enter the height : ");
22 double height = sc.nextDouble();
23 System.out.println("Volume of the cylinder is : "+ Math.round((vc.calculateVolume(radius, height)) *
100.0) / 100.0);
24 break;
25 case 2:
26 System.out.println("Enter the length : ");
27 int length = sc.nextInt();
28 System.out.println("Enter the breadth : ");
29 int breadth = sc.nextInt();
30
31 System.out.println("Enter the height : ");
32 int height1 = sc.nextInt();
33 System.out.println("Volume of the cuboid is : "+Math.round((vc.calculateVolume(length, breadth,
height1)) * 100.0) / 100.0);
34 break;
35 }
36
37 }
38
39 }
40

Ticket Price Calculation – Static


Main.java

import java.util.Scanner;
2 public class Main
3{
4 public static void main (String[] args) {
5 Scanner sc=new Scanner(System.in);
6 Ticket obj=new Ticket();
7
8 System.out.println("Enter no of bookings:");
9 int no_bookings=sc.nextInt();
10
11 System.out.println("Enter the available tickets:");
12 obj.setAvailableTickets(sc.nextInt());
13
14 while(no_bookings>0)
15 {
16 System.out.println("Enter the ticketid:");
17 obj.setTicketid(sc.nextInt());
18
19 System.out.println("Enter the price:");
20 obj.setPrice(sc.nextInt());
21
22 System.out.println("Enter the no of tickets:");
23 int no_tickets=sc.nextInt();
24 if(obj.calculateTicketCost(no_tickets)==-1)
25 {
26 continue;
27 }
28
29 System.out.println("Available tickets: "+obj.getAvailableTickets());
30
31 System.out.println("Total amount:"+obj.calculateTicketCost(no_tickets));
32
33 System.out.println("Available ticket after booking:"+obj.getAvailableTickets());
34
35 no_bookings--;
36 }
37 }
38 }
Ticket.java
public class Ticket
2{
3 private int ticketid;
4 private int price;
5 private static int availableTickets;
6
7 public void setTicketid(int ticketid)
8 {
9 this.ticketid=ticketid;
10 }
11
12 public int getTicketid()
13 {
14 return this.ticketid;
15 }
16
17 public void setPrice(int price)
18 {
19 this.price=price;
20 }
21
22 public int getPrice()
23 {
24 return this.price;
25 }
26
27 public static void setAvailableTickets(int availableTickets1)
28 { if(availableTickets1>=0)
29 {
30 availableTickets=availableTickets1;
31 }
32 }
33
34 public static int getAvailableTickets()
35 {
36 return availableTickets;
37 }
38
39 public int calculateTicketCost(int nooftickets)
40 {
41 if(availableTickets>=nooftickets)
42 {
43 availableTickets-=nooftickets;
44 return nooftickets*this.price;
45 }
46 else
47 {
48 return -1;
49 }
50 }
51 }

Employee Salary Calculation


import java.util.Scanner;
2 public class Main extends Employee {
3 public static Employee getEmployeeDetails() {
4 Scanner sc=new Scanner(System.in);
5 Employee emp=new Employee();
6 System.out.println("Enter Id:");
7 emp.setEmployeeId(sc.nextInt());
8 sc.nextLine();
9 System.out.println("Enter Name:");
10 emp.setEmployeeName(sc.nextLine());
11 System.out.println("Enter Salary:");
12 emp.setSalary(sc.nextDouble());
13
14
15 return emp;
16 }
17 public static int getPFPercentage() {
18 Scanner sc=new Scanner(System.in);
19 System.out.println("Enter PF percentage");
20 return sc.nextInt();
21 }
22 public static void main(String[] args) {
23 Employee emp=new Employee();
24 emp=getEmployeeDetails();
25 emp.calculateNetSalary(getPFPercentage());
26 System.out.println("Id: "+emp.getEmployeeId());
27 System.out.println("Name: "+emp.getEmployeeName());
28 System.out.println("Salary: "+emp.getSalary());
29 System.out.println("Net Salary: "+emp.getNetSalary());
30
31 }
32
33 }
34

Travel Details
TestMain.java
1
2 import java.util.*;
3 public class TestMain
4 {
5 public static BusTicket getTicketDetails()
6 {
7 BusTicket bt= new BusTicket();
8 Scanner sc= new Scanner(System.in);
9 System.out.println("Enter the ticket no:");
10 bt.setTicketNo(sc.nextInt());
11 System.out.println("Enter the ticket price:");
12 bt.setTicketPrice(sc.nextFloat());
13 return bt;
14 }
15 public static void main (String[] args) {
16 Scanner sc= new Scanner(System.in);
17 Person p= new Person();
18 System.out.println("Enter the passenger name:");
19 p.setName(sc.nextLine());
20 System.out.println("Enter the gender(M or F / m or f):");
21 p.setGender(sc.next().charAt(0));
22 System.out.println("Enter the age:");
23 p.setAge(sc.nextInt());
24 BusTicket bt= getTicketDetails();
25 bt.setPerson(p);
26 p= bt.getPerson();
27 System.out.println("Ticket no:"+bt.getTicketNo());
28 System.out.println("Passenger Name:"+p.getName());
29 System.out.println("Price of a ticket : "+bt.getTicketPrice());
30 bt.calculateTotal();
31 System.out.println("Total Amount : "+bt.getTotalAmount());
32 }
33 }
34
BusTicket.java
1
2 public class BusTicket
3 {
4 private int ticketNo;
5 private float ticketPrice;
6 private float totalAmount;
7 private Person person;
8 public void setPerson(Person person)
9 {
10 this.person=person;
11 }
12 public Person getPerson()
13 {
14 return person;
15 }
16 public void setTicketNo(int ticketNo)
17 {
18 this.ticketNo=ticketNo;
19 }
20 public void setTicketPrice(float ticketPrice)
21 {
22 this.ticketPrice=ticketPrice;
23 }
24 public void setTotalAmount(float totalAmount)
25 {
26 this.totalAmount= totalAmount;
27 }
28 public int getTicketNo()
29 {
30 return ticketNo;
31 }
32 public float getTicketPrice()
33 {
34 return ticketPrice;
35 }
36 public float getTotalAmount()
37 {
38 return totalAmount;
39 }
40 public void calculateTotal()
41 {
42 if(person.getAge()<=15)
43 {
44 setTotalAmount(ticketPrice/2);
45 }
46 else if(person.getAge()>=60)
47 {
48 setTotalAmount(ticketPrice*75/100);
49 }
50 else if(person.getGender()=='f'||person.getGender()=='F')
51 {
52 setTotalAmount(ticketPrice*90/100);
53 }
54 else
55 {
56 setTotalAmount(ticketPrice);
57 }
58 }
59 }

BankAccountDetails
import java.util.*;
2 public class AccountDetails
3{
4 public static Account getAccountDetails()
5 {
6 Account acc=new Account();
7 Scanner sc=new Scanner(System.in);
8 System.out.println("Enter account id: ");
9 acc.setAccountId(sc.nextInt());
10 sc.nextLine();
11 System.out.println("Enter account type: ");
12 acc.setAccountType(sc.nextLine());
13 int b;
14 do
15 {
16 System.out.println("Enter Balance");
17 acc.setBalance(sc.nextInt());
18 b=acc.getBalance();
19 if(b<=0)
20 System.out.println("Balance should be positive");
21 }
22 while(b<=0);
23 return acc;
24 }
25 public static int getWithdrawAmount()
26 {
27 Scanner sc=new Scanner(System.in);
28 int w;
29 do
30 {
31 System.out.println("Enter amount to be withdrawn:");
32 w=sc.nextInt();
33 if(w<=0)
34 System.out.println("Amount should be positive");
35 }
36 while(w<=0);
37 return w;
38 }
39 public static void main(String[] args)
40 {
41 Account accObj=new Account();
42 accObj=getAccountDetails();
43 int c=getWithdrawAmount();
44 accObj.withdraw(c);
45 }
46 }
Account.java
1 public class Account
2{
3 private int accountId;
4 private String accountType;
5 private int balance;
6 public int getAccountId()
7 {
8 return accountId;
9
10 }
11 public String getAccountType()
12 {
13 return accountType;
14 }
15 public int getBalance()
16 {
17 return balance;
18
19 }
20 public void setAccountId(int id)
21 {
22 accountId=id;
23 }
24 public void setAccountType(String s)
25 {
26 accountType=s;
27 }
28 public void setBalance(int b)
29 {
30 balance=b;
31 }
32 public boolean withdraw(int w)
33 {
34 if(getBalance()<w)
35 {
36 System.out.println("Sorry!!! No enough balance");
37 return false;
38 }
39 else
40 {
41 System.out.println("Balance amount after withdraw: "+(getBalance()-w));
42 return true;
43 }
44 }
45 }

Movie Ticket Calculation


Main.java
1 import java.util.*;
2 public class Main{
3 public static Movie getMovieDetails(){
4 Movie mov=new Movie();
5 Scanner s=new Scanner(System.in);
6 System.out.println("Enter the movie name:");
7 mov.setMovieName(s.nextLine());
8 System.out.println("Enter the movie category:");
9 mov.setMovieCategory(s.nextLine());
10 return mov;
11 }
12 public static String getCircle(){
13 Scanner s=new Scanner(System.in);
14 System.out.println("Enter the circle:");
15 return(s.nextLine());
16 }
17 public static void main (String[] args) {
18 Scanner s=new Scanner(System.in);
19 Movie m=getMovieDetails();
20 String circle=getCircle();
21 int type=m.calculateTicketCost(circle);
22 System.out.println("Movie name = "+m.getMovieName());
23 System.out.println("Movie category = "+m.getMovieCategory());
24 if(type==0)
25 System.out.println("The ticket cost is = "+m.getTicketCost());
26 if(type==-1)
27 System.out.println("Sorry there is no "+m.getMovieCategory()+" type of category in theater.");
28 if(type==-2)
29 System.out.println("Sorry!!! Circle is Invalid.");
30 if(type==-3)
31 System.out.println("Sorry!!! Both circle and category are Invalid.");
32 }
33 }
Movie.java
1 public class Movie{
2 private String movieName,movieCategory;
3 private int ticketCost;
4
5 public void setMovieName(String s){
6 movieName=s;
7 }
8 public void setMovieCategory(String s){
9 movieCategory=s;
10 }
11 public void setTicketCost(int i){
12 ticketCost=i;
13 }
14 public String getMovieName(){
15 return movieName;
16 }
17 public String getMovieCategory(){
18 return movieCategory;
19 }
20 public int getTicketCost(){
21 return ticketCost;
22 }
23 public int calculateTicketCost(String circle){
24 String cat=getMovieCategory();
25 if(circle.equalsIgnoreCase("GOLD")&& cat.equalsIgnoreCase("2d")){
26 setTicketCost(300);
27 //System.out.println("circle and cat=="+circle+cat);
28 return 0;
29 }
30 else if(circle.equalsIgnoreCase("GOLD")&& cat.equalsIgnoreCase("3d"))
31 {
32 setTicketCost(500);
33 return 0;
34 }
35 else if(circle.equalsIgnoreCase("SILVER")&&cat.equalsIgnoreCase("2d"))
36 {
37 setTicketCost(250);
38 return 0;
39 }
40 else if(circle.equalsIgnoreCase("SILVER")&&cat.equalsIgnoreCase("3d"))
41 {
42 setTicketCost(450);
43 return 0;
44 }
45 else if(circle.equalsIgnoreCase("GOLD")||circle.equalsIgnoreCase("SILVER"))
46 {
47 return -1;
48 }
49 else if(cat.equalsIgnoreCase("2d")||cat.equalsIgnoreCase("3d"))
50 {
51 return -2;
52 }
53 return -3;
54 }
55 }

Average and Grade Calculation


Student.java
1 public class Student
2{
3 private int id;
4 private String name;
5 private int marks[];
6 private float average;
7 private char grade;
8 public Student(int a,String b,int[] c)
9 {
10 id=a;
11 name=b;
12 marks=c;
13 }
14 public void setId(int n)
15 {
16 id=n;
17 }
18 public int getId()
19 {
20 return id;
21 }
22 public void setMarks(int[] marks)
23 {
24 this.marks=marks;
25 }
26 public int[] getMarks()
27 {
28 return marks;
29 }
30 public void setName(String n)
31 {
32 name=n;
33
34 }
35 public String getName()
36 {
37 return name;
38 }
39 public void setAverage(float n)
40 {
41 average=n;
42 }
43 public float getAverage()
44 {
45 return average;
46 }
47 public void setGrade(char n)
48 {
49 grade=n;
50 }
51 public char getGrade()
52 {
53 return grade;
54 }
55 public void calculateAvg()
56 {
57 float a1=0.0F;
58 for(int a=0;a<this.getMarks().length;a++)
59 {
60 a1=a1+this.marks[a];
61 }
62 this.setAverage(a1/getMarks().length);
63 }
64 public void findGrade()
65 {
66 int min=this.marks[0];
67 for(int b=0;b<this.getMarks().length;b++)
68 {
69 if(this.marks[b]<min)
70 {
71 min=this.marks[b];
72 }
73 }
74 if(min<50)
75 {
76 this.setGrade('F');
77 }
78 else if(this.getAverage()>=80 && this.getAverage()<=100)
79 {
80 this.setGrade('O');
81 }
82 else
83 {
84 this.setGrade('A');
85 }
86 }
87 }
StudentMain.java
1 import java.util.Scanner;
2 public class StudentMain
3{
4 public static void main(String[] args)
5 {
6 Student s=getStudentDetails();
7 s.calculateAvg();
8 s.findGrade();
9 System.out.println("Id:" +s.getId());
10 System.out.println("Name:" +s.getName());
11 System.out.println("Average:"+String.format("%.2f",s.getAverage()));
12 System.out.println("Grade:" +s.getGrade());
13 }
14 public static Student getStudentDetails()
15 {
16 Scanner sc=new Scanner(System.in);
17 System.out.println("Enter the id:");
18 int id=Integer.parseInt(sc.nextLine());
19 String name=sc.nextLine();
20 System.out.println("Enter the no of subjects:");
21 int n=sc.nextInt();
22 if(n<=0)
23 {
24 while(n<=0)
25 {
26 System.out.println("Invalid number of subject");
27 System.out.println("Enter the no of subjects");
28 n=sc.nextInt();
29 }
30 }
31 int arr[]=new int[n];
32 for(int a=0;a<n;a++)
33 {
34 System.out.println("Enter mark for subject "+(a+1)+":");
35 int b=sc.nextInt();
36 if(b<0||b>100)
37 {
38 System.out.println("Invalid Mark");
39 System.out.println("Enter mark for subject "+(a+1)+":");
40 b=sc.nextInt();
41 }
42 arr[a]=b;
43 }
44 Student obj=new Student(id,name,arr);
45 obj.setId(id);
46 obj.setName(name);
47 return obj;
48 }
49 }

Student and Department Detail


TestMain.java
1 import java.util.*;
2 public class TestMain {
3 public static Department d1;
4 public static Student s1;
5 public static Student createStudent(){
6 Scanner sc = new Scanner(System.in);
7 s1 = new Student();
8 d1 = new Department();
9 System.out.println("Enter the Department id:");
10 int did = sc.nextInt();
11 sc.nextLine();
12 System.out.println("Enter the Department name:");
13 String dname=sc.nextLine();
14 System.out.println("Enter the Student id:");
15 int sid = sc.nextInt();
16 sc.nextLine();
17 System.out.println("Enter the Student name:");
18 String sname=sc.nextLine();
19 d1.setDid(did);
20 d1.setDname(dname);
21 s1.setSid(sid);
22 s1.setSname(sname);
23 s1.setDepartment(d1);
24 return s1;
25 }
26 public static void main(String args[]){
27 s1=TestMain.createStudent();
28 System.out.println("Department id:"+d1.getDid());
29 System.out.println("Department name:"+d1.getDname());
30 System.out.println("Student id:"+s1.getSid());
31 System.out.println("Student name:"+s1.getSname());
32 }
33 }
Student.java
1 public class Student {
2 private int sid;
3 private String sname;
4 private Department department;
5 public void setSid(int sid) {
6 this.sid=sid;
7 }
8 public int getSid(){
9 return sid;
10 }
11 public void setSname(String sname) {
12 this.sname=sname;
13 }
14 public String getSname() {
15 return sname;
16 }
17 public void setDepartment(Department department){
18 this.department=department;
19 }
20 public Department getDepartment(){
21 return department;
22 }
23 }

Department.java
1 public class Department {
2 private int did;
3 private String dname;
4 public void setDid(int did){
5 this.did=did;
6 }
7 public int getDid(){
8 return did;
9 }
10 public void setDname(String dname){
11 this.dname=dname;
12 }
13 public String getDname() {
14 return dname;
15 }
16 }
Display array values
import java.util.Scanner;
2
3 public class Main
4 {
5 public static void main(String args[]) {
6 Scanner in = new Scanner(System.in);
7 System.out.println("Enter the array size");
8 int n = in.nextInt();
9
10 int arr[] = new int[n];
11 System.out.println("Enter the values");
12 for (int i = 0; i < n; i++) {
13 arr[i] = in.nextInt();
14 }
15
16
17 for (int i = 0; i < n; i++) {
18 System.out.println( arr[i]);
19
20 }
21
22
23 }
24 }

Display array values in reverse order


import java.util.Scanner;
2 class Main
3 {
4 public static void main(String[] args)
5 {
6 int n, res,i,j=0;
7 Scanner s = new Scanner(System.in);
8 System.out.print("Enter the array size:");
9 n = s.nextInt();
10 int array[] = new int[n];
11 int rev[] = new int[n];
12 System.out.println("Enter the values ");
13 for( i=0; i < n; i++)
14 {
15 array[i] = s.nextInt();
16 }
17
18 for( i=n;i>0 ; i--,j++)
19 {
20 rev[j] = array[i-1];
21 System.out.println(rev[j]);
22
23 }
24 }
25 }

Find & Display the position of a number


import java.util.Scanner;
2 public class Main
3 {
4 public static void main(String[] args)
5 {
6
7 int n, x, flag = 0, i = 0;
8 Scanner s = new Scanner(System.in);
9 System.out.println("Enter the array size,");
10 n = s.nextInt();
11 int a[] = new int[n];
12 System.out.println(" Enter the values");
13 for(i =0; i < n; i++)
14 {
15 a[i] = s.nextInt();
16 }
17 System.out.println(" Enter the number to find,");
18 x = s.nextInt();
19 for(i = 0; i < n; i++)
20 {
21 if(a[i] == x)
22 {
23 flag = 1;
24 break;
25 }
26 else
27 {
28 flag = 0;
29 }
30 }
31 if(flag == 1)
32 {
33 System.out.println((i + 1));
34 }
35 else
36 {
37 System.out.println( "0");
38 }
39 }
40 }

Sort the values


Main.java
1 import java.util.Scanner;
2
3 public class Main
4
5 {
6
7 public static void main(String[] args)
8
9 {
10
11 int n, temp;
12
13 Scanner s = new Scanner(System.in);
14
15 System.out.print("Enter the array size");
16
17 n = s.nextInt();
18
19 int a[] = new int[n];
20
21 System.out.println("Enter the values");
22
23 for (int i = 0; i < n; i++)
24
25 {
26
27 a[i] = s.nextInt();
28
29 }
30
31 for (int i = 0; i < n; i++)
32
33 {
34
35 for (int j = i + 1; j < n; j++)
36
37 {
38
39 if (a[i] > a[j])
40
41 {
42
43 temp = a[i];
44
45 a[i] = a[j];
46
47 a[j] = temp;
48
49 }
50
51 }
52
53 }
54
55
56
57 for (int i = 0; i < n - 1; i++)
58
59 {
60
61 System.out.print(a[i] + ",");
62
63 }
64
65 System.out.print(a[n - 1]);
66
67 }
68
69 }

Sort the values in descending order


Main.java
1 import java.util.Scanner;
2 public class Main
3{
4 public static void main(String[] args)
5 {
6 int n, temp;
7 Scanner s = new Scanner(System.in);
8 System.out.print("Enter the array size:");
9 n = s.nextInt();
10 int a[] = new int[n];
11 System.out.println("Enter the values:");
12 for (int i = 0; i < n; i++)
13 {
14 a[i] = s.nextInt();
15 }
16 for (int i = 0; i < n; i++)
17 {
18 for (int j = i + 1; j < n; j++)
19 {
20 if (a[i] < a[j])
21 {
22 temp = a[i];
23 a[i] = a[j];
24 a[j] = temp;
25 }
26 }
27 }
28 System.out.print("Descending Order:");
29 for (int i = 0; i < n - 1; i++)
30 {
31 System.out.print(a[i] + ",");
32 }
33 System.out.print(a[n - 1]);
34 }
35 }

Find the Player Details


Player.java
1
2 public class Player {
3
4 private int playerId;
5 private String playerName;
6 private int age;
7 private long phoneNumber;
8 public int getPlayerId() {
9 return playerId;
10 }
11 public void setPlayerId(int playerId) {
12 this.playerId = playerId;
13
14 }
15 public String getPlayerName() {
16 return playerName;
17 }
18 public void setPlayerName(String playerName) {
19 this.playerName = playerName;
20 }
21 public int getAge() {
22 return age;
23 }
24 public void setAge(int age) {
25 this.age = age;
26 }
27 public long getPhoneNumber() {
28 return phoneNumber;
29 }
30 public void setPhoneNumber(long phoneNumber) {
31 this.phoneNumber = phoneNumber;
32 }
33 public Player(int playerId, String playerName, int age, long phoneNumber) {
34 super();
35 this.playerId = playerId;
36 this.playerName = playerName;
37 this.age = age;
38 this.phoneNumber = phoneNumber;
39 }
40
41
42
43
44 }
45
PlayerUtility.java
1 import java.io.*;
2 import java.util.*;
3 public class PlayerUtility{
4 public Player findPlayerDetailsById(Player []arr,int playerIdToSearch){
5 int arrlength=arr.length;
6 for(int i=0;i<arrlength;i++){
7 if(arr[i].getPlayerId()==playerIdToSearch){
8 return arr[i];
9 }
10 }
11 return null;
12 }
13 }
14

Main.java
1 import java.io.*;
2 import java.util.*;
3 public class Main{
4 public static void main(String args[]){
5 Player[] pObj=new Player[4];
6 pObj[0]=new Player(146,"Athul",16,9876543210l);
7 pObj[1]=new Player(135,"Rakshana",16,9956231045l);
8 pObj[2]=new Player(168,"Christy",16,9856471230l);
9 pObj[3]=new Player(194,"Athulya",16,9768541230l);
10
11 Scanner sc=new Scanner(System.in);
12 System.out.println("Enter the id to be searched");
13 int x=sc.nextInt();
14 PlayerUtility obj=new PlayerUtility();
15 Player finded=obj.findPlayerDetailsById(pObj,x);
16 if(finded==null)
17 {
18 System.out.println("No player found");
19 }
20 else
21 { System.out.println("Name:"+finded.getPlayerName());
22 System.out.print("Phone number:"+finded.getPhoneNumber());
23 }
24
25 }
26 }

Pass and Fail Count


Count.java
1 import java.util.*;
2 public class Count {
3 public static void main (String[] args) {
4 Scanner sc=new Scanner(System.in);
5
6 System.out.println("Enter the no of subjects:");
7 int no_sub=sc.nextInt();
8
9 if(no_sub>0 && no_sub<=20)
10 {
11 int marks[]=new int[no_sub];
12
13 for(int i=0;i<no_sub;i++)
14 {
15 marks[i]=sc.nextInt();
16 }
17
18 int pass=0,fail=0;
19 for(int i=0;i<no_sub;i++)
20 {
21 if(marks[i]<50)
22 {
23 fail++;
24 }
25 else
26 {
27 pass++;
28 }
29 }
30
31 if(fail==0)
32 {
33 System.out.println("Ram passed in all subjects");
34 }
35 else if(pass==0)
36 {
37 System.out.println("Ram failed in all subjects");
38 }
39 else
40 {
41 System.out.println("Ram passed in "+pass+" subjects and failed in "+fail+" subjects");
42 }
43 }
44 else
45 {
46 System.out.println("Invalid input range");
47 }
48
49 }
50 }
Search a Course
Course.java
1 import java.util.*;
2 public class Course
3{
4 public static void main (String[] args) {
5 Scanner sc=new Scanner(System.in);
6 System.out.println("Enter no of course:");
7 int no_crs=sc.nextInt();
8 if(no_crs>0)
9 {
10 System.out.println("Enter course names:");
11
12 String crs[]=new String[no_crs];
13
14 for(int i=0;i<no_crs;i++)
15 {
16 crs[i]=sc.next();
17 }
18
19 System.out.println("Enter the course to be searched:");
20 String srch=sc.next();
21
22 int flag=0;
23 for(int i=0;i<no_crs;i++)
24 {
25 if(srch.equals(crs[i]))
26 {
27 flag++;
28 }
29 }
30
31 if(flag!=0)
32 {
33 System.out.println(srch+" course is available");
34 }
35 else
36 {
37 System.out.println(srch+" course is not available");
38 }
39 }
40 else
41 {
42 System.out.println("Invalid Range");
43 }
44
45 }
46 }

Sum of the maximum and the minimum element


Sum.java
1 import java.util.Scanner;
2
3
4 public class Sum {
5
6
7 public static void main(String[] args) {
8 Scanner sc = new Scanner(System.in);
9 System.out.println("Enter the size of an array:");
10 int arraySize = sc.nextInt();
11 //check if array size is less than or equal to zero
12 if(arraySize<=0)
13 {
14 //print the error message
15 System.out.println("Invalid Array Size");
16 }
17 else
18 {
19 //declare the array of size entered by user
20 int array[] = new int[arraySize];
21
22 //ask user to enter the array element
23 System.out.println("Enter the elements:");
24 for(int i=0;i<arraySize;i++)
25 {
26 array[i] = sc.nextInt();
27 }
28
29 int min =array[0];
30 int max = array[0];
31 //run for loop to calculate the min and max
32 for(int i=0;i<arraySize;i++)
33 {
34 if(array[i]<min)
35 {
36 min = array[i];
37 }
38 if(array[i]>max)
39 {
40 max = array[i];
41 }
42 }
43 //calculate the sum of min and max
44 int minMaxSum = min+max;
45 //print the sum
46 System.out.println(minMaxSum);
47 }
48 sc.close();
49
50
51 }
52
53
54 }
55

Cumulative sum in an array


import java.util.Scanner;
2 import java.util.StringJoiner;
3
4 class CumulativeSum {
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 int n;
8
9 System.out.println("Enter the number of elements");
10 n = scanner.nextInt();
11
12 if (n <= 0) {
13 System.out.println("Invalid Range");
14 } else {
15 int sum = 0;
16 StringJoiner stringJoiner = new StringJoiner(" ");
17
18 System.out.println("Enter the elements");
19
20 for (int i = 0; i < n; ++i) {
21 int num = scanner.nextInt();
22 sum += num;
23 stringJoiner.add(String.valueOf(sum));
24 }
25
26 System.out.println(stringJoiner.toString());
27 }
28 }
29 }
30
31

Array Compatiblilty
CompatibleArrays.java
1 import java.util.*;
2 public class CompatibleArrays
3 {
4 public static void main (String[] args) {
5 Scanner sc=new Scanner(System.in);
6 System.out.println("Enter the size for First array:");
7 int size1=sc.nextInt();
8 if(size1<=0)
9 System.out.println("Invalid array size");
10 else
11 {
12 int arr1[]=new int[size1];
13 System.out.println("Enter the elements for First array:");
14 for(int i=0;i<size1;i++)
15 arr1[i]=sc.nextInt();
16 System.out.println("Enter the size for Second array:");
17 int size2=sc.nextInt();
18 if(size2<=0)
19 System.out.println("Invalid array size");
20 else
21 {
22 int count=0;
23 int arr2[]=new int[size2];
24 System.out.println("Enter the elements for Second array:");
25 for(int i=0;i<size2;i++)
26 {
27 arr2[i]=sc.nextInt();
28 }
29 if(size1==size2)
30 {
31 for(int i=0;i<size1;i++)
32 {
33 if(arr1[i]>=arr2[i])
34 count++;
35 }
36 if(count==(size1))
37 System.out.println("Arrays are Compatible");
38 else
39 {
40 System.out.println("Arrays are Not Compatible");
41 }
42 }
43 else
44 System.out.println("Arrays are not Compatible");
45 }
46 }
47 }
48 }

Sort the first and second half of an array


ArraySort.java
1 import java.util.*;
2 public class ArraySort
3{
4 public static void main (String[] args) {
5 Scanner sc=new Scanner(System.in);
6
7 System.out.println("Enter the size of an array:");
8 int n=sc.nextInt();
9
10 if(n>0)
11 {
12 int[] arr=new int[n];
13 System.out.println("Enter the elements:");
14 for(int i=0;i<n;i++)
15 {
16 arr[i]=sc.nextInt();
17 }
18
19 int[] arr1=Arrays.copyOfRange(arr, 0, (n+1)/2);
20 int[] arr2=Arrays.copyOfRange(arr, (n+1)/2, n );
21
22 Arrays.sort(arr1);
23 //Arrays.sort(arr2, Collections.reverseOrder());
24
25 for(int i=0;i<n/2;i++)
26 {
27 for(int j=0;j<(n/2)-1;j++)
28 {
29 if(arr2[j]<arr2[j+1])
30 {
31 int temp=arr2[j];
32 arr2[j]=arr2[j+1];
33 arr2[j+1]=temp;
34 }
35 }
36 }
37
38 int len2=arr1.length+arr2.length;
39 int[] res=new int[len2];
40
41 System.arraycopy(arr1, 0, res, 0, (n+1)/2 );
42 System.arraycopy(arr2, 0, res, (n+1)/2, n/2 );
43
44 for(int i : res)
45 {
46 System.out.println(i);
47 }
48 }
49 else
50 {
51 System.out.println("Array size should be greater than 0");
52 }
53 }
54 }

Highest Mark in Each Semester


import java.util.*;
2 import java.util.stream.Collectors;
3
4 class Semester {
5 private final int subjects;
6 List<Integer> marks;
7
8 public Semester(int subjects) {
9 this.subjects = subjects;
10 marks = new ArrayList<>(subjects);
11 }
12
13 public void setMarks(int[] marks) {
14 this.marks.addAll(Arrays.stream(marks).boxed().collect(Collectors.toList()));
15 }
16
17 public int getSubjects() {
18 return subjects;
19 }
20
21 public int getMaximumMarks() {
22 return Collections.max(marks);
23 }
24 }
25
26 class HighestMarkPerSem {
27 public static void main(String[] args) {
28 Scanner scanner = new Scanner(System.in);
29 int sems = 0;
30
31 System.out.println("Enter no of semester:");
32 sems = scanner.nextInt();
33
34 if (sems <= 0) {
35 System.out.println("Invalid Input");
36 } else {
37 List<Semester> semesters = new ArrayList<>();
38
39 for (int i = 0; i < sems; ++i) {
40 int subjects;
41
42 System.out.println("Enter no of subjects in " + (i + 1) + " semester:");
43 subjects = scanner.nextInt();
44
45 if (subjects < 0) {
46 System.out.println("Invalid Input");
47 System.exit(0);
48 } else {
49 Semester semester = new Semester(subjects);
50 semesters.add(semester);
51 }
52 }
53
54 for (int i = 0; i < sems; ++i) {
55 Semester semester = semesters.get(i);
56 int subjects = semester.getSubjects();
57 int[] marks = new int[subjects];
58
59 System.out.println("Marks obtained in semester " + (i + 1) + ":");
60
61 for (int j = 0; j < subjects; ++j) {
62 int mark = scanner.nextInt();
63
64 if (mark < 0 || mark > 100) {
65 System.out.println("You have entered invalid mark.");
66 System.exit(0);
67 } else {
68 marks[j] = mark;
69 }
70 }
71
72 semester.setMarks(marks);
73 }
74
75 for (int i = 0; i < semesters.size(); i++) {
76 System.out.println("Maximum mark in " + (i + 1) + " semester:" +
semesters.get(i).getMaximumMarks());
77 }
78 }
79 }
80 }

Sum of factorial of positive and single digit numbers in an


array
Factorial.java
1 import java.util.*;
2
3
4 public class Factorial
5
6{
7
8 public static void main(String[] args)
9
10 {
11
12
13
14
15
16 Scanner sc = new Scanner(System.in); //System.in is a standard input stream
17
18 System.out.println("Enter the size of an array:");
19
20 int size = sc.nextInt();
21
22 int[] arr = new int[size];
23 System.out.println("Enter the elements:");
24 for (int i = 0; i < size; i++)
25
26 {
27
28
29
30 int number = sc.nextInt();
31
32 arr[i] = number;
33
34 }
35
36
37
38 int sum = 0;
39
40
41
42 for (int i = 0; i < size; i++)
43
44 {
45
46 if (arr[i] < 10 && arr[i] > 0)
47
48 {
49
50 int val = calc(arr[i]);
51
52 sum += val;
53
54 }
55
56 }
57
58 if (sum == 0) {
59 System.out.println("No positive and single digit numbers found in an array");
60 } else {
61 System.out.println(sum);
62 }
63
64
65 }
66
67
68
69 static int calc(int val)
70
71 {
72
73 int i, fact = 1;
74
75 for (i = 1; i <= val; i++) {
76
77 fact = fact * i;
78
79 }
80
81
82
83 return fact;
84
85 }
86
87
88
89 }
90

String length
Main.java
1 import java.util.Scanner;
2
3 public class Main {
4
5 public static void main(String[] args){
6 Scanner sc=new Scanner(System.in);
7 String str=sc.nextLine();
8 int len=str.length();
9 if(len%2 ==0){
10 System.out.println(len+" even");
11 }
12 else{
13 System.out.println(len+" odd");
14 }
15 }
16 }
17

Count of alphabets
import java.util.Scanner;
2
3 public class Main {
4
5 public static void main(String[] args){
6 Scanner sc=new Scanner(System.in);
7 String str=sc.nextLine();
8 int count=0;
9 for(int i=0;i<str.length();i++){
10 if(Character.isLetter(str.charAt(i)))
11 {
12 count++;
13 }
14 System.out.println(count);
15 }
16 }
17
18 }
19
20
21
SubString
2 import java.util.Scanner;
3
4 public class Main {
5
6 public static void main(String[] args){
7 Scanner sc=new Scanner(System.in);
8 String str=sc.nextLine();
9 int start=sc.nextInt();
10 int end=sc.nextInt();
11 System.out.println(str.substring(start,end));
12 }
13
14 }
15
String Contains
Main.java
1 import java.util.Scanner;
2
3 public class Main {
4
5 public static void main(String[] args){
6 Scanner sc=new Scanner(System.in);
7 String str=sc.nextLine();
8 String sub_str=sc.nextLine();
9 if(str.indexOf(sub_str)!=-1){
10 System.out.println(sub_str+" is contained in a sentence");
11 }
12 else{
13 System.out.println(sub_str+" is not contained in a sentence");
14 }
15 }
16
17 }
18

Compare two strings


Main.java
1 import java.util.Scanner;
2
3 public class Main {
4
5 public static void main(String[] args){
6 Scanner sc=new Scanner(System.in);
7 String str1= sc.nextLine();
8 String str2=sc.nextLine();
9 if(str1.equals(str2))
10 System.out.println("Case sensitive");
11 else if(str1.equalsIgnoreCase(str2))
12 System.out.println("Case insensitive");
13 else
14 System.out.println("Not equal");
15 }
16
17 }
18
String Spilt-Up
Main.java
1 import java.util.*;
2
3 public class Main {
4
5 public static void main(String[] args){
6 Scanner sc=new Scanner(System.in);
7 String str=sc.nextLine();
8 String splt=sc.nextLine();
9
10 String[] spltstr=str.split(splt);
11 int len=spltstr.length;
12 for(int i=0;i<len;i++){
13 System.out.println(spltstr[i]);
14
15 }
16
17 }
18
19 }
20
21

String - Find and replace the character (First occurrence)


FirstOccurence.java
1 import java.util.*;
2 public class FirstOccurence
3{
4 public static void main (String[] args) {
5 Scanner sc=new Scanner(System.in);
6 System.out.println("Enter the string:");
7 char[] str=sc.nextLine().toCharArray();
8
9 System.out.println("Enter the character to be searched:");
10 char srch=sc.next().charAt(0);
11
12 System.out.println("Enter the character to replace:");
13 char replace=sc.next().charAt(0);
14
15 int flag=0;
16 int len=str.length;
17 for(int i=0;i<len;i++)
18 {char r=str[i];
19 if(r==srch)
20 {
21 str[i]=replace;
22 flag++;
23 break;
24 }
25 }
26 if(flag==0)
27 {
28 System.out.println("character not found");
29 }
30 else
31 {
32 for(char i : str)
33 {
34 System.out.print(i);
35 }
36 }
37 }
38 }

String Concatenation
Authority.java
1 import java.util.*;
2 class Authority
3 {
4 public static void main (String[] args) {
5 Scanner sc=new Scanner(System.in);
6 System.out.print("Inmate's name:");
7 String name=sc.nextLine();
8 System.out.print("Inmate's father's name:");
9 String fname=sc.nextLine();
10 name=name.concat(" ").concat(fname);
11 char []chars=name.toCharArray();
12 for(char c:chars)
13 {
14 if(Character.isDigit(c)||c=='+'||c=='!'||c=='@'||c=='#'||c=='%'||c=='$'||c=='^'||c=='&'||c=='*'||c=='('
15 ||c==')'||c=='-'||c=='='||c=='/'||c=='<'||c=='>'||c=='?'||c=='{'||c=='}'||c=='['||c==']')
16 {
17 System.out.print("Invalid name");
18 System.exit(0);
19 }
20 }
21 System.out.println(name.toUpperCase());
22 }w
23 }

Length of String
import java.util.*;
2
3 class FindLength {
4
5 public static void main(String[] args) {
6
7 String testString;
8
9 Scanner in = new Scanner(System.in);
10
11 System.out.println("Enter String : ");
12
13 testString = in .nextLine();
14
15 for (int i = 0; i < testString.length(); i++) {
16
17 if ((testString.charAt(i) >= 'A' && testString.charAt(i) <= 'Z') || (testString.charAt(i) >= 'a' &&
testString.charAt(i) <= 'z') || testString.charAt(i) == ' ')
18
19 {
20
21 continue;
22
23 } else
24
25 {
26
27 System.out.println("Invalid String.");
28
29 System.exit(0);
30
31 }
32
33 }
34
35 System.out.println("No. of characters is : " + testString.length());
36
37 }
38
39 }
40
41

PAN Card Validation


PanCard.java
1 import java.util.*;
2 public class PanCard{
3 public static void main (String[] args) {
4 Scanner in=new Scanner(System.in);
5 System.out.println("Enter the PAN no:");
6 String s1=in.nextLine();
7 if(s1.matches("[A-Z]{5}[0-9]{4}[A-Z]{1}"))
8 {
9 System.out.println("Valid PAN no");
10 }
11 else{
12 System.out.println("Invalid PAN no");
13 }
14 }
15 }
Print unique characters
import java.util.*;
2
3 class UniqueChar {
4 private static boolean printUnique(String sentence) {
5 char[] chars = sentence.toCharArray();
6 Map<Character, Integer> map = new LinkedHashMap<>();
7
8 for (char ch : chars) {
9 if (Character.isDigit(ch)) {
10 return false;
11 } else if (!Character.isWhitespace(ch)) {
12 map.put(ch, map.getOrDefault(ch, 0) + 1);
13 }
14 }
15
16 List<Character> uniqueCharacters = new ArrayList<>();
17
18 for (char key : map.keySet()) {
19 if (map.get(key) == 1) {
20 uniqueCharacters.add(key);
21 }
22 }
23
24 if (uniqueCharacters.isEmpty()) {
25 System.out.println("No unique characters");
26 } else {
27 System.out.println("Unique characters:");
28
29 for (char ch : uniqueCharacters) {
30 System.out.println(ch);
31 }
32 }
33
34 return true;
35 }
36
37 public static void main(String[] args) {
38 Scanner scanner = new Scanner(System.in);
39 String sentence;
40
41 System.out.println("Enter the sentence:");
42 sentence = scanner.nextLine();
43
44 if (!printUnique(sentence)) {
45 System.out.println("Invalid Sentence");
46 }
47 }
48 }
49

InitCap
InitCap.java
1 import java.util.*;
2 public class InitCap
3 {
4 static void convert(String str)
5 {
6 int count=0;
7 char []ch=str.toCharArray();
8 for(int i=0;i<str.length();i++)
9 {
10 if(i==0&&ch[i]!=' '||ch[i]!=' '&&ch[i-1]==' ')
11 {
12 if(ch[i]>='a'&& ch[i]<='z')
13 {
14 ch[i]=(char)(ch[i]-'a'+'A');
15 count++;
16 }
17 }
18 else if(ch[i]>='A'&&ch[i]<='Z')
19 ch[i]=(char)(ch[i]+'a'-'A');
20 }
21 if(count==0)
22 System.out.println("First character of each word is already in uppercase");
23 else
24 {
25 String st=new String(ch);
26 System.out.println(st);
27 }
28 }
29 public static void main (String[] args) {
30 Scanner sc=new Scanner(System.in);
31 System.out.println("Enter the String:");
32 String str=sc.nextLine();
33 convert(str);
34 }
35 }

Palindrome
Palindrome.java
1 import java.util.*;
2 import java.util.regex.Matcher;
3 import java.util.regex.Pattern;
4 public class Palindrome
5{
6 public static void main (String[] args) {
7 Scanner sc= new Scanner(System.in);
8
9 System.out.println("Enter the word :");
10 String str=sc.nextLine();
11 int len=str.length();
12
13 Pattern p=Pattern.compile("[^a-z0-9]", Pattern.CASE_INSENSITIVE);
14 Matcher m=p.matcher(str);
15 boolean b=m.find();
16
17 if(b)
18 {
19 System.out.println("Invalid Input");
20 }
21 else
22 {
23 String rvs=new String();
24
25 for(int i=(len-1); i>=0; i--)
26 {
27 rvs=rvs+str.charAt(i);
28 }
29
30 if(str.equalsIgnoreCase(rvs))
31 {
32 System.out.println(str+" is a Palindrome");
33 }
34 else
35 {
36 System.out.println(str+" is not a Palindrome");
37 }
38 }
39 }
40 }
Find and Replace
ReplaceWord.java
1 import java.util.*;
2 public class ReplaceWord{
3 public static void main (String[] args) {
4 Scanner in=new Scanner(System.in);
5 System.out.println("Enter the String:");
6 String a=in.nextLine();
7 Character dot=a.charAt(a.length()-1);
8 System.out.println("Enter the word to be searched:");
9 String b=in.nextLine();
10 System.out.println("Enter the word to be replaced:");
11 String n=in.nextLine();
12 String res="";
13 int count=0;
14 String[] words=a.split("\\W");
15
16 for(String word:words){
17 if(word.equals(b)){
18 res=res+n+" ";
19 count=count+1;
20 }
21 else{
22 res=res+word+" ";
23 }
24 }
25 if(count>=1){
26 System.out.print(res.trim());
27 if(dot=='.')
28 System.out.print(".");
29 }
30 else{
31 System.out.print("The word "+b+" not found");
32 }
33 }
34 }

You might also like