Program File
Program File
class Demo{
public static void main(){
System.out.println("Hello java");
System.out.println(Integer.parseInt(kvch[0]) +
Integer.parseInt(kvch[1]));
}
}
class Demo8{
public static void main(String[]args){
int a=10;
int b= -10;
System.out.println(args[0]+args[1]);
}
}
class Demo{
public static void main(){
System.out.println("Hello java");
System.out.println(Integer.parseInt(kvch[0]) +
Integer.parseInt(kvch[1]));
}
}
Identifiers:
/* Identifiears */
import java.util.Scanner;
class Demo4{
public static void main(String[]args){
int a=10, b=20;
int c=a+b;
Operators:
import java.util.Scanner;
class Demo3{
public static void main(String[]args){
Scanner ob= new Scanner(System.in);
}
}
class OperatorEx{
public static void main(String[]args){
int a=10,b=3;
//Airthmeatic Operators
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);
//Asignment Operators
int c=10,d=7;
System.out.println(c);
c=c+d;
System.out.println(c);
c+=d;
System.out.println(c);
c-=d;
System.out.println(c);
c*=d;
System.out.println(c);
c/=d;
System.out.println(c);
c%=d;
System.out.println(c);
}
}
class OperatorEx1{
public static void main(String[]args){
boolean a = true, b=false;
// //Logical Operators
System.out.println(a&&b);
System.out.println(b||a);
// Bitwise Operators
int c=17,d=26;
System.out.println(c&d);
System.out.println(c|d);
System.out.println(c^d);
//Unary Operators
int e=15, f=-17;
System.out.println(~e);
System.out.println(~f);// in it the value is increases by 1 and the sign
get changed
System.out.println(!a);
//Shift Operators
int g=3, h=5;;
System.out.println(g<<h);
System.out.println(36>>3);
System.out.println(35>>3);
// Relational Operators
int x=10, y=20;
System.out.println(x<y);
System.out.println(x>y);
System.out.println(x<=y);
System.out.println(x>=y);
System.out.println(x==y);
System.out.println(x!=y);
}
}
Areas :
/* Operators */
import java.util.Scanner;
class Areas{
public static void main(String[]args){
Scanner ob = new Scanner(System.in);
// //Area of Circle
// double Area,r;
// System.out.println("Enter the Radius of Circle");
// r = ob.nextDouble();
// Area =3.14*r*r;
// System.out.println("The area of Circle is: "+Area);
//Area of reactangle
//Area of triangle
// tri= (h*b)/2;
// System.out.println("The Area of triangle is "+ tri);
a=a+b;
b=a-b;
a=a-b;
System.out.println("The value of a : "+a);
System.out.println("The value of b : "+b);
}
}
Conditional Statement:
Performing addition, subtraction, multiplication and division by entering
their sign
import java.util.Scanner;
import javax.lang.model.util.ElementScanner14;
int a = 20;
if (a > 20) {
System.out.println("Hello Java");
} else {
System.out.println("Number is smaller the 20");
}
int a = 10, b = 5;
if (c.equals("+"))
System.out.println("Sum=" + (a + b));
else if (c.equals("-"))
System.out.println("Sub=" + (a - b));
else if (c.equals("*"))
System.out.println("Mul=" + (a * b));
else if (c.equals("/"))
System.out.println("Div=" + (a / b));
else {
System.out.println("Sorry......");
}
}
}
If else programs:
/* if else Conditional Statement */
import java.util.Scanner;
class ConditionalStatement{
public static void main(String[]args){
if(c %2 ==0 ){
System.out.println("The number is Even");
}
else{
System.out.println("The number is odd ");
}
int age;
System.out.println("Enter the age");
age= ob.nextInt();
if(age>18){
System.out.println("The Person can Vote");
}
else{
System.out.println("Cann't Vote");
}
// Enter Character is vowel or not
String a="b";
if(a=="a"||a=="e"||a=="o"||a=="u")
System.out.println(a+" is vowel");
else
System.out.println(a+" is not Vowel");
}
}
import javax.lang.model.util.ElementScanner14;
avg= (a+b+c)/3;
if(avg>=60){
System.out.println("First Division");
}
else{
System.out.println("Fail");
}
}
}
class LeapYr{
public static void main(String[] args) {
class FindingNextLeapyr{
public static void main(String[] args) {
Scanner ob = new Scanner(System.in);
System.out.println("Enter the Year");
int yr = ob.nextInt();
if(yr%4==0){
System.out.println("Leap Yr");
}
else{
System.out.println("Not a leap year");
}
int temp = 4-(yr%4);
System.out.println("Next leap year = "+ (yr+temp));
}
}
}
}
}
While Loop
Basic program
public class whileLoopEx2 {
public static void main(String[] args) {
int i=2, a=9;
while(i<a)
{
if(a%i==0)
System.out.println("hello java");
i++;
}
}
}
Printing the table of entered number:
import java.util.Scanner;
}
}
while(i<a){
if(a%i==0)
c++;
i++;
}
//System.out.println(c);
if(c==0){
System.out.println("Numebr is Prime");
}
else{
System.out.println("Number is not Prime");
}
}
}
For loop
import javax.lang.model.util.ElementScanner14;
// int a = 8;
// int c = 0;
// for (int i = 2; i < a; i++) {
// if (a % i == 0)
// c++;
// }
// if (c == 0)
// System.out.println("Prime Number ");
// else
// System.out.println("Note Prime Number");
// Finding given number is armstrong or not
// int n = 153;
// int temp = n;
// int s = 0;
// for (; n > 0;) {
// int rem = n % 10;
// s = s + rem * rem * rem;
// // System.out.println(s);
// n = n / 10;
// }
// // System.out.println(s);
// if (s == temp) {
// System.out.println("Armstrong Number");
// } else {
// System.out.println("Not Armstrong");
// }
Do while loop
// import javax.lang.model.util.ElementScanner14;
int r = a % 10;
s = s + r * r * r;
a = a / 10;
if (s == i) {
System.out.println("Armstrong number");
} else {
System.out.println("Not Armstrong");
}
}
}
do{
if(a%i==0)
c++;
i++;
}while(i<a);
if(c==0)
System.out.println(a+" is prime number");
else
System.out.println(a+" is not a prime number");
}
}
Printing the reverse number of given number
class DoWhileReverseNumber{
public static void main(String[] args) {
// int a=123;
// int s=0;
// while(a>0){
// int rem=a%10;
// s=s*10+rem;
// a=a/10;
// }
// System.out.println(s);
int a=345;
int s=0;
do{
int rem=a%10;
s=s*10+rem;
a=a/10;
}while(a>0);
System.out.println("The reverse number is "+s);
}
}
}
while(i<=10);
}
}
Printing the Armstrong number between 100 to 900 with for loop, while
loop, do-while loop
// Printing the prime number between 100 to 900 with for loop
// Printing the prime number between 100 to 900 with while loop
// int i=100;
// while(i<=900){
// int a=i,s=0;
// while(a>0){
// int rem=a%10;
// s=s+rem*rem*rem;
// a=a/10;
// }
// if(s==i)
// System.out.println(s);
// i++;
// }
// Printing the prime number between 100 to 900 with do while loop
int i=100;
do{
int s=0 ,a=i;
do{
int rem=a%10;
s=s+rem*rem*rem;
a=a/10;
}while(a>0);
if(s==i){
System.out.println(i);
}
i++;
}while(i<=999);
}
}
Printing the palindrom number using for loop, while loop and do while
loop
import java.util.Scanner;
// int i = 10;
// System.out.println("The Palindrom numbers are ");
// while (i < 100) {
// int s = 0;
// int a = i;
// while (a > 0) {
// int r = a % 10;
// s = s * 10 + r;
// a = a / 10;
// }
// if (s == i)
// System.out.println(i);
// i++;
// }
Printing the Prime number using for loop, while loop and do while loop
public class PrimeNumberExss {
public static void main(String[] args) {
//Prime number by while loop
int i = 1;
while (i <= 50) {
int j = 2;
int c = 0;
while (j < i) {
if (i % j == 0)
c++;
j++;
}
if (c == 0)
System.out.println(i);
i++;
}
//prime number by do-while loop
// int i = 1;
// do {
// int c = 0;
// int j = 2;
// while (j < i) {
// if (i % j == 0) {
// c++;
// j++;
// }
// if (c == 0)
// System.out.println(i);
// i++;
// }
// } while (i <= 50);
}
}
Reverse number using for loop , while loop and do while loop
class DoWhileReverseNumber{
public static void main(String[] args) {
//Reverse number using while loop
// int a=123;
// int s=0;
// while(a>0){
// int rem=a%10;
// s=s*10+rem;
// a=a/10;
// }
// System.out.println(" The reverse number is "+s);
// int a=345;
// int s=0;
// do{
// int rem=a%10;
// s=s*10+rem;
// a=a/10;
// }while(a>0);
// System.out.println("The reverse number is "+s);
import java.util.Locale.IsoCountryCode;
import java.util.Scanner;
// int sum = 0;
// int arr[] = { 12,22,31,21,23,45,64,56,89 };
// for (int i = 0; i < arr.length; i++) {
// System.out.println(arr[i]); //to print array
// if(arr[i]%2==0) // even num
// if(arr[i]%2==1) // odd number
// if(i%2==1) // sum odd index number
// if(i%2==0) // sum even index number
// sum += arr[i];
// int a=arr[i];
// int c=0;
// for(int j=2;j<a;j++){
// if(a%j==0)
// c++;
// }
// if(c==0){
// System.out.println("The prime number in the array are : "+arr[i]);
// sum+=arr[i];
// }
// }
// System.out.println("Sum of array : "+ sum);
// System.out.println("Sum of even number of array : "+ sum);
// System.out.println("Sum of odd number of array : "+ sum);
// System.out.println("Sum of odd index number of array : "+ sum);
// System.out.println("Sum of even index number of array : " + sum);
// System.out.println("Sum of prime number of array : "+sum);
// int s=0;
// for(int i=0; i<arr.length;i++){
// s+=arr[i];
// }
// System.out.println(" The sum of array "+s);
// int arr[]={2,3,4,3,2,45,3,2,4,3,32};
// int s=0;
// int i=0;
// while(i<arr.length){`
// if(arr[i]%2==0)
// s+=arr[i];
// i++;
// }
// System.out.println(s);
// int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// int s = 0;
// int i = 0;
// do {
// if (arr[i] % 2 == 0) {
// // System.out.println(arr[i]);
// s+= arr[i];
// }
// i++;
// }
// }
}
System.out.println();
2D Array
import java.util.Scanner;
import javax.sound.sampled.SourceDataLine;
import javax.swing.event.SwingPropertyChangeSupport;
// int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// for (int i = 0; i < 3; i++) {
// for (int j = 0; j < 3; j++) {
// System.out.print("\t" + arr[i][j]); // printing element of array
// }
// System.out.println();
// }
// int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// int i = 0;
// while (i < 3) {
// int j = 0;
// while (j < 3) {
// System.out.print("\t" + arr[i][j]); // printing element of array
// j++;
// }
// System.out.println();
// i++;
// }
// int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// int i = 0;
// do {
// int j = 0;
// do {
// System.out.print("\t" + arr[i][j]); // printing element of array
// j++;
// } while (j < 3);
// System.out.println();
// i++;
// }while (i < 3);
// int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// int sum = 0;
// int i = 0;
// do {
// int j = 0;
// do {
// System.out.print("\t" + arr[i][j]); // printing element of array
// sum += arr[i][j];// Sum of array
// j++;
// } while (j < 3);
// System.out.println();
// i++;
// }while (i < 3);
// System.out.println(" sum of 2D array :" + sum);
// int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// int sum = 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) {
// System.out.print("\t"+a);
// sum += arr[i][j];
// }
// System.out.println();
// }
// }
// System.out.println("Sum of Prime number of array element "+sum); //
correcet
// int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// int n = 15, c = 0;
// for (int i = 0; i < 3; i++)
// {
// for (int j = 0; j < 3; j++)
// {
// System.out.println("\t" + arr[i][j]);
// if (arr[i][j] == n)
// c++;
// }
// System.out.println();
// }
// if (c==0)
// System.out.println("Number Not found ");
// else
// System.out.println("Number found ");
// 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;
// }
// if (c == 0 && a != 1) {
// System.out.print("\t" + a);
// s += arr[i][j];
// }
// // System.out.println();
// }
// System.out.println();
// }
// System.out.println(" the sum of prime number is : " + s);
int s = 0;
int c = 0;
int in1 = 0, in2 = 0;
}
}
// System.out.println();
}
if (c == 0) {
System.out.println("Number not found ");
} else {
System.out.println("number found");
}
System.out.println("Element found at i " + in1 + " j " + in2 + " th
position.");
// System.out.println(s);
}
}
Matrix
// 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 diagonal of matrix"+s);
//Printing sum of right diagonal
// }
// System.out.println();
// }
// System.out.println("sum of right diagonal of matrix " + s);
// int arr[][] = { { 1, 4, 9 }, { 4, 7, 6 }, { 7, 2, 1 } };
// 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 second row of matrix " + s);
int arr[][] = { { 1, 4, 9 }, { 4, 7, 6 }, { 7, 2, 1 } };
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();
}
System.out.println("sum of second col of matrix " + s);
}