You are on page 1of 7

Basic Java Programs

=========================================

Note: == is the comparison operator which compares the value of two different objects.
Visual studio te run korle normal oi code er modhe run or dan dike oi play button click korlie hbe
and jodi manually koris tahole command promot khulbi jekhane store korechis sekhane opor er
cmd likbi then

javac filename.java // class file tori korbe javac enter maarbi


java filename // run korbe jaabe enter marbi

1. Armstrong number:

Example: 153 = (1)3 + (5)3 + (3)3 also 1634 = (1)4 + (6)4 + (3)4 + (4)4
Etar mane hoeche jodi 5 digit er number hoi tahole seta proti ta number 5 times multiply
hobe then sob kota jog kore newa hbe kintu eta laage na amra 153 ki sob somae use kori.

Eta dekhe nis aage: https://www.youtube.com/watch?v=TqcGTx-L3wY

import java.util.*;
public class Armstrong_Num { // mone rakis java main class capital letter diye start hoi and class name
and file name same hoi sob somae nahole run korbe naa
public static void main(String args[])
{
int n, temp, rev=0, num;
System.out.println("Enter the number:");
Scanner sc = new Scanner(System.in);
n = sc.nextInt(); // read the limit of the number given by the user
num = n; // assigning number into the temporary variable which is n
while(n != 0){ //checking weather the n is not equal to 0, if it is 0 it will not go inside the while loop
temp = n%10; // determines the last digit from the number
rev = rev + temp*temp*temp; // multiply ‘temp’ thrice, and add to ‘rev’, and make that
the current ‘rev’.
n = n/10; // removes the last digit
}
if(num == rev) { // compares the rev with original number
System.out.println(num + " is Armstrong");
}
else {
System.out.println(num + " is not Armstrong");
}
}
}
Eta hoche jodi anek boro number er armstrong korar hoi tokhn deklei bhuje jaabi:

import java.util.Scanner;
public class Armstrong_Num {
public static void main(String[] args) {
int org_num, n, rem, rev_num, power; // Org is the orginal number, n is the
temporary variable where orginal number, rem is remainder, rev_num is the reverse
number, power means to the power of the number
rev_num = 0; // It is always initialized to 0
Scanner sc = new Scanner(System.in); // creating a new scanner class object as sc
System.out.print("Enter the number : ");
org_num = sc.nextInt(); // read the number by user defined
System.out.println("Enter the power of the digit:");
power = sc.nextInt(); // mathe rakbi joto boro shokhar number check korbi toto
number er power hbe
n = org_num; // original number is being stored in n
while (n != 0){ // to check weather the number is 0 or not if 0 then it will not
enter the while loop
rem = n % 10; // determine the last digit of the number by dividing it with 10
to get the remainder. That means if 153 is the number then it will give 3 then 5 then 1
rev_num += Math.pow(rem, power); // += means rev_num = rev_num + MAth.pow(rem,
power) it will multiply 3 upto three times
n /= 10; // means n = n/10 it will return 15 from 153
}
if(rev_num == org_num) // check weather the reverse number is same to the original
number
System.out.println(org_num + " is an Armstrong number");
else
System.out.println(org_num + " is not an Armstrong number");
}
}

2. Palindrome number:

If 121 is written in reverse then it will be the same but not 123.

Eta dekbi aage: https://www.youtube.com/watch?v=I0XXg9pt4_U

import java.util.*;
public class Palindrome
{
public static void main(String[] args)
{
int rev_num = 0,temp, n; // jene rakbi rev_num mane jetae main calucation ta
store kora hbe seta 0 diye e initialize korte hbe
System.out.println("Enter the number =>");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int org_num = n; // original number is stored in the n
while(n != 0){ // to check weather the number is equal to 0 or not
temp = n%10; // determine the last digit of the number by dividing it with
10 to get the remainder means diving 121/10 which will give a remainder 1 and this 1 is
stored in the temp variable.
rev_num = rev_num * 10 + temp; // here rev_num is 0. So, it will be (0*10)
+ 1 = 1 this 1 will be value of rev_num
n = n/10; // etar maane 121/10 maane 12 abr ei 12 tai abr while loop e
ghurbe bhujli
}
if(org_num == rev_num) // now we are checking weather the orginal number is
equal to reverse number or not
{
System.out.println("It is a palindrome number");
}else{
System.out.println("It is not a palindrome number");
}
}
}
// Note: jodi amra palindrome er jaegae reverse number kori tahole if condition korte
laage na as amra okhane sudu reverse e baar korchi bhujli check korbo naa orginal
number er sathe

3. Average number:

Average = (10+20+30+40+50)/5 = 30
Tr jodi bhujte problem hoi bolis bole debo okk

import java.util.Scanner;
public class Avg_num
{
public static void main(String args[])
{
int n;
double res=0; // double e declare or float datatype use korlam karon average
point e ae
Scanner sc = new Scanner(System.in); // Scanner class er notun object create
korlm bhujli
System.out.println("Enter how many numbers that you want to avg:");
n=sc.nextInt(); // n eta ke scanner read korbe jaate user runtime e value dite
paare je koto gulo number se print korbe suppose 5 ta number
int a[]=new int[n]; // java te array size ta bhave declare kore new keyword use
kore jaar modhe koto gulo number store hbe array er modhe ar array starting index 0
theke start hoi
System.out.println("Enter the "+n+" numbers");
for(int i=0;i<n;i++){
a[i]=sc.nextInt(); // ei loop tae array modhe koto gulo number user nebe maane
jodi 5 ta nile 5 ta define korte hbe
}
for(int i=0;i<n;i++) // bhalo kore dek ei loop ta use korchi jaate 5 ta number
add kore res variable er modhe store korte pari
res = res+a[i]; // but tor mone hbe je res + a[i] kno karon jokhn kono value add
kore store kora hoi tokhn ei bhaave java te likte hbe
System.out.println("Average="+res/n); // then res/n korlam as n hoche koto gulo
number arr tui to janis e average jokhn kori tokhn 5 ta number add kore 5 diye divide
}
}

