Learn JAVA with me.

4 min read

Here is a my daily learning code of java.
Hello world in java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Adding two integer.
package Luck;
public class hnn {
public static void main(String[] args) {
int a = 10;
int b = 20;
int sum = a + b;
System.out.println("Sum of a and b is: " + sum);
}
}
Check Even or Odd.
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (num % 2 == 0) {
System.out.println(num + " is even.");
} else {
System.out.println(num + " is odd.");
}
scanner.close();
}
}
Find the Largest of Three Numbers
import java.util.Scanner;
public class LargestNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter three numbers: ");
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
int largest = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
System.out.println("The largest number is: " + largest);
scanner.close();
}
}
Factorial of a Number
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
System.out.println("Factorial of " + num + " is: " + fact);
scanner.close();
}
}
To find reverse.
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println("Reversed number: " + reversed);
scanner.close();
}
}
How to make calculator by using java.
package Luck;
import java.util.Scanner;
public class My {
public static void main(String[] args)
{
Scanner value= new Scanner(System.in);
char operator;
int a,b;
int result=0;
System.out.println("Enter the operator + - * % /");
operator=value.next().charAt(0);
System.out.println("Enter the value a");
a=value.nextInt();
System.out.println("Enter the value b");
b=value.nextInt();
if(operator=='+')
{
result=a+b;
}
else if(operator=='-')
{
result=a-b;
}
else if(operator=='*')
{
result=a*b;
}
else if(operator=='%')
{
result=a%b;
}
else if(operator=='/')
{
result=a/b;
}
else {
System.out.println("Error");
}
System.out.println(a+ ""+ operator + ""+ b +""+"="+result);
}
Fibonnaci series.
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = scanner.nextInt();
int a = 0, b = 1;
System.out.print("Fibonacci Series: " + a + " " + b + " ");
for (int i = 2; i < n; i++) {
int next = a + b;
System.out.print(next + " ");
a = b;
b = next;
}
scanner.close();
}
}
To find palindrome
package Luck;
import java.util.Scanner;
public class swit {
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.println("Please enter a num");
int num=input.nextInt();
input.close();
int fact=1;
int dupnum=num;
int rev=0, rem;
while(dupnum>0) {
rem=dupnum % 10;
rev=rev*10 +rem;
dupnum/=10;
}
if(rev == num)
{
System.out.println("Palindrome " + rev);
}
else {
System.out.println("not Palindrome " + rev);
}
}
}
OOPS in java
package Luck;
public class OOPS {
public static void main(String[] args)
{
// classname objectname= new classname();
// Animal dog = new Animal();
// dog.id =10;
// dog.name ="Tommy";
// System.out.println(dog.id+ "" +dog.name);
//
// Animal cat = new Animal();
// cat.id=30;
// cat.name="smome";
// System.out.println(cat.name);
// }
//}
//class Animal
// {
// String name;
// int id;
// }
//
student cl= new student();
cl.name="Anil";
cl.id=1;
cl.gender="male";
cl.section=1;
System.out.println("name is:" +cl.name );
System.out.println(cl.id);
System.out.println(cl.gender);
System.out.println(cl.section );
System.out.println("\n" );
System.out.println("\n" );
cl.name="Aashustos";
cl.id=2;
cl.gender="male";
cl.section=2;
System.out.println(cl.name);
System.out.println(cl.id);
System.out.println(cl.gender);
System.out.println(cl.section);
System.out.println("\n" );
System.out.println("\n" );
cl.name="Samikshya";
cl.id=3;
cl.gender="female";
cl.section=2;
System.out.println(cl.name);
System.out.println(cl.id);
System.out.println(cl.gender);
System.out.println(cl.section);
}
}
class student{
String name;
int id;
String gender;
int section;
}
0
Subscribe to my newsletter
Read articles from Anil Shrestha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Anil Shrestha
Anil Shrestha
hello there I am Anil Shrestha.