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

Std-12 Java Programming

Uploaded by

asphalt9.ved
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Std-12 Java Programming

Uploaded by

asphalt9.ved
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

JAVA PROGRAMMING

1. Java program to print welcome message.


class message
{
public static void main(String [] args)
{
System.out.println("Welcome");
}
}
2. Java program to print total bill amount.
public class billamount
{
public static void main(String [] args)
{
float qty, amt, rate;
qty = 5; rate = 10;
amt = qty * rate;
System.out.println("Total Bill amount is : - " +amt);
}
}

3. Java program to print area of circle.


public class areaofcircle
{
public static void main(String [] args)
{
double pi, r, area;
pi = 3.14; r = 5;
area = pi * r * r;
System.out.println("Area of Circle is : - " +area);
}
}

4. Java program to print simple interest.


public class simpleinterest
{
public static void main(String [] args)
{
double p, r, i, n, t;
p = 10000; r = 8; n = 5;
i = (p * r * n)/100;
t = p + i;
System.out.println("Interest is :- " +i);
System.out.println("Total amount is: " +t);
}
}

5. Java program to perform Arithmetic operation.


public class arithmaticoperation
{
public static void main(String [] args)
{
int a = 5, b = 2;
System.out.println("The sum of a and b is:- " +(a+b));
System.out.println("The subtraction of a and b is:- " +(a-b));
System.out.println("The Multiplication of a and b is:- " +(a*b));
float x = 5, y = 2;
System.out.println("The Division is:- " +(x/y));
}

}
6. Java Program to check whether the student is pass or fail.
public class passfail
{
public static void main(String [] args)
{
int marks = 50;
if (marks>=33)
System.out.println("You are Pass.");
else
System.out.println("You are Fail.");
}
}

7. Java program to check given number is even or odd.


public class evenodd
{
public static void main(String [] args)
{
int n = 7;
if (n%2 == 0)
System.out.println("Number is Even.");
else
System.out.println("Number is Odd.");
}
}

8. Java program to check entered year is leap year or not.


public class leapyear
{
public static void main(String [] args)
{
int year = 2000;
if (year % 4 == 0)
System.out.println("Yes It is Leap Year.");
else
System.out.println("No, It is not leap year.");
}
}

9. Java program to find the larger number.


public class largernumber
{
public static void main(String [] args)
{
int m1 = 80, m2 = 60;
if (m1>m2)
System.out.println("m1 is larger.");
else
System.out.println("m2 is larger.");
}
}

10. Java program to check whether eligible for vote or not.


public class voting
{
public static void main(String [] args)
{
int age = 12;
if (age>=18)
System.out.println("Eligible for voting.");
else
System.out.println("Not eligible for voting.");
}
}

11. Java program to print 1 to 10 using While loop.


public class Looping
{
public static void main(String [] args)
{
int i = 0;
while (i<11)
{
System.out.println(i++);
}

}
}
12. Java program to check whether entered number is prime or not.
import java.util.Scanner;
class Prime
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a number to check if it is truly prime number or not: ");
int number= sc.nextInt();
if(isPrime(number))
{
System.out.println(number + " is prime number");
}
else
{
System.out.println(number + " is a non-prime number");
}
}
static boolean isPrime(int num)
{
if(num<=1)
{
return false;
}
for(int i=2;i<=num/2;i++)
{
if((num%i)==0)
return false;
}
return true;

}
}
13. Java program to print odd number series using while loop.
public class oddseries
{
public static void main(String [] args)
{
int i = 1;
while (i<=30)
{
System.out.println(i);
i = i + 2;
}

}
}
14. Java program to print even number series using For loop.
public class evenseries
{
public static void main(String [] args)
{
for(int i=2; i<=30; i=i+2)
{
System.out.println(i);
}

}
}

15. Java program to find the Grade of Students.


public class grade
{
public static void main(String [] args)
{
int marks = 72;
char grade;
if (marks >=70)
grade = 'A';
else if (marks >=60)
grade = 'B';
else if (marks>=50)
grade = 'C';
else if (marks>=33)
grade = 'D';
else
grade = 'E';
System.out.println("Grade is : " +grade);
}
}
16. Java program to convert the string in uppercase.
public class uppercase
{
public static void main(String [] args)
{
String str = "computer";
System.out.println("String is : - " +str);
System.out.println("Upper case is : - " +str.toUpperCase());
}
}
17. Java program to convert the string in lowercase.
public class lowercase
{
public static void main(String [] args)
{
String str = "COMPUTER";
System.out.println("String is : - " +str);
System.out.println("Upper case is : - " +str.toLowerCase());
}
}
18. Java program to perform basic mathematical operation.
public class mathoperation
{
public static void main(String [] args)
{
int a=5, b=2;
System.out.println("Sum is : " +(a+b));
System.out.println("Sum is : " +(a-b));
System.out.println("Sum is : " +(a*b));
System.out.println("Sum is : " +(a%b));
float x=5, y=2;
System.out.println("Division is: " +(x/y));
}
}
19. Java program to swap numbers.
public class SwapNumbers
{
public static void main(String[] args)
{
int first = 12, second = 15;
System.out.println("--Before swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
// Value of first is assigned to temporary
int temporary = first;
// Value of second is assigned to first
first = second;
// Value of temporary (which contains the initial value of first) is assigned to second
second = temporary;
System.out.println("--After swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
}
}
20. Java Program to reverse String.
import java.lang.*;
import java.io.*;
import java.util.*;
public class strReverse
{
public static void main(String[] args)
{
String stringInput = "WELCOME";
//Get the String length
int iStrLength=stringInput.length();

//Using For loop

for(iStrLength=stringInput.length();iStrLength >0;-- iStrLength)

{
System.out.print(stringInput.charAt(iStrLength -1));
}

}
21. Java program to find out square root and power of a number.
import java.lang.Math;
public class math
{
public static void main(String [] args)
{
//Square root
double a = 16;
System.out.println("Square Root : " +Math.sqrt(a));

//Power
double b = 5, c=2;
System.out.println("Power : " +Math.pow(b,c));
}
}
22. Java program to compare string.
public class compare
{
public static void main(String [] args)
{
String s1 = "abc";
String s2 = "abc";
System.out.println(s1.compareTo(s2));
String s3 = "Abc";
String s4 = "abc";
System.out.println(s3.compareTo(s4));
String s5 = "abc";
String s6 = "ABC";
System.out.println(s5.compareTo(s6));
}
}

public class equals


{
public static void main(String [] args)
{
//Same word or string
String s1 = "Computer";
String s2 = "computer";
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));
//Uppercase / Lowercase
String m1="COMPUTER";
String m2="computer";
System.out.println(m1.equals(m2));
System.out.println(m1.equalsIgnoreCase(m2));
//Different String
String b1="computer";
String b2="welcome to HBK";
System.out.println(b1.equals(b2));
System.out.println(b1.equalsIgnoreCase(b2));
}
}
23. Java program to display current date and time.
import java.util.*;
public class dateandtime
{
public static void main(String [] args)
{
System.out.println("Date and time is : " +new Date());
}
}

You might also like