// Note new keyword is used to create new objects

4. Check whether the number is odd or even.

2 is even but 3 is odd.

import java.util.Scanner;
public class Odd_Even {
/*Java Program to check whether a number is even or odd*/
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:");
int n = sc.nextInt();
if (n%2 == 0) // here we are checking if the number is divided by 2 then its
remainder is equal to 0 or not thats it
System.out.println("The entered number is even");
else
System.out.println("The entered number is odd");
}
}

5. Fibonacci Series:

Example: 0 1 1 2 3 5 8……. ( just number gulo add hoche 0 + 1 = 1 + 1 = 2 + 1 = 3 + 2 = 5……..


Eta dekis aage: https://www.youtube.com/watch?v=mhirNWmYxWM

Logic 1: (Most simple logic eta)

import java.util.*;
class Fibonaaci_num {
public static void main(String args[])
{
int n1=0,n2=1,n3,i,n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the nth term to print for fibonaaci series: ");
n = sc.nextInt();
System.out.print(n1+" "+n2);//printing 0 and 1
for(i=2;i<n;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2; // n3 = 0 + 1 = 1
System.out.print(" " + n3); // " " etar maane number gulor maaje space diychi
ar n3 value 1 hoe gelo ekhane arr por por number gulo print korbe
n1=n2; // here n2 is 1 which is stored in n1 as 1
n2=n3; // and n3 which is 1 stored in n2 as 1 and this will again repeated from
n3 = n1 + n2 where n1 will be 1 and n2 will be 1 which will become 2
}
}
}

Logic 2: (Common logic)

import java.util.*;
public class Fibonacci_num
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a = 0, b = 1;
System.out.print("Enter the number of terms: ");
int n=sc.nextInt();
System.out.println("Fibonnaci series: ");
for(int i = 1; i <= n; i++) // if n is 5 then the for loop condition will
become true joto khn na n er value 1 hoche manne n = 5/4/3/2/1 end
{
System.out.print(a + " "); // 0 + " " this we have to write as a value
value will get incremented with the series means 0 1 1 2 3
int c = a + b; // c = 0 + 1 = 1 then c = 1 + 1 = 2 then c = 1 + 2 = 3 then
c = 2 + 3 = 5
a = b; // a = 1 then a = 1 then a = 2 then a = 3
b = c; // b = 1 then b = 2 then b = 3 then b = 5
}
}
}

Logic 3: (Fibonacci series using recursion)

Eta tar agge dekhe nis: https://www.youtube.com/watch?v=8W1DQUyoZzk

Actually ki bolto fibonacci series using recursion ta anek rokhm er logic er saathe kora jaae bhjli
Ami toke sob theke simple logic tai dekhie chi jeta ami bhujte perechi
Note: The static keyword is used in methods and variables, if we declare any variable and
method as static then it becomes a class level property that we can access without creating an
object. Ex.: Block, Variable, Methods, Classes.

import java.util.*;
class Fibo_num {
static int a = 0, b = 1 , c;
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number: ");
int n = sc.nextInt(); // n will be read by scanner class
Fibo_num obj = new Fibo_num(); // Main class object has been created to call
the function printFib
System.out.println("Fibonacci Series: ");
obj.printFib(n); // here we have called the function printFib where value of n
is 5
}
void printFib(int n) // int 5 it will not change
{ // Note : if(n>=0) is written then it will take the 0th index also means it
will print 0 1 1 2 3 5 not 0 1 1 2 3
if(n>=1) { // if(5 >= 1) -> True, if(4 >= 1) -> True, if(3 >= 1) -> True, if(2
>= 1) -> True, if(1 >= 1) -> True, if(0 >= 1) -> False condition arr egoo be naa
c = a+b; // c = 0 + 1 = 1 then c = 1 + 1 = 2 then c = 1 + 2 = 3 then c = 2
+ 3 = 5 then c = 3 + 5 = 8
System.out.print(a + " "); // 0, 1, 1, 2, 3
a = b; // a = 1 then a = 1 then a = 2 then a = 3 then a = 5
b = c; // b = 1 then b = 2 then b = 3 then b = 5 then b = 8
printFib(n-1); // 5-1 = 4 it will again go to if condition until the
condition is meet, it will be (4-1) = 3 then (3-1) = 2 then (2-1) = 1 then (1-1) = 0
}
}
}

6. Swap two numbers:

import java.util.*;
class Swap_num {
public static void main (String[] args) {
int i, n, temp=0, rem = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the two numbers to swap: ");
System.out.println("1st number: ");
int a = sc.nextInt(); // a = 10
System.out.println("2nd number: ");
int b = sc.nextInt(); // b = 20
a = a+b; // a = 10 + 20 = 30
b = a-b; // b = 30 - 20 = 10
a = a-b; // a = 30 - 10 = 20
System.out.println("After swapping: ");
System.out.println("a: " + a + " b: " + b); // a = 20 and b = 10

}
}
// Note we can also use temporary variable to store the number ar etar logic holo
temp = a; // temp = 10;
a = b; // a = 20;
b = temp; // b = 10;

That is a = 20 and b = 10.

You might also like