Java File
Java File
OPERATORS :
1- Arithemic Operators
2- class Arithemic{
3- public static void main(String[] args) {
4- int a= 15,b=3;
5- System.out.println(a+b);
6- System.out.println(a-b);
7- System.out.println(a*b);
8- System.out.println(a/b);
9- System.out.println(a%b);
2 –Relational Operators
Class Relational{
public static void main(String[] args) {
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a>=b);
System.out.println(a<=b);
System.out.println(a==b);
System.out.println(a!=b);
}
}
3-Assignment Operators
class Assi {
public static void main(String[] args) {
int a=5,b=7;
System.out.println(a);
a=a+b;
System.out.println(a);
a+=b; /*a=a+b */
System.out.println(a);
a-=b;/*a=a-b; */
System.out.println(a);
a*=b;/*a=a*b */
System.out.println(a);
a/=b;/*a=a/b */
System.out.println(a);
a%=b;/*a=a%b */
System.out.println(a);
}
}
4- Bitwise Operators
class Bitwise {
public static void main(String[] args) {
int a= 17,b=26;
int p=15;
int r=-17;
5- Unary Operators
class Unary{
public static void main(String[] args) {
boolean a= true,b=false;
int x=55;
System.out.println(++x);
System.out.println(x++);
System.out.println(!a);
System.out.println(!b);
}
}
6- Logical Operators:
class Logic{
public static void main(String[] args) {
boolean a=true;
boolean b=false;
System.out.println(a && a);
System.out.println(a && b);
System.out.println(b && a);
System.out.println(b && b );
System.out.println(a||a);
System.out.println(a || b);
System.out.println(b || a);
System.out.println(b|| b );
}}
7-Shift Operator:
class Shift{
public static void main(String[] args) {
int a=5;
int b=3;
System.out.println(a>>b);
System.out.println(a<<b);
System.out.println(a>>>b);
}
}
Area of Circle:
import java.util.Scanner;
class Areaofcircle{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
float pi=3.14f;
float r= sc.nextInt();
float formula=pi*r*r;
System.out.println(formula);
}
}
Area of triangle:
import java.util.Scanner;
class Triangle{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int h=sc.nextInt();
int b=sc.nextInt();
int area_of_triangle=h*b/2;
System.out.println(area_of_triangle);
}
}
// area of rectangle
import java.util.Scanner;
class Practise{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int l=sc.nextInt();
int b=sc.nextInt();
int area_of_rectangle=l*b;
System.out.println( area_of_rectangle );
class Swapping {
public static void main(String[] args) {
int a = 10;
int b = 20;
int temp = a;
a = b;
b = temp;
System.out.println(a);
System.out.println(b);
}
}
Conditional Statements:-
1 if-else
class Condition {
public static void main(String[] args) {
int a=10;
if(a>10)
{
System.out.println("hello sir");
}
else
{
System.out.println("hello maam");
} }}
2nd Program
import java.util.Scanner;
class Condition1{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
if(num%2==0)
{
System.out.println("entered number is even");
}
else{
System.out.println("entered number id odd");
}}}
3rd Program
import java.util.Scanner;
class Condition1{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int age=sc.nextInt();
if(age>=18){
System.out.println("person can vote");
}
else{
System.out.println("person can not vote");
}}}
4th Program
import java.util.Scanner;
class Normal_Vowel{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter Your Alphabet:");
char x=sc.next().charAt(0);
if(x=='a'||x=='e'||x=='i'||x=='o'||x=='u'){
System.out.println(x+" Alphabet iss Vowel.");
}
else
{
System.out.println(x+"Alphabet is not Vowel."); } }}
6th program
import java.util.Scanner;
class Condition1{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String vow=sc.nextLine();
if(vow.equals("a") || vow.equals("e") || vow.equals("i") || vow.equals("o") || vow.equals("u"))
{
System.out.println(vow+" is vowel");
}
else{
System.out.println(vow +" not vowel");
}}}
5th Program
import java.util.Scanner;
class June12{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=50;
int b=60,c=70;
double percentage=a+b+c/3;
if (percentage<32)
System.out.println("u are fail");
else if(percentage>=33||percentage<44)
System.out.println("you are pass with third division");
else if (percentage>=45||percentage<59)
System.out.println("pass with second diviion");
else if (percentage>=60)
System.out.println("pass with third diision");
else{
System.out.println("fail");
}
import java.util.Scanner;
class Elseif{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter your symbol");
char c = sc.next().charAt(0);
int a=10,b=20;
if(c=='+')
System.out.println("Sum"+(a+b));
else if(c=='-')
System.out.println("Sub"+(a-b));
else if(c=='*')
System.out.println("mul"+(a*b));
else if(c=='/')
System.out.println("div"+(a/b));
else
System.out.println("you entered wrong symbol");
}}
Switch Condition:-
import java.util.Scanner;
class Switch {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int c=sc.nextInt();
int a=10,b=20;
switch(c){
case 1:
System.out.println(a+b);
break;
case 2:
System.out.println(a-b);
break;
case 3:
System.out.println(a*b);
break;
case 4:
System.out.println(a/b);
break;
default :
System.out.println("Wrong Choice");
} } }
Leap year:
import java.util.Scanner;
class Leap1{
import java.util.Scanner;
class Leap{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int a =sc.nextInt();
if (a%400==0||a%100==0&&a%4==0)
{System.out.println("entered number is leap year");
}
else {
System.out.println("enter number is not leap year");
int t=4-a%4;
System.out.println("next leap year="+(a++));
} }}
LOOPS:-
While Loop:
import java.util.Scanner;
class June12{
public static void main(String[] args) {
int a =5;
int i=0;
while(i<10)
{i++;
System.out.println("5x"+i+"="+a*i);}}}
To print the Factorial of the given number.
import java.util.Scanner;
class Switch {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int a= sc.nextInt();
int i=1,f=1;
while(i<=a)
{
f=f*i;
i++;
}
System.out.println(f);
To check it is a Prime number or Not .
import java.util.Scanner;
class Switch {
public static void main(String[] args) {
int i=2;
int a=13,c=0;
while(i<a)
{
if(a%i==0)
c++;
i++;
}
if(c==0){
System.out.println("prime");
}
else
{
System.out.println("not prime");
}}}
Sum of digit:
import java.util.Scanner;
class Switch {
public static void main(String[] args) {
int a =579;
int s=0;
while(a>0){
int r= a%10;
s=s+r;
// System.out.println(s);
a=a/10;
System.out.println("sum of digits "+s);
}}}
TO check number is Palindrome or NOT using while loop.
import java.util.Scanner;
class Switch {
public static void main(String[] args) {
int a =121;
int t =a;
int s=0;
while(a>0){
int r= a%10;
s=s*10+r;
System.out.println(s);
a=a/10;
}
System.out.println(s);
if(s==t)
System.out.println("number polindrone");
else
System.out.println("number is not polindrone");
import java.util.Scanner;
class Switch {
public static void main(String[] args) {
int a =579;
int s=1;
while(a>0){
int r= a%10;
s=s*r;
// System.out.println(s);
a=a/10;
System.out.println("sum of digits "+s);
}}}
To print reverse number using while loop.
class Reverse{
public static void main(String[] args) {
int a=321;
int s=0;
while(a>0){
int r=a%10;
s=s*10+r;
a=a/10;
}
System.out.println(s);
}}
To print the armstrong number between 100 to 999.
class Arm{
public static void main(String[] args) {
int i=100;
while(i<=999){
int a=i,s=0;
while(a>0)
{
int r=a%10;
s=s+r*r*r;
a=a/10;
}
if (s==i)
{
System.out.println(i);
}
i++;} }
Palindrome number between 10 to 100 using while loop.
class padli{
public static void main(String[] args) {
int i=10;
while(i<100){
int a=i,s=0;
while(a>0){
int r=a%10;
s=s*10+r;
a=a/10;
}
if(s==i)
System.out.println(i);
i++;
}
}
}
Check Entered number is Armstrong or not.
import java.util.Scanner;
class Armstrongwhile{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int s=0;
int t=a;
while(a>0){
int r=a%10;
s=s+r*r*r;
a=a/10;
}
if(s==t)
System.out.println("Entered number is Armstrong: "+s);
else
System.out.println("Entered number is not Armstrong: "+s);}}
For Loop:-
class For_loop {
public static void main(String[] args) {
int a= 5;
for(int i=1;i<=10;i++)
{
System.out.println(a*i);
}}}
Printing tha Factorail of the number.
class For_loop {
public static void main(String[] args) {
int a=5;
int f=1;
for(int i=1; i<=a;i++)
{
f=f*i;
}
System.out.println(f);
}}
Printing the Reverse number.
class For_loop {
public static void main(String[] args) {
int a=371;
int s=0;
for(; a>0;){
int r=a%10;
s=s*10+r;
a=a/10;
}
System.out.println(s);
To check given number is Prime or Not using for loop between 1 to 50.
class For{
public static void main(String[] args) {
for(int i=1;i<50;i++)
{
int c=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
c++;
}
if(c==0)
System.out.println("it is a prime"+i);
else
System.out.println("it is not a prime"+i);
}}}
To print the Armstrong number between 100 to 999 using for loop.
class Arm{
public static void main(String[] args) {
int i=100;
while(i<=999){
int a=i,s=0;
while(a>0)
{
int r=a%10;
s=s+r*r*r;
a=a/10;
}
if (s==i)
{
System.out.println(i);
}
i++;
} }}
Print the Palindrome number between 10 to100.
class Palidrome{
public static void main(String[] args) {
for (int i=10;i<100;i++)
{
int a=i,s=0;
for( ;a>0 ; ){
int r=a%10;
s=s*10+r;
a=a/10;
}
if(s==i)
System.out.println(i);
}
}
}
3- Do while Loop:
To print the Armstrong number between 100 to 999 using Do while loop.
class Armst{
public static void main(String[] args) {
int i=100;
do{
int s=0,a=i;
do{
int r=a%10;
s=s+r*r*r;
a=a/10;
}
while(a>0);
{
if(s==i)
System.out.println(i);
}
i++;
}
while(i<=999);
}
}
Print the Palindrome number between 10 to100.
class Padl{
public static void main(String[] args) {
int i=10;
do{
int s=0;
int a=i;
do{
int r=a%10;
s=s*10+r;
a=a/10;
}
while(a>0);
{
if(s==i)
System.out.println(i);
i++;
}
}
while(i<100);
}
}
To check given number is Prime or Not using Do while loop
import java.util.Scanner;
class Prime_number{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int i=2;
int c=0;
do{
System.out.println("your entered number is"+a);
}
while(i>a);
{if(a%i==0)
c++;
i++;
}
if(c==0)
System.out.println("it is a prime number ");
else
System.out.println("it is not a prime number" ); }
}
To check given number is Prime or Not using Do while loop between 1 to 50
class Questions {
public static void main(String[] args) {
int i = 1;
do {
int j = 2;
int c = 0;
// System.out.println("prime number between 1 to 50 "+i);
do{
if (i % j == 0)
c++;
j++;
}
while (j < i);
if (c == 0)
System.out.println(i);
i++;
}
while(i<=50);
}}
Check given number is Armstrong or not using Do while loop.
class Do_WhileArm{
public static void main(String[] args) {
int s=0,a=370;
int temp=a;
do{
int r=a%10;
s=s+r*r*r;
a=a/10;
}
while(a>0);
{
if(s==temp)
System.out.println(s+" is Armstrong Number");
else
System.out.println(s+" is not Armstrong Number");}} }
Array:-
class Array1 {
public static void main(String[] args) {
int[] arr={21,45,25,36,56,78,95,65,44,52,63,98};
System.out.println(arr[2]);
System.out.println(arr[1]);
int[] arr={12,45,63,52,41,74,85,96};
int i=0;
while(i<arr.length){
System.out.println(arr[i]);
i++;
}
int[] arr={21,45,25,36,56,78,95,65,44,52,63,98};
int i=0;
do{
System.out.println(arr[i]);
i++;
}
while(i<arr.length);
int arr[]={12,45,63,8,59,65,22,45,75,52,63};
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
int arr[]={12,2,15,4,5,21,548,4,15};
int s=0;
for(int i=0;i<=arr.length;i++){
s+=arr[i];
}
System.out.println("sum of the elements="+s);
}
System.out.println("sumof the numbers= " + s);
}while(i<arr.length);
System.out.println("sum of numbers = "+s);
// sum of even numbers using for LOOP
}
System.out.println("sum of even numbers= "+s );
// sum of even numbers using while LOOP
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int s = 0;
int i = 0;
do {
if (arr[i] % 2 == 0) {
s += arr[i];
}
i++;
int arr[] = { 1,2,3,4,5,6,7,8,99};
int s=0;
}
System.out.println("sum of odd numbers= "+s );
int [] arr={1,3,5,7,9};
int s=0;
int i=0;
while(i<arr.length){
int a =arr[i];
int c=0;
int j=2;
while(j<a){
if(a%j==0)
c++;
j++;
}
if (c==0){
s+=arr[i];
}
System.out.println(arr[i]);
i++;
}
System.out.println("prime number"+s);
}
}
2D Array:-
import java.util.Scanner;
class Array2d {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int [] arr=new int[10];
for(int i=0;i<10;i++){
arr[i]=sc.nextInt();
}
for( int j=0;j<10;j++){
System.out.println(arr[j]);
}
arr[i]=sc.nextInt();
}
for( int i=0;i<5;i++){
System.out.print(" "+arr[i]);
}
// to print the sum of 1D array by user input
arr[i]=sc.nextInt();
}
for( int i=0;i<5;i++){
s+=arr[i];
}
System.out.print(" "+s);
arr[i][j]=sc.nextInt();
}
}
for( int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(" "+arr[i][j]);
}
System.out.println();
}
arr[i][j]=sc.nextInt();
}
}
for( int i=0;i<3;i++){
for(int j=0;j<3;j++){
s+=arr[i][j];
}
System.out.println();
}
System.out.print(" "+s);
}
System.out.println();
}
System.out.println("\t"+s);
}
}
Array:-
class Array2 {
public static void main(String[] args) {
}
int[][] arr = { { 4, 8, 6 }, { 4, 8, 9 }, { 4, 8, 9 } };
int i = 0;
while (i < 3) {
int j = 0;
while (j < 3) {
System.out.print(" \t" + arr[i][j]);
j++;
}
System.out.println();
i++;
}
int[][] arr = { { 4, 8, 6 }, { 4, 8, 9 }, { 4, 8, 9 } };
int i = 0;
do{
int j = 0;
do{
System.out.print(" \t" + arr[i][j]);
j++;
}while(j<3);
i++;
System.out.println();}
int arr[][]={{2,9,1},{1,2,3},{8,5,8}};
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
int a=arr[i][j];
int c=0;
for(int k=2;k<a;k++){
if(a%k==0)
c++;
}
if(c==0 &&a!=1)
System.out.println(a);
}
}
int arr[][]={{2,9,1},{1,2,3},{8,5,8}};
int s=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
int a=arr[i][j];
int c=0;
for(int k=2;k<a;k++){
if(a%k==0)
c++;
}
if(c==0 &&a!=1)
s+=a;
}
}
System.out.println(s);
Program on MATRIX:-
class Matrix {
public static void main(String[] args) {
int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int s=0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
System.out.print("\t" + arr[i][j]);
if (i == j)
System.out.print(" " + arr[i][j]);
s=s+arr[i][j];
}
System.out.println();
}
System.out.println(" Sum of left diag of the matrix:"+s);
int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int s = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
System.out.print( "\t" + arr[i][j] );
if (i+j==2)
s = s + arr[i][j];
}
System.out.println();
}
int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int s = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
System.out.print("\t" + arr[i][j]);
if (i ==1)
s = s + arr[i][j];
}
System.out.println();
}
System.out.println("sum of 2nd row of matrix "+s);
int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int s=0;
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
System.out.print("\t"+arr[i][j]);
if (j==1)
s=s+arr[i][j];
}
System.out.println();
}
}